2 * FreeBSD install - a package for the installation and maintainance
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 * This is the version module. Based on pkg_version.pl by Bruce A. Mah.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
31 struct index_head Index = SLIST_HEAD_INITIALIZER(Index);
33 static int pkg_do(char *);
34 static void show_version(Package, const char *, const char *);
37 * This is the traditional pkg_perform, except that the argument is _not_
38 * a list of packages. It is the index file from the command line.
40 * We loop over the installed packages, matching them with the -s flag
41 * if needed and calling pkg_do(). Before hand we set up a few things,
42 * and after we tear them down...
45 pkg_perform(char **indexarg)
47 char tmp[PATH_MAX], **pkgs, *pat[2], **patterns;
48 struct index_entry *ie;
53 * Try to find and open the INDEX. We only check IndexFile != NULL
54 * later, if we actually need the INDEX.
56 if (*indexarg == NULL)
57 snprintf(tmp, PATH_MAX, "%s/%s", PORTS_DIR, INDEX_FNAME);
59 strlcpy(tmp, *indexarg, PATH_MAX);
61 IndexFile = fetchGetURL(tmp, "");
63 IndexFile = fopen(tmp, "r");
65 /* Get either a list of matching or all packages */
66 if (MatchName != NULL) {
69 MatchType = RegexExtended ? MATCH_EREGEX : MATCH_REGEX;
72 MatchType = MATCH_ALL;
76 if (LookUpOrigin != NULL)
77 pkgs = matchbyorigin(LookUpOrigin, &err_cnt);
79 pkgs = matchinstalled(MatchType, patterns, &err_cnt);
82 errx(2, "Unable to find package database directory!");
84 if (LookUpOrigin != NULL) {
85 warnx("no packages recorded with this origin");
90 warnx("no packages installed");
94 warnx("no packages match pattern");
102 for (i = 0; pkgs[i] != NULL; i++)
103 err_cnt += pkg_do(pkgs[i]);
105 /* If we opened the INDEX in pkg_do(), clean up. */
106 while (!SLIST_EMPTY(&Index)) {
107 ie = SLIST_FIRST(&Index);
108 SLIST_REMOVE_HEAD(&Index, next);
109 if (ie->name != NULL)
111 if (ie->origin != NULL)
115 if (IndexFile != NULL)
122 * Traditional pkg_do(). We take the package name we are passed and
123 * first slurp in the CONTENTS file, getting name and origin, then
124 * we look for it's corresponding Makefile. If that fails we pull in
125 * the INDEX, and check there.
130 char *ch, tmp[PATH_MAX], tmp2[PATH_MAX], *latest = NULL;
132 struct index_entry *ie;
136 /* Suck in the contents list. */
137 plist.head = plist.tail = NULL;
138 plist.name = plist.origin = NULL;
139 snprintf(tmp, PATH_MAX, "%s/%s/%s", LOG_DIR, pkg, CONTENTS_FNAME);
140 fp = fopen(tmp, "r");
142 warnx("the package info for package '%s' is corrupt", pkg);
145 read_plist(&plist, fp);
147 if (plist.name == NULL) {
148 warnx("%s does not appear to be a valid package!", pkg);
153 * First we check if the installed package has an origin, and try
154 * looking for it's Makefile. If we find the Makefile we get the
155 * latest version from there. If we fail, we start looking in the
156 * INDEX, first matching the origin and then the package name.
158 if (plist.origin != NULL && !UseINDEXOnly) {
159 snprintf(tmp, PATH_MAX, "%s/%s", PORTS_DIR, plist.origin);
160 if (isdir(tmp) && chdir(tmp) != FAIL && isfile("Makefile")) {
161 if ((latest = vpipe("/usr/bin/make -V PKGNAME", tmp)) == NULL)
162 warnx("Failed to get PKGNAME from %s/Makefile!", tmp);
164 show_version(plist, latest, "port");
167 if (latest == NULL) {
168 /* Report package as not found in INDEX if the INDEX is not required. */
169 if (IndexFile == NULL && !UseINDEXOnly)
170 show_version(plist, NULL, plist.origin);
172 /* We only pull in the INDEX once, if needed. */
173 if (SLIST_EMPTY(&Index)) {
175 errx(2, "Unable to open INDEX in %s.", __func__);
176 while ((ch = fgetln(IndexFile, &len)) != NULL) {
178 * Don't use strlcpy() because fgetln() doesn't
179 * return a valid C string.
181 strncpy(tmp, ch, MIN(len, PATH_MAX));
182 tmp[PATH_MAX-1] = '\0';
183 /* The INDEX has pkgname|portdir|... */
184 if ((ch = strchr(tmp, '|')) != NULL)
186 if (ch != NULL && (ch = strchr(&ch[1], '|')) != NULL)
188 /* Look backwards for the last two dirs = origin */
189 while (ch != NULL && *--ch != '/')
192 while (ch != NULL && *--ch != '/')
196 errx(2, "The INDEX does not appear to be valid!");
197 if ((ie = malloc(sizeof(struct index_entry))) == NULL)
198 errx(2, "Unable to allocate memory in %s.", __func__);
199 bzero(ie, sizeof(struct index_entry));
200 ie->name = strdup(tmp);
201 ie->origin = strdup(&ch[1]);
202 /* Who really cares if we reverse the index... */
203 SLIST_INSERT_HEAD(&Index, ie, next);
206 /* Now that we've slurped in the INDEX... */
207 SLIST_FOREACH(ie, &Index, next) {
208 if (plist.origin != NULL) {
209 if (strcmp(plist.origin, ie->origin) == 0)
210 latest = strdup(ie->name);
212 strlcpy(tmp, ie->name, PATH_MAX);
213 strlcpy(tmp2, plist.name, PATH_MAX);
214 /* Chop off the versions and compare. */
215 if ((ch = strrchr(tmp, '-')) == NULL)
216 errx(2, "The INDEX does not appear to be valid!");
218 if ((ch = strrchr(tmp2, '-')) == NULL)
219 warnx("%s is not a valid package!", plist.name);
222 if (strcmp(tmp2, tmp) == 0) {
223 if (latest != NULL) {
224 /* Multiple matches */
225 snprintf(tmp, PATH_MAX, "%s|%s", latest, ie->name);
227 latest = strdup(tmp);
229 latest = strdup(ie->name);
234 show_version(plist, NULL, NULL);
236 show_version(plist, latest, "index");
245 #define OUTPUT(c) ((PreventChars != NULL && !strchr(PreventChars, (c))) || \
246 (LimitChars != NULL && strchr(LimitChars, (c))) || \
247 (PreventChars == NULL && LimitChars == NULL))
250 * Do the work of comparing and outputing. Ugly, but well that's what
251 * You get when you try to match perl output in C ;-).
254 show_version(Package plist, const char *latest, const char *source)
256 char *ch, tmp[PATH_MAX];
260 if (!plist.name || strlen(plist.name) == 0)
262 if (ShowOrigin != FALSE && plist.origin != NULL)
263 strlcpy(tmp, plist.origin, PATH_MAX);
265 strlcpy(tmp, plist.name, PATH_MAX);
267 if ((ch = strrchr(tmp, '-')) != NULL)
271 if (latest == NULL) {
272 if (source == NULL && OUTPUT('!')) {
273 printf("%-34s !", tmp);
275 printf(" Comparison failed");
277 } else if (OUTPUT('?')) {
278 printf("%-34s ?", tmp);
280 printf(" orphaned: %s", plist.origin);
283 } else if (strchr(latest,'|') != NULL) {
285 printf("%-34s *", tmp);
287 strlcpy(tmp, latest, PATH_MAX);
288 ch = strchr(tmp, '|');
291 ver = strrchr(tmp, '-');
292 ver = ver ? &ver[1] : tmp;
293 printf(" multiple versions (index has %s", ver);
295 ver = strrchr(&ch[1], '-');
296 ver = ver ? &ver[1] : &ch[1];
297 if ((ch = strchr(&ch[1], '|')) != NULL)
300 } while (ch != NULL);
306 cmp = version_cmp(plist.name, latest);
307 ver = strrchr(latest, '-');
308 ver = ver ? &ver[1] : latest;
309 if (cmp < 0 && OUTPUT('<')) {
310 printf("%-34s %c", tmp, Quiet ? '\0' : '<');
312 printf(" needs updating (%s has %s)", source, ver);
314 } else if (cmp == 0 && OUTPUT('=')) {
315 printf("%-34s %c", tmp, Quiet ? '\0' : '=');
317 printf(" up-to-date with %s", source);
319 } else if (cmp > 0 && OUTPUT('>')) {
320 printf("%-34s %c", tmp, Quiet ? '\0' : '>');
322 printf(" succeeds %s (%s has %s)", source, source, ver);
329 version_match(char *pattern, const char *pkgname)
334 Boolean isTMP = FALSE;
336 if (isURL(pkgname)) {
337 fp = fetchGetURL(pkgname, "");
341 errx(2, "Unable to open %s.", pkgname);
342 } else if (pkgname[0] == '/') {
343 fp = fopen(pkgname, "r");
347 errx(2, "Unable to open %s.", pkgname);
348 } else if (strcmp(pkgname, "-") == 0) {
351 } else if (isURL(pattern)) {
352 fp = fetchGetURL(pattern, "");
356 errx(2, "Unable to open %s.", pattern);
357 } else if (pattern[0] == '/') {
358 fp = fopen(pattern, "r");
362 errx(2, "Unable to open %s.", pattern);
363 } else if (strcmp(pattern, "-") == 0) {
367 ret = pattern_match(MATCH_GLOB, pattern, pkgname);
373 while ((line = fgetln(fp, &len)) != NULL) {
377 if (len > 0 && line[len-1] == '\n')
380 if (lnlen > sizeof(ln)-1)
381 lnlen = sizeof(ln)-1;
382 memcpy(ln, line, lnlen);
384 if ((ch = strchr(ln, '|')) != NULL)
387 match = pattern_match(MATCH_GLOB, pattern, ln);
389 match = pattern_match(MATCH_GLOB, ln, pkgname);
392 printf("%.*s\n", (int)len, line);