int pkg_order(const char *pattern, const char *first_pkg, const char *second_pkg) { const char *first_version; const char *second_version; if (first_pkg == NULL && second_pkg == NULL) return 0; if (first_pkg == NULL) return pkg_match(pattern, second_pkg) ? 2 : 0; if (second_pkg == NULL) return pkg_match(pattern, first_pkg) ? 1 : 0; first_version = strrchr(first_pkg, '-'); second_version = strrchr(second_pkg, '-'); if (first_version == NULL || !pkg_match(pattern, first_pkg)) return pkg_match(pattern, second_pkg) ? 2 : 0; if (second_version == NULL || !pkg_match(pattern, second_pkg)) return pkg_match(pattern, first_pkg) ? 1 : 0; if (dewey_cmp(first_version + 1, DEWEY_GT, second_version + 1)) return 1; else if (dewey_cmp(first_version + 1, DEWEY_LT, second_version + 1)) return 2; else if (strcmp(first_pkg, second_pkg) < 0) return 1; else return 2; }
/* * Returns -1, 0 or 1 depending on if the version components of * pkg1 is less than, equal to or greater than pkg2. No comparison * comparison of the basenames is done. */ int xbps_cmpver(const char *pkg1, const char *pkg2) { if (dewey_cmp(pkg1, DEWEY_LT, pkg2)) return -1; else if (dewey_cmp(pkg1, DEWEY_GT, pkg2)) return 1; else return 0; }
/* * Perform dewey match on "pkg" against "pattern". * Return 1 on match, 0 on non-match, -1 on error. */ int dewey_match(const char *pattern, const char *pkg) { const char *version; const char *sep, *sep2; int op, op2; int n; /* compare names */ if ((version=strrchr(pkg, '-')) == NULL) { return 0; } if ((sep = strpbrk(pattern, "<>")) == NULL) return -1; /* compare name lengths */ if ((sep-pattern != version-pkg) || strncmp(pkg, pattern, (size_t)(version-pkg)) != 0) return 0; version++; /* extract comparison operator */ if ((n = dewey_mktest(&op, sep)) < 0) { return 0; } /* skip operator */ sep += n; /* if greater than, look for less than */ sep2 = NULL; if (op == DEWEY_GT || op == DEWEY_GE) { if ((sep2 = strchr(sep, '<')) != NULL) { if ((n = dewey_mktest(&op2, sep2)) < 0) { return 0; } /* compare upper limit */ if (!dewey_cmp(version, op2, sep2+n)) return 0; } } /* compare only pattern / lower limit */ if (sep2) { char ver[PKG_PATTERN_MAX]; strlcpy(ver, sep, MIN((ssize_t)sizeof(ver), sep2-sep+1)); if (dewey_cmp(version, op, ver)) return 1; } else { if (dewey_cmp(version, op, sep)) return 1; } return 0; }
const char * pkg_order(const char *match1, const char *match2) { const char *v1, *v2; v1 = strrchr(match1, '-'); v2 = strrchr(match2, '-'); if (v1 == NULL || v2 == NULL) errx(1, "Internal error"); ++v1; ++v2; if (dewey_cmp(v1, DEWEY_GT, v2)) return match1; else if (dewey_cmp(v2, DEWEY_GT, v1)) return match2; else if (strcmp(match1, match2) > 0) return match1; else return match2; }