Magellan Linux

Contents of /branches/mage-next/src/highest_magefile.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2810 - (show annotations) (download)
Wed Sep 3 11:58:06 2014 UTC (9 years, 8 months ago) by niro
File MIME type: text/plain
File size: 2534 byte(s)
-added highest_magefile program
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <sys/types.h>
4 #include <glob.h>
5 #include <sys/stat.h>
6 #include <unistd.h>
7 #include <string.h>
8 #include <ctype.h>
9
10 #include "package.h"
11
12 #define BASENAME "highest_magefile"
13
14 static void usage(void)
15 {
16 fprintf(stderr, "usage: %s <path/to/mage/directory/PNAME>\n", BASENAME);
17 }
18
19 int main(int argc, char *argv[])
20 {
21 char *directory = NULL;
22 char *searchstr = NULL;
23 char **magefiles = NULL;
24 char *highest = NULL;
25 glob_t globbuf;
26 int i;
27 int counter = 0;
28 int ret = -1;
29 int size;
30
31 if(argc == 1) {
32 usage();
33 return 2;
34 }
35 if(argc > 1 &&
36 (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0
37 || strcmp(argv[1], "--usage") == 0)) {
38 usage();
39 return 0;
40 }
41 if(argc > 1) {
42 directory = strdup(argv[1]);
43 }
44
45 // add search value
46 size = asprintf(&searchstr, "%s%s%s", directory, "/*.", MAGESUFFIX);
47 if(size == -1) {
48 printf("error allocating memory using asprintf\n");
49 return -1;
50 }
51
52 // enum stable packages from dir
53 if(glob(searchstr, 0, NULL, &globbuf) == 0)
54 {
55 for(i = 0; i < globbuf.gl_pathc; i++)
56 {
57 // resize array
58 magefiles = (char **)realloc(magefiles, (counter + 1) * sizeof(char *));
59 if ( check_stable_package(globbuf.gl_pathv[i]) == 0) {
60 magefiles[counter++] = strdup(globbuf.gl_pathv[i]);
61 }
62 }
63 }
64
65 //debug
66 // printf("counter %i\n", counter);
67 // for(i = 0; i < counter; i++)
68 // printf("magefiles[%d] == %s\n", i, magefiles[i]);
69
70 // nothing found, exit error
71 if(counter == 0)
72 return EXIT_FAILURE;
73
74 // only one argument given, that is the highest one
75 if(counter == 1) {
76 highest = strdup(magefiles[0]);
77 }
78
79 // fasten things up
80 if(counter == 2) {
81 ret = alpm_pkg_vercmp(magefiles[0], magefiles[1]);
82 if(ret < 0) {
83 highest = strdup(magefiles[1]);
84 }
85 if(ret == 0) {
86 highest = strdup(magefiles[0]);
87 }
88 if(ret > 0) {
89 highest = strdup(magefiles[0]);
90 }
91 }
92
93 if(counter > 2) {
94 for(i = 0; i < counter; i++)
95 {
96 if (i == 1) {
97 highest = strdup(magefiles[i]);
98 }
99 ret = alpm_pkg_vercmp(highest, magefiles[i+1]);
100 // retval >0 or 0 means saved i the higher on, no need to strcmp
101 if(ret < 0) {
102 highest = strdup(magefiles[i+1]);
103 }
104 }
105 }
106
107 if(strlen(highest) > 0) {
108 printf("%s\n", highest);
109 }
110
111 /*
112 // free the string array
113 // Note: You must delete each individual string
114 // before you delete the array of pointers
115 */
116 for(i = 0; i < counter; i++)
117 free(magefiles[i]);
118 free(magefiles);
119 globfree(&globbuf);
120 free(directory);
121 free(searchstr);
122 free(highest);
123
124 return EXIT_SUCCESS;
125 }