xref: /titanic_52/usr/src/cmd/bart/rules.c (revision c40f76e346ad844b9326c2049644b7b1d1a93e48)
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
5*c40f76e3Sjc144527  * Common Development and Distribution License (the "License").
6*c40f76e3Sjc144527  * 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 /*
22*c40f76e3Sjc144527  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <dirent.h>
287c478bd9Sstevel@tonic-gate #include <fnmatch.h>
29f7bbf134Shm123892 #include <string.h>
307c478bd9Sstevel@tonic-gate #include "bart.h"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate static int count_slashes(const char *);
337c478bd9Sstevel@tonic-gate static struct rule *gen_rulestruct(void);
347c478bd9Sstevel@tonic-gate static struct tree_modifier *gen_tree_modifier(void);
357c478bd9Sstevel@tonic-gate static struct dir_component *gen_dir_component(void);
367c478bd9Sstevel@tonic-gate static void init_rule(uint_t, struct rule *);
377c478bd9Sstevel@tonic-gate static void add_modifier(struct rule *, char *);
387c478bd9Sstevel@tonic-gate static struct rule *add_subtree_rule(char *, char *, int, int *);
397c478bd9Sstevel@tonic-gate static struct rule *add_single_rule(char *);
407c478bd9Sstevel@tonic-gate static void dirs_cleanup(struct dir_component *);
417c478bd9Sstevel@tonic-gate static void add_dir(struct dir_component **, char *);
427c478bd9Sstevel@tonic-gate static char *lex(FILE *);
437c478bd9Sstevel@tonic-gate static int match_subtree(const char *, char *);
447c478bd9Sstevel@tonic-gate static struct rule *get_last_entry(boolean_t);
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate static int	lex_linenum;	/* line number in current input file	*/
477c478bd9Sstevel@tonic-gate static struct rule	*first_rule = NULL, *current_rule = NULL;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * This function is responsible for validating whether or not a given file
517c478bd9Sstevel@tonic-gate  * should be cataloged, based upon the modifiers for a subtree.
527c478bd9Sstevel@tonic-gate  * For example, a line in the rules file: '/home/nickiso *.c' should only
537c478bd9Sstevel@tonic-gate  * catalog the C files (based upon pattern matching) in the subtree
547c478bd9Sstevel@tonic-gate  * '/home/nickiso'.
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * Return non-zero if it should be excluded, 0 if it should be cataloged.
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate int
597c478bd9Sstevel@tonic-gate exclude_fname(const char *fname, char fname_type, struct rule *rule_ptr)
607c478bd9Sstevel@tonic-gate {
61*c40f76e3Sjc144527 	char	*pattern, *ptr, *fname_ptr, fname_cp[PATH_MAX],
62*c40f76e3Sjc144527 		pattern_cp[PATH_MAX], saved_char;
637c478bd9Sstevel@tonic-gate 	int	match, num_pattern_slash, num_fname_slash, i, slashes_to_adv,
647c478bd9Sstevel@tonic-gate 		ret_val = 0;
657c478bd9Sstevel@tonic-gate 	struct  tree_modifier   *mod_ptr;
667c478bd9Sstevel@tonic-gate 	boolean_t		dir_flag;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	/*
697c478bd9Sstevel@tonic-gate 	 * For a given entry in the rules struct, the modifiers, e.g., '*.c',
707c478bd9Sstevel@tonic-gate 	 * are kept in a linked list.  Get a ptr to the head of the list.
717c478bd9Sstevel@tonic-gate 	 */
727c478bd9Sstevel@tonic-gate 	mod_ptr = rule_ptr->modifiers;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	/*
757c478bd9Sstevel@tonic-gate 	 * Walk through all the modifiers until its they are exhausted OR
767c478bd9Sstevel@tonic-gate 	 * until the file should definitely be excluded.
777c478bd9Sstevel@tonic-gate 	 */
787c478bd9Sstevel@tonic-gate 	while ((mod_ptr != NULL) && !ret_val) {
797c478bd9Sstevel@tonic-gate 		/* First, see if we should be matching files or dirs */
807c478bd9Sstevel@tonic-gate 		if (mod_ptr->mod_str[(strlen(mod_ptr->mod_str)-1)] == '/')
817c478bd9Sstevel@tonic-gate 			dir_flag = B_TRUE;
827c478bd9Sstevel@tonic-gate 		else
837c478bd9Sstevel@tonic-gate 			dir_flag = B_FALSE;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 		if (mod_ptr->mod_str[0] == '!') {
867c478bd9Sstevel@tonic-gate 			pattern = (mod_ptr->mod_str + 1);
877c478bd9Sstevel@tonic-gate 		} else {
887c478bd9Sstevel@tonic-gate 			pattern = mod_ptr->mod_str;
897c478bd9Sstevel@tonic-gate 		}
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 		if (dir_flag == B_FALSE) {
927c478bd9Sstevel@tonic-gate 			/*
937c478bd9Sstevel@tonic-gate 			 * In the case when a user is trying to filter on
947c478bd9Sstevel@tonic-gate 			 * FILES and the entry is a directory, its excluded!
957c478bd9Sstevel@tonic-gate 			 */
967c478bd9Sstevel@tonic-gate 			if (fname_type == 'D') {
977c478bd9Sstevel@tonic-gate 				ret_val = 1;
987c478bd9Sstevel@tonic-gate 				break;
997c478bd9Sstevel@tonic-gate 			}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 			/*
1027c478bd9Sstevel@tonic-gate 			 * Match patterns against filenames.
1037c478bd9Sstevel@tonic-gate 			 * Need to be able to handle multi-level patterns,
1047c478bd9Sstevel@tonic-gate 			 * eg. "SCCS/<star-wildcard>.c", which means
1057c478bd9Sstevel@tonic-gate 			 * 'only match C files under SCCS directories.
1067c478bd9Sstevel@tonic-gate 			 *
1077c478bd9Sstevel@tonic-gate 			 * Determine the number of levels in the filename and
1087c478bd9Sstevel@tonic-gate 			 * in the pattern.
1097c478bd9Sstevel@tonic-gate 			 */
1107c478bd9Sstevel@tonic-gate 			num_pattern_slash = count_slashes(pattern);
1117c478bd9Sstevel@tonic-gate 			num_fname_slash = count_slashes(fname);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 			/* Check for trivial exclude condition */
1147c478bd9Sstevel@tonic-gate 			if (num_pattern_slash > num_fname_slash) {
1157c478bd9Sstevel@tonic-gate 				ret_val = 1;
1167c478bd9Sstevel@tonic-gate 				break;
1177c478bd9Sstevel@tonic-gate 			}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 			/*
1207c478bd9Sstevel@tonic-gate 			 * Do an apples to apples comparison, based upon the
1217c478bd9Sstevel@tonic-gate 			 * number of levels:
1227c478bd9Sstevel@tonic-gate 			 *
1237c478bd9Sstevel@tonic-gate 			 * Assume fname is /A/B/C/D/E and the pattern is D/E.
1247c478bd9Sstevel@tonic-gate 			 * In that case, 'ptr' will point to "D/E" and
1257c478bd9Sstevel@tonic-gate 			 * 'slashes_to_adv' will be '4'.
1267c478bd9Sstevel@tonic-gate 			 */
1277c478bd9Sstevel@tonic-gate 			(void) strlcpy(fname_cp, fname, sizeof (fname_cp));
1287c478bd9Sstevel@tonic-gate 			ptr = fname_cp;
1297c478bd9Sstevel@tonic-gate 			slashes_to_adv = num_fname_slash - num_pattern_slash;
1307c478bd9Sstevel@tonic-gate 			for (i = 0; i < slashes_to_adv; i++)  {
1317c478bd9Sstevel@tonic-gate 				ptr = strchr(ptr, '/');
1327c478bd9Sstevel@tonic-gate 				ptr++;
1337c478bd9Sstevel@tonic-gate 			}
1347c478bd9Sstevel@tonic-gate 			if ((pattern[0] == '.') && (pattern[1] == '.') &&
1357c478bd9Sstevel@tonic-gate 			    (pattern[2] == '/')) {
1367c478bd9Sstevel@tonic-gate 				pattern = strchr(pattern, '/');
1377c478bd9Sstevel@tonic-gate 				ptr = strchr(ptr, '/');
1387c478bd9Sstevel@tonic-gate 			}
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 			/* OK, now do the fnmatch() and set the return value */
1427c478bd9Sstevel@tonic-gate 			match = fnmatch(pattern, ptr, FNM_PATHNAME);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 			if (match == 0)
1457c478bd9Sstevel@tonic-gate 				ret_val = 0;
1467c478bd9Sstevel@tonic-gate 			else
1477c478bd9Sstevel@tonic-gate 				ret_val = 1;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 		} else {
1507c478bd9Sstevel@tonic-gate 			/*
1517c478bd9Sstevel@tonic-gate 			 * The rule requires directory matching.
1527c478bd9Sstevel@tonic-gate 			 *
1537c478bd9Sstevel@tonic-gate 			 * First, make copies, since both the pattern and
1547c478bd9Sstevel@tonic-gate 			 * filename need to be modified.
1557c478bd9Sstevel@tonic-gate 			 *
1567c478bd9Sstevel@tonic-gate 			 * When copying 'fname', ignore the relocatable root
1577c478bd9Sstevel@tonic-gate 			 * since pattern matching is done for the string AFTER
1587c478bd9Sstevel@tonic-gate 			 * the relocatable root.  For example, if the
1597c478bd9Sstevel@tonic-gate 			 * relocatable root is "/dir1/dir2/dir3" and the
1607c478bd9Sstevel@tonic-gate 			 * pattern is "dir3/", we do NOT want to include every
1617c478bd9Sstevel@tonic-gate 			 * directory in the relocatable root.  Instead, we
1627c478bd9Sstevel@tonic-gate 			 * only want to include subtrees that look like:
1637c478bd9Sstevel@tonic-gate 			 * "/dir1/dir2/dir3/....dir3/....."
1647c478bd9Sstevel@tonic-gate 			 *
165*c40f76e3Sjc144527 			 * NOTE: the 'fname_cp' does NOT have a trailing '/':
1667c478bd9Sstevel@tonic-gate 			 * necessary for fnmatch().
1677c478bd9Sstevel@tonic-gate 			 */
1687c478bd9Sstevel@tonic-gate 			(void) strlcpy(fname_cp,
1697c478bd9Sstevel@tonic-gate 			    (fname+strlen(rule_ptr->subtree)),
1707c478bd9Sstevel@tonic-gate 			    sizeof (fname_cp));
1717c478bd9Sstevel@tonic-gate 			(void) strlcpy(pattern_cp, pattern,
1727c478bd9Sstevel@tonic-gate 			    sizeof (pattern_cp));
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 			/*
1757c478bd9Sstevel@tonic-gate 			 * For non-directory entries, remove the trailing
1767c478bd9Sstevel@tonic-gate 			 * name, e.g., for a file /A/B/C/D where 'D' is
1777c478bd9Sstevel@tonic-gate 			 * the actual filename, remove the 'D' since it
1787c478bd9Sstevel@tonic-gate 			 * should *not* be considered in the directory match.
1797c478bd9Sstevel@tonic-gate 			 */
1807c478bd9Sstevel@tonic-gate 			if (fname_type != 'D') {
1817c478bd9Sstevel@tonic-gate 				ptr = strrchr(fname_cp, '/');
1827c478bd9Sstevel@tonic-gate 				if (ptr != NULL)
1837c478bd9Sstevel@tonic-gate 					*ptr = '\0';
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 				/* Trivial case: simple filename */
1867c478bd9Sstevel@tonic-gate 				if (strlen(fname_cp) == 0) {
187*c40f76e3Sjc144527 					if (mod_ptr->include == B_FALSE)
188*c40f76e3Sjc144527 						ret_val = 0;
189*c40f76e3Sjc144527 					else
1907c478bd9Sstevel@tonic-gate 						ret_val = 1;
1917c478bd9Sstevel@tonic-gate 					break;
1927c478bd9Sstevel@tonic-gate 				}
1937c478bd9Sstevel@tonic-gate 			}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 			/* Count the # of slashes in the pattern and fname */
1967c478bd9Sstevel@tonic-gate 			num_pattern_slash = count_slashes(pattern_cp);
1977c478bd9Sstevel@tonic-gate 			num_fname_slash = count_slashes(fname_cp);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 			/*
2007c478bd9Sstevel@tonic-gate 			 * fname_cp is too short, bail!
2017c478bd9Sstevel@tonic-gate 			 */
2027c478bd9Sstevel@tonic-gate 			if (num_pattern_slash > num_fname_slash) {
203*c40f76e3Sjc144527 				if (mod_ptr->include == B_FALSE)
204*c40f76e3Sjc144527 					ret_val = 0;
205*c40f76e3Sjc144527 				else
2067c478bd9Sstevel@tonic-gate 					ret_val = 1;
207*c40f76e3Sjc144527 
2087c478bd9Sstevel@tonic-gate 				break;
2097c478bd9Sstevel@tonic-gate 			}
2107c478bd9Sstevel@tonic-gate 
211*c40f76e3Sjc144527 			/* set the return value before we enter the loop */
2127c478bd9Sstevel@tonic-gate 			ret_val = 1;
213*c40f76e3Sjc144527 
214*c40f76e3Sjc144527 			/*
215*c40f76e3Sjc144527 			 * Take the leading '/' from fname_cp before
216*c40f76e3Sjc144527 			 * decrementing the number of slashes.
217*c40f76e3Sjc144527 			 */
218*c40f76e3Sjc144527 			if (fname_cp[0] == '/') {
219*c40f76e3Sjc144527 				(void) strlcpy(fname_cp,
220*c40f76e3Sjc144527 				    strchr(fname_cp, '/') + 1,
221*c40f76e3Sjc144527 				    sizeof (fname_cp));
222*c40f76e3Sjc144527 				num_fname_slash--;
2237c478bd9Sstevel@tonic-gate 			}
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 			/*
226*c40f76e3Sjc144527 			 * Begin the loop, walk through the file name until
227*c40f76e3Sjc144527 			 * it can be determined that there is no match.
228*c40f76e3Sjc144527 			 * For example: if pattern is C/D/, and fname_cp is
229*c40f76e3Sjc144527 			 * A/B/C/D/E then compare A/B/ with C/D/, if it doesn't
230*c40f76e3Sjc144527 			 * match, then walk further so that the next iteration
231*c40f76e3Sjc144527 			 * checks B/C/ against C/D/, continue until we have
232*c40f76e3Sjc144527 			 * exhausted options.
233*c40f76e3Sjc144527 			 * In the above case, the 3rd iteration will match
234*c40f76e3Sjc144527 			 * C/D/ with C/D/.
2357c478bd9Sstevel@tonic-gate 			 */
236*c40f76e3Sjc144527 			while (num_pattern_slash <= num_fname_slash) {
237*c40f76e3Sjc144527 				/* get a pointer to our filename */
238*c40f76e3Sjc144527 				fname_ptr = fname_cp;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 				/*
241*c40f76e3Sjc144527 				 * Walk the filename through the slashes
242*c40f76e3Sjc144527 				 * so that we have a component of the same
243*c40f76e3Sjc144527 				 * number of slashes as the pattern.
2447c478bd9Sstevel@tonic-gate 				 */
245*c40f76e3Sjc144527 
246*c40f76e3Sjc144527 				for (i = 0; i < num_pattern_slash; i++) {
247*c40f76e3Sjc144527 					ptr = strchr(fname_ptr, '/');
248*c40f76e3Sjc144527 					fname_ptr = ptr + 1;
249*c40f76e3Sjc144527 				}
250*c40f76e3Sjc144527 
251*c40f76e3Sjc144527 				/*
252*c40f76e3Sjc144527 				 * Save the character after our target slash
253*c40f76e3Sjc144527 				 * before breaking the string for use with
254*c40f76e3Sjc144527 				 * fnmatch
255*c40f76e3Sjc144527 				 */
256*c40f76e3Sjc144527 				saved_char = *(++ptr);
257*c40f76e3Sjc144527 
258*c40f76e3Sjc144527 				*ptr = '\0';
259*c40f76e3Sjc144527 
260*c40f76e3Sjc144527 				/*
261*c40f76e3Sjc144527 				 * Try to match the current component with the
262*c40f76e3Sjc144527 				 * pattern we are looking for.
263*c40f76e3Sjc144527 				 */
264*c40f76e3Sjc144527 				match = fnmatch(pattern_cp, fname_cp,
2657c478bd9Sstevel@tonic-gate 				    FNM_PATHNAME);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 				/*
268*c40f76e3Sjc144527 				 * If we matched, set ret_val and break.
269*c40f76e3Sjc144527 				 * No need to invert ret_val here as it is done
270*c40f76e3Sjc144527 				 * outside of this inner while loop.
2717c478bd9Sstevel@tonic-gate 				 */
2727c478bd9Sstevel@tonic-gate 				if (match == 0) {
2737c478bd9Sstevel@tonic-gate 					ret_val = 0;
2747c478bd9Sstevel@tonic-gate 					break;
2757c478bd9Sstevel@tonic-gate 				}
276*c40f76e3Sjc144527 
277*c40f76e3Sjc144527 				/*
278*c40f76e3Sjc144527 				 * We didn't match, so restore the saved
279*c40f76e3Sjc144527 				 * character to the original position.
280*c40f76e3Sjc144527 				 */
281*c40f76e3Sjc144527 				*ptr = saved_char;
282*c40f76e3Sjc144527 
283*c40f76e3Sjc144527 				/*
284*c40f76e3Sjc144527 				 * Break down fname_cp, if it was A/B/C
285*c40f76e3Sjc144527 				 * then after this operation it will be B/C
286*c40f76e3Sjc144527 				 * in preparation for the next iteration.
287*c40f76e3Sjc144527 				 */
288*c40f76e3Sjc144527 				(void) strlcpy(fname_cp,
289*c40f76e3Sjc144527 				    strchr(fname_cp, '/') + 1,
290*c40f76e3Sjc144527 				    sizeof (fname_cp));
291*c40f76e3Sjc144527 
292*c40f76e3Sjc144527 				/*
293*c40f76e3Sjc144527 				 * Decrement the number of slashes to
294*c40f76e3Sjc144527 				 * compensate for the one removed above.
295*c40f76e3Sjc144527 				 */
296*c40f76e3Sjc144527 				num_fname_slash--;
297*c40f76e3Sjc144527 			}
298*c40f76e3Sjc144527 
299*c40f76e3Sjc144527 			/*
300*c40f76e3Sjc144527 			 * If we didn't get a match above then we may be on the
301*c40f76e3Sjc144527 			 * last component of our filename.
302*c40f76e3Sjc144527 			 * This is to handle the following cases
303*c40f76e3Sjc144527 			 *    - filename is A/B/C/D/E and pattern may be D/E/
304*c40f76e3Sjc144527 			 *    - filename is D/E and pattern may be D/E/
305*c40f76e3Sjc144527 			 */
306*c40f76e3Sjc144527 			if ((ret_val != 0) &&
307*c40f76e3Sjc144527 			    (num_pattern_slash == (num_fname_slash + 1))) {
308*c40f76e3Sjc144527 
309*c40f76e3Sjc144527 				/* strip the trailing slash from the pattern */
310*c40f76e3Sjc144527 				ptr = strrchr(pattern_cp, '/');
311*c40f76e3Sjc144527 				*ptr = '\0';
312*c40f76e3Sjc144527 
313*c40f76e3Sjc144527 				match = fnmatch(pattern_cp,
314*c40f76e3Sjc144527 				    fname_cp, FNM_PATHNAME);
315*c40f76e3Sjc144527 				if (match == 0) {
316*c40f76e3Sjc144527 					/*
317*c40f76e3Sjc144527 					 * No need to invert ret_val as it is
318*c40f76e3Sjc144527 					 * done below.
319*c40f76e3Sjc144527 					 */
320*c40f76e3Sjc144527 					ret_val = 0;
3217c478bd9Sstevel@tonic-gate 				}
3227c478bd9Sstevel@tonic-gate 			}
3237c478bd9Sstevel@tonic-gate 		}
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 		/*
3267c478bd9Sstevel@tonic-gate 		 * Take into account whether or not this rule began with
3277c478bd9Sstevel@tonic-gate 		 * a '!'
3287c478bd9Sstevel@tonic-gate 		 */
3297c478bd9Sstevel@tonic-gate 		if (mod_ptr->include == B_FALSE) {
3307c478bd9Sstevel@tonic-gate 			if (ret_val == 0)
3317c478bd9Sstevel@tonic-gate 				ret_val = 1;
3327c478bd9Sstevel@tonic-gate 			else ret_val = 0;
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 		/* Advance to the next modifier */
3367c478bd9Sstevel@tonic-gate 		mod_ptr = mod_ptr->next;
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	return (ret_val);
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate static int
3437c478bd9Sstevel@tonic-gate count_slashes(const char *in_path)
3447c478bd9Sstevel@tonic-gate {
3457c478bd9Sstevel@tonic-gate 	int num_fname_slash = 0;
3467c478bd9Sstevel@tonic-gate 	const char *p;
3477c478bd9Sstevel@tonic-gate 	for (p = in_path; *p != '\0'; p++)
3487c478bd9Sstevel@tonic-gate 		if (*p == '/')
3497c478bd9Sstevel@tonic-gate 			num_fname_slash++;
3507c478bd9Sstevel@tonic-gate 	return (num_fname_slash);
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate static struct rule *
3547c478bd9Sstevel@tonic-gate gen_rulestruct(void)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate 	struct rule	*new_rule;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	new_rule = (struct rule *)safe_calloc(sizeof (struct rule));
3597c478bd9Sstevel@tonic-gate 	new_rule->traversed = B_FALSE;
3607c478bd9Sstevel@tonic-gate 	return (new_rule);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate static struct tree_modifier *
3647c478bd9Sstevel@tonic-gate gen_tree_modifier(void)
3657c478bd9Sstevel@tonic-gate {
3667c478bd9Sstevel@tonic-gate 	struct tree_modifier	*new_modifier;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	new_modifier = (struct tree_modifier *)safe_calloc
3697c478bd9Sstevel@tonic-gate 	    (sizeof (struct tree_modifier));
3707c478bd9Sstevel@tonic-gate 	return (new_modifier);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate static struct dir_component *
3747c478bd9Sstevel@tonic-gate gen_dir_component(void)
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate 	struct dir_component	*new_dir;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	new_dir = (struct dir_component *)safe_calloc
3797c478bd9Sstevel@tonic-gate 	    (sizeof (struct dir_component));
3807c478bd9Sstevel@tonic-gate 	return (new_dir);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate /*
3847c478bd9Sstevel@tonic-gate  * Set up a default rule when there is no rules file.
3857c478bd9Sstevel@tonic-gate  */
3867c478bd9Sstevel@tonic-gate static struct rule *
3877c478bd9Sstevel@tonic-gate setup_default_rule(char *reloc_root, uint_t flags)
3887c478bd9Sstevel@tonic-gate {
3897c478bd9Sstevel@tonic-gate 	struct	rule	*new_rule;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	new_rule = add_single_rule(reloc_root[0] == '\0' ? "/" : reloc_root);
3927c478bd9Sstevel@tonic-gate 	init_rule(flags, new_rule);
3937c478bd9Sstevel@tonic-gate 	add_modifier(new_rule, "*");
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	return (new_rule);
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate /*
3997c478bd9Sstevel@tonic-gate  * Utility function, used to initialize the flag in a new rule structure.
4007c478bd9Sstevel@tonic-gate  */
4017c478bd9Sstevel@tonic-gate static void
4027c478bd9Sstevel@tonic-gate init_rule(uint_t flags, struct rule *new_rule)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	if (new_rule == NULL)
4067c478bd9Sstevel@tonic-gate 		return;
4077c478bd9Sstevel@tonic-gate 	new_rule->attr_list = flags;
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate /*
4117c478bd9Sstevel@tonic-gate  * Function to read the rulesfile.  Used by both 'bart create' and
4127c478bd9Sstevel@tonic-gate  * 'bart compare'.
4137c478bd9Sstevel@tonic-gate  */
4147c478bd9Sstevel@tonic-gate int
4157c478bd9Sstevel@tonic-gate read_rules(FILE *file, char *reloc_root, uint_t in_flags, int create)
4167c478bd9Sstevel@tonic-gate {
4177c478bd9Sstevel@tonic-gate 	char		*s;
4187c478bd9Sstevel@tonic-gate 	struct rule	*block_begin = NULL, *new_rule, *rp;
4197c478bd9Sstevel@tonic-gate 	struct attr_keyword *akp;
4207c478bd9Sstevel@tonic-gate 	int		check_flag, ignore_flag, syntax_err, ret_code;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	ret_code = EXIT;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	lex_linenum = 0;
4257c478bd9Sstevel@tonic-gate 	check_flag = 0;
4267c478bd9Sstevel@tonic-gate 	ignore_flag = 0;
4277c478bd9Sstevel@tonic-gate 	syntax_err = 0;
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] == '/') {
4577c478bd9Sstevel@tonic-gate 			new_rule = add_subtree_rule(s, reloc_root, create,
4587c478bd9Sstevel@tonic-gate 			    &ret_code);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 			s = lex(0);
4617c478bd9Sstevel@tonic-gate 			while ((s != NULL) && (*s != 0) && (*s != '#')) {
4627c478bd9Sstevel@tonic-gate 				add_modifier(new_rule, s);
4637c478bd9Sstevel@tonic-gate 				s = lex(0);
4647c478bd9Sstevel@tonic-gate 			}
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 			/* Found a new block, keep track of the beginning */
4677c478bd9Sstevel@tonic-gate 			if (block_begin == NULL ||
4687c478bd9Sstevel@tonic-gate 			    (ignore_flag != 0) || (check_flag != 0)) {
4697c478bd9Sstevel@tonic-gate 				block_begin = new_rule;
4707c478bd9Sstevel@tonic-gate 				check_flag = 0;
4717c478bd9Sstevel@tonic-gate 				ignore_flag = 0;
4727c478bd9Sstevel@tonic-gate 			}
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 			/* Apply global settings to this block, if any */
4757c478bd9Sstevel@tonic-gate 			init_rule(in_flags, new_rule);
4767c478bd9Sstevel@tonic-gate 		} else if (IGNORE_KEYWORD(s) || CHECK_KEYWORD(s)) {
4777c478bd9Sstevel@tonic-gate 			int check_kw;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 			if (IGNORE_KEYWORD(s)) {
4807c478bd9Sstevel@tonic-gate 				ignore_flag++;
4817c478bd9Sstevel@tonic-gate 				check_kw = 0;
4827c478bd9Sstevel@tonic-gate 			} else {
4837c478bd9Sstevel@tonic-gate 				check_flag++;
4847c478bd9Sstevel@tonic-gate 				check_kw = 1;
4857c478bd9Sstevel@tonic-gate 			}
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 			/* Parse next token */
4887c478bd9Sstevel@tonic-gate 			s = lex(0);
4897c478bd9Sstevel@tonic-gate 			while ((s != NULL) && (*s != 0) && (*s != '#')) {
4907c478bd9Sstevel@tonic-gate 				akp = attr_keylookup(s);
4917c478bd9Sstevel@tonic-gate 				if (akp == NULL) {
4927c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, SYNTAX_ERR, s);
4937c478bd9Sstevel@tonic-gate 					syntax_err++;
4947c478bd9Sstevel@tonic-gate 					exit(2);
4957c478bd9Sstevel@tonic-gate 				}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 				/*
4987c478bd9Sstevel@tonic-gate 				 * For all the flags, check if this is a global
4997c478bd9Sstevel@tonic-gate 				 * IGNORE/CHECK. If so, set the global flag.
5007c478bd9Sstevel@tonic-gate 				 *
5017c478bd9Sstevel@tonic-gate 				 * NOTE: The only time you can have a
5027c478bd9Sstevel@tonic-gate 				 * global ignore is when its the
5037c478bd9Sstevel@tonic-gate 				 * stmt before any blocks have been
5047c478bd9Sstevel@tonic-gate 				 * spec'd.
5057c478bd9Sstevel@tonic-gate 				 */
5067c478bd9Sstevel@tonic-gate 				if (block_begin == NULL) {
5077c478bd9Sstevel@tonic-gate 					if (check_kw)
5087c478bd9Sstevel@tonic-gate 						in_flags |= akp->ak_flags;
5097c478bd9Sstevel@tonic-gate 					else
5107c478bd9Sstevel@tonic-gate 						in_flags &= ~(akp->ak_flags);
5117c478bd9Sstevel@tonic-gate 				} else {
5127c478bd9Sstevel@tonic-gate 					for (rp = block_begin; rp != NULL;
5137c478bd9Sstevel@tonic-gate 					    rp = rp->next) {
5147c478bd9Sstevel@tonic-gate 						if (check_kw)
5157c478bd9Sstevel@tonic-gate 							rp->attr_list |=
5167c478bd9Sstevel@tonic-gate 							    akp->ak_flags;
5177c478bd9Sstevel@tonic-gate 						else
5187c478bd9Sstevel@tonic-gate 							rp->attr_list &=
5197c478bd9Sstevel@tonic-gate 							    ~(akp->ak_flags);
5207c478bd9Sstevel@tonic-gate 					}
5217c478bd9Sstevel@tonic-gate 				}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 				/* Parse next token */
5247c478bd9Sstevel@tonic-gate 				s = lex(0);
5257c478bd9Sstevel@tonic-gate 			}
5267c478bd9Sstevel@tonic-gate 		} else {
5277c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, SYNTAX_ERR, s);
5287c478bd9Sstevel@tonic-gate 			s = lex(0);
5297c478bd9Sstevel@tonic-gate 			while (s != NULL && *s != 0) {
5307c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, " %s", s);
5317c478bd9Sstevel@tonic-gate 				s = lex(0);
5327c478bd9Sstevel@tonic-gate 			}
5337c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "\n");
5347c478bd9Sstevel@tonic-gate 			syntax_err++;
5357c478bd9Sstevel@tonic-gate 		}
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	(void) fclose(file);
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	if (syntax_err) {
5417c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, SYNTAX_ABORT);
5427c478bd9Sstevel@tonic-gate 		exit(2);
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	return (ret_code);
5467c478bd9Sstevel@tonic-gate }
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate static void
5497c478bd9Sstevel@tonic-gate add_modifier(struct rule *rule, char *modifier_str)
5507c478bd9Sstevel@tonic-gate {
5517c478bd9Sstevel@tonic-gate 	struct tree_modifier	*new_mod_ptr, *curr_mod_ptr;
5527c478bd9Sstevel@tonic-gate 	struct rule		*this_rule;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	this_rule = rule;
5557c478bd9Sstevel@tonic-gate 	while (this_rule != NULL) {
5567c478bd9Sstevel@tonic-gate 		new_mod_ptr = gen_tree_modifier();
5577c478bd9Sstevel@tonic-gate 		new_mod_ptr->mod_str = safe_strdup(modifier_str);
5587c478bd9Sstevel@tonic-gate 		/* Next, see if the pattern is an include or an exclude */
5597c478bd9Sstevel@tonic-gate 		if (new_mod_ptr->mod_str[0] == '!') {
5607c478bd9Sstevel@tonic-gate 			new_mod_ptr->mod_str = (new_mod_ptr->mod_str + 1);
5617c478bd9Sstevel@tonic-gate 			new_mod_ptr->include = B_FALSE;
5627c478bd9Sstevel@tonic-gate 		} else {
5637c478bd9Sstevel@tonic-gate 			new_mod_ptr->include = B_TRUE;
5647c478bd9Sstevel@tonic-gate 		}
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 		if (this_rule->modifiers == NULL)
5677c478bd9Sstevel@tonic-gate 			this_rule->modifiers = new_mod_ptr;
5687c478bd9Sstevel@tonic-gate 		else {
5697c478bd9Sstevel@tonic-gate 			curr_mod_ptr = this_rule->modifiers;
5707c478bd9Sstevel@tonic-gate 			while (curr_mod_ptr->next != NULL)
5717c478bd9Sstevel@tonic-gate 				curr_mod_ptr = curr_mod_ptr->next;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 			curr_mod_ptr->next = new_mod_ptr;
5747c478bd9Sstevel@tonic-gate 		}
5757c478bd9Sstevel@tonic-gate 		this_rule = this_rule->next;
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate }
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate /*
5807c478bd9Sstevel@tonic-gate  * This funtion is invoked when reading rulesfiles.  A subtree may have
5817c478bd9Sstevel@tonic-gate  * wildcards in it, e.g., '/home/n*', which is expected to match all home
5827c478bd9Sstevel@tonic-gate  * dirs which start with an 'n'.
5837c478bd9Sstevel@tonic-gate  *
5847c478bd9Sstevel@tonic-gate  * This function needs to break down the subtree into its components.  For
5857c478bd9Sstevel@tonic-gate  * each component, see how many directories match.  Take the subtree list just
5867c478bd9Sstevel@tonic-gate  * generated and run it through again, this time looking at the next component.
5877c478bd9Sstevel@tonic-gate  * At each iteration, keep a linked list of subtrees that currently match.
5887c478bd9Sstevel@tonic-gate  * Once the final list is created, invoke add_single_rule() to create the
5897c478bd9Sstevel@tonic-gate  * rule struct with the correct information.
5907c478bd9Sstevel@tonic-gate  *
5917c478bd9Sstevel@tonic-gate  * This function returns a ptr to the first element in the block of subtrees
5927c478bd9Sstevel@tonic-gate  * which matched the subtree def'n in the rulesfile.
5937c478bd9Sstevel@tonic-gate  */
5947c478bd9Sstevel@tonic-gate static struct rule *
5957c478bd9Sstevel@tonic-gate add_subtree_rule(char *rule, char *reloc_root, int create, int *err_code)
5967c478bd9Sstevel@tonic-gate {
5977c478bd9Sstevel@tonic-gate 	char			full_path[PATH_MAX], pattern[PATH_MAX],
5987c478bd9Sstevel@tonic-gate 				new_dirname[PATH_MAX], *beg_pattern,
5997c478bd9Sstevel@tonic-gate 				*end_pattern, *curr_dirname;
6007c478bd9Sstevel@tonic-gate 	struct	dir_component	*current_level = NULL, *next_level = NULL,
6017c478bd9Sstevel@tonic-gate 				*tmp_ptr;
6027c478bd9Sstevel@tonic-gate 	DIR			*dir_ptr;
6037c478bd9Sstevel@tonic-gate 	struct dirent		*dir_entry;
6047c478bd9Sstevel@tonic-gate 	struct rule		*begin_rule = NULL;
6057c478bd9Sstevel@tonic-gate 	int			ret;
6067c478bd9Sstevel@tonic-gate 	struct stat64		statb;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	(void) snprintf(full_path, sizeof (full_path),
6097c478bd9Sstevel@tonic-gate 	    (rule[0] == '/') ? "%s%s" : "%s/%s", reloc_root, rule);
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	/*
6127c478bd9Sstevel@tonic-gate 	 * In the case of 'bart compare', don't validate
6137c478bd9Sstevel@tonic-gate 	 * the subtrees, since the machine running the
6147c478bd9Sstevel@tonic-gate 	 * comparison may not be the machine which generated
6157c478bd9Sstevel@tonic-gate 	 * the manifest.
6167c478bd9Sstevel@tonic-gate 	 */
6177c478bd9Sstevel@tonic-gate 	if (create == 0)
6187c478bd9Sstevel@tonic-gate 		return (add_single_rule(full_path));
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	/* Insert 'current_level' into the linked list */
6227c478bd9Sstevel@tonic-gate 	add_dir(&current_level, NULL);
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	/* Special case: occurs when -R is "/" and the subtree is "/" */
6257c478bd9Sstevel@tonic-gate 	if (strcmp(full_path, "/") == 0)
6267c478bd9Sstevel@tonic-gate 		(void) strcpy(current_level->dirname, "/");
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	beg_pattern = full_path;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	while (beg_pattern != NULL) {
6317c478bd9Sstevel@tonic-gate 		/*
6327c478bd9Sstevel@tonic-gate 		 * Extract the pathname component starting at 'beg_pattern'.
6337c478bd9Sstevel@tonic-gate 		 * Take those chars and put them into 'pattern'.
6347c478bd9Sstevel@tonic-gate 		 */
6357c478bd9Sstevel@tonic-gate 		while (*beg_pattern == '/')
6367c478bd9Sstevel@tonic-gate 			beg_pattern++;
6377c478bd9Sstevel@tonic-gate 		if (*beg_pattern == '\0')	/* end of pathname */
6387c478bd9Sstevel@tonic-gate 			break;
6397c478bd9Sstevel@tonic-gate 		end_pattern = strchr(beg_pattern, '/');
6407c478bd9Sstevel@tonic-gate 		if (end_pattern != NULL)
6417c478bd9Sstevel@tonic-gate 			(void) strlcpy(pattern, beg_pattern,
6427c478bd9Sstevel@tonic-gate 			    end_pattern - beg_pattern + 1);
6437c478bd9Sstevel@tonic-gate 		else
6447c478bd9Sstevel@tonic-gate 			(void) strlcpy(pattern, beg_pattern, sizeof (pattern));
6457c478bd9Sstevel@tonic-gate 		beg_pattern = end_pattern;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 		/*
6487c478bd9Sstevel@tonic-gate 		 * At this point, search for 'pattern' as a *subdirectory* of
6497c478bd9Sstevel@tonic-gate 		 * the dirs in the linked list.
6507c478bd9Sstevel@tonic-gate 		 */
6517c478bd9Sstevel@tonic-gate 		while (current_level != NULL) {
6527c478bd9Sstevel@tonic-gate 			/* curr_dirname used to make the code more readable */
6537c478bd9Sstevel@tonic-gate 			curr_dirname = current_level->dirname;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 			/* Initialization case */
6567c478bd9Sstevel@tonic-gate 			if (strlen(curr_dirname) == 0)
6577c478bd9Sstevel@tonic-gate 				(void) strcpy(curr_dirname, "/");
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 			/* Open up the dir for this element in the list */
6607c478bd9Sstevel@tonic-gate 			dir_ptr = opendir(curr_dirname);
6617c478bd9Sstevel@tonic-gate 			dir_entry = NULL;
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 			if (dir_ptr == NULL) {
6647c478bd9Sstevel@tonic-gate 				perror(curr_dirname);
6657c478bd9Sstevel@tonic-gate 				*err_code = WARNING_EXIT;
6667c478bd9Sstevel@tonic-gate 			} else
6677c478bd9Sstevel@tonic-gate 				dir_entry = readdir(dir_ptr);
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 			/*
6707c478bd9Sstevel@tonic-gate 			 * Now iterate through the subdirs of 'curr_dirname'
6717c478bd9Sstevel@tonic-gate 			 * In the case of a match against 'pattern',
6727c478bd9Sstevel@tonic-gate 			 * add the path to the next linked list, which
6737c478bd9Sstevel@tonic-gate 			 * will be matched on the next iteration.
6747c478bd9Sstevel@tonic-gate 			 */
6757c478bd9Sstevel@tonic-gate 			while (dir_entry != NULL) {
6767c478bd9Sstevel@tonic-gate 				/* Skip the dirs "." and ".." */
6777c478bd9Sstevel@tonic-gate 				if ((strcmp(dir_entry->d_name, ".") == 0) ||
6787c478bd9Sstevel@tonic-gate 				    (strcmp(dir_entry->d_name, "..") == 0)) {
6797c478bd9Sstevel@tonic-gate 					dir_entry = readdir(dir_ptr);
6807c478bd9Sstevel@tonic-gate 					continue;
6817c478bd9Sstevel@tonic-gate 				}
6827c478bd9Sstevel@tonic-gate 				if (fnmatch(pattern, dir_entry->d_name,
6837c478bd9Sstevel@tonic-gate 				    FNM_PATHNAME) == 0) {
6847c478bd9Sstevel@tonic-gate 					/*
6857c478bd9Sstevel@tonic-gate 					 * Build 'new_dirname' which will be
6867c478bd9Sstevel@tonic-gate 					 * examined on the next iteration.
6877c478bd9Sstevel@tonic-gate 					 */
6887c478bd9Sstevel@tonic-gate 					if (curr_dirname[strlen(curr_dirname)-1]
6897c478bd9Sstevel@tonic-gate 									!= '/')
6907c478bd9Sstevel@tonic-gate 						(void) snprintf(new_dirname,
6917c478bd9Sstevel@tonic-gate 						    sizeof (new_dirname),
6927c478bd9Sstevel@tonic-gate 						    "%s/%s", curr_dirname,
6937c478bd9Sstevel@tonic-gate 						    dir_entry->d_name);
6947c478bd9Sstevel@tonic-gate 					else
6957c478bd9Sstevel@tonic-gate 						(void) snprintf(new_dirname,
6967c478bd9Sstevel@tonic-gate 						    sizeof (new_dirname),
6977c478bd9Sstevel@tonic-gate 						    "%s%s", curr_dirname,
6987c478bd9Sstevel@tonic-gate 						    dir_entry->d_name);
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 					/* Add to the next lined list */
7017c478bd9Sstevel@tonic-gate 					add_dir(&next_level, new_dirname);
7027c478bd9Sstevel@tonic-gate 				}
7037c478bd9Sstevel@tonic-gate 				dir_entry = readdir(dir_ptr);
7047c478bd9Sstevel@tonic-gate 			}
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 			/* Close directory */
7077c478bd9Sstevel@tonic-gate 			if (dir_ptr != NULL)
7087c478bd9Sstevel@tonic-gate 				(void) closedir(dir_ptr);
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 			/* Free this entry and move on.... */
7117c478bd9Sstevel@tonic-gate 			tmp_ptr = current_level;
7127c478bd9Sstevel@tonic-gate 			current_level = current_level->next;
7137c478bd9Sstevel@tonic-gate 			free(tmp_ptr);
7147c478bd9Sstevel@tonic-gate 		}
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 		/*
7177c478bd9Sstevel@tonic-gate 		 * OK, done with this level.  Move to the next level and
7187c478bd9Sstevel@tonic-gate 		 * advance the ptrs which indicate the component name.
7197c478bd9Sstevel@tonic-gate 		 */
7207c478bd9Sstevel@tonic-gate 		current_level = next_level;
7217c478bd9Sstevel@tonic-gate 		next_level = NULL;
7227c478bd9Sstevel@tonic-gate 	}
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	tmp_ptr = current_level;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	/* Error case: the subtree doesn't exist! */
7277c478bd9Sstevel@tonic-gate 	if (current_level == NULL) {
7287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, INVALID_SUBTREE, full_path);
7297c478bd9Sstevel@tonic-gate 		*err_code = WARNING_EXIT;
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	/*
7337c478bd9Sstevel@tonic-gate 	 * Iterate through all the dirnames which match the pattern and
7347c478bd9Sstevel@tonic-gate 	 * add them to to global list of subtrees which must be examined.
7357c478bd9Sstevel@tonic-gate 	 */
7367c478bd9Sstevel@tonic-gate 	while (current_level != NULL) {
7377c478bd9Sstevel@tonic-gate 		/*
7387c478bd9Sstevel@tonic-gate 		 * Sanity check for 'bart create', make sure the subtree
7397c478bd9Sstevel@tonic-gate 		 * points to a valid object.
7407c478bd9Sstevel@tonic-gate 		 */
7417c478bd9Sstevel@tonic-gate 		ret = lstat64(current_level->dirname, &statb);
7427c478bd9Sstevel@tonic-gate 		if (ret < 0) {
7437c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, INVALID_SUBTREE,
7447c478bd9Sstevel@tonic-gate 			    current_level->dirname);
7457c478bd9Sstevel@tonic-gate 			current_level = current_level->next;
7467c478bd9Sstevel@tonic-gate 			*err_code = WARNING_EXIT;
7477c478bd9Sstevel@tonic-gate 			continue;
7487c478bd9Sstevel@tonic-gate 		}
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 		if (begin_rule == NULL) {
7517c478bd9Sstevel@tonic-gate 			begin_rule =
7527c478bd9Sstevel@tonic-gate 			    add_single_rule(current_level->dirname);
7537c478bd9Sstevel@tonic-gate 		} else
7547c478bd9Sstevel@tonic-gate 			(void) add_single_rule(current_level->dirname);
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 		current_level = current_level->next;
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	/*
7607c478bd9Sstevel@tonic-gate 	 * Free up the memory and return a ptr to the first entry in the
7617c478bd9Sstevel@tonic-gate 	 * subtree block.  This is necessary for the parser, which may need
7627c478bd9Sstevel@tonic-gate 	 * to add modifier strings to all the elements in this block.
7637c478bd9Sstevel@tonic-gate 	 */
7647c478bd9Sstevel@tonic-gate 	dirs_cleanup(tmp_ptr);
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	return (begin_rule);
7677c478bd9Sstevel@tonic-gate }
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate /*
7717c478bd9Sstevel@tonic-gate  * Add a single entry to the linked list of rules to be read.  Does not do
7727c478bd9Sstevel@tonic-gate  * the wildcard expansion of 'add_subtree_rule', so is much simpler.
7737c478bd9Sstevel@tonic-gate  */
7747c478bd9Sstevel@tonic-gate static struct rule *
7757c478bd9Sstevel@tonic-gate add_single_rule(char *path)
7767c478bd9Sstevel@tonic-gate {
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	/*
7797c478bd9Sstevel@tonic-gate 	 * If the rules list does NOT exist, then create it.
7807c478bd9Sstevel@tonic-gate 	 * If the rules list does exist, then traverse the next element.
7817c478bd9Sstevel@tonic-gate 	 */
7827c478bd9Sstevel@tonic-gate 	if (first_rule == NULL) {
7837c478bd9Sstevel@tonic-gate 		first_rule = gen_rulestruct();
7847c478bd9Sstevel@tonic-gate 		current_rule = first_rule;
7857c478bd9Sstevel@tonic-gate 	} else {
7867c478bd9Sstevel@tonic-gate 		current_rule->next = gen_rulestruct();
7877c478bd9Sstevel@tonic-gate 		current_rule->next->prev = current_rule;
7887c478bd9Sstevel@tonic-gate 		current_rule = current_rule->next;
7897c478bd9Sstevel@tonic-gate 	}
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	/* Setup the rule struct, handle relocatable roots, i.e. '-R' option */
7927c478bd9Sstevel@tonic-gate 	(void) strlcpy(current_rule->subtree, path,
7937c478bd9Sstevel@tonic-gate 	    sizeof (current_rule->subtree));
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	return (current_rule);
7967c478bd9Sstevel@tonic-gate }
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate /*
7997c478bd9Sstevel@tonic-gate  * Code stolen from filesync utility, used by read_rules() to read in the
8007c478bd9Sstevel@tonic-gate  * rulesfile.
8017c478bd9Sstevel@tonic-gate  */
8027c478bd9Sstevel@tonic-gate static char *
8037c478bd9Sstevel@tonic-gate lex(FILE *file)
8047c478bd9Sstevel@tonic-gate {
8057c478bd9Sstevel@tonic-gate 	char c, delim;
8067c478bd9Sstevel@tonic-gate 	char *p;
8077c478bd9Sstevel@tonic-gate 	char *s;
8087c478bd9Sstevel@tonic-gate 	static char *savep;
8097c478bd9Sstevel@tonic-gate 	static char namebuf[ BUF_SIZE ];
8107c478bd9Sstevel@tonic-gate 	static char inbuf[ BUF_SIZE ];
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	if (file) {			/* read a new line		*/
8137c478bd9Sstevel@tonic-gate 		p = inbuf + sizeof (inbuf);
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 		s = inbuf;
8167c478bd9Sstevel@tonic-gate 		/* read the next input line, with all continuations	*/
8177c478bd9Sstevel@tonic-gate 		while (savep = fgets(s, p - s, file)) {
8187c478bd9Sstevel@tonic-gate 			lex_linenum++;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 			/* go find the last character of the input line	*/
8217c478bd9Sstevel@tonic-gate 			while (*s && s[1])
8227c478bd9Sstevel@tonic-gate 				s++;
8237c478bd9Sstevel@tonic-gate 			if (*s == '\n')
8247c478bd9Sstevel@tonic-gate 				s--;
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 			/* see whether or not we need a continuation	*/
8277c478bd9Sstevel@tonic-gate 			if (s < inbuf || *s != '\\')
8287c478bd9Sstevel@tonic-gate 				break;
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 			continue;
8317c478bd9Sstevel@tonic-gate 		}
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 		if (savep == NULL)
8347c478bd9Sstevel@tonic-gate 			return (0);
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 		s = inbuf;
8377c478bd9Sstevel@tonic-gate 	} else {			/* continue with old line	*/
8387c478bd9Sstevel@tonic-gate 		if (savep == NULL)
8397c478bd9Sstevel@tonic-gate 			return (0);
8407c478bd9Sstevel@tonic-gate 		s = savep;
8417c478bd9Sstevel@tonic-gate 	}
8427c478bd9Sstevel@tonic-gate 	savep = NULL;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	/* skip over leading white space	*/
8457c478bd9Sstevel@tonic-gate 	while (isspace(*s))
8467c478bd9Sstevel@tonic-gate 		s++;
8477c478bd9Sstevel@tonic-gate 	if (*s == 0)
8487c478bd9Sstevel@tonic-gate 		return (0);
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	/* see if this is a quoted string	*/
8517c478bd9Sstevel@tonic-gate 	c = *s;
8527c478bd9Sstevel@tonic-gate 	if (c == '\'' || c == '"') {
8537c478bd9Sstevel@tonic-gate 		delim = c;
8547c478bd9Sstevel@tonic-gate 		s++;
8557c478bd9Sstevel@tonic-gate 	} else
8567c478bd9Sstevel@tonic-gate 		delim = 0;
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	/* copy the token into the buffer	*/
8597c478bd9Sstevel@tonic-gate 	for (p = namebuf; (c = *s) != 0; s++) {
8607c478bd9Sstevel@tonic-gate 		/* literal escape		*/
8617c478bd9Sstevel@tonic-gate 		if (c == '\\') {
8627c478bd9Sstevel@tonic-gate 			s++;
8637c478bd9Sstevel@tonic-gate 			*p++ = *s;
8647c478bd9Sstevel@tonic-gate 			continue;
8657c478bd9Sstevel@tonic-gate 		}
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 		/* closing delimiter		*/
8687c478bd9Sstevel@tonic-gate 		if (c == delim) {
8697c478bd9Sstevel@tonic-gate 			s++;
8707c478bd9Sstevel@tonic-gate 			break;
8717c478bd9Sstevel@tonic-gate 		}
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 		/* delimiting white space	*/
8747c478bd9Sstevel@tonic-gate 		if (delim == 0 && isspace(c))
8757c478bd9Sstevel@tonic-gate 			break;
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 		/* ordinary characters		*/
8787c478bd9Sstevel@tonic-gate 		*p++ = *s;
8797c478bd9Sstevel@tonic-gate 	}
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	/* remember where we left off		*/
8837c478bd9Sstevel@tonic-gate 	savep = *s ? s : 0;
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	/* null terminate and return the buffer	*/
8867c478bd9Sstevel@tonic-gate 	*p = 0;
8877c478bd9Sstevel@tonic-gate 	return (namebuf);
8887c478bd9Sstevel@tonic-gate }
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate /*
8917c478bd9Sstevel@tonic-gate  * Iterate through the dir strcutures and free memory.
8927c478bd9Sstevel@tonic-gate  */
8937c478bd9Sstevel@tonic-gate static void
8947c478bd9Sstevel@tonic-gate dirs_cleanup(struct dir_component *dir)
8957c478bd9Sstevel@tonic-gate {
8967c478bd9Sstevel@tonic-gate 	struct	dir_component	*next;
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	while (dir != NULL) {
8997c478bd9Sstevel@tonic-gate 		next = dir->next;
9007c478bd9Sstevel@tonic-gate 		free(dir);
9017c478bd9Sstevel@tonic-gate 		dir = next;
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate }
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate /*
9067c478bd9Sstevel@tonic-gate  * Create and initialize a new dir structure.  Used by add_subtree_rule() when
9077c478bd9Sstevel@tonic-gate  * doing expansion of directory names caused by wildcards.
9087c478bd9Sstevel@tonic-gate  */
9097c478bd9Sstevel@tonic-gate static void
9107c478bd9Sstevel@tonic-gate add_dir(struct dir_component **dir, char *dirname)
9117c478bd9Sstevel@tonic-gate {
9127c478bd9Sstevel@tonic-gate 	struct	dir_component	*new, *next_dir;
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	new = gen_dir_component();
9157c478bd9Sstevel@tonic-gate 	if (dirname != NULL)
9167c478bd9Sstevel@tonic-gate 		(void) strlcpy(new->dirname, dirname, sizeof (new->dirname));
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	if (*dir == NULL)
9197c478bd9Sstevel@tonic-gate 		*dir = new;
9207c478bd9Sstevel@tonic-gate 	else {
9217c478bd9Sstevel@tonic-gate 		next_dir = *dir;
9227c478bd9Sstevel@tonic-gate 		while (next_dir->next != NULL)
9237c478bd9Sstevel@tonic-gate 			next_dir = next_dir->next;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 		next_dir->next = new;
9267c478bd9Sstevel@tonic-gate 	}
9277c478bd9Sstevel@tonic-gate }
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate /*
9307c478bd9Sstevel@tonic-gate  * Traverse the linked list of rules in a REVERSE order.
9317c478bd9Sstevel@tonic-gate  */
9327c478bd9Sstevel@tonic-gate static struct rule *
9337c478bd9Sstevel@tonic-gate get_last_entry(boolean_t reset)
9347c478bd9Sstevel@tonic-gate {
9357c478bd9Sstevel@tonic-gate 	static struct rule	*curr_root = NULL;
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	if (reset) {
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 		curr_root = first_rule;
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 		/* RESET: set cur_root to the end of the list */
9427c478bd9Sstevel@tonic-gate 		while (curr_root != NULL)
9437c478bd9Sstevel@tonic-gate 			if (curr_root->next == NULL)
9447c478bd9Sstevel@tonic-gate 				break;
9457c478bd9Sstevel@tonic-gate 			else
9467c478bd9Sstevel@tonic-gate 				curr_root = curr_root->next;
9477c478bd9Sstevel@tonic-gate 	} else
9487c478bd9Sstevel@tonic-gate 		curr_root = (curr_root->prev);
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	return (curr_root);
9517c478bd9Sstevel@tonic-gate }
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate /*
9547c478bd9Sstevel@tonic-gate  * Traverse the first entry, used by 'bart create' to iterate through
9557c478bd9Sstevel@tonic-gate  * subtrees or individual filenames.
9567c478bd9Sstevel@tonic-gate  */
9577c478bd9Sstevel@tonic-gate struct rule *
9587c478bd9Sstevel@tonic-gate get_first_subtree()
9597c478bd9Sstevel@tonic-gate {
9607c478bd9Sstevel@tonic-gate 	return (first_rule);
9617c478bd9Sstevel@tonic-gate }
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate /*
9647c478bd9Sstevel@tonic-gate  * Traverse the next entry, used by 'bart create' to iterate through
9657c478bd9Sstevel@tonic-gate  * subtrees or individual filenames.
9667c478bd9Sstevel@tonic-gate  */
9677c478bd9Sstevel@tonic-gate struct rule *
9687c478bd9Sstevel@tonic-gate get_next_subtree(struct rule *entry)
9697c478bd9Sstevel@tonic-gate {
9707c478bd9Sstevel@tonic-gate 	return (entry->next);
9717c478bd9Sstevel@tonic-gate }
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate char *
9747c478bd9Sstevel@tonic-gate safe_strdup(char *s)
9757c478bd9Sstevel@tonic-gate {
9767c478bd9Sstevel@tonic-gate 	char *ret;
9777c478bd9Sstevel@tonic-gate 	size_t len;
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 	len = strlen(s) + 1;
9807c478bd9Sstevel@tonic-gate 	ret = safe_calloc(len);
9817c478bd9Sstevel@tonic-gate 	(void) strlcpy(ret, s, len);
9827c478bd9Sstevel@tonic-gate 	return (ret);
9837c478bd9Sstevel@tonic-gate }
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate /*
9867c478bd9Sstevel@tonic-gate  * Function to match a filename against the subtrees in the link list
9877c478bd9Sstevel@tonic-gate  * of 'rule' strcutures.  Upon finding a matching rule, see if it should
9887c478bd9Sstevel@tonic-gate  * be excluded.  Keep going until a match is found OR all rules have been
9897c478bd9Sstevel@tonic-gate  * exhausted.
9907c478bd9Sstevel@tonic-gate  * NOTES: Rules are parsed in reverse;
9917c478bd9Sstevel@tonic-gate  * satisfies the spec that "Last rule wins".  Also, the default rule should
9927c478bd9Sstevel@tonic-gate  * always match, so this function should NEVER return NULL.
9937c478bd9Sstevel@tonic-gate  */
9947c478bd9Sstevel@tonic-gate struct rule *
9957c478bd9Sstevel@tonic-gate check_rules(const char *fname, char type)
9967c478bd9Sstevel@tonic-gate {
9977c478bd9Sstevel@tonic-gate 	struct rule		*root;
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	root = get_last_entry(B_TRUE);
10007c478bd9Sstevel@tonic-gate 	while (root != NULL) {
10017c478bd9Sstevel@tonic-gate 		if (match_subtree(fname, root->subtree)) {
10027c478bd9Sstevel@tonic-gate 			if (exclude_fname(fname, type, root) == 0)
10037c478bd9Sstevel@tonic-gate 				break;
10047c478bd9Sstevel@tonic-gate 		}
10057c478bd9Sstevel@tonic-gate 		root = get_last_entry(B_FALSE);
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	return (root);
10097c478bd9Sstevel@tonic-gate }
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate /*
1012f7bbf134Shm123892  * Function to determine if an entry in a rules file (see bart_rules(4)) applies
1013f7bbf134Shm123892  * to a filename. We truncate "fname" such that it has the same number of
1014f7bbf134Shm123892  * components as "rule" and let fnmatch(3C) do the rest. A "component" is one
1015f7bbf134Shm123892  * part of an fname as delimited by slashes ('/'). So "/A/B/C/D" has four
1016f7bbf134Shm123892  * components: "A", "B", "C" and "D".
1017f7bbf134Shm123892  *
1018f7bbf134Shm123892  * For example:
1019f7bbf134Shm123892  *
1020f7bbf134Shm123892  * 1. the rule "/home/nickiso" applies to fname "/home/nickiso/src/foo.c" so
1021f7bbf134Shm123892  * should match.
1022f7bbf134Shm123892  *
1023f7bbf134Shm123892  * 2. the rule "/home/nickiso/temp/src" does not apply to fname
1024f7bbf134Shm123892  * "/home/nickiso/foo.c" so should not match.
10257c478bd9Sstevel@tonic-gate  */
10267c478bd9Sstevel@tonic-gate static int
10277c478bd9Sstevel@tonic-gate match_subtree(const char *fname, char *rule)
10287c478bd9Sstevel@tonic-gate {
1029f7bbf134Shm123892 	int	match, num_rule_slash;
1030f7bbf134Shm123892 	char	*ptr, fname_cp[PATH_MAX];
10317c478bd9Sstevel@tonic-gate 
1032f7bbf134Shm123892 	/* If rule has more components than fname, it cannot match. */
1033f7bbf134Shm123892 	if ((num_rule_slash = count_slashes(rule)) > count_slashes(fname))
1034f7bbf134Shm123892 		return (0);
1035f7bbf134Shm123892 
1036f7bbf134Shm123892 	/* Create a copy of fname that we can truncate. */
1037f7bbf134Shm123892 	(void) strlcpy(fname_cp, fname, sizeof (fname_cp));
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	/*
1040f7bbf134Shm123892 	 * Truncate fname_cp such that it has the same number of components
1041f7bbf134Shm123892 	 * as rule. If rule ends with '/', so should fname_cp. ie:
10427c478bd9Sstevel@tonic-gate 	 *
1043f7bbf134Shm123892 	 * rule		fname			fname_cp	matches
1044f7bbf134Shm123892 	 * ----		-----			--------	-------
1045f7bbf134Shm123892 	 * /home/dir*	/home/dir0/dir1/fileA	/home/dir0	yes
1046f7bbf134Shm123892 	 * /home/dir/	/home/dir0/dir1/fileA	/home/dir0/	no
10477c478bd9Sstevel@tonic-gate 	 */
1048f7bbf134Shm123892 	for (ptr = fname_cp; num_rule_slash > 0; num_rule_slash--, ptr++)
1049f7bbf134Shm123892 		ptr = strchr(ptr, '/');
1050f7bbf134Shm123892 	if (*(rule + strlen(rule) - 1) != '/') {
1051f7bbf134Shm123892 		while (*ptr != '\0') {
1052f7bbf134Shm123892 			if (*ptr == '/')
1053f7bbf134Shm123892 				break;
1054f7bbf134Shm123892 			ptr++;
1055f7bbf134Shm123892 		}
1056f7bbf134Shm123892 	}
1057f7bbf134Shm123892 	*ptr = '\0';
10587c478bd9Sstevel@tonic-gate 
1059f7bbf134Shm123892 	/* OK, now see if they match. */
1060f7bbf134Shm123892 	match = fnmatch(rule, fname_cp, FNM_PATHNAME);
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	/* No match, return failure */
10637c478bd9Sstevel@tonic-gate 	if (match != 0)
10647c478bd9Sstevel@tonic-gate 		return (0);
10657c478bd9Sstevel@tonic-gate 	else
10667c478bd9Sstevel@tonic-gate 		return (1);
10677c478bd9Sstevel@tonic-gate }
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate void
10707c478bd9Sstevel@tonic-gate process_glob_ignores(char *ignore_list, uint_t *flags)
10717c478bd9Sstevel@tonic-gate {
10727c478bd9Sstevel@tonic-gate 	char	*cp;
10737c478bd9Sstevel@tonic-gate 	struct attr_keyword *akp;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	if (ignore_list == NULL)
10767c478bd9Sstevel@tonic-gate 		usage();
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	cp = strtok(ignore_list, ",");
10797c478bd9Sstevel@tonic-gate 	while (cp != NULL) {
10807c478bd9Sstevel@tonic-gate 		akp = attr_keylookup(cp);
10817c478bd9Sstevel@tonic-gate 		if (akp == NULL)
10827c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ERROR: Invalid keyword %s\n",
10837c478bd9Sstevel@tonic-gate 			    cp);
10847c478bd9Sstevel@tonic-gate 		else
10857c478bd9Sstevel@tonic-gate 			*flags &= ~akp->ak_flags;
10867c478bd9Sstevel@tonic-gate 		cp = strtok(NULL, ",");
10877c478bd9Sstevel@tonic-gate 	}
10887c478bd9Sstevel@tonic-gate }
1089