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