xref: /netbsd-src/usr.bin/cvslatest/cvslatest.c (revision 03dcb730d46d34d85c9f496c1f5a3a6a43f2b7b3)
1 /*	$NetBSD: cvslatest.c,v 1.5 2017/01/12 14:27:14 christos 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 HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35 
36 #include <sys/cdefs.h>
37 __RCSID("$NetBSD: cvslatest.c,v 1.5 2017/01/12 14:27:14 christos Exp $");
38 
39 /*
40  * Find the latest timestamp in a set of CVS trees, by examining the
41  * Entries files
42  */
43 
44 #include <sys/param.h>
45 #include <sys/types.h>
46 
47 #include <stdio.h>
48 #include <string.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <err.h>
52 #include <fts.h>
53 #include <time.h>
54 
55 static int debug = 0;
56 static int ignore = 0;
57 
58 struct latest {
59 	time_t time;
60 	char path[MAXPATHLEN];
61 };
62 
63 static void
64 printlat(const struct latest *lat)
65 {
66 	fprintf(stderr, "%s %s", lat->path, ctime(&lat->time));
67 }
68 
69 static void
70 getrepo(const FTSENT *e, char *repo, size_t maxrepo)
71 {
72 	FILE *fp;
73 	char name[MAXPATHLEN], ename[MAXPATHLEN];
74 	char *ptr;
75 
76 	snprintf(name, sizeof(name), "%s/Repository", e->fts_accpath);
77 	snprintf(ename, sizeof(ename), "%s/Repository", e->fts_path);
78 	if ((fp = fopen(name, "r")) == NULL)
79 		err(EXIT_FAILURE, "Can't open `%s'", ename);
80 	if (fgets(repo, (int)maxrepo, fp) == NULL)
81 		err(EXIT_FAILURE, "Can't read `%s'", ename);
82 	if ((ptr = strchr(repo, '\n')) == NULL)
83 		errx(EXIT_FAILURE, "Malformed line in `%s'", ename);
84 	*ptr = '\0';
85 	fclose(fp);
86 }
87 
88 static void
89 getlatest(const FTSENT *e, const char *repo, struct latest *lat)
90 {
91 	static const char fmt[] = "%a %b %d %H:%M:%S %Y";
92 	char name[MAXPATHLEN], ename[MAXPATHLEN];
93 	char entry[MAXPATHLEN * 2];
94 	char *fn, *dt, *p;
95 	time_t t;
96 	struct tm tm;
97 	FILE *fp;
98 
99 	snprintf(name, sizeof(name), "%s/Entries", e->fts_accpath);
100 	snprintf(ename, sizeof(ename), "%s/Entries", e->fts_path);
101 	if ((fp = fopen(name, "r")) == NULL)
102 		err(EXIT_FAILURE, "Can't open `%s'", ename);
103 
104 	while (fgets(entry, (int)sizeof(entry), fp) != NULL) {
105 		if (entry[0] != '/')
106 		    continue;
107 		if ((fn = strtok(entry, "/")) == NULL)
108 			goto mal;
109 		if (strtok(NULL, "/") == NULL)
110 			goto mal;
111 		if ((dt = strtok(NULL, "/")) == NULL)
112 			goto mal;
113 		if ((p = strptime(dt, fmt, &tm)) == NULL || *p) {
114 			warnx("Malformed time `%s' in `%s'", dt, ename);
115 			if (!ignore)
116 				exit(EXIT_FAILURE);
117 		}
118 		tm.tm_isdst = 0;	// We are in GMT anyway
119 		if ((t = mktime(&tm)) == (time_t)-1)
120 			errx(EXIT_FAILURE, "Time conversion `%s' in `%s'",
121 			    dt, ename);
122 		if (lat->time == 0 || lat->time < t) {
123 			lat->time = t;
124 			snprintf(lat->path, sizeof(lat->path),
125 			    "%s/%s", repo, fn);
126 			if (debug > 1)
127 				printlat(lat);
128 		}
129 	}
130 
131 	fclose(fp);
132 	return;
133 
134 mal:
135 	errx(EXIT_FAILURE, "Malformed line in `%s'", ename);
136 }
137 
138 static void
139 cvsscan(char **pathv, const char *name, struct latest *lat)
140 {
141         FTS *dh;
142 	char repo[MAXPATHLEN];
143         FTSENT *entry;
144 
145 	lat->time = 0;
146 
147         dh = fts_open(pathv, FTS_PHYSICAL, NULL);
148         if (dh == NULL)
149 		err(EXIT_FAILURE, "fts_open `%s'", pathv[0]);
150 
151         while ((entry = fts_read(dh)) != NULL) {
152                 if (entry->fts_info != FTS_D)
153 			continue;
154 
155 		if (strcmp(entry->fts_name, name) != 0)
156                         continue;
157 
158 		getrepo(entry, repo, sizeof(repo));
159 		getlatest(entry, repo, lat);
160         }
161 
162         (void)fts_close(dh);
163 }
164 
165 static __dead void
166 usage(void)
167 {
168 	fprintf(stderr, "Usage: %s [-di] [-n <name>] <path> ...\n",
169 	    getprogname());
170 	exit(EXIT_FAILURE);
171 }
172 
173 int
174 main(int argc, char *argv[])
175 {
176 	struct latest lat;
177 	const char *name = "CVS";
178 	int c;
179 
180 	while ((c = getopt(argc, argv, "din:")) != -1)
181 		switch (c) {
182 		case 'i':
183 			ignore++;
184 			break;
185 		case 'd':
186 			debug++;
187 			break;
188 		case 'n':
189 			name = optarg;
190 			break;
191 		default:
192 			usage();
193 		}
194 
195 	if (argc == optind)
196 		usage();
197 
198 	// So that mktime behaves consistently
199 	setenv("TZ", "UTC", 1);
200 
201 	cvsscan(argv + optind, name, &lat);
202 	if (debug)
203 		printlat(&lat);
204 	printf("%jd\n", (intmax_t)lat.time);
205 	return 0;
206 }
207