#include #include #include #include #include #include #include #include #include "package.h" #define BASENAME "highest_magefile" static void usage(void) { fprintf(stderr, "usage: %s \n", BASENAME); } int main(int argc, char *argv[]) { char *directory = NULL; char *searchstr = NULL; char **magefiles = NULL; char *highest = NULL; glob_t globbuf; int i; int counter = 0; int ret = -1; int size; if(argc == 1) { usage(); return 2; } if(argc > 1 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "--usage") == 0)) { usage(); return 0; } if(argc > 1) { directory = strdup(argv[1]); } // add search value size = asprintf(&searchstr, "%s%s%s", directory, "/*.", MAGESUFFIX); if(size == -1) { printf("error allocating memory using asprintf\n"); return -1; } // enum stable packages from dir if(glob(searchstr, 0, NULL, &globbuf) == 0) { for(i = 0; i < globbuf.gl_pathc; i++) { // resize array magefiles = (char **)realloc(magefiles, (counter + 1) * sizeof(char *)); if ( check_stable_package(globbuf.gl_pathv[i]) == 0) { magefiles[counter++] = strdup(globbuf.gl_pathv[i]); } } } //debug // printf("counter %i\n", counter); // for(i = 0; i < counter; i++) // printf("magefiles[%d] == %s\n", i, magefiles[i]); // nothing found, exit error if(counter == 0) return EXIT_FAILURE; // only one argument given, that is the highest one if(counter == 1) { highest = strdup(magefiles[0]); } // fasten things up if(counter == 2) { ret = alpm_pkg_vercmp(magefiles[0], magefiles[1]); if(ret < 0) { highest = strdup(magefiles[1]); } if(ret == 0) { highest = strdup(magefiles[0]); } if(ret > 0) { highest = strdup(magefiles[0]); } } if(counter > 2) { for(i = 0; i < counter; i++) { if (i == 1) { highest = strdup(magefiles[i]); } ret = alpm_pkg_vercmp(highest, magefiles[i+1]); // retval >0 or 0 means saved i the higher on, no need to strcmp if(ret < 0) { highest = strdup(magefiles[i+1]); } } } if(strlen(highest) > 0) { printf("%s\n", highest); } /* // free the string array // Note: You must delete each individual string // before you delete the array of pointers */ for(i = 0; i < counter; i++) free(magefiles[i]); free(magefiles); globfree(&globbuf); free(directory); free(searchstr); free(highest); return EXIT_SUCCESS; }