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