xref: /netbsd-src/usr.bin/cvslatest/cvslatest.c (revision 9616dacfef448e70e3fbbd865bddf60d54b656c5)
1 /*	$NetBSD: cvslatest.c,v 1.4 2016/12/26 14:53:17 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.4 2016/12/26 14:53:17 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 char *path, char *repo, size_t maxrepo)
71 {
72 	FILE *fp;
73 	char name[MAXPATHLEN];
74 	char *ptr;
75 
76 	snprintf(name, sizeof(name), "%s/Repository", path);
77 	if ((fp = fopen(name, "r")) == NULL)
78 		err(EXIT_FAILURE, "Can't open `%s'", name);
79 	if (fgets(repo, (int)maxrepo, fp) == NULL)
80 		err(EXIT_FAILURE, "Can't read `%s'", name);
81 	if ((ptr = strchr(repo, '\n')) == NULL)
82 		errx(EXIT_FAILURE, "Malformed line in `%s'", name);
83 	*ptr = '\0';
84 	fclose(fp);
85 }
86 
87 static void
88 getlatest(const char *path, const char *repo, struct latest *lat)
89 {
90 	static const char fmt[] = "%a %b %d %H:%M:%S %Y";
91 	char name[MAXPATHLEN];
92 	char entry[MAXPATHLEN * 2];
93 	char *fn, *dt, *p;
94 	time_t t;
95 	struct tm tm;
96 	FILE *fp;
97 
98 	snprintf(name, sizeof(name), "%s/Entries", path);
99 	if ((fp = fopen(name, "r")) == NULL)
100 		err(EXIT_FAILURE, "Can't open `%s'", name);
101 
102 	while (fgets(entry, (int)sizeof(entry), fp) != NULL) {
103 		if (entry[0] != '/')
104 		    continue;
105 		if ((fn = strtok(entry, "/")) == NULL)
106 			goto mal;
107 		if (strtok(NULL, "/") == NULL)
108 			goto mal;
109 		if ((dt = strtok(NULL, "/")) == NULL)
110 			goto mal;
111 		if ((p = strptime(dt, fmt, &tm)) == NULL || *p) {
112 			warnx("Malformed time `%s' in `%s'", dt, name);
113 			if (!ignore)
114 				exit(EXIT_FAILURE);
115 		}
116 		tm.tm_isdst = 0;	// We are in GMT anyway
117 		if ((t = mktime(&tm)) == (time_t)-1)
118 			errx(EXIT_FAILURE, "Time conversion `%s' in `%s'",
119 			    dt, name);
120 		if (lat->time == 0 || lat->time < t) {
121 			lat->time = t;
122 			snprintf(lat->path, sizeof(lat->path),
123 			    "%s/%s", repo, fn);
124 			if (debug > 1)
125 				printlat(lat);
126 		}
127 	}
128 
129 	fclose(fp);
130 	return;
131 
132 mal:
133 	errx(EXIT_FAILURE, "Malformed line in `%s'", name);
134 }
135 
136 static void
137 cvsscan(char **pathv, const char *name, struct latest *lat)
138 {
139         FTS *dh;
140 	char repo[MAXPATHLEN];
141         FTSENT *entry;
142 
143 	lat->time = 0;
144 
145         dh = fts_open(pathv, FTS_PHYSICAL, NULL);
146         if (dh == NULL)
147 		err(EXIT_FAILURE, "fts_open `%s'", pathv[0]);
148 
149         while ((entry = fts_read(dh)) != NULL) {
150                 if (entry->fts_info != FTS_D)
151 			continue;
152 
153 		if (strcmp(entry->fts_name, name) != 0)
154                         continue;
155 
156 		getrepo(entry->fts_accpath, repo, sizeof(repo));
157 		getlatest(entry->fts_accpath, repo, lat);
158         }
159 
160         (void)fts_close(dh);
161 }
162 
163 static __dead void
164 usage(void)
165 {
166 	fprintf(stderr, "Usage: %s [-di] [-n <name>] <path> ...\n",
167 	    getprogname());
168 	exit(EXIT_FAILURE);
169 }
170 
171 int
172 main(int argc, char *argv[])
173 {
174 	struct latest lat;
175 	const char *name = "CVS";
176 	int c;
177 
178 	while ((c = getopt(argc, argv, "din:")) != -1)
179 		switch (c) {
180 		case 'i':
181 			ignore++;
182 			break;
183 		case 'd':
184 			debug++;
185 			break;
186 		case 'n':
187 			name = optarg;
188 			break;
189 		default:
190 			usage();
191 		}
192 
193 	if (argc == optind)
194 		usage();
195 
196 	// So that mktime behaves consistently
197 	setenv("TZ", "UTC", 1);
198 
199 	cvsscan(argv + optind, name, &lat);
200 	if (debug)
201 		printlat(&lat);
202 	printf("%jd\n", (intmax_t)lat.time);
203 	return 0;
204 }
205