xref: /illumos-gate/usr/src/cmd/bart/rules.c (revision bbf21555)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5c40f76e3Sjc  * Common Development and Distribution License (the "License").
6c40f76e3Sjc  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2299389cdeSJan Parcel  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <dirent.h>
267c478bd9Sstevel@tonic-gate #include <fnmatch.h>
27f7bbf134Shm #include <string.h>
287c478bd9Sstevel@tonic-gate #include "bart.h"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate static int count_slashes(const char *);
317c478bd9Sstevel@tonic-gate static struct rule *gen_rulestruct(void);
327c478bd9Sstevel@tonic-gate static struct tree_modifier *gen_tree_modifier(void);
337c478bd9Sstevel@tonic-gate static struct dir_component *gen_dir_component(void);
347c478bd9Sstevel@tonic-gate static void init_rule(uint_t, struct rule *);
357c478bd9Sstevel@tonic-gate static void add_modifier(struct rule *, char *);
367c478bd9Sstevel@tonic-gate static struct rule *add_subtree_rule(char *, char *, int, int *);
377c478bd9Sstevel@tonic-gate static struct rule *add_single_rule(char *);
387c478bd9Sstevel@tonic-gate static void dirs_cleanup(struct dir_component *);
397c478bd9Sstevel@tonic-gate static void add_dir(struct dir_component **, char *);
407c478bd9Sstevel@tonic-gate static char *lex(FILE *);
417c478bd9Sstevel@tonic-gate static int match_subtree(const char *, char *);
427c478bd9Sstevel@tonic-gate static struct rule *get_last_entry(boolean_t);
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static int	lex_linenum;	/* line number in current input file	*/
457c478bd9Sstevel@tonic-gate static struct rule	*first_rule = NULL, *current_rule = NULL;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * This function is responsible for validating whether or not a given file
497c478bd9Sstevel@tonic-gate  * should be cataloged, based upon the modifiers for a subtree.
507c478bd9Sstevel@tonic-gate  * For example, a line in the rules file: '/home/nickiso *.c' should only
517c478bd9Sstevel@tonic-gate  * catalog the C files (based upon pattern matching) in the subtree
527c478bd9Sstevel@tonic-gate  * '/home/nickiso'.
537c478bd9Sstevel@tonic-gate  *
5499389cdeSJan Parcel  * exclude_fname depends on having the modifiers be pre-sorted to put
5599389cdeSJan Parcel  * negative directory modifiers first, so that the logic does
5699389cdeSJan Parcel  * not need to save complex state information.  This is valid because
5799389cdeSJan Parcel  * we are only cataloging things that meet all modifiers (AND logic.)
5899389cdeSJan Parcel  *
5999389cdeSJan Parcel  * Returns:
6099389cdeSJan Parcel  * NO_EXCLUDE
6199389cdeSJan Parcel  * EXCLUDE_SKIP
6299389cdeSJan Parcel  * EXCLUDE_PRUNE
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate int
exclude_fname(const char * fname,char fname_type,struct rule * rule_ptr)657c478bd9Sstevel@tonic-gate exclude_fname(const char *fname, char fname_type, struct rule *rule_ptr)
667c478bd9Sstevel@tonic-gate {
6799389cdeSJan Parcel 	char	*pattern, *ptr, *fname_ptr, saved_char;
6899389cdeSJan Parcel 	char 	fname_cp[PATH_MAX], pattern_cp[PATH_MAX];
6999389cdeSJan Parcel 	int	num_pattern_slash,  i, num_fname_slash, slashes_to_adv;
707c478bd9Sstevel@tonic-gate 	struct  tree_modifier   *mod_ptr;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	/*
7399389cdeSJan Parcel 	 * If this is create and there are no modifiers, bail.
7499389cdeSJan Parcel 	 * This will have to change once create handles multiple rules
7599389cdeSJan Parcel 	 * during walk.
767c478bd9Sstevel@tonic-gate 	 */
7799389cdeSJan Parcel 	if (rule_ptr->modifiers == NULL)
7899389cdeSJan Parcel 		if (rule_ptr->attr_list == 0)
7999389cdeSJan Parcel 			return (EXCLUDE_PRUNE);
8099389cdeSJan Parcel 		else
8199389cdeSJan Parcel 			return (NO_EXCLUDE);
827c478bd9Sstevel@tonic-gate 	/*
837c478bd9Sstevel@tonic-gate 	 * Walk through all the modifiers until its they are exhausted OR
847c478bd9Sstevel@tonic-gate 	 * until the file should definitely be excluded.
857c478bd9Sstevel@tonic-gate 	 */
8699389cdeSJan Parcel 	for (mod_ptr = rule_ptr->modifiers; mod_ptr != NULL;
8799389cdeSJan Parcel 	    mod_ptr = mod_ptr->next) {
8899389cdeSJan Parcel 		/* leading !'s were processed in add_modifier */
8999389cdeSJan Parcel 		pattern = mod_ptr->mod_str;
9099389cdeSJan Parcel 		if (mod_ptr->is_dir == B_FALSE) {
917c478bd9Sstevel@tonic-gate 			/*
9299389cdeSJan Parcel 			 * Pattern is a file pattern.
9399389cdeSJan Parcel 			 *
947c478bd9Sstevel@tonic-gate 			 * In the case when a user is trying to filter on
9599389cdeSJan Parcel 			 * a file pattern and the entry is a directory,
9699389cdeSJan Parcel 			 * this is not a match.
9799389cdeSJan Parcel 			 *
9899389cdeSJan Parcel 			 * If a match is required, skip this file.  If
9999389cdeSJan Parcel 			 * a match is forbidden, keep looking at modifiers.
1007c478bd9Sstevel@tonic-gate 			 */
1017c478bd9Sstevel@tonic-gate 			if (fname_type == 'D') {
10299389cdeSJan Parcel 				if (mod_ptr->include == B_TRUE)
10399389cdeSJan Parcel 					return (EXCLUDE_SKIP);
1049c84d166Srm 				else
10599389cdeSJan Parcel 					continue;
1067c478bd9Sstevel@tonic-gate 			}
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 			/*
1097c478bd9Sstevel@tonic-gate 			 * Match patterns against filenames.
1107c478bd9Sstevel@tonic-gate 			 * Need to be able to handle multi-level patterns,
1117c478bd9Sstevel@tonic-gate 			 * eg. "SCCS/<star-wildcard>.c", which means
1127c478bd9Sstevel@tonic-gate 			 * 'only match C files under SCCS directories.
1137c478bd9Sstevel@tonic-gate 			 *
1147c478bd9Sstevel@tonic-gate 			 * Determine the number of levels in the filename and
1157c478bd9Sstevel@tonic-gate 			 * in the pattern.
1167c478bd9Sstevel@tonic-gate 			 */
1177c478bd9Sstevel@tonic-gate 			num_pattern_slash = count_slashes(pattern);
1187c478bd9Sstevel@tonic-gate 			num_fname_slash = count_slashes(fname);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 			/* Check for trivial exclude condition */
1217c478bd9Sstevel@tonic-gate 			if (num_pattern_slash > num_fname_slash) {
12299389cdeSJan Parcel 				if (mod_ptr->include == B_TRUE)
12399389cdeSJan Parcel 					return (EXCLUDE_SKIP);
1247c478bd9Sstevel@tonic-gate 			}
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 			/*
1277c478bd9Sstevel@tonic-gate 			 * Do an apples to apples comparison, based upon the
1287c478bd9Sstevel@tonic-gate 			 * number of levels:
1297c478bd9Sstevel@tonic-gate 			 *
1307c478bd9Sstevel@tonic-gate 			 * Assume fname is /A/B/C/D/E and the pattern is D/E.
1317c478bd9Sstevel@tonic-gate 			 * In that case, 'ptr' will point to "D/E" and
1327c478bd9Sstevel@tonic-gate 			 * 'slashes_to_adv' will be '4'.
1337c478bd9Sstevel@tonic-gate 			 */
1347c478bd9Sstevel@tonic-gate 			(void) strlcpy(fname_cp, fname, sizeof (fname_cp));
1357c478bd9Sstevel@tonic-gate 			ptr = fname_cp;
1367c478bd9Sstevel@tonic-gate 			slashes_to_adv = num_fname_slash - num_pattern_slash;
1377c478bd9Sstevel@tonic-gate 			for (i = 0; i < slashes_to_adv; i++)  {
1387c478bd9Sstevel@tonic-gate 				ptr = strchr(ptr, '/');
1397c478bd9Sstevel@tonic-gate 				ptr++;
1407c478bd9Sstevel@tonic-gate 			}
1417c478bd9Sstevel@tonic-gate 			if ((pattern[0] == '.') && (pattern[1] == '.') &&
1427c478bd9Sstevel@tonic-gate 			    (pattern[2] == '/')) {
1437c478bd9Sstevel@tonic-gate 				pattern = strchr(pattern, '/');
1447c478bd9Sstevel@tonic-gate 				ptr = strchr(ptr, '/');
1457c478bd9Sstevel@tonic-gate 			}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 
14899389cdeSJan Parcel 			/* OK, now do the fnmatch() compare to the file */
14999389cdeSJan Parcel 			if (fnmatch(pattern, ptr, FNM_PATHNAME) == 0) {
15099389cdeSJan Parcel 				/* matches, is it an exclude? */
15199389cdeSJan Parcel 				if (mod_ptr->include == B_FALSE)
15299389cdeSJan Parcel 					return (EXCLUDE_SKIP);
15399389cdeSJan Parcel 			} else if (mod_ptr->include == B_TRUE) {
15499389cdeSJan Parcel 				/* failed a required filename match */
15599389cdeSJan Parcel 				return (EXCLUDE_SKIP);
15699389cdeSJan Parcel 			}
1577c478bd9Sstevel@tonic-gate 		} else {
1587c478bd9Sstevel@tonic-gate 			/*
1597c478bd9Sstevel@tonic-gate 			 * The rule requires directory matching.
1607c478bd9Sstevel@tonic-gate 			 *
16199389cdeSJan Parcel 			 * Unlike filename matching, directory matching can
16299389cdeSJan Parcel 			 * prune.
16399389cdeSJan Parcel 			 *
1647c478bd9Sstevel@tonic-gate 			 * First, make copies, since both the pattern and
1657c478bd9Sstevel@tonic-gate 			 * filename need to be modified.
1667c478bd9Sstevel@tonic-gate 			 *
1677c478bd9Sstevel@tonic-gate 			 * When copying 'fname', ignore the relocatable root
1687c478bd9Sstevel@tonic-gate 			 * since pattern matching is done for the string AFTER
1697c478bd9Sstevel@tonic-gate 			 * the relocatable root.  For example, if the
1707c478bd9Sstevel@tonic-gate 			 * relocatable root is "/dir1/dir2/dir3" and the
1717c478bd9Sstevel@tonic-gate 			 * pattern is "dir3/", we do NOT want to include every
1727c478bd9Sstevel@tonic-gate 			 * directory in the relocatable root.  Instead, we
1737c478bd9Sstevel@tonic-gate 			 * only want to include subtrees that look like:
1747c478bd9Sstevel@tonic-gate 			 * "/dir1/dir2/dir3/....dir3/....."
1757c478bd9Sstevel@tonic-gate 			 *
176c40f76e3Sjc 			 * NOTE: the 'fname_cp' does NOT have a trailing '/':
1777c478bd9Sstevel@tonic-gate 			 * necessary for fnmatch().
1787c478bd9Sstevel@tonic-gate 			 */
1797c478bd9Sstevel@tonic-gate 			(void) strlcpy(fname_cp,
1807c478bd9Sstevel@tonic-gate 			    (fname+strlen(rule_ptr->subtree)),
1817c478bd9Sstevel@tonic-gate 			    sizeof (fname_cp));
1827c478bd9Sstevel@tonic-gate 			(void) strlcpy(pattern_cp, pattern,
1837c478bd9Sstevel@tonic-gate 			    sizeof (pattern_cp));
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 			/*
18699389cdeSJan Parcel 			 * For non-directory files, remove the trailing
1877c478bd9Sstevel@tonic-gate 			 * name, e.g., for a file /A/B/C/D where 'D' is
1887c478bd9Sstevel@tonic-gate 			 * the actual filename, remove the 'D' since it
1897c478bd9Sstevel@tonic-gate 			 * should *not* be considered in the directory match.
1907c478bd9Sstevel@tonic-gate 			 */
1917c478bd9Sstevel@tonic-gate 			if (fname_type != 'D') {
1927c478bd9Sstevel@tonic-gate 				ptr = strrchr(fname_cp, '/');
1937c478bd9Sstevel@tonic-gate 				if (ptr != NULL)
1947c478bd9Sstevel@tonic-gate 					*ptr = '\0';
1957c478bd9Sstevel@tonic-gate 
19699389cdeSJan Parcel 				/*
19799389cdeSJan Parcel 				 * Trivial case: a simple filename does
19899389cdeSJan Parcel 				 * not match a directory by definition,
19999389cdeSJan Parcel 				 * so skip if match is required,
20099389cdeSJan Parcel 				 * keep analyzing otherwise.
20199389cdeSJan Parcel 				 */
20299389cdeSJan Parcel 
20399389cdeSJan Parcel 				if (strlen(fname_cp) == 0)
20499389cdeSJan Parcel 					if (mod_ptr->include == B_TRUE)
20599389cdeSJan Parcel 						return (EXCLUDE_SKIP);
2067c478bd9Sstevel@tonic-gate 			}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 			/* Count the # of slashes in the pattern and fname */
2097c478bd9Sstevel@tonic-gate 			num_pattern_slash = count_slashes(pattern_cp);
2107c478bd9Sstevel@tonic-gate 			num_fname_slash = count_slashes(fname_cp);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 			/*
21399389cdeSJan Parcel 			 * fname_cp is too short if this is not a dir
2147c478bd9Sstevel@tonic-gate 			 */
21599389cdeSJan Parcel 			if ((num_pattern_slash > num_fname_slash) &&
21699389cdeSJan Parcel 			    (fname_type != 'D')) {
21799389cdeSJan Parcel 				if (mod_ptr->include == B_TRUE)
21899389cdeSJan Parcel 					return (EXCLUDE_SKIP);
2197c478bd9Sstevel@tonic-gate 			}
2207c478bd9Sstevel@tonic-gate 
221c40f76e3Sjc 
222c40f76e3Sjc 			/*
223c40f76e3Sjc 			 * Take the leading '/' from fname_cp before
224c40f76e3Sjc 			 * decrementing the number of slashes.
225c40f76e3Sjc 			 */
226c40f76e3Sjc 			if (fname_cp[0] == '/') {
227c40f76e3Sjc 				(void) strlcpy(fname_cp,
228c40f76e3Sjc 				    strchr(fname_cp, '/') + 1,
229c40f76e3Sjc 				    sizeof (fname_cp));
230c40f76e3Sjc 				num_fname_slash--;
231c40f76e3Sjc 			}
232c40f76e3Sjc 
2337c478bd9Sstevel@tonic-gate 			/*
234c40f76e3Sjc 			 * Begin the loop, walk through the file name until
235c40f76e3Sjc 			 * it can be determined that there is no match.
236c40f76e3Sjc 			 * For example: if pattern is C/D/, and fname_cp is
237c40f76e3Sjc 			 * A/B/C/D/E then compare A/B/ with C/D/, if it doesn't
238c40f76e3Sjc 			 * match, then walk further so that the next iteration
239c40f76e3Sjc 			 * checks B/C/ against C/D/, continue until we have
240c40f76e3Sjc 			 * exhausted options.
241c40f76e3Sjc 			 * In the above case, the 3rd iteration will match
242c40f76e3Sjc 			 * C/D/ with C/D/.
2437c478bd9Sstevel@tonic-gate 			 */
244c40f76e3Sjc 			while (num_pattern_slash <= num_fname_slash) {
245c40f76e3Sjc 				/* get a pointer to our filename */
246c40f76e3Sjc 				fname_ptr = fname_cp;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 				/*
249c40f76e3Sjc 				 * Walk the filename through the slashes
250c40f76e3Sjc 				 * so that we have a component of the same
251c40f76e3Sjc 				 * number of slashes as the pattern.
2527c478bd9Sstevel@tonic-gate 				 */
253c40f76e3Sjc 
254c40f76e3Sjc 				for (i = 0; i < num_pattern_slash; i++) {
255c40f76e3Sjc 					ptr = strchr(fname_ptr, '/');
256c40f76e3Sjc 					fname_ptr = ptr + 1;
2577c478bd9Sstevel@tonic-gate 				}
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 				/*
260c40f76e3Sjc 				 * Save the character after our target slash
261c40f76e3Sjc 				 * before breaking the string for use with
262c40f76e3Sjc 				 * fnmatch
2637c478bd9Sstevel@tonic-gate 				 */
264c40f76e3Sjc 				saved_char = *(++ptr);
2657c478bd9Sstevel@tonic-gate 
266c40f76e3Sjc 				*ptr = '\0';
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 				/*
26999389cdeSJan Parcel 				 * Call compare function for the current
27099389cdeSJan Parcel 				 * component with the pattern we are looking
27199389cdeSJan Parcel 				 * for.
2727c478bd9Sstevel@tonic-gate 				 */
27399389cdeSJan Parcel 				if (fnmatch(pattern_cp, fname_cp,
27499389cdeSJan Parcel 				    FNM_PATHNAME) == 0) {
27599389cdeSJan Parcel 					if (mod_ptr->include == B_TRUE) {
27699389cdeSJan Parcel 						break;
27799389cdeSJan Parcel 					} else if (fname_type == 'D')
27899389cdeSJan Parcel 						return (EXCLUDE_PRUNE);
27999389cdeSJan Parcel 					else
28099389cdeSJan Parcel 						return (EXCLUDE_SKIP);
28199389cdeSJan Parcel 				} else if (mod_ptr->include == B_TRUE) {
28299389cdeSJan Parcel 					if (fname_type == 'D')
28399389cdeSJan Parcel 						return (EXCLUDE_PRUNE);
28499389cdeSJan Parcel 					else
28599389cdeSJan Parcel 						return (EXCLUDE_SKIP);
2867c478bd9Sstevel@tonic-gate 				}
2877c478bd9Sstevel@tonic-gate 				/*
288c40f76e3Sjc 				 * We didn't match, so restore the saved
289c40f76e3Sjc 				 * character to the original position.
290c40f76e3Sjc 				 */
291c40f76e3Sjc 				*ptr = saved_char;
292c40f76e3Sjc 
293c40f76e3Sjc 				/*
294c40f76e3Sjc 				 * Break down fname_cp, if it was A/B/C
295c40f76e3Sjc 				 * then after this operation it will be B/C
296c40f76e3Sjc 				 * in preparation for the next iteration.
2977c478bd9Sstevel@tonic-gate 				 */
298c40f76e3Sjc 				(void) strlcpy(fname_cp,
299c40f76e3Sjc 				    strchr(fname_cp, '/') + 1,
300c40f76e3Sjc 				    sizeof (fname_cp));
301c40f76e3Sjc 
302c40f76e3Sjc 				/*
303c40f76e3Sjc 				 * Decrement the number of slashes to
304c40f76e3Sjc 				 * compensate for the one removed above.
305c40f76e3Sjc 				 */
306c40f76e3Sjc 				num_fname_slash--;
30799389cdeSJan Parcel 			} /* end while loop looking down the path */
308c40f76e3Sjc 
309c40f76e3Sjc 			/*
310c40f76e3Sjc 			 * If we didn't get a match above then we may be on the
311c40f76e3Sjc 			 * last component of our filename.
312c40f76e3Sjc 			 * This is to handle the following cases
313c40f76e3Sjc 			 *    - filename is A/B/C/D/E and pattern may be D/E/
314c40f76e3Sjc 			 *    - filename is D/E and pattern may be D/E/
315c40f76e3Sjc 			 */
31699389cdeSJan Parcel 			if (num_pattern_slash == (num_fname_slash + 1)) {
317c40f76e3Sjc 
318c40f76e3Sjc 				/* strip the trailing slash from the pattern */
319c40f76e3Sjc 				ptr = strrchr(pattern_cp, '/');
320c40f76e3Sjc 				*ptr = '\0';
321c40f76e3Sjc 
32299389cdeSJan Parcel 				/* fnmatch returns 0 for a match */
32399389cdeSJan Parcel 				if (fnmatch(pattern_cp, fname_cp,
32499389cdeSJan Parcel 				    FNM_PATHNAME) == 0) {
32599389cdeSJan Parcel 					if (mod_ptr->include == B_FALSE) {
32699389cdeSJan Parcel 						if (fname_type == 'D')
32799389cdeSJan Parcel 							return (EXCLUDE_PRUNE);
32899389cdeSJan Parcel 						else
32999389cdeSJan Parcel 							return (EXCLUDE_SKIP);
33099389cdeSJan Parcel 					}
33199389cdeSJan Parcel 				} else if (mod_ptr->include == B_TRUE)
33299389cdeSJan Parcel 					return (EXCLUDE_SKIP);
33399389cdeSJan Parcel 
3347c478bd9Sstevel@tonic-gate 			}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 		}
3377c478bd9Sstevel@tonic-gate 	}
33899389cdeSJan Parcel 	return (NO_EXCLUDE);
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate static int
count_slashes(const char * in_path)3427c478bd9Sstevel@tonic-gate count_slashes(const char *in_path)
3437c478bd9Sstevel@tonic-gate {
3447c478bd9Sstevel@tonic-gate 	int num_fname_slash = 0;
3457c478bd9Sstevel@tonic-gate 	const char *p;
3467c478bd9Sstevel@tonic-gate 	for (p = in_path; *p != '\0'; p++)
3477c478bd9Sstevel@tonic-gate 		if (*p == '/')
3487c478bd9Sstevel@tonic-gate 			num_fname_slash++;
3497c478bd9Sstevel@tonic-gate 	return (num_fname_slash);
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate static struct rule *
gen_rulestruct(void)3537c478bd9Sstevel@tonic-gate gen_rulestruct(void)
3547c478bd9Sstevel@tonic-gate {
3557c478bd9Sstevel@tonic-gate 	struct rule	*new_rule;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	new_rule = (struct rule *)safe_calloc(sizeof (struct rule));
3587c478bd9Sstevel@tonic-gate 	return (new_rule);
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate static struct tree_modifier *
gen_tree_modifier(void)3627c478bd9Sstevel@tonic-gate gen_tree_modifier(void)
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	struct tree_modifier	*new_modifier;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	new_modifier = (struct tree_modifier *)safe_calloc
3677c478bd9Sstevel@tonic-gate 	    (sizeof (struct tree_modifier));
3687c478bd9Sstevel@tonic-gate 	return (new_modifier);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate static struct dir_component *
gen_dir_component(void)3727c478bd9Sstevel@tonic-gate gen_dir_component(void)
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 	struct dir_component	*new_dir;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	new_dir = (struct dir_component *)safe_calloc
3777c478bd9Sstevel@tonic-gate 	    (sizeof (struct dir_component));
3787c478bd9Sstevel@tonic-gate 	return (new_dir);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate /*
3827c478bd9Sstevel@tonic-gate  * Set up a default rule when there is no rules file.
3837c478bd9Sstevel@tonic-gate  */
3847c478bd9Sstevel@tonic-gate static struct rule *
setup_default_rule(char * reloc_root,uint_t flags)3857c478bd9Sstevel@tonic-gate setup_default_rule(char *reloc_root, uint_t flags)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate 	struct	rule	*new_rule;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	new_rule = add_single_rule(reloc_root[0] == '\0' ? "/" : reloc_root);
3907c478bd9Sstevel@tonic-gate 	init_rule(flags, new_rule);
3917c478bd9Sstevel@tonic-gate 	add_modifier(new_rule, "*");
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	return (new_rule);
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate /*
3977c478bd9Sstevel@tonic-gate  * Utility function, used to initialize the flag in a new rule structure.
3987c478bd9Sstevel@tonic-gate  */
3997c478bd9Sstevel@tonic-gate static void
init_rule(uint_t flags,struct rule * new_rule)4007c478bd9Sstevel@tonic-gate init_rule(uint_t flags, struct rule *new_rule)
4017c478bd9Sstevel@tonic-gate {
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	if (new_rule == NULL)
4047c478bd9Sstevel@tonic-gate 		return;
4057c478bd9Sstevel@tonic-gate 	new_rule->attr_list = flags;
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate /*
4097c478bd9Sstevel@tonic-gate  * Function to read the rulesfile.  Used by both 'bart create' and
4107c478bd9Sstevel@tonic-gate  * 'bart compare'.
4117c478bd9Sstevel@tonic-gate  */
4127c478bd9Sstevel@tonic-gate int
read_rules(FILE * file,char * reloc_root,uint_t in_flags,int create)4137c478bd9Sstevel@tonic-gate read_rules(FILE *file, char *reloc_root, uint_t in_flags, int create)
4147c478bd9Sstevel@tonic-gate {
4157c478bd9Sstevel@tonic-gate 	char		*s;
4167c478bd9Sstevel@tonic-gate 	struct rule	*block_begin = NULL, *new_rule, *rp;
4177c478bd9Sstevel@tonic-gate 	struct attr_keyword *akp;
4187c478bd9Sstevel@tonic-gate 	int		check_flag, ignore_flag, syntax_err, ret_code;
4199c84d166Srm 	int		global_block;
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	ret_code = EXIT;
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	lex_linenum = 0;
4247c478bd9Sstevel@tonic-gate 	check_flag = 0;
4257c478bd9Sstevel@tonic-gate 	ignore_flag = 0;
4267c478bd9Sstevel@tonic-gate 	syntax_err = 0;
4279c84d166Srm 	global_block = 1;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	if (file == NULL) {
4307c478bd9Sstevel@tonic-gate 		(void) setup_default_rule(reloc_root, in_flags);
4317c478bd9Sstevel@tonic-gate 		return (ret_code);
4327c478bd9Sstevel@tonic-gate 	} else if (!create) {
4337c478bd9Sstevel@tonic-gate 		block_begin = setup_default_rule("/", in_flags);
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	while (!feof(file)) {
4377c478bd9Sstevel@tonic-gate 		/* Read a line from the file */
4387c478bd9Sstevel@tonic-gate 		s = lex(file);
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 		/* skip blank lines and comments */
4417c478bd9Sstevel@tonic-gate 		if (s == NULL || *s == 0 || *s == '#')
4427c478bd9Sstevel@tonic-gate 			continue;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 		/*
4457c478bd9Sstevel@tonic-gate 		 * Beginning of a subtree and possibly a new block.
4467c478bd9Sstevel@tonic-gate 		 *
4477c478bd9Sstevel@tonic-gate 		 * If this is a new block, keep track of the beginning of
4487c478bd9Sstevel@tonic-gate 		 * the block. if there are directives later on, we need to
4497c478bd9Sstevel@tonic-gate 		 * apply that directive to all members of the block.
4507c478bd9Sstevel@tonic-gate 		 *
4517c478bd9Sstevel@tonic-gate 		 * If the first stmt in the file was an 'IGNORE all' or
4527c478bd9Sstevel@tonic-gate 		 * 'IGNORE contents', we need to keep track of it and
4537c478bd9Sstevel@tonic-gate 		 * automatically switch off contents checking for new
4547c478bd9Sstevel@tonic-gate 		 * subtrees.
4557c478bd9Sstevel@tonic-gate 		 */
4567c478bd9Sstevel@tonic-gate 		if (s[0] == '/') {
4579c84d166Srm 			/* subtree definition hence not a global block */
4589c84d166Srm 			global_block = 0;
4599c84d166Srm 
4607c478bd9Sstevel@tonic-gate 			new_rule = add_subtree_rule(s, reloc_root, create,
4617c478bd9Sstevel@tonic-gate 			    &ret_code);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 			s = lex(0);
4647c478bd9Sstevel@tonic-gate 			while ((s != NULL) && (*s != 0) && (*s != '#')) {
4657c478bd9Sstevel@tonic-gate 				add_modifier(new_rule, s);
4667c478bd9Sstevel@tonic-gate 				s = lex(0);
4677c478bd9Sstevel@tonic-gate 			}
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 			/* Found a new block, keep track of the beginning */
4707c478bd9Sstevel@tonic-gate 			if (block_begin == NULL ||
4717c478bd9Sstevel@tonic-gate 			    (ignore_flag != 0) || (check_flag != 0)) {
4727c478bd9Sstevel@tonic-gate 				block_begin = new_rule;
4737c478bd9Sstevel@tonic-gate 				check_flag = 0;
4747c478bd9Sstevel@tonic-gate 				ignore_flag = 0;
4757c478bd9Sstevel@tonic-gate 			}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 			/* Apply global settings to this block, if any */
4787c478bd9Sstevel@tonic-gate 			init_rule(in_flags, new_rule);
4797c478bd9Sstevel@tonic-gate 		} else if (IGNORE_KEYWORD(s) || CHECK_KEYWORD(s)) {
4807c478bd9Sstevel@tonic-gate 			int check_kw;
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 			if (IGNORE_KEYWORD(s)) {
4837c478bd9Sstevel@tonic-gate 				ignore_flag++;
4847c478bd9Sstevel@tonic-gate 				check_kw = 0;
4857c478bd9Sstevel@tonic-gate 			} else {
4867c478bd9Sstevel@tonic-gate 				check_flag++;
4877c478bd9Sstevel@tonic-gate 				check_kw = 1;
4887c478bd9Sstevel@tonic-gate 			}
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 			/* Parse next token */
4917c478bd9Sstevel@tonic-gate 			s = lex(0);
4927c478bd9Sstevel@tonic-gate 			while ((s != NULL) && (*s != 0) && (*s != '#')) {
4937c478bd9Sstevel@tonic-gate 				akp = attr_keylookup(s);
4947c478bd9Sstevel@tonic-gate 				if (akp == NULL) {
4957c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, SYNTAX_ERR, s);
4967c478bd9Sstevel@tonic-gate 					syntax_err++;
4977c478bd9Sstevel@tonic-gate 					exit(2);
4987c478bd9Sstevel@tonic-gate 				}
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 				/*
5017c478bd9Sstevel@tonic-gate 				 * For all the flags, check if this is a global
5029c84d166Srm 				 * IGNORE/CHECK. If so, set the global flags.
5037c478bd9Sstevel@tonic-gate 				 *
5047c478bd9Sstevel@tonic-gate 				 * NOTE: The only time you can have a
5057c478bd9Sstevel@tonic-gate 				 * global ignore is when its the
5067c478bd9Sstevel@tonic-gate 				 * stmt before any blocks have been
5077c478bd9Sstevel@tonic-gate 				 * spec'd.
5087c478bd9Sstevel@tonic-gate 				 */
5099c84d166Srm 				if (global_block) {
5107c478bd9Sstevel@tonic-gate 					if (check_kw)
5117c478bd9Sstevel@tonic-gate 						in_flags |= akp->ak_flags;
5127c478bd9Sstevel@tonic-gate 					else
5137c478bd9Sstevel@tonic-gate 						in_flags &= ~(akp->ak_flags);
5147c478bd9Sstevel@tonic-gate 				} else {
5157c478bd9Sstevel@tonic-gate 					for (rp = block_begin; rp != NULL;
5167c478bd9Sstevel@tonic-gate 					    rp = rp->next) {
5177c478bd9Sstevel@tonic-gate 						if (check_kw)
5187c478bd9Sstevel@tonic-gate 							rp->attr_list |=
5197c478bd9Sstevel@tonic-gate 							    akp->ak_flags;
5207c478bd9Sstevel@tonic-gate 						else
5217c478bd9Sstevel@tonic-gate 							rp->attr_list &=
5227c478bd9Sstevel@tonic-gate 							    ~(akp->ak_flags);
5237c478bd9Sstevel@tonic-gate 					}
5247c478bd9Sstevel@tonic-gate 				}
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 				/* Parse next token */
5277c478bd9Sstevel@tonic-gate 				s = lex(0);
5287c478bd9Sstevel@tonic-gate 			}
5297c478bd9Sstevel@tonic-gate 		} else {
5307c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, SYNTAX_ERR, s);
5317c478bd9Sstevel@tonic-gate 			s = lex(0);
5327c478bd9Sstevel@tonic-gate 			while (s != NULL && *s != 0) {
5337c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, " %s", s);
5347c478bd9Sstevel@tonic-gate 				s = lex(0);
5357c478bd9Sstevel@tonic-gate 			}
5367c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "\n");
5377c478bd9Sstevel@tonic-gate 			syntax_err++;
5387c478bd9Sstevel@tonic-gate 		}
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	(void) fclose(file);
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	if (syntax_err) {
5447c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, SYNTAX_ABORT);
5457c478bd9Sstevel@tonic-gate 		exit(2);
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	return (ret_code);
5497c478bd9Sstevel@tonic-gate }
55099389cdeSJan Parcel /*
55199389cdeSJan Parcel  * Add a modifier to the mod_ptr list in each rule, putting negative
55299389cdeSJan Parcel  * directory entries
55399389cdeSJan Parcel  * first to guarantee walks will be appropriately pruned.
55499389cdeSJan Parcel  */
5557c478bd9Sstevel@tonic-gate static void
add_modifier(struct rule * rule,char * modifier_str)5567c478bd9Sstevel@tonic-gate add_modifier(struct rule *rule, char *modifier_str)
5577c478bd9Sstevel@tonic-gate {
55899389cdeSJan Parcel 	int	include, is_dir;
55999389cdeSJan Parcel 	char	*pattern;
5607c478bd9Sstevel@tonic-gate 	struct tree_modifier	*new_mod_ptr, *curr_mod_ptr;
5617c478bd9Sstevel@tonic-gate 	struct rule		*this_rule;
5627c478bd9Sstevel@tonic-gate 
56399389cdeSJan Parcel 	include = B_TRUE;
56499389cdeSJan Parcel 	pattern = modifier_str;
56599389cdeSJan Parcel 
56699389cdeSJan Parcel 	/* see if the pattern is an include or an exclude */
56799389cdeSJan Parcel 	if (pattern[0] == '!') {
56899389cdeSJan Parcel 		include = B_FALSE;
56999389cdeSJan Parcel 		pattern++;
57099389cdeSJan Parcel 	}
57199389cdeSJan Parcel 
57299389cdeSJan Parcel 	is_dir = (pattern[0] != '\0' && pattern[strlen(pattern) - 1] == '/');
57399389cdeSJan Parcel 
57499389cdeSJan Parcel 	for (this_rule = rule; this_rule != NULL; this_rule = this_rule->next) {
5757c478bd9Sstevel@tonic-gate 		new_mod_ptr = gen_tree_modifier();
57699389cdeSJan Parcel 		new_mod_ptr->include = include;
57799389cdeSJan Parcel 		new_mod_ptr->is_dir = is_dir;
57899389cdeSJan Parcel 		new_mod_ptr->mod_str = safe_strdup(pattern);
5797c478bd9Sstevel@tonic-gate 
58099389cdeSJan Parcel 		if (is_dir && !include) {
58199389cdeSJan Parcel 			new_mod_ptr->next = this_rule->modifiers;
58299389cdeSJan Parcel 			this_rule->modifiers = new_mod_ptr;
58399389cdeSJan Parcel 		} else if (this_rule->modifiers == NULL)
5847c478bd9Sstevel@tonic-gate 			this_rule->modifiers = new_mod_ptr;
5857c478bd9Sstevel@tonic-gate 		else {
5867c478bd9Sstevel@tonic-gate 			curr_mod_ptr = this_rule->modifiers;
5877c478bd9Sstevel@tonic-gate 			while (curr_mod_ptr->next != NULL)
5887c478bd9Sstevel@tonic-gate 				curr_mod_ptr = curr_mod_ptr->next;
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 			curr_mod_ptr->next = new_mod_ptr;
5917c478bd9Sstevel@tonic-gate 		}
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate }
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate /*
5967c478bd9Sstevel@tonic-gate  * This funtion is invoked when reading rulesfiles.  A subtree may have
5977c478bd9Sstevel@tonic-gate  * wildcards in it, e.g., '/home/n*', which is expected to match all home
5987c478bd9Sstevel@tonic-gate  * dirs which start with an 'n'.
5997c478bd9Sstevel@tonic-gate  *
6007c478bd9Sstevel@tonic-gate  * This function needs to break down the subtree into its components.  For
6017c478bd9Sstevel@tonic-gate  * each component, see how many directories match.  Take the subtree list just
6027c478bd9Sstevel@tonic-gate  * generated and run it through again, this time looking at the next component.
6037c478bd9Sstevel@tonic-gate  * At each iteration, keep a linked list of subtrees that currently match.
6047c478bd9Sstevel@tonic-gate  * Once the final list is created, invoke add_single_rule() to create the
6057c478bd9Sstevel@tonic-gate  * rule struct with the correct information.
6067c478bd9Sstevel@tonic-gate  *
6077c478bd9Sstevel@tonic-gate  * This function returns a ptr to the first element in the block of subtrees
6087c478bd9Sstevel@tonic-gate  * which matched the subtree def'n in the rulesfile.
6097c478bd9Sstevel@tonic-gate  */
6107c478bd9Sstevel@tonic-gate static struct rule *
add_subtree_rule(char * rule,char * reloc_root,int create,int * err_code)6117c478bd9Sstevel@tonic-gate add_subtree_rule(char *rule, char *reloc_root, int create, int *err_code)
6127c478bd9Sstevel@tonic-gate {
61399389cdeSJan Parcel 	char			full_path[PATH_MAX], pattern[PATH_MAX];
61499389cdeSJan Parcel 	char			new_dirname[PATH_MAX];
61599389cdeSJan Parcel 	char			*beg_pattern, *end_pattern, *curr_dirname;
61699389cdeSJan Parcel 	struct	dir_component	*current_level = NULL, *next_level = NULL;
61799389cdeSJan Parcel 	struct	dir_component	*tmp_ptr;
6187c478bd9Sstevel@tonic-gate 	DIR			*dir_ptr;
6197c478bd9Sstevel@tonic-gate 	struct dirent		*dir_entry;
6207c478bd9Sstevel@tonic-gate 	struct rule		*begin_rule = NULL;
6217c478bd9Sstevel@tonic-gate 	int			ret;
6227c478bd9Sstevel@tonic-gate 	struct stat64		statb;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	(void) snprintf(full_path, sizeof (full_path),
6257c478bd9Sstevel@tonic-gate 	    (rule[0] == '/') ? "%s%s" : "%s/%s", reloc_root, rule);
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	/*
6287c478bd9Sstevel@tonic-gate 	 * In the case of 'bart compare', don't validate
6297c478bd9Sstevel@tonic-gate 	 * the subtrees, since the machine running the
6307c478bd9Sstevel@tonic-gate 	 * comparison may not be the machine which generated
6317c478bd9Sstevel@tonic-gate 	 * the manifest.
6327c478bd9Sstevel@tonic-gate 	 */
6337c478bd9Sstevel@tonic-gate 	if (create == 0)
6347c478bd9Sstevel@tonic-gate 		return (add_single_rule(full_path));
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	/* Insert 'current_level' into the linked list */
6387c478bd9Sstevel@tonic-gate 	add_dir(&current_level, NULL);
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	/* Special case: occurs when -R is "/" and the subtree is "/" */
6417c478bd9Sstevel@tonic-gate 	if (strcmp(full_path, "/") == 0)
6427c478bd9Sstevel@tonic-gate 		(void) strcpy(current_level->dirname, "/");
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	beg_pattern = full_path;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	while (beg_pattern != NULL) {
6477c478bd9Sstevel@tonic-gate 		/*
6487c478bd9Sstevel@tonic-gate 		 * Extract the pathname component starting at 'beg_pattern'.
6497c478bd9Sstevel@tonic-gate 		 * Take those chars and put them into 'pattern'.
6507c478bd9Sstevel@tonic-gate 		 */
6517c478bd9Sstevel@tonic-gate 		while (*beg_pattern == '/')
6527c478bd9Sstevel@tonic-gate 			beg_pattern++;
6537c478bd9Sstevel@tonic-gate 		if (*beg_pattern == '\0')	/* end of pathname */
6547c478bd9Sstevel@tonic-gate 			break;
6557c478bd9Sstevel@tonic-gate 		end_pattern = strchr(beg_pattern, '/');
6567c478bd9Sstevel@tonic-gate 		if (end_pattern != NULL)
6577c478bd9Sstevel@tonic-gate 			(void) strlcpy(pattern, beg_pattern,
6587c478bd9Sstevel@tonic-gate 			    end_pattern - beg_pattern + 1);
6597c478bd9Sstevel@tonic-gate 		else
6607c478bd9Sstevel@tonic-gate 			(void) strlcpy(pattern, beg_pattern, sizeof (pattern));
6617c478bd9Sstevel@tonic-gate 		beg_pattern = end_pattern;
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 		/*
6647c478bd9Sstevel@tonic-gate 		 * At this point, search for 'pattern' as a *subdirectory* of
6657c478bd9Sstevel@tonic-gate 		 * the dirs in the linked list.
6667c478bd9Sstevel@tonic-gate 		 */
6677c478bd9Sstevel@tonic-gate 		while (current_level != NULL) {
6687c478bd9Sstevel@tonic-gate 			/* curr_dirname used to make the code more readable */
6697c478bd9Sstevel@tonic-gate 			curr_dirname = current_level->dirname;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 			/* Initialization case */
6727c478bd9Sstevel@tonic-gate 			if (strlen(curr_dirname) == 0)
6737c478bd9Sstevel@tonic-gate 				(void) strcpy(curr_dirname, "/");
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 			/* Open up the dir for this element in the list */
6767c478bd9Sstevel@tonic-gate 			dir_ptr = opendir(curr_dirname);
6777c478bd9Sstevel@tonic-gate 			dir_entry = NULL;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 			if (dir_ptr == NULL) {
6807c478bd9Sstevel@tonic-gate 				perror(curr_dirname);
6817c478bd9Sstevel@tonic-gate 				*err_code = WARNING_EXIT;
6827c478bd9Sstevel@tonic-gate 			} else
6837c478bd9Sstevel@tonic-gate 				dir_entry = readdir(dir_ptr);
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 			/*
6867c478bd9Sstevel@tonic-gate 			 * Now iterate through the subdirs of 'curr_dirname'
6877c478bd9Sstevel@tonic-gate 			 * In the case of a match against 'pattern',
6887c478bd9Sstevel@tonic-gate 			 * add the path to the next linked list, which
6897c478bd9Sstevel@tonic-gate 			 * will be matched on the next iteration.
6907c478bd9Sstevel@tonic-gate 			 */
6917c478bd9Sstevel@tonic-gate 			while (dir_entry != NULL) {
6927c478bd9Sstevel@tonic-gate 				/* Skip the dirs "." and ".." */
6937c478bd9Sstevel@tonic-gate 				if ((strcmp(dir_entry->d_name, ".") == 0) ||
6947c478bd9Sstevel@tonic-gate 				    (strcmp(dir_entry->d_name, "..") == 0)) {
6957c478bd9Sstevel@tonic-gate 					dir_entry = readdir(dir_ptr);
6967c478bd9Sstevel@tonic-gate 					continue;
6977c478bd9Sstevel@tonic-gate 				}
6987c478bd9Sstevel@tonic-gate 				if (fnmatch(pattern, dir_entry->d_name,
6997c478bd9Sstevel@tonic-gate 				    FNM_PATHNAME) == 0) {
7007c478bd9Sstevel@tonic-gate 					/*
7017c478bd9Sstevel@tonic-gate 					 * Build 'new_dirname' which will be
7027c478bd9Sstevel@tonic-gate 					 * examined on the next iteration.
7037c478bd9Sstevel@tonic-gate 					 */
7047c478bd9Sstevel@tonic-gate 					if (curr_dirname[strlen(curr_dirname)-1]
70599389cdeSJan Parcel 					    != '/')
7067c478bd9Sstevel@tonic-gate 						(void) snprintf(new_dirname,
7077c478bd9Sstevel@tonic-gate 						    sizeof (new_dirname),
7087c478bd9Sstevel@tonic-gate 						    "%s/%s", curr_dirname,
7097c478bd9Sstevel@tonic-gate 						    dir_entry->d_name);
7107c478bd9Sstevel@tonic-gate 					else
7117c478bd9Sstevel@tonic-gate 						(void) snprintf(new_dirname,
7127c478bd9Sstevel@tonic-gate 						    sizeof (new_dirname),
7137c478bd9Sstevel@tonic-gate 						    "%s%s", curr_dirname,
7147c478bd9Sstevel@tonic-gate 						    dir_entry->d_name);
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 					/* Add to the next lined list */
7177c478bd9Sstevel@tonic-gate 					add_dir(&next_level, new_dirname);
7187c478bd9Sstevel@tonic-gate 				}
7197c478bd9Sstevel@tonic-gate 				dir_entry = readdir(dir_ptr);
7207c478bd9Sstevel@tonic-gate 			}
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 			/* Close directory */
7237c478bd9Sstevel@tonic-gate 			if (dir_ptr != NULL)
7247c478bd9Sstevel@tonic-gate 				(void) closedir(dir_ptr);
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 			/* Free this entry and move on.... */
7277c478bd9Sstevel@tonic-gate 			tmp_ptr = current_level;
7287c478bd9Sstevel@tonic-gate 			current_level = current_level->next;
7297c478bd9Sstevel@tonic-gate 			free(tmp_ptr);
7307c478bd9Sstevel@tonic-gate 		}
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 		/*
7337c478bd9Sstevel@tonic-gate 		 * OK, done with this level.  Move to the next level and
7347c478bd9Sstevel@tonic-gate 		 * advance the ptrs which indicate the component name.
7357c478bd9Sstevel@tonic-gate 		 */
7367c478bd9Sstevel@tonic-gate 		current_level = next_level;
7377c478bd9Sstevel@tonic-gate 		next_level = NULL;
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	tmp_ptr = current_level;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	/* Error case: the subtree doesn't exist! */
7437c478bd9Sstevel@tonic-gate 	if (current_level == NULL) {
7447c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, INVALID_SUBTREE, full_path);
7457c478bd9Sstevel@tonic-gate 		*err_code = WARNING_EXIT;
7467c478bd9Sstevel@tonic-gate 	}
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	/*
7497c478bd9Sstevel@tonic-gate 	 * Iterate through all the dirnames which match the pattern and
7507c478bd9Sstevel@tonic-gate 	 * add them to to global list of subtrees which must be examined.
7517c478bd9Sstevel@tonic-gate 	 */
7527c478bd9Sstevel@tonic-gate 	while (current_level != NULL) {
7537c478bd9Sstevel@tonic-gate 		/*
7547c478bd9Sstevel@tonic-gate 		 * Sanity check for 'bart create', make sure the subtree
7557c478bd9Sstevel@tonic-gate 		 * points to a valid object.
7567c478bd9Sstevel@tonic-gate 		 */
7577c478bd9Sstevel@tonic-gate 		ret = lstat64(current_level->dirname, &statb);
7587c478bd9Sstevel@tonic-gate 		if (ret < 0) {
7597c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, INVALID_SUBTREE,
7607c478bd9Sstevel@tonic-gate 			    current_level->dirname);
7617c478bd9Sstevel@tonic-gate 			current_level = current_level->next;
7627c478bd9Sstevel@tonic-gate 			*err_code = WARNING_EXIT;
7637c478bd9Sstevel@tonic-gate 			continue;
7647c478bd9Sstevel@tonic-gate 		}
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 		if (begin_rule == NULL) {
7677c478bd9Sstevel@tonic-gate 			begin_rule =
7687c478bd9Sstevel@tonic-gate 			    add_single_rule(current_level->dirname);
7697c478bd9Sstevel@tonic-gate 		} else
7707c478bd9Sstevel@tonic-gate 			(void) add_single_rule(current_level->dirname);
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 		current_level = current_level->next;
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	/*
7767c478bd9Sstevel@tonic-gate 	 * Free up the memory and return a ptr to the first entry in the
7777c478bd9Sstevel@tonic-gate 	 * subtree block.  This is necessary for the parser, which may need
7787c478bd9Sstevel@tonic-gate 	 * to add modifier strings to all the elements in this block.
7797c478bd9Sstevel@tonic-gate 	 */
7807c478bd9Sstevel@tonic-gate 	dirs_cleanup(tmp_ptr);
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	return (begin_rule);
7837c478bd9Sstevel@tonic-gate }
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate /*
7877c478bd9Sstevel@tonic-gate  * Add a single entry to the linked list of rules to be read.  Does not do
7887c478bd9Sstevel@tonic-gate  * the wildcard expansion of 'add_subtree_rule', so is much simpler.
7897c478bd9Sstevel@tonic-gate  */
7907c478bd9Sstevel@tonic-gate static struct rule *
add_single_rule(char * path)7917c478bd9Sstevel@tonic-gate add_single_rule(char *path)
7927c478bd9Sstevel@tonic-gate {
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	/*
7957c478bd9Sstevel@tonic-gate 	 * If the rules list does NOT exist, then create it.
7967c478bd9Sstevel@tonic-gate 	 * If the rules list does exist, then traverse the next element.
7977c478bd9Sstevel@tonic-gate 	 */
7987c478bd9Sstevel@tonic-gate 	if (first_rule == NULL) {
7997c478bd9Sstevel@tonic-gate 		first_rule = gen_rulestruct();
8007c478bd9Sstevel@tonic-gate 		current_rule = first_rule;
8017c478bd9Sstevel@tonic-gate 	} else {
8027c478bd9Sstevel@tonic-gate 		current_rule->next = gen_rulestruct();
8037c478bd9Sstevel@tonic-gate 		current_rule->next->prev = current_rule;
8047c478bd9Sstevel@tonic-gate 		current_rule = current_rule->next;
8057c478bd9Sstevel@tonic-gate 	}
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	/* Setup the rule struct, handle relocatable roots, i.e. '-R' option */
8087c478bd9Sstevel@tonic-gate 	(void) strlcpy(current_rule->subtree, path,
8097c478bd9Sstevel@tonic-gate 	    sizeof (current_rule->subtree));
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	return (current_rule);
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate /*
8157c478bd9Sstevel@tonic-gate  * Code stolen from filesync utility, used by read_rules() to read in the
8167c478bd9Sstevel@tonic-gate  * rulesfile.
8177c478bd9Sstevel@tonic-gate  */
8187c478bd9Sstevel@tonic-gate static char *
lex(FILE * file)8197c478bd9Sstevel@tonic-gate lex(FILE *file)
8207c478bd9Sstevel@tonic-gate {
8217c478bd9Sstevel@tonic-gate 	char c, delim;
8227c478bd9Sstevel@tonic-gate 	char *p;
8237c478bd9Sstevel@tonic-gate 	char *s;
8247c478bd9Sstevel@tonic-gate 	static char *savep;
8257c478bd9Sstevel@tonic-gate 	static char namebuf[ BUF_SIZE ];
8267c478bd9Sstevel@tonic-gate 	static char inbuf[ BUF_SIZE ];
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	if (file) {			/* read a new line		*/
8297c478bd9Sstevel@tonic-gate 		p = inbuf + sizeof (inbuf);
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 		s = inbuf;
8327c478bd9Sstevel@tonic-gate 		/* read the next input line, with all continuations	*/
8337c478bd9Sstevel@tonic-gate 		while (savep = fgets(s, p - s, file)) {
8347c478bd9Sstevel@tonic-gate 			lex_linenum++;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 			/* go find the last character of the input line	*/
8377c478bd9Sstevel@tonic-gate 			while (*s && s[1])
8387c478bd9Sstevel@tonic-gate 				s++;
8397c478bd9Sstevel@tonic-gate 			if (*s == '\n')
8407c478bd9Sstevel@tonic-gate 				s--;
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 			/* see whether or not we need a continuation	*/
8437c478bd9Sstevel@tonic-gate 			if (s < inbuf || *s != '\\')
8447c478bd9Sstevel@tonic-gate 				break;
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 			continue;
8477c478bd9Sstevel@tonic-gate 		}
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 		if (savep == NULL)
8507c478bd9Sstevel@tonic-gate 			return (0);
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 		s = inbuf;
8537c478bd9Sstevel@tonic-gate 	} else {			/* continue with old line	*/
8547c478bd9Sstevel@tonic-gate 		if (savep == NULL)
8557c478bd9Sstevel@tonic-gate 			return (0);
8567c478bd9Sstevel@tonic-gate 		s = savep;
8577c478bd9Sstevel@tonic-gate 	}
8587c478bd9Sstevel@tonic-gate 	savep = NULL;
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	/* skip over leading white space	*/
8617c478bd9Sstevel@tonic-gate 	while (isspace(*s))
8627c478bd9Sstevel@tonic-gate 		s++;
8637c478bd9Sstevel@tonic-gate 	if (*s == 0)
8647c478bd9Sstevel@tonic-gate 		return (0);
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	/* see if this is a quoted string	*/
8677c478bd9Sstevel@tonic-gate 	c = *s;
8687c478bd9Sstevel@tonic-gate 	if (c == '\'' || c == '"') {
8697c478bd9Sstevel@tonic-gate 		delim = c;
8707c478bd9Sstevel@tonic-gate 		s++;
8717c478bd9Sstevel@tonic-gate 	} else
8727c478bd9Sstevel@tonic-gate 		delim = 0;
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	/* copy the token into the buffer	*/
8757c478bd9Sstevel@tonic-gate 	for (p = namebuf; (c = *s) != 0; s++) {
8767c478bd9Sstevel@tonic-gate 		/* literal escape		*/
8777c478bd9Sstevel@tonic-gate 		if (c == '\\') {
8787c478bd9Sstevel@tonic-gate 			s++;
8797c478bd9Sstevel@tonic-gate 			*p++ = *s;
8807c478bd9Sstevel@tonic-gate 			continue;
8817c478bd9Sstevel@tonic-gate 		}
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 		/* closing delimiter		*/
8847c478bd9Sstevel@tonic-gate 		if (c == delim) {
8857c478bd9Sstevel@tonic-gate 			s++;
8867c478bd9Sstevel@tonic-gate 			break;
8877c478bd9Sstevel@tonic-gate 		}
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 		/* delimiting white space	*/
8907c478bd9Sstevel@tonic-gate 		if (delim == 0 && isspace(c))
8917c478bd9Sstevel@tonic-gate 			break;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 		/* ordinary characters		*/
8947c478bd9Sstevel@tonic-gate 		*p++ = *s;
8957c478bd9Sstevel@tonic-gate 	}
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	/* remember where we left off		*/
8997c478bd9Sstevel@tonic-gate 	savep = *s ? s : 0;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	/* null terminate and return the buffer	*/
9027c478bd9Sstevel@tonic-gate 	*p = 0;
9037c478bd9Sstevel@tonic-gate 	return (namebuf);
9047c478bd9Sstevel@tonic-gate }
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate /*
9077c478bd9Sstevel@tonic-gate  * Iterate through the dir strcutures and free memory.
9087c478bd9Sstevel@tonic-gate  */
9097c478bd9Sstevel@tonic-gate static void
dirs_cleanup(struct dir_component * dir)9107c478bd9Sstevel@tonic-gate dirs_cleanup(struct dir_component *dir)
9117c478bd9Sstevel@tonic-gate {
9127c478bd9Sstevel@tonic-gate 	struct	dir_component	*next;
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	while (dir != NULL) {
9157c478bd9Sstevel@tonic-gate 		next = dir->next;
9167c478bd9Sstevel@tonic-gate 		free(dir);
9177c478bd9Sstevel@tonic-gate 		dir = next;
9187c478bd9Sstevel@tonic-gate 	}
9197c478bd9Sstevel@tonic-gate }
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate /*
9227c478bd9Sstevel@tonic-gate  * Create and initialize a new dir structure.  Used by add_subtree_rule() when
9237c478bd9Sstevel@tonic-gate  * doing expansion of directory names caused by wildcards.
9247c478bd9Sstevel@tonic-gate  */
9257c478bd9Sstevel@tonic-gate static void
add_dir(struct dir_component ** dir,char * dirname)9267c478bd9Sstevel@tonic-gate add_dir(struct dir_component **dir, char *dirname)
9277c478bd9Sstevel@tonic-gate {
9287c478bd9Sstevel@tonic-gate 	struct	dir_component	*new, *next_dir;
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	new = gen_dir_component();
9317c478bd9Sstevel@tonic-gate 	if (dirname != NULL)
9327c478bd9Sstevel@tonic-gate 		(void) strlcpy(new->dirname, dirname, sizeof (new->dirname));
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	if (*dir == NULL)
9357c478bd9Sstevel@tonic-gate 		*dir = new;
9367c478bd9Sstevel@tonic-gate 	else {
9377c478bd9Sstevel@tonic-gate 		next_dir = *dir;
9387c478bd9Sstevel@tonic-gate 		while (next_dir->next != NULL)
9397c478bd9Sstevel@tonic-gate 			next_dir = next_dir->next;
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 		next_dir->next = new;
9427c478bd9Sstevel@tonic-gate 	}
9437c478bd9Sstevel@tonic-gate }
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate /*
9467c478bd9Sstevel@tonic-gate  * Traverse the linked list of rules in a REVERSE order.
9477c478bd9Sstevel@tonic-gate  */
9487c478bd9Sstevel@tonic-gate static struct rule *
get_last_entry(boolean_t reset)9497c478bd9Sstevel@tonic-gate get_last_entry(boolean_t reset)
9507c478bd9Sstevel@tonic-gate {
9517c478bd9Sstevel@tonic-gate 	static struct rule	*curr_root = NULL;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	if (reset) {
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 		curr_root = first_rule;
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 		/* RESET: set cur_root to the end of the list */
9587c478bd9Sstevel@tonic-gate 		while (curr_root != NULL)
9597c478bd9Sstevel@tonic-gate 			if (curr_root->next == NULL)
9607c478bd9Sstevel@tonic-gate 				break;
9617c478bd9Sstevel@tonic-gate 			else
9627c478bd9Sstevel@tonic-gate 				curr_root = curr_root->next;
9637c478bd9Sstevel@tonic-gate 	} else
9647c478bd9Sstevel@tonic-gate 		curr_root = (curr_root->prev);
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	return (curr_root);
9677c478bd9Sstevel@tonic-gate }
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate /*
9707c478bd9Sstevel@tonic-gate  * Traverse the first entry, used by 'bart create' to iterate through
9717c478bd9Sstevel@tonic-gate  * subtrees or individual filenames.
9727c478bd9Sstevel@tonic-gate  */
9737c478bd9Sstevel@tonic-gate struct rule *
get_first_subtree()9747c478bd9Sstevel@tonic-gate get_first_subtree()
9757c478bd9Sstevel@tonic-gate {
9767c478bd9Sstevel@tonic-gate 	return (first_rule);
9777c478bd9Sstevel@tonic-gate }
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate /*
9807c478bd9Sstevel@tonic-gate  * Traverse the next entry, used by 'bart create' to iterate through
9817c478bd9Sstevel@tonic-gate  * subtrees or individual filenames.
9827c478bd9Sstevel@tonic-gate  */
9837c478bd9Sstevel@tonic-gate struct rule *
get_next_subtree(struct rule * entry)9847c478bd9Sstevel@tonic-gate get_next_subtree(struct rule *entry)
9857c478bd9Sstevel@tonic-gate {
9867c478bd9Sstevel@tonic-gate 	return (entry->next);
9877c478bd9Sstevel@tonic-gate }
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate char *
safe_strdup(char * s)9907c478bd9Sstevel@tonic-gate safe_strdup(char *s)
9917c478bd9Sstevel@tonic-gate {
9927c478bd9Sstevel@tonic-gate 	char *ret;
9937c478bd9Sstevel@tonic-gate 	size_t len;
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	len = strlen(s) + 1;
9967c478bd9Sstevel@tonic-gate 	ret = safe_calloc(len);
9977c478bd9Sstevel@tonic-gate 	(void) strlcpy(ret, s, len);
9987c478bd9Sstevel@tonic-gate 	return (ret);
9997c478bd9Sstevel@tonic-gate }
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate /*
10027c478bd9Sstevel@tonic-gate  * Function to match a filename against the subtrees in the link list
10037c478bd9Sstevel@tonic-gate  * of 'rule' strcutures.  Upon finding a matching rule, see if it should
10047c478bd9Sstevel@tonic-gate  * be excluded.  Keep going until a match is found OR all rules have been
10057c478bd9Sstevel@tonic-gate  * exhausted.
10067c478bd9Sstevel@tonic-gate  * NOTES: Rules are parsed in reverse;
10077c478bd9Sstevel@tonic-gate  * satisfies the spec that "Last rule wins".  Also, the default rule should
10087c478bd9Sstevel@tonic-gate  * always match, so this function should NEVER return NULL.
10097c478bd9Sstevel@tonic-gate  */
10107c478bd9Sstevel@tonic-gate struct rule *
check_rules(const char * fname,char type)10117c478bd9Sstevel@tonic-gate check_rules(const char *fname, char type)
10127c478bd9Sstevel@tonic-gate {
10137c478bd9Sstevel@tonic-gate 	struct rule		*root;
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	root = get_last_entry(B_TRUE);
10167c478bd9Sstevel@tonic-gate 	while (root != NULL) {
10177c478bd9Sstevel@tonic-gate 		if (match_subtree(fname, root->subtree)) {
101899389cdeSJan Parcel 			if (exclude_fname(fname, type, root) == NO_EXCLUDE)
10197c478bd9Sstevel@tonic-gate 				break;
10207c478bd9Sstevel@tonic-gate 		}
10217c478bd9Sstevel@tonic-gate 		root = get_last_entry(B_FALSE);
10227c478bd9Sstevel@tonic-gate 	}
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	return (root);
10257c478bd9Sstevel@tonic-gate }
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate /*
1028*bbf21555SRichard Lowe  * Function to determine if an entry in a rules file (see bart_rules(5)) applies
1029f7bbf134Shm  * to a filename. We truncate "fname" such that it has the same number of
1030f7bbf134Shm  * components as "rule" and let fnmatch(3C) do the rest. A "component" is one
1031f7bbf134Shm  * part of an fname as delimited by slashes ('/'). So "/A/B/C/D" has four
1032f7bbf134Shm  * components: "A", "B", "C" and "D".
1033f7bbf134Shm  *
1034f7bbf134Shm  * For example:
1035f7bbf134Shm  *
1036f7bbf134Shm  * 1. the rule "/home/nickiso" applies to fname "/home/nickiso/src/foo.c" so
1037f7bbf134Shm  * should match.
1038f7bbf134Shm  *
1039f7bbf134Shm  * 2. the rule "/home/nickiso/temp/src" does not apply to fname
1040f7bbf134Shm  * "/home/nickiso/foo.c" so should not match.
10417c478bd9Sstevel@tonic-gate  */
10427c478bd9Sstevel@tonic-gate static int
match_subtree(const char * fname,char * rule)10437c478bd9Sstevel@tonic-gate match_subtree(const char *fname, char *rule)
10447c478bd9Sstevel@tonic-gate {
1045f7bbf134Shm 	int	match, num_rule_slash;
1046f7bbf134Shm 	char	*ptr, fname_cp[PATH_MAX];
10477c478bd9Sstevel@tonic-gate 
1048f7bbf134Shm 	/* If rule has more components than fname, it cannot match. */
1049f7bbf134Shm 	if ((num_rule_slash = count_slashes(rule)) > count_slashes(fname))
1050f7bbf134Shm 		return (0);
1051f7bbf134Shm 
1052f7bbf134Shm 	/* Create a copy of fname that we can truncate. */
1053f7bbf134Shm 	(void) strlcpy(fname_cp, fname, sizeof (fname_cp));
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	/*
1056f7bbf134Shm 	 * Truncate fname_cp such that it has the same number of components
1057f7bbf134Shm 	 * as rule. If rule ends with '/', so should fname_cp. ie:
10587c478bd9Sstevel@tonic-gate 	 *
1059f7bbf134Shm 	 * rule		fname			fname_cp	matches
1060f7bbf134Shm 	 * ----		-----			--------	-------
1061f7bbf134Shm 	 * /home/dir*	/home/dir0/dir1/fileA	/home/dir0	yes
1062f7bbf134Shm 	 * /home/dir/	/home/dir0/dir1/fileA	/home/dir0/	no
10637c478bd9Sstevel@tonic-gate 	 */
1064f7bbf134Shm 	for (ptr = fname_cp; num_rule_slash > 0; num_rule_slash--, ptr++)
1065f7bbf134Shm 		ptr = strchr(ptr, '/');
1066f7bbf134Shm 	if (*(rule + strlen(rule) - 1) != '/') {
1067f7bbf134Shm 		while (*ptr != '\0') {
1068f7bbf134Shm 			if (*ptr == '/')
1069f7bbf134Shm 				break;
1070f7bbf134Shm 			ptr++;
1071f7bbf134Shm 		}
1072f7bbf134Shm 	}
1073f7bbf134Shm 	*ptr = '\0';
10747c478bd9Sstevel@tonic-gate 
1075f7bbf134Shm 	/* OK, now see if they match. */
1076f7bbf134Shm 	match = fnmatch(rule, fname_cp, FNM_PATHNAME);
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	/* No match, return failure */
10797c478bd9Sstevel@tonic-gate 	if (match != 0)
10807c478bd9Sstevel@tonic-gate 		return (0);
10817c478bd9Sstevel@tonic-gate 	else
10827c478bd9Sstevel@tonic-gate 		return (1);
10837c478bd9Sstevel@tonic-gate }
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate void
process_glob_ignores(char * ignore_list,uint_t * flags)10867c478bd9Sstevel@tonic-gate process_glob_ignores(char *ignore_list, uint_t *flags)
10877c478bd9Sstevel@tonic-gate {
10887c478bd9Sstevel@tonic-gate 	char	*cp;
10897c478bd9Sstevel@tonic-gate 	struct attr_keyword *akp;
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	if (ignore_list == NULL)
10927c478bd9Sstevel@tonic-gate 		usage();
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 	cp = strtok(ignore_list, ",");
10957c478bd9Sstevel@tonic-gate 	while (cp != NULL) {
10967c478bd9Sstevel@tonic-gate 		akp = attr_keylookup(cp);
10977c478bd9Sstevel@tonic-gate 		if (akp == NULL)
10987c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ERROR: Invalid keyword %s\n",
10997c478bd9Sstevel@tonic-gate 			    cp);
11007c478bd9Sstevel@tonic-gate 		else
11017c478bd9Sstevel@tonic-gate 			*flags &= ~akp->ak_flags;
11027c478bd9Sstevel@tonic-gate 		cp = strtok(NULL, ",");
11037c478bd9Sstevel@tonic-gate 	}
11047c478bd9Sstevel@tonic-gate }
1105