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$");
19 #include "pathnames.h"
21 typedef struct installedport {
22 struct installedport *next; /* List of installed ports. */
23 char name[LINE_MAX]; /* Name of the installed port. */
28 static char opts[] = "d:f:h";
29 static struct option longopts[] = {
30 { "date", required_argument, NULL, 'd' },
31 { "file", required_argument, NULL, 'f' },
32 { "help", no_argument, NULL, 'h' },
37 * Parse /usr/port/UPDATING for corresponding entries. If no argument is
38 * passed to pkg_updating all entries for all installed ports are displayed.
39 * If a list of portnames is passed to pkg_updating only entries for the
40 * given portnames are displayed. Use the -d option to define that only newer
41 * entries as this date are shown.
44 main(int argc, char *argv[])
46 /* Keyword for searching portname in UPDATING. */
47 const char *affects = "AFFECTS";
48 /* Indicate a date -> end of a entry. Will fail on 2100-01-01... */
49 const char *end = "20";
50 /* Keyword for searching origin portname of installed port. */
51 const char *origin = "@comment ORIGIN:";
52 const char *pkgdbpath = LOG_DIR; /* Location of pkgdb */
53 const char *updatingfile = UPDATING; /* Location of UPDATING */
55 char *date = NULL; /* Passed -d argument */
56 char *dateline = NULL; /* Saved date of an entry */
57 /* Tmp lines for parsing file */
58 char *tmpline1 = NULL;
59 char *tmpline2 = NULL;
61 char originline[LINE_MAX]; /* Line of +CONTENTS */
62 /* Temporary variable to create path to +CONTENTS for installed ports. */
63 char tmp_file[MAXPATHLEN];
64 char updatingline[LINE_MAX]; /* Line of UPDATING */
66 int ch; /* Char used by getopt */
67 int found = 0; /* Found an entry */
68 int linelength; /* Length of parsed line */
69 int maxcharperline = LINE_MAX; /* Max chars per line */
70 int dflag = 0; /* -d option set */
71 /* If pflag = 0 UPDATING will be checked for all installed ports. */
74 size_t n; /* Offset to create path */
76 struct dirent *pkgdbdir; /* pkgdb directory */
77 struct stat attribute; /* attribute of pkgdb element */
79 /* Needed nodes for linked list with installed ports. */
80 INSTALLEDPORT *head = (INSTALLEDPORT *) NULL;
81 INSTALLEDPORT *curr = (INSTALLEDPORT *) NULL;
86 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
93 updatingfile = optarg;
103 /* Check if passed date has a correct format. */
105 linelength = strlen(date);
108 if (strspn(date, "0123456789") != 8) {
109 fprintf(stderr, "unknown date format: %s\n", date);
114 /* Save the list of passed portnames. */
118 if((curr = (INSTALLEDPORT *)
119 malloc(sizeof(INSTALLEDPORT))) == NULL)
120 (void)exit(EXIT_FAILURE);
121 strlcpy (curr->name, *argv, strlen(*argv) + 1);
129 * UPDATING will be parsed for all installed ports
130 * if no portname is passed.
133 /* Open /var/db/pkg and search for all installed ports. */
134 if((dir = opendir(pkgdbpath)) != NULL) {
135 while ((pkgdbdir = readdir(dir)) != NULL) {
136 if (strcmp(pkgdbdir->d_name, ".") != 0 &&
137 strcmp(pkgdbdir->d_name, "..") !=0) {
139 /* Create path to +CONTENTS file for each installed port */
140 n = strlcpy(tmp_file, pkgdbpath, strlen(pkgdbpath)+1);
141 n = strlcpy(tmp_file + n, "/", sizeof(tmp_file) - n);
142 n = strlcat(tmp_file + n, pkgdbdir->d_name,
143 sizeof(tmp_file) - n);
144 if(stat(tmp_file, &attribute) == -1) {
145 fprintf(stderr, "can't open %s: %s\n",
146 tmp_file, strerror(errno));
149 if(attribute.st_mode & S_IFREG)
151 (void)strlcat(tmp_file + n, "/",
152 sizeof(tmp_file) - n);
153 (void)strlcat(tmp_file + n, CONTENTS_FNAME,
154 sizeof(tmp_file) - n);
156 /* Open +CONTENT file */
157 fd = fopen(tmp_file, "r");
159 fprintf(stderr, "warning: can't open %s: %s\n",
160 tmp_file, strerror(errno));
165 * Parses +CONTENT for ORIGIN line and
166 * put element into linked list.
168 while(fgets(originline, maxcharperline, fd) != NULL) {
169 tmpline1 = strstr(originline, origin);
170 if( tmpline1 != NULL ) {
171 /* Tmp variable to store port name. */
173 pname = strrchr(originline, (int)':');
175 if((curr = (INSTALLEDPORT *)
176 malloc(sizeof(INSTALLEDPORT))) == NULL)
177 (void)exit(EXIT_FAILURE);
178 if (pname[strlen(pname) - 1] == '\n')
179 pname[strlen(pname) - 1] = '\0';
180 strlcpy (curr->name, pname, strlen(pname)+1);
187 fprintf(stderr, "error reading input\n");
198 /* Open UPDATING file */
199 fd = fopen(updatingfile, "r");
201 fprintf(stderr, "can't open %s: %s\n",
202 updatingfile, strerror(errno));
203 exit(EX_UNAVAILABLE);
206 /* Parse opened UPDATING file. */
207 while(fgets(updatingline, maxcharperline, fd) != NULL) {
208 /* No entry is found so far */
210 /* Search for AFFECTS line to parse the portname. */
211 tmpline1 = strstr(updatingline, affects);
213 if( tmpline1 != NULL ) {
215 while(curr != NULL) {
216 tmpline2 = strstr(updatingline, curr->name);
217 if( tmpline2 != NULL )
221 if( tmpline2 != NULL ) {
222 /* If -d is set, check if entry is newer than the date. */
223 if ( (dflag == 1) && (strncmp(dateline, date, 8) < 0))
225 printf("%s", dateline);
226 printf("%s", updatingline);
231 /* Search for the end of an entry, if not found print the line. */
233 tmpline1 = strstr(updatingline, end);
234 if( tmpline1 == NULL )
235 printf("%s", updatingline);
237 linelength = strlen(updatingline);
238 if (linelength == 10)
241 printf("%s", updatingline);
244 /* Save the actual line, it could be a date. */
245 dateline = strdup(updatingline);
249 fprintf(stderr, "error reading input\n");
261 "usage: pkg_updating [-h] [-d YYYYMMDD] [-f file] [portname ...]\n");