xref: /onnv-gate/usr/src/tools/findunref/findunref.c (revision 10313:ca42e2f0424a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57078Smjnelson  * Common Development and Distribution License (the "License").
67078Smjnelson  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
20*10313SPeter.Memishian@Sun.COM  *
21*10313SPeter.Memishian@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
227078Smjnelson  * Use is subject to license terms.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate  * Finds all unreferenced files in a source tree that do not match a list of
270Sstevel@tonic-gate  * permitted pathnames.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <ctype.h>
310Sstevel@tonic-gate #include <errno.h>
320Sstevel@tonic-gate #include <fnmatch.h>
330Sstevel@tonic-gate #include <ftw.h>
340Sstevel@tonic-gate #include <stdarg.h>
350Sstevel@tonic-gate #include <stdio.h>
360Sstevel@tonic-gate #include <stdlib.h>
370Sstevel@tonic-gate #include <string.h>
380Sstevel@tonic-gate #include <time.h>
390Sstevel@tonic-gate #include <unistd.h>
400Sstevel@tonic-gate #include <sys/param.h>
410Sstevel@tonic-gate #include <sys/stat.h>
420Sstevel@tonic-gate #include <sys/types.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * Pathname set: a simple datatype for storing pathname pattern globs and
460Sstevel@tonic-gate  * for checking whether a given pathname is matched by a pattern glob in
470Sstevel@tonic-gate  * the set.
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate typedef struct {
500Sstevel@tonic-gate 	char		**paths;
510Sstevel@tonic-gate 	unsigned int	npath;
520Sstevel@tonic-gate 	unsigned int	maxpaths;
530Sstevel@tonic-gate } pnset_t;
540Sstevel@tonic-gate 
55*10313SPeter.Memishian@Sun.COM /*
56*10313SPeter.Memishian@Sun.COM  * Data associated with the current Mercurial manifest.
57*10313SPeter.Memishian@Sun.COM  */
58*10313SPeter.Memishian@Sun.COM typedef struct hgdata {
59*10313SPeter.Memishian@Sun.COM 	pnset_t		*manifest;
60*10313SPeter.Memishian@Sun.COM 	char		hgpath[MAXPATHLEN];
61*10313SPeter.Memishian@Sun.COM 	char		root[MAXPATHLEN];
62*10313SPeter.Memishian@Sun.COM 	unsigned int	rootlen;
63*10313SPeter.Memishian@Sun.COM 	boolean_t	rootwarn;
64*10313SPeter.Memishian@Sun.COM } hgdata_t;
65*10313SPeter.Memishian@Sun.COM 
66*10313SPeter.Memishian@Sun.COM /*
67*10313SPeter.Memishian@Sun.COM  * Hooks used to check if a given unreferenced file is known to an SCM
68*10313SPeter.Memishian@Sun.COM  * (currently Mercurial and TeamWare).
69*10313SPeter.Memishian@Sun.COM  */
70*10313SPeter.Memishian@Sun.COM typedef int checkscm_func_t(const char *, const struct FTW *);
71*10313SPeter.Memishian@Sun.COM typedef void chdirscm_func_t(const char *);
72*10313SPeter.Memishian@Sun.COM 
73*10313SPeter.Memishian@Sun.COM typedef struct {
74*10313SPeter.Memishian@Sun.COM 	const char	*name;
75*10313SPeter.Memishian@Sun.COM 	checkscm_func_t	*checkfunc;
76*10313SPeter.Memishian@Sun.COM 	chdirscm_func_t	*chdirfunc;
77*10313SPeter.Memishian@Sun.COM } scm_t;
78*10313SPeter.Memishian@Sun.COM 
79*10313SPeter.Memishian@Sun.COM static checkscm_func_t check_tw, check_hg;
80*10313SPeter.Memishian@Sun.COM static chdirscm_func_t chdir_hg;
810Sstevel@tonic-gate static int	pnset_add(pnset_t *, const char *);
820Sstevel@tonic-gate static int	pnset_check(const pnset_t *, const char *);
830Sstevel@tonic-gate static void	pnset_empty(pnset_t *);
84*10313SPeter.Memishian@Sun.COM static void	pnset_free(pnset_t *);
850Sstevel@tonic-gate static int	checkpath(const char *, const struct stat *, int, struct FTW *);
860Sstevel@tonic-gate static pnset_t	*make_exset(const char *);
870Sstevel@tonic-gate static void	warn(const char *, ...);
880Sstevel@tonic-gate static void	die(const char *, ...);
890Sstevel@tonic-gate 
90*10313SPeter.Memishian@Sun.COM static const scm_t scms[] = {
91*10313SPeter.Memishian@Sun.COM 	{ "tw",		check_tw,	NULL		},
92*10313SPeter.Memishian@Sun.COM 	{ "teamware",	check_tw,	NULL		},
93*10313SPeter.Memishian@Sun.COM 	{ "hg",		check_hg,	chdir_hg 	},
94*10313SPeter.Memishian@Sun.COM 	{ "mercurial",	check_hg,	chdir_hg	},
95*10313SPeter.Memishian@Sun.COM 	{ NULL,		NULL, 		NULL		}
96*10313SPeter.Memishian@Sun.COM };
97*10313SPeter.Memishian@Sun.COM 
98*10313SPeter.Memishian@Sun.COM static const scm_t	*scm;
99*10313SPeter.Memishian@Sun.COM static hgdata_t		hgdata;
1000Sstevel@tonic-gate static time_t		tstamp;		/* timestamp to compare files to */
1010Sstevel@tonic-gate static pnset_t		*exsetp;	/* pathname globs to ignore */
1020Sstevel@tonic-gate static const char	*progname;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate int
main(int argc,char * argv[])1050Sstevel@tonic-gate main(int argc, char *argv[])
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	int c;
1080Sstevel@tonic-gate 	char path[MAXPATHLEN];
1090Sstevel@tonic-gate 	char subtree[MAXPATHLEN] = "./";
1100Sstevel@tonic-gate 	char *tstampfile = ".build.tstamp";
1110Sstevel@tonic-gate 	struct stat tsstat;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	progname = strrchr(argv[0], '/');
1140Sstevel@tonic-gate 	if (progname == NULL)
1150Sstevel@tonic-gate 		progname = argv[0];
1160Sstevel@tonic-gate 	else
1170Sstevel@tonic-gate 		progname++;
1180Sstevel@tonic-gate 
119*10313SPeter.Memishian@Sun.COM 	while ((c = getopt(argc, argv, "as:t:S:")) != EOF) {
1200Sstevel@tonic-gate 		switch (c) {
1210Sstevel@tonic-gate 		case 'a':
122*10313SPeter.Memishian@Sun.COM 			/* for compatibility; now the default */
1230Sstevel@tonic-gate 			break;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 		case 's':
1260Sstevel@tonic-gate 			(void) strlcat(subtree, optarg, MAXPATHLEN);
1270Sstevel@tonic-gate 			break;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 		case 't':
1300Sstevel@tonic-gate 			tstampfile = optarg;
1310Sstevel@tonic-gate 			break;
1320Sstevel@tonic-gate 
133*10313SPeter.Memishian@Sun.COM 		case 'S':
134*10313SPeter.Memishian@Sun.COM 			for (scm = scms; scm->name != NULL; scm++) {
135*10313SPeter.Memishian@Sun.COM 				if (strcmp(scm->name, optarg) == 0)
136*10313SPeter.Memishian@Sun.COM 					break;
137*10313SPeter.Memishian@Sun.COM 			}
138*10313SPeter.Memishian@Sun.COM 			if (scm->name == NULL)
139*10313SPeter.Memishian@Sun.COM 				die("unsupported SCM `%s'\n", optarg);
140*10313SPeter.Memishian@Sun.COM 			break;
141*10313SPeter.Memishian@Sun.COM 
1420Sstevel@tonic-gate 		default:
1430Sstevel@tonic-gate 		case '?':
1440Sstevel@tonic-gate 			goto usage;
1450Sstevel@tonic-gate 		}
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	argc -= optind;
1490Sstevel@tonic-gate 	argv += optind;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	if (argc != 2) {
152*10313SPeter.Memishian@Sun.COM usage:		(void) fprintf(stderr, "usage: %s [-s <subtree>] "
153*10313SPeter.Memishian@Sun.COM 		    "[-t <tstampfile>] [-S hg|tw] <srcroot> <exceptfile>\n",
154*10313SPeter.Memishian@Sun.COM 		    progname);
1550Sstevel@tonic-gate 		return (EXIT_FAILURE);
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	/*
1590Sstevel@tonic-gate 	 * Interpret a relative timestamp path as relative to srcroot.
1600Sstevel@tonic-gate 	 */
1610Sstevel@tonic-gate 	if (tstampfile[0] == '/')
1620Sstevel@tonic-gate 		(void) strlcpy(path, tstampfile, MAXPATHLEN);
1630Sstevel@tonic-gate 	else
1640Sstevel@tonic-gate 		(void) snprintf(path, MAXPATHLEN, "%s/%s", argv[0], tstampfile);
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	if (stat(path, &tsstat) == -1)
1670Sstevel@tonic-gate 		die("cannot stat timestamp file \"%s\"", path);
1680Sstevel@tonic-gate 	tstamp = tsstat.st_mtime;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	/*
1710Sstevel@tonic-gate 	 * Create the exception pathname set.
1720Sstevel@tonic-gate 	 */
1730Sstevel@tonic-gate 	exsetp = make_exset(argv[1]);
1740Sstevel@tonic-gate 	if (exsetp == NULL)
1750Sstevel@tonic-gate 		die("cannot make exception pathname set\n");
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	/*
1780Sstevel@tonic-gate 	 * Walk the specified subtree of the tree rooted at argv[0].
1790Sstevel@tonic-gate 	 */
180*10313SPeter.Memishian@Sun.COM 	if (chdir(argv[0]) == -1)
181*10313SPeter.Memishian@Sun.COM 		die("cannot change directory to \"%s\"", argv[0]);
182*10313SPeter.Memishian@Sun.COM 
1830Sstevel@tonic-gate 	if (nftw(subtree, checkpath, 100, FTW_PHYS) != 0)
1840Sstevel@tonic-gate 		die("cannot walk tree rooted at \"%s\"\n", argv[0]);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	pnset_empty(exsetp);
1870Sstevel@tonic-gate 	return (EXIT_SUCCESS);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate /*
191*10313SPeter.Memishian@Sun.COM  * Load and return a pnset for the manifest for the Mercurial repo at `hgroot'.
192*10313SPeter.Memishian@Sun.COM  */
193*10313SPeter.Memishian@Sun.COM static pnset_t *
load_manifest(const char * hgroot)194*10313SPeter.Memishian@Sun.COM load_manifest(const char *hgroot)
195*10313SPeter.Memishian@Sun.COM {
196*10313SPeter.Memishian@Sun.COM 	FILE	*fp = NULL;
197*10313SPeter.Memishian@Sun.COM 	char	*hgcmd = NULL;
198*10313SPeter.Memishian@Sun.COM 	char	*newline;
199*10313SPeter.Memishian@Sun.COM 	pnset_t	*pnsetp;
200*10313SPeter.Memishian@Sun.COM 	char	path[MAXPATHLEN];
201*10313SPeter.Memishian@Sun.COM 
202*10313SPeter.Memishian@Sun.COM 	pnsetp = calloc(sizeof (pnset_t), 1);
203*10313SPeter.Memishian@Sun.COM 	if (pnsetp == NULL ||
204*10313SPeter.Memishian@Sun.COM 	    asprintf(&hgcmd, "/usr/bin/hg manifest -R %s", hgroot) == -1)
205*10313SPeter.Memishian@Sun.COM 		goto fail;
206*10313SPeter.Memishian@Sun.COM 
207*10313SPeter.Memishian@Sun.COM 	fp = popen(hgcmd, "r");
208*10313SPeter.Memishian@Sun.COM 	if (fp == NULL)
209*10313SPeter.Memishian@Sun.COM 		goto fail;
210*10313SPeter.Memishian@Sun.COM 
211*10313SPeter.Memishian@Sun.COM 	while (fgets(path, sizeof (path), fp) != NULL) {
212*10313SPeter.Memishian@Sun.COM 		newline = strrchr(path, '\n');
213*10313SPeter.Memishian@Sun.COM 		if (newline != NULL)
214*10313SPeter.Memishian@Sun.COM 			*newline = '\0';
215*10313SPeter.Memishian@Sun.COM 
216*10313SPeter.Memishian@Sun.COM 		if (pnset_add(pnsetp, path) == 0)
217*10313SPeter.Memishian@Sun.COM 			goto fail;
218*10313SPeter.Memishian@Sun.COM 	}
219*10313SPeter.Memishian@Sun.COM 
220*10313SPeter.Memishian@Sun.COM 	(void) pclose(fp);
221*10313SPeter.Memishian@Sun.COM 	free(hgcmd);
222*10313SPeter.Memishian@Sun.COM 	return (pnsetp);
223*10313SPeter.Memishian@Sun.COM fail:
224*10313SPeter.Memishian@Sun.COM 	warn("cannot load hg manifest at %s", hgroot);
225*10313SPeter.Memishian@Sun.COM 	if (fp != NULL)
226*10313SPeter.Memishian@Sun.COM 		(void) pclose(fp);
227*10313SPeter.Memishian@Sun.COM 	free(hgcmd);
228*10313SPeter.Memishian@Sun.COM 	pnset_free(pnsetp);
229*10313SPeter.Memishian@Sun.COM 	return (NULL);
230*10313SPeter.Memishian@Sun.COM }
231*10313SPeter.Memishian@Sun.COM 
232*10313SPeter.Memishian@Sun.COM /*
233*10313SPeter.Memishian@Sun.COM  * If necessary, change our active manifest to be appropriate for `path'.
234*10313SPeter.Memishian@Sun.COM  */
235*10313SPeter.Memishian@Sun.COM static void
chdir_hg(const char * path)236*10313SPeter.Memishian@Sun.COM chdir_hg(const char *path)
237*10313SPeter.Memishian@Sun.COM {
238*10313SPeter.Memishian@Sun.COM 	char hgpath[MAXPATHLEN];
239*10313SPeter.Memishian@Sun.COM 	char basepath[MAXPATHLEN];
240*10313SPeter.Memishian@Sun.COM 	char *slash;
241*10313SPeter.Memishian@Sun.COM 
242*10313SPeter.Memishian@Sun.COM 	(void) snprintf(hgpath, MAXPATHLEN, "%s/.hg", path);
243*10313SPeter.Memishian@Sun.COM 
244*10313SPeter.Memishian@Sun.COM 	/*
245*10313SPeter.Memishian@Sun.COM 	 * Change our active manifest if any one of the following is true:
246*10313SPeter.Memishian@Sun.COM 	 *
247*10313SPeter.Memishian@Sun.COM 	 *   1. No manifest is loaded.  Find the nearest hgroot to load from.
248*10313SPeter.Memishian@Sun.COM 	 *
249*10313SPeter.Memishian@Sun.COM 	 *   2. A manifest is loaded, but we've moved into a directory with
250*10313SPeter.Memishian@Sun.COM 	 *	its own hgroot (e.g., usr/closed).  Load from its hgroot.
251*10313SPeter.Memishian@Sun.COM 	 *
252*10313SPeter.Memishian@Sun.COM 	 *   3. A manifest is loaded, but no longer applies (e.g., the manifest
253*10313SPeter.Memishian@Sun.COM 	 *	under usr/closed is loaded, but we've moved to usr/src).
254*10313SPeter.Memishian@Sun.COM 	 */
255*10313SPeter.Memishian@Sun.COM 	if (hgdata.manifest == NULL ||
256*10313SPeter.Memishian@Sun.COM 	    strcmp(hgpath, hgdata.hgpath) != 0 && access(hgpath, X_OK) == 0 ||
257*10313SPeter.Memishian@Sun.COM 	    strncmp(path, hgdata.root, hgdata.rootlen - 1) != 0) {
258*10313SPeter.Memishian@Sun.COM 		pnset_free(hgdata.manifest);
259*10313SPeter.Memishian@Sun.COM 		hgdata.manifest = NULL;
260*10313SPeter.Memishian@Sun.COM 
261*10313SPeter.Memishian@Sun.COM 		(void) strlcpy(basepath, path, MAXPATHLEN);
262*10313SPeter.Memishian@Sun.COM 
263*10313SPeter.Memishian@Sun.COM 		/*
264*10313SPeter.Memishian@Sun.COM 		 * Walk up the directory tree looking for .hg subdirectories.
265*10313SPeter.Memishian@Sun.COM 		 */
266*10313SPeter.Memishian@Sun.COM 		while (access(hgpath, X_OK) == -1) {
267*10313SPeter.Memishian@Sun.COM 			slash = strrchr(basepath, '/');
268*10313SPeter.Memishian@Sun.COM 			if (slash == NULL) {
269*10313SPeter.Memishian@Sun.COM 				if (!hgdata.rootwarn) {
270*10313SPeter.Memishian@Sun.COM 					warn("no hg root for \"%s\"\n", path);
271*10313SPeter.Memishian@Sun.COM 					hgdata.rootwarn = B_TRUE;
272*10313SPeter.Memishian@Sun.COM 				}
273*10313SPeter.Memishian@Sun.COM 				return;
274*10313SPeter.Memishian@Sun.COM 			}
275*10313SPeter.Memishian@Sun.COM 			*slash = '\0';
276*10313SPeter.Memishian@Sun.COM 			(void) snprintf(hgpath, MAXPATHLEN, "%s/.hg", basepath);
277*10313SPeter.Memishian@Sun.COM 		}
278*10313SPeter.Memishian@Sun.COM 
279*10313SPeter.Memishian@Sun.COM 		/*
280*10313SPeter.Memishian@Sun.COM 		 * We found a directory with an .hg subdirectory; record it
281*10313SPeter.Memishian@Sun.COM 		 * and load its manifest.
282*10313SPeter.Memishian@Sun.COM 		 */
283*10313SPeter.Memishian@Sun.COM 		(void) strlcpy(hgdata.hgpath, hgpath, MAXPATHLEN);
284*10313SPeter.Memishian@Sun.COM 		(void) strlcpy(hgdata.root, basepath, MAXPATHLEN);
285*10313SPeter.Memishian@Sun.COM 		hgdata.manifest = load_manifest(hgdata.root);
286*10313SPeter.Memishian@Sun.COM 
287*10313SPeter.Memishian@Sun.COM 		/*
288*10313SPeter.Memishian@Sun.COM 		 * The logic in check_hg() depends on hgdata.root having a
289*10313SPeter.Memishian@Sun.COM 		 * single trailing slash, so only add it if it's missing.
290*10313SPeter.Memishian@Sun.COM 		 */
291*10313SPeter.Memishian@Sun.COM 		if (hgdata.root[strlen(hgdata.root) - 1] != '/')
292*10313SPeter.Memishian@Sun.COM 			(void) strlcat(hgdata.root, "/", MAXPATHLEN);
293*10313SPeter.Memishian@Sun.COM 		hgdata.rootlen = strlen(hgdata.root);
294*10313SPeter.Memishian@Sun.COM 	}
295*10313SPeter.Memishian@Sun.COM }
296*10313SPeter.Memishian@Sun.COM 
297*10313SPeter.Memishian@Sun.COM /*
298*10313SPeter.Memishian@Sun.COM  * Check if a file is under Mercurial control by checking against the manifest.
299*10313SPeter.Memishian@Sun.COM  */
300*10313SPeter.Memishian@Sun.COM /* ARGSUSED */
301*10313SPeter.Memishian@Sun.COM static int
check_hg(const char * path,const struct FTW * ftwp)302*10313SPeter.Memishian@Sun.COM check_hg(const char *path, const struct FTW *ftwp)
303*10313SPeter.Memishian@Sun.COM {
304*10313SPeter.Memishian@Sun.COM 	/*
305*10313SPeter.Memishian@Sun.COM 	 * The manifest paths are relative to the manifest root; skip past it.
306*10313SPeter.Memishian@Sun.COM 	 */
307*10313SPeter.Memishian@Sun.COM 	path += hgdata.rootlen;
308*10313SPeter.Memishian@Sun.COM 
309*10313SPeter.Memishian@Sun.COM 	return (hgdata.manifest != NULL && pnset_check(hgdata.manifest, path));
310*10313SPeter.Memishian@Sun.COM }
311*10313SPeter.Memishian@Sun.COM 
312*10313SPeter.Memishian@Sun.COM /*
313*10313SPeter.Memishian@Sun.COM  * Check if a file is under TeamWare control by checking for its corresponding
314*10313SPeter.Memishian@Sun.COM  * SCCS "s-dot" file.
315*10313SPeter.Memishian@Sun.COM  */
316*10313SPeter.Memishian@Sun.COM static int
check_tw(const char * path,const struct FTW * ftwp)317*10313SPeter.Memishian@Sun.COM check_tw(const char *path, const struct FTW *ftwp)
318*10313SPeter.Memishian@Sun.COM {
319*10313SPeter.Memishian@Sun.COM 	char sccspath[MAXPATHLEN];
320*10313SPeter.Memishian@Sun.COM 
321*10313SPeter.Memishian@Sun.COM 	(void) snprintf(sccspath, MAXPATHLEN, "%.*s/SCCS/s.%s", ftwp->base,
322*10313SPeter.Memishian@Sun.COM 	    path, path + ftwp->base);
323*10313SPeter.Memishian@Sun.COM 
324*10313SPeter.Memishian@Sun.COM 	return (access(sccspath, F_OK) == 0);
325*10313SPeter.Memishian@Sun.COM }
326*10313SPeter.Memishian@Sun.COM 
327*10313SPeter.Memishian@Sun.COM /*
3280Sstevel@tonic-gate  * Using `exceptfile' and a built-in list of exceptions, build and return a
3290Sstevel@tonic-gate  * pnset_t consisting of all of the pathnames globs which are allowed to be
3300Sstevel@tonic-gate  * unreferenced in the source tree.
3310Sstevel@tonic-gate  */
3320Sstevel@tonic-gate static pnset_t *
make_exset(const char * exceptfile)3330Sstevel@tonic-gate make_exset(const char *exceptfile)
3340Sstevel@tonic-gate {
3350Sstevel@tonic-gate 	FILE		*fp;
3360Sstevel@tonic-gate 	char		line[MAXPATHLEN];
3370Sstevel@tonic-gate 	char		*newline;
3380Sstevel@tonic-gate 	pnset_t		*pnsetp;
3390Sstevel@tonic-gate 	unsigned int	i;
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	pnsetp = calloc(sizeof (pnset_t), 1);
3420Sstevel@tonic-gate 	if (pnsetp == NULL)
3430Sstevel@tonic-gate 		return (NULL);
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	/*
3460Sstevel@tonic-gate 	 * Add any exceptions from the file.
3470Sstevel@tonic-gate 	 */
3480Sstevel@tonic-gate 	fp = fopen(exceptfile, "r");
3490Sstevel@tonic-gate 	if (fp == NULL) {
3500Sstevel@tonic-gate 		warn("cannot open exception file \"%s\"", exceptfile);
3510Sstevel@tonic-gate 		goto fail;
3520Sstevel@tonic-gate 	}
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	while (fgets(line, sizeof (line), fp) != NULL) {
3550Sstevel@tonic-gate 		newline = strrchr(line, '\n');
3560Sstevel@tonic-gate 		if (newline != NULL)
3570Sstevel@tonic-gate 			*newline = '\0';
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 		for (i = 0; isspace(line[i]); i++)
3600Sstevel@tonic-gate 			;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 		if (line[i] == '#' || line[i] == '\0')
3630Sstevel@tonic-gate 			continue;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 		if (pnset_add(pnsetp, line) == 0) {
3660Sstevel@tonic-gate 			(void) fclose(fp);
3670Sstevel@tonic-gate 			goto fail;
3680Sstevel@tonic-gate 		}
3690Sstevel@tonic-gate 	}
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	(void) fclose(fp);
3720Sstevel@tonic-gate 	return (pnsetp);
3730Sstevel@tonic-gate fail:
374*10313SPeter.Memishian@Sun.COM 	pnset_free(pnsetp);
3750Sstevel@tonic-gate 	return (NULL);
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate /*
3790Sstevel@tonic-gate  * FTW callback: print `path' if it's older than `tstamp' and not in `exsetp'.
3800Sstevel@tonic-gate  */
3810Sstevel@tonic-gate static int
checkpath(const char * path,const struct stat * statp,int type,struct FTW * ftwp)3820Sstevel@tonic-gate checkpath(const char *path, const struct stat *statp, int type,
3830Sstevel@tonic-gate     struct FTW *ftwp)
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate 	switch (type) {
3860Sstevel@tonic-gate 	case FTW_F:
3870Sstevel@tonic-gate 		/*
3880Sstevel@tonic-gate 		 * Skip if the file is referenced or in the exception list.
3890Sstevel@tonic-gate 		 */
3900Sstevel@tonic-gate 		if (statp->st_atime >= tstamp || pnset_check(exsetp, path))
3910Sstevel@tonic-gate 			return (0);
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 		/*
394*10313SPeter.Memishian@Sun.COM 		 * If requested, restrict ourselves to unreferenced files
395*10313SPeter.Memishian@Sun.COM 		 * under SCM control.
3960Sstevel@tonic-gate 		 */
397*10313SPeter.Memishian@Sun.COM 		if (scm == NULL || scm->checkfunc(path, ftwp))
398*10313SPeter.Memishian@Sun.COM 			(void) puts(path);
3990Sstevel@tonic-gate 		return (0);
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	case FTW_D:
4020Sstevel@tonic-gate 		/*
4030Sstevel@tonic-gate 		 * Prune any directories in the exception list.
4040Sstevel@tonic-gate 		 */
405*10313SPeter.Memishian@Sun.COM 		if (pnset_check(exsetp, path)) {
4060Sstevel@tonic-gate 			ftwp->quit = FTW_PRUNE;
407*10313SPeter.Memishian@Sun.COM 			return (0);
408*10313SPeter.Memishian@Sun.COM 		}
409*10313SPeter.Memishian@Sun.COM 
410*10313SPeter.Memishian@Sun.COM 		/*
411*10313SPeter.Memishian@Sun.COM 		 * If necessary, advise the SCM logic of our new directory.
412*10313SPeter.Memishian@Sun.COM 		 */
413*10313SPeter.Memishian@Sun.COM 		if (scm != NULL && scm->chdirfunc != NULL)
414*10313SPeter.Memishian@Sun.COM 			scm->chdirfunc(path);
415*10313SPeter.Memishian@Sun.COM 
4160Sstevel@tonic-gate 		return (0);
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	case FTW_DNR:
4190Sstevel@tonic-gate 		warn("cannot read \"%s\"", path);
4200Sstevel@tonic-gate 		return (0);
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	case FTW_NS:
4230Sstevel@tonic-gate 		warn("cannot stat \"%s\"", path);
4240Sstevel@tonic-gate 		return (0);
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	default:
4270Sstevel@tonic-gate 		break;
4280Sstevel@tonic-gate 	}
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	return (0);
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate /*
4340Sstevel@tonic-gate  * Add `path' to the pnset_t pointed to by `pnsetp'.
4350Sstevel@tonic-gate  */
4360Sstevel@tonic-gate static int
pnset_add(pnset_t * pnsetp,const char * path)4370Sstevel@tonic-gate pnset_add(pnset_t *pnsetp, const char *path)
4380Sstevel@tonic-gate {
4390Sstevel@tonic-gate 	char **newpaths;
440*10313SPeter.Memishian@Sun.COM 	unsigned int maxpaths;
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	if (pnsetp->npath == pnsetp->maxpaths) {
443*10313SPeter.Memishian@Sun.COM 		maxpaths = (pnsetp->maxpaths == 0) ? 512 : pnsetp->maxpaths * 2;
444*10313SPeter.Memishian@Sun.COM 		newpaths = realloc(pnsetp->paths, sizeof (char *) * maxpaths);
4450Sstevel@tonic-gate 		if (newpaths == NULL)
4460Sstevel@tonic-gate 			return (0);
4470Sstevel@tonic-gate 		pnsetp->paths = newpaths;
448*10313SPeter.Memishian@Sun.COM 		pnsetp->maxpaths = maxpaths;
4490Sstevel@tonic-gate 	}
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	pnsetp->paths[pnsetp->npath] = strdup(path);
4520Sstevel@tonic-gate 	if (pnsetp->paths[pnsetp->npath] == NULL)
4530Sstevel@tonic-gate 		return (0);
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	pnsetp->npath++;
4560Sstevel@tonic-gate 	return (1);
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate /*
4600Sstevel@tonic-gate  * Check `path' against the pnset_t pointed to by `pnsetp'.
4610Sstevel@tonic-gate  */
4620Sstevel@tonic-gate static int
pnset_check(const pnset_t * pnsetp,const char * path)4630Sstevel@tonic-gate pnset_check(const pnset_t *pnsetp, const char *path)
4640Sstevel@tonic-gate {
4650Sstevel@tonic-gate 	unsigned int i;
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	for (i = 0; i < pnsetp->npath; i++) {
4680Sstevel@tonic-gate 		if (fnmatch(pnsetp->paths[i], path, 0) == 0)
4690Sstevel@tonic-gate 			return (1);
4700Sstevel@tonic-gate 	}
4710Sstevel@tonic-gate 	return (0);
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate /*
4750Sstevel@tonic-gate  * Empty the pnset_t pointed to by `pnsetp'.
4760Sstevel@tonic-gate  */
4770Sstevel@tonic-gate static void
pnset_empty(pnset_t * pnsetp)4780Sstevel@tonic-gate pnset_empty(pnset_t *pnsetp)
4790Sstevel@tonic-gate {
4800Sstevel@tonic-gate 	while (pnsetp->npath-- != 0)
4810Sstevel@tonic-gate 		free(pnsetp->paths[pnsetp->npath]);
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	free(pnsetp->paths);
4840Sstevel@tonic-gate 	pnsetp->maxpaths = 0;
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate 
487*10313SPeter.Memishian@Sun.COM /*
488*10313SPeter.Memishian@Sun.COM  * Free the pnset_t pointed to by `pnsetp'.
489*10313SPeter.Memishian@Sun.COM  */
490*10313SPeter.Memishian@Sun.COM static void
pnset_free(pnset_t * pnsetp)491*10313SPeter.Memishian@Sun.COM pnset_free(pnset_t *pnsetp)
492*10313SPeter.Memishian@Sun.COM {
493*10313SPeter.Memishian@Sun.COM 	if (pnsetp != NULL) {
494*10313SPeter.Memishian@Sun.COM 		pnset_empty(pnsetp);
495*10313SPeter.Memishian@Sun.COM 		free(pnsetp);
496*10313SPeter.Memishian@Sun.COM 	}
497*10313SPeter.Memishian@Sun.COM }
498*10313SPeter.Memishian@Sun.COM 
4990Sstevel@tonic-gate /* PRINTFLIKE1 */
5000Sstevel@tonic-gate static void
warn(const char * format,...)5010Sstevel@tonic-gate warn(const char *format, ...)
5020Sstevel@tonic-gate {
5030Sstevel@tonic-gate 	va_list alist;
5040Sstevel@tonic-gate 	char *errstr = strerror(errno);
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	if (errstr == NULL)
5070Sstevel@tonic-gate 		errstr = "<unknown error>";
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", progname);
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	va_start(alist, format);
5120Sstevel@tonic-gate 	(void) vfprintf(stderr, format, alist);
5130Sstevel@tonic-gate 	va_end(alist);
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	if (strrchr(format, '\n') == NULL)
5160Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s\n", errstr);
5170Sstevel@tonic-gate }
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate /* PRINTFLIKE1 */
5200Sstevel@tonic-gate static void
die(const char * format,...)5210Sstevel@tonic-gate die(const char *format, ...)
5220Sstevel@tonic-gate {
5230Sstevel@tonic-gate 	va_list alist;
5240Sstevel@tonic-gate 	char *errstr = strerror(errno);
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	if (errstr == NULL)
5270Sstevel@tonic-gate 		errstr = "<unknown error>";
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: fatal: ", progname);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	va_start(alist, format);
5320Sstevel@tonic-gate 	(void) vfprintf(stderr, format, alist);
5330Sstevel@tonic-gate 	va_end(alist);
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	if (strrchr(format, '\n') == NULL)
5360Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s\n", errstr);
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	exit(EXIT_FAILURE);
5390Sstevel@tonic-gate }
540