xref: /netbsd-src/usr.bin/cvslatest/cvslatest.c (revision 796c32c94f6e154afc9de0f63da35c91bb739b45)
1 /*	$NetBSD: cvslatest.c,v 1.6 2017/09/24 09:43:27 joerg Exp $	*/
2 
3 /*-
4  * Copyright (c) 2016 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifdef __linux__
33 #define _GNU_SOURCE
34 #endif
35 
36 #ifdef HAVE_NBTOOL_CONFIG_H
37 #include "nbtool_config.h"
38 #endif
39 
40 #include <sys/cdefs.h>
41 __RCSID("$NetBSD: cvslatest.c,v 1.6 2017/09/24 09:43:27 joerg Exp $");
42 
43 /*
44  * Find the latest timestamp in a set of CVS trees, by examining the
45  * Entries files
46  */
47 
48 #include <sys/param.h>
49 #include <sys/types.h>
50 
51 #include <stdio.h>
52 #include <string.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55 #include <err.h>
56 #include <fts.h>
57 #include <time.h>
58 
59 static int debug = 0;
60 static int ignore = 0;
61 
62 struct latest {
63 	time_t time;
64 	char path[MAXPATHLEN];
65 };
66 
67 static void
68 printlat(const struct latest *lat)
69 {
70 	fprintf(stderr, "%s %s", lat->path, ctime(&lat->time));
71 }
72 
73 static void
74 getrepo(const FTSENT *e, char *repo, size_t maxrepo)
75 {
76 	FILE *fp;
77 	char name[MAXPATHLEN], ename[MAXPATHLEN];
78 	char *ptr;
79 
80 	snprintf(name, sizeof(name), "%s/Repository", e->fts_accpath);
81 	snprintf(ename, sizeof(ename), "%s/Repository", e->fts_path);
82 	if ((fp = fopen(name, "r")) == NULL)
83 		err(EXIT_FAILURE, "Can't open `%s'", ename);
84 	if (fgets(repo, (int)maxrepo, fp) == NULL)
85 		err(EXIT_FAILURE, "Can't read `%s'", ename);
86 	if ((ptr = strchr(repo, '\n')) == NULL)
87 		errx(EXIT_FAILURE, "Malformed line in `%s'", ename);
88 	*ptr = '\0';
89 	fclose(fp);
90 }
91 
92 static void
93 getlatest(const FTSENT *e, const char *repo, struct latest *lat)
94 {
95 	static const char fmt[] = "%a %b %d %H:%M:%S %Y";
96 	char name[MAXPATHLEN], ename[MAXPATHLEN];
97 	char entry[MAXPATHLEN * 2];
98 	char *fn, *dt, *p;
99 	time_t t;
100 	struct tm tm;
101 	FILE *fp;
102 
103 	snprintf(name, sizeof(name), "%s/Entries", e->fts_accpath);
104 	snprintf(ename, sizeof(ename), "%s/Entries", e->fts_path);
105 	if ((fp = fopen(name, "r")) == NULL)
106 		err(EXIT_FAILURE, "Can't open `%s'", ename);
107 
108 	while (fgets(entry, (int)sizeof(entry), fp) != NULL) {
109 		if (entry[0] != '/')
110 		    continue;
111 		if ((fn = strtok(entry, "/")) == NULL)
112 			goto mal;
113 		if (strtok(NULL, "/") == NULL)
114 			goto mal;
115 		if ((dt = strtok(NULL, "/")) == NULL)
116 			goto mal;
117 		if ((p = strptime(dt, fmt, &tm)) == NULL || *p) {
118 			warnx("Malformed time `%s' in `%s'", dt, ename);
119 			if (!ignore)
120 				exit(EXIT_FAILURE);
121 		}
122 		tm.tm_isdst = 0;	// We are in GMT anyway
123 		if ((t = mktime(&tm)) == (time_t)-1)
124 			errx(EXIT_FAILURE, "Time conversion `%s' in `%s'",
125 			    dt, ename);
126 		if (lat->time == 0 || lat->time < t) {
127 			lat->time = t;
128 			snprintf(lat->path, sizeof(lat->path),
129 			    "%s/%s", repo, fn);
130 			if (debug > 1)
131 				printlat(lat);
132 		}
133 	}
134 
135 	fclose(fp);
136 	return;
137 
138 mal:
139 	errx(EXIT_FAILURE, "Malformed line in `%s'", ename);
140 }
141 
142 static void
143 cvsscan(char **pathv, const char *name, struct latest *lat)
144 {
145         FTS *dh;
146 	char repo[MAXPATHLEN];
147         FTSENT *entry;
148 
149 	lat->time = 0;
150 
151         dh = fts_open(pathv, FTS_PHYSICAL, NULL);
152         if (dh == NULL)
153 		err(EXIT_FAILURE, "fts_open `%s'", pathv[0]);
154 
155         while ((entry = fts_read(dh)) != NULL) {
156                 if (entry->fts_info != FTS_D)
157 			continue;
158 
159 		if (strcmp(entry->fts_name, name) != 0)
160                         continue;
161 
162 		getrepo(entry, repo, sizeof(repo));
163 		getlatest(entry, repo, lat);
164         }
165 
166         (void)fts_close(dh);
167 }
168 
169 static __dead void
170 usage(void)
171 {
172 	fprintf(stderr, "Usage: %s [-di] [-n <name>] <path> ...\n",
173 	    getprogname());
174 	exit(EXIT_FAILURE);
175 }
176 
177 int
178 main(int argc, char *argv[])
179 {
180 	struct latest lat;
181 	const char *name = "CVS";
182 	int c;
183 
184 	while ((c = getopt(argc, argv, "din:")) != -1)
185 		switch (c) {
186 		case 'i':
187 			ignore++;
188 			break;
189 		case 'd':
190 			debug++;
191 			break;
192 		case 'n':
193 			name = optarg;
194 			break;
195 		default:
196 			usage();
197 		}
198 
199 	if (argc == optind)
200 		usage();
201 
202 	// So that mktime behaves consistently
203 	setenv("TZ", "UTC", 1);
204 
205 	cvsscan(argv + optind, name, &lat);
206 	if (debug)
207 		printlat(&lat);
208 	printf("%jd\n", (intmax_t)lat.time);
209 	return 0;
210 }
211