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.
19 #include <sys/cdefs.h>
20 __FBSDID("$FreeBSD$");
26 * This routine could easily go somewhere else.
32 return LIBPKG_VERSION;
36 * Routines to assist with PLIST_FMT_VER numbers in the packing
39 * Following is the PLIST_FMT_VER history:
40 * 1.0 - Initial revision;
41 * 1.1 - When recording/checking checksum of symlink use hash of readlink()
42 * value instead of the hash of an object this links points to.
46 verscmp(Package *pkg, int major, int minor)
50 if ((pkg->fmtver_maj < major) || (pkg->fmtver_maj == major &&
51 pkg->fmtver_mnr < minor))
53 else if ((pkg->fmtver_maj > major) || (pkg->fmtver_maj == major &&
54 pkg->fmtver_mnr > minor))
61 * split_version(pkgname, endname, epoch, revision) returns a pointer to
62 * the version portion of a package name and the two special components.
64 * Syntax is: ${PORTNAME}-${PORTVERSION}[_${PORTREVISION}][,${PORTEPOCH}]
66 * Written by Oliver Eikemeier
67 * Based on work of Jeremy D. Lea.
70 split_version(const char *pkgname, const char **endname, unsigned long *epoch, unsigned long *revision)
73 const char *versionstr;
74 const char *endversionstr;
77 errx(2, "%s: Passed NULL pkgname.", __func__);
79 /* Look for the last '-' the the pkgname */
80 ch = strrchr(pkgname, '-');
81 /* Cheat if we are just passed a version, not a valid package name */
82 versionstr = ch ? ch + 1 : pkgname;
84 /* Look for the last '_' in the version string, advancing the end pointer */
85 ch = strrchr(versionstr, '_');
86 if (revision != NULL) {
87 *revision = ch ? strtoul(ch + 1, NULL, 10) : 0;
91 /* Look for the last ',' in the remaining version string */
92 ch = strrchr(endversionstr ? endversionstr + 1 : versionstr, ',');
94 *epoch = ch ? strtoul(ch + 1, NULL, 10) : 0;
96 if (ch && !endversionstr)
99 /* set the pointer behind the last character of the version without revision or epoch */
101 *endname = endversionstr ? endversionstr : strrchr(versionstr, '\0');
107 * PORTVERSIONs are composed of components separated by dots. A component
108 * consists of a version number, a letter and a patchlevel number. This does
109 * not conform to the porter's handbook, but let us formulate rules that
110 * fit the current practice and are far simpler than to make decisions
111 * based on the order of netters and lumbers. Besides, people use versions
112 * like 10b2 in the ports...
116 #ifdef __LONG_LONG_SUPPORTED
127 * get_component(position, component) gets the value of the next component
128 * (number - letter - number triple) and returns a pointer to the next character
129 * after any leading separators
131 * - components are separated by dots
132 * - characters !~ [a-zA-Z0-9.+*] are treated as separators
133 * (1.0:2003.09.16 = 1.0.2003.09.16), this may not be what you expect:
134 * 1.0.1:2003.09.16 < 1.0:2003.09.16
135 * - consecutive separators are collapsed (10..1 = 10.1)
136 * - missing separators are inserted, essentially
137 * letter number letter => letter number . letter (10a1b2 = 10a1.b2)
138 * - missing components are assumed to be equal to 0 (10 = 10.0 = 10.0.0)
139 * - the letter sort order is: [none], a, b, ..., z; numbers without letters
140 * sort first (10 < 10a < 10b)
141 * - missing version numbers (in components starting with a letter) sort as -1
143 * - a separator is inserted before the special strings "pl", "alpha", "beta",
145 * - "pl" sorts before every other letter, "alpha", "beta", "pre" and "rc"
146 * sort as a, b, p and r. (10alpha = 10.a < 10, but 10 < 10a; pl11 < alpha3
147 * < 0.1beta2 = 0.1.b2 < 0.1)
148 * - other strings use only the first letter for sorting, case is ignored
149 * (1.d2 = 1.dev2 = 1.Development2)
150 * - The special component `*' is guaranteed to be the smallest possible
151 * component (2.* < 2pl1 < 2alpha3 < 2.9f7 < 3.*)
152 * - components separated by `+' are handled by version_cmp below
157 static const struct {
163 { "alpha", 5, 'a'-'a'+1 },
164 { "beta", 4, 'b'-'a'+1 },
165 { "pre", 3, 'p'-'a'+1 },
166 { "rc", 2, 'r'-'a'+1 },
171 get_component(const char *position, version_component *component)
173 const char *pos = position;
174 int hasstage = 0, haspatchlevel = 0;
177 errx(2, "%s: Passed NULL position.", __func__);
179 /* handle version number */
182 #ifdef __LONG_LONG_SUPPORTED
183 component->n = strtoll(pos, &endptr, 10);
185 component->n = strtol(pos, &endptr, 10);
187 /* should we test for errno == ERANGE? */
189 } else if (*pos == '*') {
193 } while(*pos && *pos != '+');
201 int c = tolower(*pos);
203 /* handle special suffixes */
204 if (isalpha(pos[1])) {
206 for (i = 0; stage[i].name; i++) {
207 if (strncasecmp(pos, stage[i].name, stage[i].namelen) == 0
208 && !isalpha(pos[stage[i].namelen])) {
211 component->a = stage[i].value;
212 pos += stage[i].namelen;
223 /* unhandled above */
225 /* use the first letter and skip following */
226 component->a = c - 'a' + 1;
229 } while (isalpha(*pos));
237 /* handle patch number */
240 #ifdef __LONG_LONG_SUPPORTED
241 component->pl = strtoll(pos, &endptr, 10);
243 component->pl = strtol(pos, &endptr, 10);
245 /* should we test for errno == ERANGE? */
254 /* skip trailing separators */
255 while (*pos && !isdigit(*pos) && !isalpha(*pos) && *pos != '+' && *pos != '*') {
263 * version_cmp(pkg1, pkg2) returns -1, 0 or 1 depending on if the version
264 * components of pkg1 is less than, equal to or greater than pkg2. No
265 * comparison of the basenames is done.
267 * The port version is defined by:
268 * ${PORTVERSION}[_${PORTREVISION}][,${PORTEPOCH}]
269 * ${PORTEPOCH} supersedes ${PORTVERSION} supersedes ${PORTREVISION}.
270 * See the commit log for revision 1.349 of ports/Mk/bsd.port.mk
271 * for more information.
273 * The epoch and revision are defined to be a single number, while the rest
274 * of the version should conform to the porting guidelines. It can contain
275 * multiple components, separated by a period, including letters.
277 * The tests allow for significantly more latitude in the version numbers
278 * than is allowed in the guidelines. No point in enforcing them here.
279 * That's what portlint is for.
282 * reimplemented by Oliver Eikemeier
285 version_cmp(const char *pkg1, const char *pkg2)
287 const char *v1, *v2, *ve1, *ve2;
288 unsigned long e1, e2, r1, r2;
291 v1 = split_version(pkg1, &ve1, &e1, &r1);
292 v2 = split_version(pkg2, &ve2, &e2, &r2);
294 /* Check epoch, port version, and port revision, in that order. */
296 result = (e1 < e2 ? -1 : 1);
299 /* Shortcut check for equality before invoking the parsing routines. */
300 if (result == 0 && (ve1 - v1 != ve2 - v2 || strncasecmp(v1, v2, ve1 - v1) != 0)) {
301 /* Loop over different components (the parts separated by dots).
302 * If any component differs, we have the basis for an inequality. */
303 while(result == 0 && (v1 < ve1 || v2 < ve2)) {
306 version_component vc1 = {0, 0, 0};
307 version_component vc2 = {0, 0, 0};
308 if (v1 < ve1 && *v1 != '+') {
309 v1 = get_component(v1, &vc1);
313 if (v2 < ve2 && *v2 != '+') {
314 v2 = get_component(v2, &vc2);
318 if (block_v1 && block_v2) {
323 } else if (vc1.n != vc2.n) {
324 result = (vc1.n < vc2.n ? -1 : 1);
325 } else if (vc1.a != vc2.a) {
326 result = (vc1.a < vc2.a ? -1 : 1);
327 } else if (vc1.pl != vc2.pl) {
328 result = (vc1.pl < vc2.pl ? -1 : 1);
333 /* Compare FreeBSD revision numbers. */
334 if (result == 0 && r1 != r2) {
335 result = (r1 < r2 ? -1 : 1);