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