2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <beat@chruetertee.ch> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Beat Gätzi
7 * ----------------------------------------------------------------------------
10 #include <sys/cdefs.h>
11 __FBSDID("$FreeBSD$");
14 #include <sys/param.h>
23 #include "pathnames.h"
25 typedef struct installedport {
26 struct installedport *next; /* List of installed ports. */
27 char name[LINE_MAX]; /* Name of the installed port. */
32 static char opts[] = "d:f:h";
33 static struct option longopts[] = {
34 { "date", required_argument, NULL, 'd' },
35 { "file", required_argument, NULL, 'f' },
36 { "help", no_argument, NULL, 'h' },
41 * Parse /usr/port/UPDATING for corresponding entries. If no argument is
42 * passed to pkg_updating all entries for all installed ports are displayed.
43 * If a list of portnames is passed to pkg_updating only entries for the
44 * given portnames are displayed. Use the -d option to define that only newer
45 * entries as this date are shown.
48 main(int argc, char *argv[])
50 /* Keyword for searching portname in UPDATING. */
51 const char *affects = "AFFECTS";
52 /* Indicate a date -> end of a entry. Will fail on 2100-01-01... */
53 const char *end = "20";
54 /* Keyword for searching origin portname of installed port. */
55 const char *origin = "@comment ORIGIN:";
56 const char *pkgdbpath = LOG_DIR; /* Location of pkgdb */
57 const char *updatingfile = UPDATING; /* Location of UPDATING */
59 char *date = NULL; /* Passed -d argument */
60 char *dateline = NULL; /* Saved date of an entry */
61 /* Tmp lines for parsing file */
62 char *tmpline1 = NULL;
63 char *tmpline2 = NULL;
65 char originline[LINE_MAX]; /* Line of +CONTENTS */
66 /* Temporary variable to create path to +CONTENTS for installed ports. */
67 char tmp_file[MAXPATHLEN];
68 char updatingline[LINE_MAX]; /* Line of UPDATING */
70 int ch; /* Char used by getopt */
71 int found = 0; /* Found an entry */
72 int linelength; /* Length of parsed line */
73 int maxcharperline = LINE_MAX; /* Max chars per line */
74 int dflag = 0; /* -d option set */
75 /* If pflag = 0 UPDATING will be checked for all installed ports. */
78 size_t n; /* Offset to create path */
80 struct dirent *pkgdbdir; /* pkgdb directory */
81 struct stat attribute; /* attribute of pkgdb element */
83 /* Needed nodes for linked list with installed ports. */
84 INSTALLEDPORT *head = (INSTALLEDPORT *) NULL;
85 INSTALLEDPORT *curr = (INSTALLEDPORT *) NULL;
90 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
97 updatingfile = optarg;
107 /* Check if passed date has a correct format. */
109 linelength = strlen(date);
112 if (strspn(date, "0123456789") != 8) {
113 fprintf(stderr, "unknown date format: %s\n", date);
118 /* Save the list of passed portnames. */
122 if ((curr = (INSTALLEDPORT *)
123 malloc(sizeof(INSTALLEDPORT))) == NULL)
124 (void)exit(EXIT_FAILURE);
125 strlcpy(curr->name, *argv, strlen(*argv) + 1);
133 * UPDATING will be parsed for all installed ports
134 * if no portname is passed.
137 /* Open /var/db/pkg and search for all installed ports. */
138 if ((dir = opendir(pkgdbpath)) != NULL) {
139 while ((pkgdbdir = readdir(dir)) != NULL) {
140 if (strcmp(pkgdbdir->d_name, ".") != 0 &&
141 strcmp(pkgdbdir->d_name, "..") != 0) {
143 /* Create path to +CONTENTS file for each installed port */
144 n = strlcpy(tmp_file, pkgdbpath, strlen(pkgdbpath)+1);
145 n = strlcpy(tmp_file + n, "/", sizeof(tmp_file) - n);
146 n = strlcat(tmp_file + n, pkgdbdir->d_name,
147 sizeof(tmp_file) - n);
148 if (stat(tmp_file, &attribute) == -1) {
149 fprintf(stderr, "can't open %s: %s\n",
150 tmp_file, strerror(errno));
153 if (attribute.st_mode & S_IFREG)
155 (void)strlcat(tmp_file + n, "/",
156 sizeof(tmp_file) - n);
157 (void)strlcat(tmp_file + n, CONTENTS_FNAME,
158 sizeof(tmp_file) - n);
160 /* Open +CONTENT file */
161 fd = fopen(tmp_file, "r");
163 fprintf(stderr, "warning: can't open %s: %s\n",
164 tmp_file, strerror(errno));
169 * Parses +CONTENT for ORIGIN line and
170 * put element into linked list.
172 while (fgets(originline, maxcharperline, fd) != NULL) {
173 tmpline1 = strstr(originline, origin);
174 if (tmpline1 != NULL) {
175 /* Tmp variable to store port name. */
177 pname = strrchr(originline, (int)':');
179 if ((curr = (INSTALLEDPORT *)
180 malloc(sizeof(INSTALLEDPORT))) == NULL)
181 (void)exit(EXIT_FAILURE);
182 if (pname[strlen(pname) - 1] == '\n')
183 pname[strlen(pname) - 1] = '\0';
184 strlcpy (curr->name, pname, strlen(pname)+1);
191 fprintf(stderr, "error reading input\n");
202 /* Fetch UPDATING file if needed and open file */
203 if (isURL(updatingfile)) {
204 if ((fd = fetchGetURL(updatingfile, "")) == NULL) {
205 fprintf(stderr, "Error: Unable to get %s: %s\n",
206 updatingfile, fetchLastErrString);
207 exit(EX_UNAVAILABLE);
211 fd = fopen(updatingfile, "r");
214 fprintf(stderr, "can't open %s: %s\n",
215 updatingfile, strerror(errno));
216 exit(EX_UNAVAILABLE);
219 /* Parse opened UPDATING file. */
220 while (fgets(updatingline, maxcharperline, fd) != NULL) {
221 /* No entry is found so far */
223 /* Search for AFFECTS line to parse the portname. */
224 tmpline1 = strstr(updatingline, affects);
226 if (tmpline1 != NULL) {
228 while (curr != NULL) {
229 tmpline2 = strstr(updatingline, curr->name);
230 if (tmpline2 != NULL)
234 if (tmpline2 != NULL) {
235 /* If -d is set, check if entry is newer than the date. */
236 if ((dflag == 1) && (strncmp(dateline, date, 8) < 0))
238 printf("%s", dateline);
239 printf("%s", updatingline);
244 /* Search for the end of an entry, if not found print the line. */
246 tmpline1 = strstr(updatingline, end);
247 if (tmpline1 == NULL)
248 printf("%s", updatingline);
250 linelength = strlen(updatingline);
251 if (linelength == 10)
254 printf("%s", updatingline);
257 /* Save the actual line, it could be a date. */
258 dateline = strdup(updatingline);
262 fprintf(stderr, "error reading input\n");
274 "usage: pkg_updating [-h] [-d YYYYMMDD] [-f file] [portname ...]\n");