Last active
August 29, 2015 14:27
-
-
Save nbyouri/870a30a511b79d64590a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/pkgin.h b/pkgin.h | |
index ab277f4..2299986 100644 | |
--- a/pkgin.h | |
+++ b/pkgin.h | |
@@ -193,6 +193,17 @@ typedef struct Pkglist { | |
SLIST_ENTRY(Pkglist) next; | |
} Pkglist; | |
+/** | |
+ * \struct Pkgdesc | |
+ * | |
+ * \brief Small structure for a package name and comment | |
+ */ | |
+typedef struct Pkgdesc { | |
+ char *pkg_name; /*!< full package name with version, foo-1.0 */ | |
+ char *pkg_comment; /*!< package list comment */ | |
+ char is_inst; /*!< shows whether package is installed */ | |
+} Pkgdesc; | |
+ | |
#define comment p_un.comment | |
#define computed p_un.deptree.computed | |
#define keep p_un.deptree.keep | |
@@ -263,6 +274,7 @@ Plistnumbered *rec_pkglist(const char *, ...); | |
int pkg_is_installed(Plisthead *, Pkglist *); | |
void list_pkgs(const char *, int); | |
int search_pkg(const char *); | |
+int pkg_cmp(const void *, const void *); | |
void show_category(char *); | |
void show_pkg_category(char *); | |
void show_all_categories(void); | |
diff --git a/pkglist.c b/pkglist.c | |
index a0a8af8..0c531cb 100644 | |
--- a/pkglist.c | |
+++ b/pkglist.c | |
@@ -310,14 +310,17 @@ search_pkg(const char *pattern) | |
{ | |
Pkglist *plist; | |
regex_t re; | |
+ int i; | |
int rc; | |
char eb[64], is_inst, outpkg[BUFSIZ]; | |
int matched_pkgs; | |
+ int pkg_count; | |
char sfmt[10], pfmt[10]; | |
setfmt(&sfmt[0], &pfmt[0]); | |
matched_pkgs = 0; | |
+ pkg_count = 0; | |
if (!SLIST_EMPTY(&r_plisthead)) { | |
@@ -327,6 +330,9 @@ search_pkg(const char *pattern) | |
errx(1, "regcomp: %s: %s", pattern, eb); | |
} | |
+ Pkgdesc *found_pkgs; | |
+ XMALLOC(found_pkgs, sizeof(* found_pkgs)); | |
+ | |
SLIST_FOREACH(plist, &r_plisthead, next) { | |
is_inst = '\0'; | |
@@ -349,13 +355,29 @@ search_pkg(const char *pattern) | |
} | |
- snprintf(outpkg, BUFSIZ, sfmt, | |
- plist->full, is_inst); | |
+ XREALLOC(found_pkgs, (pkg_count + 1) * sizeof(*found_pkgs)); | |
+ | |
+ XSTRDUP(found_pkgs[pkg_count].pkg_name, plist->full); | |
+ XSTRDUP(found_pkgs[pkg_count].pkg_comment, plist->comment); | |
+ found_pkgs[pkg_count].is_inst = is_inst; | |
- printf(pfmt, outpkg, plist->comment); | |
+ pkg_count++; | |
} | |
} | |
+ qsort(found_pkgs, pkg_count, sizeof(*found_pkgs), pkg_cmp); | |
+ | |
+ for (i = 0; i < pkg_count; i++) { | |
+ snprintf(outpkg, BUFSIZ, sfmt, | |
+ found_pkgs[i].pkg_name, found_pkgs[i].is_inst); | |
+ printf(pfmt, outpkg, found_pkgs[i].pkg_comment); | |
+ | |
+ XFREE(found_pkgs[i].pkg_name); | |
+ XFREE(found_pkgs[i].pkg_comment); | |
+ } | |
+ | |
+ XFREE(found_pkgs); | |
+ | |
regfree(&re); | |
if (matched_pkgs == 1) | |
@@ -368,6 +390,25 @@ search_pkg(const char *pattern) | |
return EXIT_SUCCESS; | |
} | |
+int | |
+pkg_cmp(const void *a, const void *b) { | |
+ const Pkgdesc first_pkg = *(const Pkgdesc *)a; | |
+ const Pkgdesc second_pkg = *(const Pkgdesc *)b; | |
+ | |
+ const char *first_version; | |
+ const char *second_version; | |
+ | |
+ first_version = strrchr(first_pkg.pkg_name, '-'); | |
+ second_version = strrchr(second_pkg.pkg_name, '-'); | |
+ | |
+ if (dewey_cmp(first_version + 1, DEWEY_LT, second_version + 1)) | |
+ return 1; | |
+ else if (dewey_cmp(first_version + 1, DEWEY_GT, second_version + 1)) | |
+ return -1; | |
+ else | |
+ return 0; | |
+} | |
+ | |
void | |
show_category(char *category) | |
{ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment