[pacman-dev] portable package sorting for shell scripts

Dave Reisner d at falconindy.com
Thu Aug 4 09:01:42 EDT 2011


So it occurs to me that while it's all well and good to have a portable
package cache cleaner, it's no good at all if it all hinges on a GNU
sort flag (-V) which poorly approximates what vercmp does. Would we have
room for sort tool that implements alpm_pkg_vercmp? A simple
implementation could be as easy as the below... I think this would get a
lot of mileage out of the AUR/pacman wrapper crowd.

thoughts?

d

#include <alpm.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXLEN  4096

int vercmp(const void *p1, const void *p2) {
  return alpm_pkg_vercmp(*(const char**)p1, *(const char**)p2);
}

int main(void) {
  char **pkglist = NULL;
  char *pkg;
  char buf[MAXLEN + 1];
  int i, pkgcount = 0;

  while (fgets(buf, MAXLEN + 1, stdin)) {
    pkg = strndup(buf, MAXLEN);
    *(pkg + strlen(pkg) - 1) = '\0';
    pkglist = realloc(pkglist, ++pkgcount * sizeof(*pkglist));
    *(pkglist + pkgcount - 1) = pkg;
  }

  qsort(pkglist, pkgcount, sizeof(char*), vercmp);

  for (i = 0; i < pkgcount; i++) {
    printf("%s\n", *(pkglist + i));
    free(*(pkglist + i));
  }
  if (pkgcount > 0) {
    free(pkglist);
  }

  return 0;
}


More information about the pacman-dev mailing list