2 * FreeBSD install - a package for the installation and maintenance
3 * of non-core utilities.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
17 * $FreeBSD: src/usr.sbin/pkg_install/lib/version.c,v 1.5 2004/06/29 18:52:12 eik Exp $
18 * $DragonFly: src/usr.sbin/pkg_install/lib/Attic/version.c,v 1.3 2004/07/30 04:46:13 dillon Exp $
25 * Routines to assist with PLIST_FMT_VER numbers in the packing
28 * Following is the PLIST_FMT_VER history:
29 * 1.0 - Initial revision;
30 * 1.1 - When recording/checking checksum of symlink use hash of readlink()
31 * value instead of the hash of an object this links points to.
35 verscmp(Package *pkg, int major, int minor)
39 if ((pkg->fmtver_maj < major) || (pkg->fmtver_maj == major &&
40 pkg->fmtver_mnr < minor))
42 else if ((pkg->fmtver_maj > major) || (pkg->fmtver_maj == major &&
43 pkg->fmtver_mnr > minor))
50 * split_version(pkgname, endname, epoch, revision) returns a pointer to
51 * the version portion of a package name and the two special components.
53 * Syntax is: ${PORTNAME}-${PORTVERSION}[_${PORTREVISION}][,${PORTEPOCH}]
55 * Written by Oliver Eikemeier
56 * Based on work of Jeremy D. Lea.
59 split_version(const char *pkgname, const char **endname, unsigned long *epoch, unsigned long *revision)
62 const char *versionstr;
63 const char *endversionstr;
66 errx(2, "%s: Passed NULL pkgname.", __func__);
68 /* Look for the last '-' the the pkgname */
69 ch = strrchr(pkgname, '-');
70 /* Cheat if we are just passed a version, not a valid package name */
71 versionstr = ch ? ch + 1 : pkgname;
73 /* Look for the last '_' in the version string, advancing the end pointer */
74 ch = strrchr(versionstr, '_');
75 if (revision != NULL) {
76 *revision = ch ? strtoul(ch + 1, NULL, 10) : 0;
80 /* Look for the last ',' in the remaining version string */
81 ch = strrchr(endversionstr ? endversionstr + 1 : versionstr, ',');
83 *epoch = ch ? strtoul(ch + 1, NULL, 10) : 0;
85 if (ch && !endversionstr)
88 /* set the pointer behind the last character of the version without revision or epoch */
90 *endname = endversionstr ? endversionstr : strrchr(versionstr, '\0');
96 * PORTVERSIONs are composed of components separated by dots. A component
97 * consists of a version number, a letter and a patchlevel number. This does
98 * not conform to the porter's handbook, but let us formulate rules that
99 * fit the current practice and are far simpler than to make decisions
100 * based on the order of netters and lumbers. Besides, people use versions
101 * like 10b2 in the ports...
105 #ifdef __LONG_LONG_SUPPORTED
116 * get_component(position, component) gets the value of the next component
117 * (number - letter - number triple) and returns a pointer to the next character
118 * after any leading separators
120 * - components are separated by dots
121 * - characters !~ [a-zA-Z0-9.+*] are treated as separators
122 * (1.0:2003.09.16 = 1.0.2003.09.16), this may not be what you expect:
123 * 1.0.1:2003.09.16 < 1.0:2003.09.16
124 * - consecutive separators are collapsed (10..1 = 10.1)
125 * - missing separators are inserted, essentially
126 * letter number letter => letter number . letter (10a1b2 = 10a1.b2)
127 * - missing components are assumed to be equal to 0 (10 = 10.0 = 10.0.0)
128 * - the letter sort order is: [none], a, b, ..., z; numbers without letters
129 * sort first (10 < 10a < 10b)
130 * - missing version numbers (in components starting with a letter) sort as -1
132 * - a separator is inserted before the special strings "pl", "alpha", "beta",
134 * - "pl" sorts before every other letter, "alpha", "beta", "pre" and "rc"
135 * sort as a, b, p and r. (10alpha = 10.a < 10, but 10 < 10a; pl11 < alpha3
136 * < 0.1beta2 = 0.1.b2 < 0.1)
137 * - other strings use only the first letter for sorting, case is ignored
138 * (1.d2 = 1.dev2 = 1.Development2)
139 * - The special component `*' is guaranteed to be the smallest possible
140 * component (2.* < 2pl1 < 2alpha3 < 2.9f7 < 3.*)
141 * - components separated by `+' are handled by version_cmp below
146 static const struct {
152 { "alpha", 5, 'a'-'a'+1 },
153 { "beta", 4, 'b'-'a'+1 },
154 { "pre", 3, 'p'-'a'+1 },
155 { "rc", 2, 'r'-'a'+1 },
160 get_component(const char *position, version_component *component)
162 const char *pos = position;
163 int hasstage = 0, haspatchlevel = 0;
166 errx(2, "%s: Passed NULL position.", __func__);
168 /* handle version number */
171 #ifdef __LONG_LONG_SUPPORTED
172 component->n = strtoll(pos, &endptr, 10);
174 component->n = strtol(pos, &endptr, 10);
176 /* should we test for errno == ERANGE? */
178 } else if (*pos == '*') {
182 } while(*pos && *pos != '+');
190 int c = tolower(*pos);
192 /* handle special suffixes */
193 if (isalpha(pos[1])) {
195 for (i = 0; stage[i].name; i++) {
196 if (strncasecmp(pos, stage[i].name, stage[i].namelen) == 0
197 && !isalpha(pos[stage[i].namelen])) {
200 component->a = stage[i].value;
201 pos += stage[i].namelen;
212 /* unhandled above */
214 /* use the first letter and skip following */
215 component->a = c - 'a' + 1;
218 } while (isalpha(*pos));
226 /* handle patch number */
229 #ifdef __LONG_LONG_SUPPORTED
230 component->pl = strtoll(pos, &endptr, 10);
232 component->pl = strtol(pos, &endptr, 10);
234 /* should we test for errno == ERANGE? */
243 /* skip trailing separators */
244 while (*pos && !isdigit(*pos) && !isalpha(*pos) && *pos != '+' && *pos != '*') {
252 * version_cmp(pkg1, pkg2) returns -1, 0 or 1 depending on if the version
253 * components of pkg1 is less than, equal to or greater than pkg2. No
254 * comparison of the basenames is done.
256 * The port version is defined by:
257 * ${PORTVERSION}[_${PORTREVISION}][,${PORTEPOCH}]
258 * ${PORTEPOCH} supersedes ${PORTVERSION} supersedes ${PORTREVISION}.
259 * See the commit log for revision 1.349 of ports/Mk/bsd.port.mk
260 * for more information.
262 * The epoch and revision are defined to be a single number, while the rest
263 * of the version should conform to the porting guidelines. It can contain
264 * multiple components, separated by a period, including letters.
266 * The tests allow for significantly more latitude in the version numbers
267 * than is allowed in the guidelines. No point in enforcing them here.
268 * That's what portlint is for.
271 * reimplemented by Oliver Eikemeier
274 version_cmp(const char *pkg1, const char *pkg2)
276 const char *v1, *v2, *ve1, *ve2;
277 unsigned long e1, e2, r1, r2;
280 v1 = split_version(pkg1, &ve1, &e1, &r1);
281 v2 = split_version(pkg2, &ve2, &e2, &r2);
283 /* Check epoch, port version, and port revision, in that order. */
285 result = (e1 < e2 ? -1 : 1);
288 /* Shortcut check for equality before invoking the parsing routines. */
289 if (result == 0 && (ve1 - v1 != ve2 - v2 || strncasecmp(v1, v2, ve1 - v1) != 0)) {
290 /* Loop over different components (the parts separated by dots).
291 * If any component differs, we have the basis for an inequality. */
292 while(result == 0 && (v1 < ve1 || v2 < ve2)) {
295 version_component vc1 = {0, 0, 0};
296 version_component vc2 = {0, 0, 0};
297 if (v1 < ve1 && *v1 != '+') {
298 v1 = get_component(v1, &vc1);
302 if (v2 < ve2 && *v2 != '+') {
303 v2 = get_component(v2, &vc2);
307 if (block_v1 && block_v2) {
312 } else if (vc1.n != vc2.n) {
313 result = (vc1.n < vc2.n ? -1 : 1);
314 } else if (vc1.a != vc2.a) {
315 result = (vc1.a < vc2.a ? -1 : 1);
316 } else if (vc1.pl != vc2.pl) {
317 result = (vc1.pl < vc2.pl ? -1 : 1);
322 /* Compare FreeBSD revision numbers. */
323 if (result == 0 && r1 != r2) {
324 result = (r1 < r2 ? -1 : 1);