xref: /titanic_52/usr/src/cmd/bart/rules.c (revision f7bbf13448f2c58aafcdb75e10efb27b72fcc1fa)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*f7bbf134Shm123892  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <dirent.h>
297c478bd9Sstevel@tonic-gate #include <fnmatch.h>
30*f7bbf134Shm123892 #include <string.h>
317c478bd9Sstevel@tonic-gate #include "bart.h"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate static int count_slashes(const char *);
347c478bd9Sstevel@tonic-gate static struct rule *gen_rulestruct(void);
357c478bd9Sstevel@tonic-gate static struct tree_modifier *gen_tree_modifier(void);
367c478bd9Sstevel@tonic-gate static struct dir_component *gen_dir_component(void);
377c478bd9Sstevel@tonic-gate static void init_rule(uint_t, struct rule *);
387c478bd9Sstevel@tonic-gate static void add_modifier(struct rule *, char *);
397c478bd9Sstevel@tonic-gate static struct rule *add_subtree_rule(char *, char *, int, int *);
407c478bd9Sstevel@tonic-gate static struct rule *add_single_rule(char *);
417c478bd9Sstevel@tonic-gate static void dirs_cleanup(struct dir_component *);
427c478bd9Sstevel@tonic-gate static void add_dir(struct dir_component **, char *);
437c478bd9Sstevel@tonic-gate static char *lex(FILE *);
447c478bd9Sstevel@tonic-gate static int match_subtree(const char *, char *);
457c478bd9Sstevel@tonic-gate static struct rule *get_last_entry(boolean_t);
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate static int	lex_linenum;	/* line number in current input file	*/
487c478bd9Sstevel@tonic-gate static struct rule	*first_rule = NULL, *current_rule = NULL;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * This function is responsible for validating whether or not a given file
527c478bd9Sstevel@tonic-gate  * should be cataloged, based upon the modifiers for a subtree.
537c478bd9Sstevel@tonic-gate  * For example, a line in the rules file: '/home/nickiso *.c' should only
547c478bd9Sstevel@tonic-gate  * catalog the C files (based upon pattern matching) in the subtree
557c478bd9Sstevel@tonic-gate  * '/home/nickiso'.
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  * Return non-zero if it should be excluded, 0 if it should be cataloged.
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate int
607c478bd9Sstevel@tonic-gate exclude_fname(const char *fname, char fname_type, struct rule *rule_ptr)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	char	*pattern, *ptr, component[PATH_MAX], fname_cp[PATH_MAX],
637c478bd9Sstevel@tonic-gate 		pattern_cp[PATH_MAX];
647c478bd9Sstevel@tonic-gate 	int	match, num_pattern_slash, num_fname_slash, i, slashes_to_adv,
657c478bd9Sstevel@tonic-gate 		ret_val = 0;
667c478bd9Sstevel@tonic-gate 	struct  tree_modifier   *mod_ptr;
677c478bd9Sstevel@tonic-gate 	boolean_t		dir_flag;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	/*
707c478bd9Sstevel@tonic-gate 	 * For a given entry in the rules struct, the modifiers, e.g., '*.c',
717c478bd9Sstevel@tonic-gate 	 * are kept in a linked list.  Get a ptr to the head of the list.
727c478bd9Sstevel@tonic-gate 	 */
737c478bd9Sstevel@tonic-gate 	mod_ptr = rule_ptr->modifiers;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	/*
767c478bd9Sstevel@tonic-gate 	 * Walk through all the modifiers until its they are exhausted OR
777c478bd9Sstevel@tonic-gate 	 * until the file should definitely be excluded.
787c478bd9Sstevel@tonic-gate 	 */
797c478bd9Sstevel@tonic-gate 	while ((mod_ptr != NULL) && !ret_val) {
807c478bd9Sstevel@tonic-gate 		/* First, see if we should be matching files or dirs */
817c478bd9Sstevel@tonic-gate 		if (mod_ptr->mod_str[(strlen(mod_ptr->mod_str)-1)] == '/')
827c478bd9Sstevel@tonic-gate 			dir_flag = B_TRUE;
837c478bd9Sstevel@tonic-gate 		else
847c478bd9Sstevel@tonic-gate 			dir_flag = B_FALSE;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 		if (mod_ptr->mod_str[0] == '!') {
877c478bd9Sstevel@tonic-gate 			pattern = (mod_ptr->mod_str + 1);
887c478bd9Sstevel@tonic-gate 		} else {
897c478bd9Sstevel@tonic-gate 			pattern = mod_ptr->mod_str;
907c478bd9Sstevel@tonic-gate 		}
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 		if (dir_flag == B_FALSE) {
937c478bd9Sstevel@tonic-gate 			/*
947c478bd9Sstevel@tonic-gate 			 * In the case when a user is trying to filter on
957c478bd9Sstevel@tonic-gate 			 * FILES and the entry is a directory, its excluded!
967c478bd9Sstevel@tonic-gate 			 */
977c478bd9Sstevel@tonic-gate 			if (fname_type == 'D') {
987c478bd9Sstevel@tonic-gate 				ret_val = 1;
997c478bd9Sstevel@tonic-gate 				break;
1007c478bd9Sstevel@tonic-gate 			}
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 			/*
1037c478bd9Sstevel@tonic-gate 			 * Match patterns against filenames.
1047c478bd9Sstevel@tonic-gate 			 * Need to be able to handle multi-level patterns,
1057c478bd9Sstevel@tonic-gate 			 * eg. "SCCS/<star-wildcard>.c", which means
1067c478bd9Sstevel@tonic-gate 			 * 'only match C files under SCCS directories.
1077c478bd9Sstevel@tonic-gate 			 *
1087c478bd9Sstevel@tonic-gate 			 * Determine the number of levels in the filename and
1097c478bd9Sstevel@tonic-gate 			 * in the pattern.
1107c478bd9Sstevel@tonic-gate 			 */
1117c478bd9Sstevel@tonic-gate 			num_pattern_slash = count_slashes(pattern);
1127c478bd9Sstevel@tonic-gate 			num_fname_slash = count_slashes(fname);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 			/* Check for trivial exclude condition */
1157c478bd9Sstevel@tonic-gate 			if (num_pattern_slash > num_fname_slash) {
1167c478bd9Sstevel@tonic-gate 				ret_val = 1;
1177c478bd9Sstevel@tonic-gate 				break;
1187c478bd9Sstevel@tonic-gate 			}
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 			/*
1217c478bd9Sstevel@tonic-gate 			 * Do an apples to apples comparison, based upon the
1227c478bd9Sstevel@tonic-gate 			 * number of levels:
1237c478bd9Sstevel@tonic-gate 			 *
1247c478bd9Sstevel@tonic-gate 			 * Assume fname is /A/B/C/D/E and the pattern is D/E.
1257c478bd9Sstevel@tonic-gate 			 * In that case, 'ptr' will point to "D/E" and
1267c478bd9Sstevel@tonic-gate 			 * 'slashes_to_adv' will be '4'.
1277c478bd9Sstevel@tonic-gate 			 */
1287c478bd9Sstevel@tonic-gate 			(void) strlcpy(fname_cp, fname, sizeof (fname_cp));
1297c478bd9Sstevel@tonic-gate 			ptr = fname_cp;
1307c478bd9Sstevel@tonic-gate 			slashes_to_adv = num_fname_slash - num_pattern_slash;
1317c478bd9Sstevel@tonic-gate 			for (i = 0; i < slashes_to_adv; i++)  {
1327c478bd9Sstevel@tonic-gate 				ptr = strchr(ptr, '/');
1337c478bd9Sstevel@tonic-gate 				ptr++;
1347c478bd9Sstevel@tonic-gate 			}
1357c478bd9Sstevel@tonic-gate 			if ((pattern[0] == '.') && (pattern[1] == '.') &&
1367c478bd9Sstevel@tonic-gate 			    (pattern[2] == '/')) {
1377c478bd9Sstevel@tonic-gate 				pattern = strchr(pattern, '/');
1387c478bd9Sstevel@tonic-gate 				ptr = strchr(ptr, '/');
1397c478bd9Sstevel@tonic-gate 			}
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 			/* OK, now do the fnmatch() and set the return value */
1437c478bd9Sstevel@tonic-gate 			match = fnmatch(pattern, ptr, FNM_PATHNAME);
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 			if (match == 0)
1467c478bd9Sstevel@tonic-gate 				ret_val = 0;
1477c478bd9Sstevel@tonic-gate 			else
1487c478bd9Sstevel@tonic-gate 				ret_val = 1;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 		} else {
1517c478bd9Sstevel@tonic-gate 			/*
1527c478bd9Sstevel@tonic-gate 			 * The rule requires directory matching.
1537c478bd9Sstevel@tonic-gate 			 *
1547c478bd9Sstevel@tonic-gate 			 * First, make copies, since both the pattern and
1557c478bd9Sstevel@tonic-gate 			 * filename need to be modified.
1567c478bd9Sstevel@tonic-gate 			 *
1577c478bd9Sstevel@tonic-gate 			 * When copying 'fname', ignore the relocatable root
1587c478bd9Sstevel@tonic-gate 			 * since pattern matching is done for the string AFTER
1597c478bd9Sstevel@tonic-gate 			 * the relocatable root.  For example, if the
1607c478bd9Sstevel@tonic-gate 			 * relocatable root is "/dir1/dir2/dir3" and the
1617c478bd9Sstevel@tonic-gate 			 * pattern is "dir3/", we do NOT want to include every
1627c478bd9Sstevel@tonic-gate 			 * directory in the relocatable root.  Instead, we
1637c478bd9Sstevel@tonic-gate 			 * only want to include subtrees that look like:
1647c478bd9Sstevel@tonic-gate 			 * "/dir1/dir2/dir3/....dir3/....."
1657c478bd9Sstevel@tonic-gate 			 *
1667c478bd9Sstevel@tonic-gate 			 * NOTE: the 'pattern_cp' does NOT have a trailing '/':
1677c478bd9Sstevel@tonic-gate 			 * necessary for fnmatch().
1687c478bd9Sstevel@tonic-gate 			 */
1697c478bd9Sstevel@tonic-gate 			(void) strlcpy(fname_cp,
1707c478bd9Sstevel@tonic-gate 			    (fname+strlen(rule_ptr->subtree)),
1717c478bd9Sstevel@tonic-gate 			    sizeof (fname_cp));
1727c478bd9Sstevel@tonic-gate 			(void) strlcpy(pattern_cp, pattern,
1737c478bd9Sstevel@tonic-gate 			    sizeof (pattern_cp));
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 			/*
1767c478bd9Sstevel@tonic-gate 			 * For non-directory entries, remove the trailing
1777c478bd9Sstevel@tonic-gate 			 * name, e.g., for a file /A/B/C/D where 'D' is
1787c478bd9Sstevel@tonic-gate 			 * the actual filename, remove the 'D' since it
1797c478bd9Sstevel@tonic-gate 			 * should *not* be considered in the directory match.
1807c478bd9Sstevel@tonic-gate 			 */
1817c478bd9Sstevel@tonic-gate 			if (fname_type != 'D') {
1827c478bd9Sstevel@tonic-gate 				ptr = strrchr(fname_cp, '/');
1837c478bd9Sstevel@tonic-gate 				if (ptr != NULL)
1847c478bd9Sstevel@tonic-gate 					*ptr = '\0';
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 				/* Trivial case: simple filename */
1877c478bd9Sstevel@tonic-gate 				if (strlen(fname_cp) == 0) {
1887c478bd9Sstevel@tonic-gate 					ret_val = 1;
1897c478bd9Sstevel@tonic-gate 					break;
1907c478bd9Sstevel@tonic-gate 				}
1917c478bd9Sstevel@tonic-gate 			}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 			/* Count the # of slashes in the pattern and fname */
1947c478bd9Sstevel@tonic-gate 			num_pattern_slash = count_slashes(pattern_cp);
1957c478bd9Sstevel@tonic-gate 			num_fname_slash = count_slashes(fname_cp);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 			/*
1987c478bd9Sstevel@tonic-gate 			 * fname_cp is too short, bail!
1997c478bd9Sstevel@tonic-gate 			 */
2007c478bd9Sstevel@tonic-gate 			if (num_pattern_slash > num_fname_slash) {
2017c478bd9Sstevel@tonic-gate 				ret_val = 1;
2027c478bd9Sstevel@tonic-gate 				break;
2037c478bd9Sstevel@tonic-gate 			}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 			/*
2067c478bd9Sstevel@tonic-gate 			 * OK, walk through the filename and check for the
2077c478bd9Sstevel@tonic-gate 			 * pattern.
2087c478bd9Sstevel@tonic-gate 			 * This loop will termate when the match is found OR
2097c478bd9Sstevel@tonic-gate 			 * there fname is too short to possibly match.
2107c478bd9Sstevel@tonic-gate 			 */
2117c478bd9Sstevel@tonic-gate 			while (strlen(fname_cp) > 0) {
2127c478bd9Sstevel@tonic-gate 				num_fname_slash = count_slashes(fname_cp);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 				/*
2157c478bd9Sstevel@tonic-gate 				 * fname is too short, bail!
2167c478bd9Sstevel@tonic-gate 				 */
2177c478bd9Sstevel@tonic-gate 				if (num_pattern_slash > num_fname_slash) {
2187c478bd9Sstevel@tonic-gate 					ret_val = 1;
2197c478bd9Sstevel@tonic-gate 					break;
2207c478bd9Sstevel@tonic-gate 				}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 				/*
2237c478bd9Sstevel@tonic-gate 				 * The next stanza selects an appropriate
2247c478bd9Sstevel@tonic-gate 				 * substring of the filename.
2257c478bd9Sstevel@tonic-gate 				 * For example, if pattern is 'C/D/E' and
2267c478bd9Sstevel@tonic-gate 				 * filename is '/A/B/C/D/E', this stanza will
2277c478bd9Sstevel@tonic-gate 				 * set ptr to 'C/D/E'.
2287c478bd9Sstevel@tonic-gate 				 */
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 				component[0] = '\0';
2317c478bd9Sstevel@tonic-gate 				if (num_fname_slash > 0) {
2327c478bd9Sstevel@tonic-gate 					ptr = (fname_cp+1);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 					for (i = 0; i < (num_pattern_slash-1);
2357c478bd9Sstevel@tonic-gate 					    i++) {
2367c478bd9Sstevel@tonic-gate 						ptr = strchr(ptr, '/');
2377c478bd9Sstevel@tonic-gate 						ptr++;
2387c478bd9Sstevel@tonic-gate 					}
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 					if (ptr != NULL)
2417c478bd9Sstevel@tonic-gate 						(void) strlcpy(component, ptr,
2427c478bd9Sstevel@tonic-gate 						    sizeof (component));
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 				} else
2457c478bd9Sstevel@tonic-gate 					(void) strlcpy(component, fname_cp,
2467c478bd9Sstevel@tonic-gate 					    sizeof (component));
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 				/*
2497c478bd9Sstevel@tonic-gate 				 * See if they match.  If they do, set exclude
2507c478bd9Sstevel@tonic-gate 				 * to appropriate value and exit.
2517c478bd9Sstevel@tonic-gate 				 */
2527c478bd9Sstevel@tonic-gate 				match =  fnmatch(pattern_cp, component,
2537c478bd9Sstevel@tonic-gate 				    FNM_PATHNAME);
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 				/*
2567c478bd9Sstevel@tonic-gate 				 * Special case: match "/" and "*" or "?".
2577c478bd9Sstevel@tonic-gate 				 * Necessary since explicitly NOT matched by
2587c478bd9Sstevel@tonic-gate 				 * fnmatch()
2597c478bd9Sstevel@tonic-gate 				 */
2607c478bd9Sstevel@tonic-gate 				if ((match == 1) && (strlen(component) == 1) &&
2617c478bd9Sstevel@tonic-gate 				    (component[0] == '/') &&
2627c478bd9Sstevel@tonic-gate 				    (strlen(pattern_cp) == 1)) {
2637c478bd9Sstevel@tonic-gate 					if ((pattern_cp[0] == '?') ||
2647c478bd9Sstevel@tonic-gate 					    (pattern_cp[0] == '*'))
2657c478bd9Sstevel@tonic-gate 						match = 0;
2667c478bd9Sstevel@tonic-gate 				}
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 				/*
2697c478bd9Sstevel@tonic-gate 				 * Test to see if there is a match.
2707c478bd9Sstevel@tonic-gate 				 *
2717c478bd9Sstevel@tonic-gate 				 * If it matches we are done for this rule.
2727c478bd9Sstevel@tonic-gate 				 *
2737c478bd9Sstevel@tonic-gate 				 * If there is NOT a match, then we need
2747c478bd9Sstevel@tonic-gate 				 * to iterate down the filename until it
2757c478bd9Sstevel@tonic-gate 				 * matches OR we determine it cannot match.
2767c478bd9Sstevel@tonic-gate 				 */
2777c478bd9Sstevel@tonic-gate 				if (match == 0) {
2787c478bd9Sstevel@tonic-gate 					ret_val = 0;
2797c478bd9Sstevel@tonic-gate 					break;
2807c478bd9Sstevel@tonic-gate 				} else {
2817c478bd9Sstevel@tonic-gate 					/*
2827c478bd9Sstevel@tonic-gate 					 * No match.  Remove the last 'segment'
2837c478bd9Sstevel@tonic-gate 					 * of the filename, e.g., if its
2847c478bd9Sstevel@tonic-gate 					 * "/A/B/C/D/E", then remove "/E".
2857c478bd9Sstevel@tonic-gate 					 *  If nothing left to remove, we are
2867c478bd9Sstevel@tonic-gate 					 * done.
2877c478bd9Sstevel@tonic-gate 					 */
2887c478bd9Sstevel@tonic-gate 					ptr = strrchr(fname_cp, '/');
2897c478bd9Sstevel@tonic-gate 					if (ptr != NULL)
2907c478bd9Sstevel@tonic-gate 						*ptr = '\0';
2917c478bd9Sstevel@tonic-gate 					else {
2927c478bd9Sstevel@tonic-gate 						fname_cp[0] = '\0';
2937c478bd9Sstevel@tonic-gate 						ret_val = 1;
2947c478bd9Sstevel@tonic-gate 					}
2957c478bd9Sstevel@tonic-gate 				}
2967c478bd9Sstevel@tonic-gate 			}
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 		/*
3007c478bd9Sstevel@tonic-gate 		 * Take into account whether or not this rule began with
3017c478bd9Sstevel@tonic-gate 		 * a '!'
3027c478bd9Sstevel@tonic-gate 		 */
3037c478bd9Sstevel@tonic-gate 		if (mod_ptr->include == B_FALSE) {
3047c478bd9Sstevel@tonic-gate 			if (ret_val == 0)
3057c478bd9Sstevel@tonic-gate 				ret_val = 1;
3067c478bd9Sstevel@tonic-gate 			else ret_val = 0;
3077c478bd9Sstevel@tonic-gate 		}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 		/* Advance to the next modifier */
3107c478bd9Sstevel@tonic-gate 		mod_ptr = mod_ptr->next;
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	return (ret_val);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate static int
3177c478bd9Sstevel@tonic-gate count_slashes(const char *in_path)
3187c478bd9Sstevel@tonic-gate {
3197c478bd9Sstevel@tonic-gate 	int num_fname_slash = 0;
3207c478bd9Sstevel@tonic-gate 	const char *p;
3217c478bd9Sstevel@tonic-gate 	for (p = in_path; *p != '\0'; p++)
3227c478bd9Sstevel@tonic-gate 		if (*p == '/')
3237c478bd9Sstevel@tonic-gate 			num_fname_slash++;
3247c478bd9Sstevel@tonic-gate 	return (num_fname_slash);
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate static struct rule *
3287c478bd9Sstevel@tonic-gate gen_rulestruct(void)
3297c478bd9Sstevel@tonic-gate {
3307c478bd9Sstevel@tonic-gate 	struct rule	*new_rule;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	new_rule = (struct rule *)safe_calloc(sizeof (struct rule));
3337c478bd9Sstevel@tonic-gate 	new_rule->traversed = B_FALSE;
3347c478bd9Sstevel@tonic-gate 	return (new_rule);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate static struct tree_modifier *
3387c478bd9Sstevel@tonic-gate gen_tree_modifier(void)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	struct tree_modifier	*new_modifier;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	new_modifier = (struct tree_modifier *)safe_calloc
3437c478bd9Sstevel@tonic-gate 	    (sizeof (struct tree_modifier));
3447c478bd9Sstevel@tonic-gate 	return (new_modifier);
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate static struct dir_component *
3487c478bd9Sstevel@tonic-gate gen_dir_component(void)
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	struct dir_component	*new_dir;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	new_dir = (struct dir_component *)safe_calloc
3537c478bd9Sstevel@tonic-gate 	    (sizeof (struct dir_component));
3547c478bd9Sstevel@tonic-gate 	return (new_dir);
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate /*
3587c478bd9Sstevel@tonic-gate  * Set up a default rule when there is no rules file.
3597c478bd9Sstevel@tonic-gate  */
3607c478bd9Sstevel@tonic-gate static struct rule *
3617c478bd9Sstevel@tonic-gate setup_default_rule(char *reloc_root, uint_t flags)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 	struct	rule	*new_rule;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	new_rule = add_single_rule(reloc_root[0] == '\0' ? "/" : reloc_root);
3667c478bd9Sstevel@tonic-gate 	init_rule(flags, new_rule);
3677c478bd9Sstevel@tonic-gate 	add_modifier(new_rule, "*");
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	return (new_rule);
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate /*
3737c478bd9Sstevel@tonic-gate  * Utility function, used to initialize the flag in a new rule structure.
3747c478bd9Sstevel@tonic-gate  */
3757c478bd9Sstevel@tonic-gate static void
3767c478bd9Sstevel@tonic-gate init_rule(uint_t flags, struct rule *new_rule)
3777c478bd9Sstevel@tonic-gate {
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	if (new_rule == NULL)
3807c478bd9Sstevel@tonic-gate 		return;
3817c478bd9Sstevel@tonic-gate 	new_rule->attr_list = flags;
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate /*
3857c478bd9Sstevel@tonic-gate  * Function to read the rulesfile.  Used by both 'bart create' and
3867c478bd9Sstevel@tonic-gate  * 'bart compare'.
3877c478bd9Sstevel@tonic-gate  */
3887c478bd9Sstevel@tonic-gate int
3897c478bd9Sstevel@tonic-gate read_rules(FILE *file, char *reloc_root, uint_t in_flags, int create)
3907c478bd9Sstevel@tonic-gate {
3917c478bd9Sstevel@tonic-gate 	char		*s;
3927c478bd9Sstevel@tonic-gate 	struct rule	*block_begin = NULL, *new_rule, *rp;
3937c478bd9Sstevel@tonic-gate 	struct attr_keyword *akp;
3947c478bd9Sstevel@tonic-gate 	int		check_flag, ignore_flag, syntax_err, ret_code;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	ret_code = EXIT;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	lex_linenum = 0;
3997c478bd9Sstevel@tonic-gate 	check_flag = 0;
4007c478bd9Sstevel@tonic-gate 	ignore_flag = 0;
4017c478bd9Sstevel@tonic-gate 	syntax_err = 0;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	if (file == NULL) {
4047c478bd9Sstevel@tonic-gate 		(void) setup_default_rule(reloc_root, in_flags);
4057c478bd9Sstevel@tonic-gate 		return (ret_code);
4067c478bd9Sstevel@tonic-gate 	} else if (!create) {
4077c478bd9Sstevel@tonic-gate 		block_begin = setup_default_rule("/", in_flags);
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	while (!feof(file)) {
4117c478bd9Sstevel@tonic-gate 		/* Read a line from the file */
4127c478bd9Sstevel@tonic-gate 		s = lex(file);
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 		/* skip blank lines and comments */
4157c478bd9Sstevel@tonic-gate 		if (s == NULL || *s == 0 || *s == '#')
4167c478bd9Sstevel@tonic-gate 			continue;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 		/*
4197c478bd9Sstevel@tonic-gate 		 * Beginning of a subtree and possibly a new block.
4207c478bd9Sstevel@tonic-gate 		 *
4217c478bd9Sstevel@tonic-gate 		 * If this is a new block, keep track of the beginning of
4227c478bd9Sstevel@tonic-gate 		 * the block. if there are directives later on, we need to
4237c478bd9Sstevel@tonic-gate 		 * apply that directive to all members of the block.
4247c478bd9Sstevel@tonic-gate 		 *
4257c478bd9Sstevel@tonic-gate 		 * If the first stmt in the file was an 'IGNORE all' or
4267c478bd9Sstevel@tonic-gate 		 * 'IGNORE contents', we need to keep track of it and
4277c478bd9Sstevel@tonic-gate 		 * automatically switch off contents checking for new
4287c478bd9Sstevel@tonic-gate 		 * subtrees.
4297c478bd9Sstevel@tonic-gate 		 */
4307c478bd9Sstevel@tonic-gate 		if (s[0] == '/') {
4317c478bd9Sstevel@tonic-gate 			new_rule = add_subtree_rule(s, reloc_root, create,
4327c478bd9Sstevel@tonic-gate 			    &ret_code);
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 			s = lex(0);
4357c478bd9Sstevel@tonic-gate 			while ((s != NULL) && (*s != 0) && (*s != '#')) {
4367c478bd9Sstevel@tonic-gate 				add_modifier(new_rule, s);
4377c478bd9Sstevel@tonic-gate 				s = lex(0);
4387c478bd9Sstevel@tonic-gate 			}
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 			/* Found a new block, keep track of the beginning */
4417c478bd9Sstevel@tonic-gate 			if (block_begin == NULL ||
4427c478bd9Sstevel@tonic-gate 			    (ignore_flag != 0) || (check_flag != 0)) {
4437c478bd9Sstevel@tonic-gate 				block_begin = new_rule;
4447c478bd9Sstevel@tonic-gate 				check_flag = 0;
4457c478bd9Sstevel@tonic-gate 				ignore_flag = 0;
4467c478bd9Sstevel@tonic-gate 			}
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 			/* Apply global settings to this block, if any */
4497c478bd9Sstevel@tonic-gate 			init_rule(in_flags, new_rule);
4507c478bd9Sstevel@tonic-gate 		} else if (IGNORE_KEYWORD(s) || CHECK_KEYWORD(s)) {
4517c478bd9Sstevel@tonic-gate 			int check_kw;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 			if (IGNORE_KEYWORD(s)) {
4547c478bd9Sstevel@tonic-gate 				ignore_flag++;
4557c478bd9Sstevel@tonic-gate 				check_kw = 0;
4567c478bd9Sstevel@tonic-gate 			} else {
4577c478bd9Sstevel@tonic-gate 				check_flag++;
4587c478bd9Sstevel@tonic-gate 				check_kw = 1;
4597c478bd9Sstevel@tonic-gate 			}
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 			/* Parse next token */
4627c478bd9Sstevel@tonic-gate 			s = lex(0);
4637c478bd9Sstevel@tonic-gate 			while ((s != NULL) && (*s != 0) && (*s != '#')) {
4647c478bd9Sstevel@tonic-gate 				akp = attr_keylookup(s);
4657c478bd9Sstevel@tonic-gate 				if (akp == NULL) {
4667c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, SYNTAX_ERR, s);
4677c478bd9Sstevel@tonic-gate 					syntax_err++;
4687c478bd9Sstevel@tonic-gate 					exit(2);
4697c478bd9Sstevel@tonic-gate 				}
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 				/*
4727c478bd9Sstevel@tonic-gate 				 * For all the flags, check if this is a global
4737c478bd9Sstevel@tonic-gate 				 * IGNORE/CHECK. If so, set the global flag.
4747c478bd9Sstevel@tonic-gate 				 *
4757c478bd9Sstevel@tonic-gate 				 * NOTE: The only time you can have a
4767c478bd9Sstevel@tonic-gate 				 * global ignore is when its the
4777c478bd9Sstevel@tonic-gate 				 * stmt before any blocks have been
4787c478bd9Sstevel@tonic-gate 				 * spec'd.
4797c478bd9Sstevel@tonic-gate 				 */
4807c478bd9Sstevel@tonic-gate 				if (block_begin == NULL) {
4817c478bd9Sstevel@tonic-gate 					if (check_kw)
4827c478bd9Sstevel@tonic-gate 						in_flags |= akp->ak_flags;
4837c478bd9Sstevel@tonic-gate 					else
4847c478bd9Sstevel@tonic-gate 						in_flags &= ~(akp->ak_flags);
4857c478bd9Sstevel@tonic-gate 				} else {
4867c478bd9Sstevel@tonic-gate 					for (rp = block_begin; rp != NULL;
4877c478bd9Sstevel@tonic-gate 					    rp = rp->next) {
4887c478bd9Sstevel@tonic-gate 						if (check_kw)
4897c478bd9Sstevel@tonic-gate 							rp->attr_list |=
4907c478bd9Sstevel@tonic-gate 							    akp->ak_flags;
4917c478bd9Sstevel@tonic-gate 						else
4927c478bd9Sstevel@tonic-gate 							rp->attr_list &=
4937c478bd9Sstevel@tonic-gate 							    ~(akp->ak_flags);
4947c478bd9Sstevel@tonic-gate 					}
4957c478bd9Sstevel@tonic-gate 				}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 				/* Parse next token */
4987c478bd9Sstevel@tonic-gate 				s = lex(0);
4997c478bd9Sstevel@tonic-gate 			}
5007c478bd9Sstevel@tonic-gate 		} else {
5017c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, SYNTAX_ERR, s);
5027c478bd9Sstevel@tonic-gate 			s = lex(0);
5037c478bd9Sstevel@tonic-gate 			while (s != NULL && *s != 0) {
5047c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, " %s", s);
5057c478bd9Sstevel@tonic-gate 				s = lex(0);
5067c478bd9Sstevel@tonic-gate 			}
5077c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "\n");
5087c478bd9Sstevel@tonic-gate 			syntax_err++;
5097c478bd9Sstevel@tonic-gate 		}
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	(void) fclose(file);
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	if (syntax_err) {
5157c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, SYNTAX_ABORT);
5167c478bd9Sstevel@tonic-gate 		exit(2);
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	return (ret_code);
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate static void
5237c478bd9Sstevel@tonic-gate add_modifier(struct rule *rule, char *modifier_str)
5247c478bd9Sstevel@tonic-gate {
5257c478bd9Sstevel@tonic-gate 	struct tree_modifier	*new_mod_ptr, *curr_mod_ptr;
5267c478bd9Sstevel@tonic-gate 	struct rule		*this_rule;
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	this_rule = rule;
5297c478bd9Sstevel@tonic-gate 	while (this_rule != NULL) {
5307c478bd9Sstevel@tonic-gate 		new_mod_ptr = gen_tree_modifier();
5317c478bd9Sstevel@tonic-gate 		new_mod_ptr->mod_str = safe_strdup(modifier_str);
5327c478bd9Sstevel@tonic-gate 		/* Next, see if the pattern is an include or an exclude */
5337c478bd9Sstevel@tonic-gate 		if (new_mod_ptr->mod_str[0] == '!') {
5347c478bd9Sstevel@tonic-gate 			new_mod_ptr->mod_str = (new_mod_ptr->mod_str + 1);
5357c478bd9Sstevel@tonic-gate 			new_mod_ptr->include = B_FALSE;
5367c478bd9Sstevel@tonic-gate 		} else {
5377c478bd9Sstevel@tonic-gate 			new_mod_ptr->include = B_TRUE;
5387c478bd9Sstevel@tonic-gate 		}
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 		if (this_rule->modifiers == NULL)
5417c478bd9Sstevel@tonic-gate 			this_rule->modifiers = new_mod_ptr;
5427c478bd9Sstevel@tonic-gate 		else {
5437c478bd9Sstevel@tonic-gate 			curr_mod_ptr = this_rule->modifiers;
5447c478bd9Sstevel@tonic-gate 			while (curr_mod_ptr->next != NULL)
5457c478bd9Sstevel@tonic-gate 				curr_mod_ptr = curr_mod_ptr->next;
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 			curr_mod_ptr->next = new_mod_ptr;
5487c478bd9Sstevel@tonic-gate 		}
5497c478bd9Sstevel@tonic-gate 		this_rule = this_rule->next;
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate /*
5547c478bd9Sstevel@tonic-gate  * This funtion is invoked when reading rulesfiles.  A subtree may have
5557c478bd9Sstevel@tonic-gate  * wildcards in it, e.g., '/home/n*', which is expected to match all home
5567c478bd9Sstevel@tonic-gate  * dirs which start with an 'n'.
5577c478bd9Sstevel@tonic-gate  *
5587c478bd9Sstevel@tonic-gate  * This function needs to break down the subtree into its components.  For
5597c478bd9Sstevel@tonic-gate  * each component, see how many directories match.  Take the subtree list just
5607c478bd9Sstevel@tonic-gate  * generated and run it through again, this time looking at the next component.
5617c478bd9Sstevel@tonic-gate  * At each iteration, keep a linked list of subtrees that currently match.
5627c478bd9Sstevel@tonic-gate  * Once the final list is created, invoke add_single_rule() to create the
5637c478bd9Sstevel@tonic-gate  * rule struct with the correct information.
5647c478bd9Sstevel@tonic-gate  *
5657c478bd9Sstevel@tonic-gate  * This function returns a ptr to the first element in the block of subtrees
5667c478bd9Sstevel@tonic-gate  * which matched the subtree def'n in the rulesfile.
5677c478bd9Sstevel@tonic-gate  */
5687c478bd9Sstevel@tonic-gate static struct rule *
5697c478bd9Sstevel@tonic-gate add_subtree_rule(char *rule, char *reloc_root, int create, int *err_code)
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate 	char			full_path[PATH_MAX], pattern[PATH_MAX],
5727c478bd9Sstevel@tonic-gate 				new_dirname[PATH_MAX], *beg_pattern,
5737c478bd9Sstevel@tonic-gate 				*end_pattern, *curr_dirname;
5747c478bd9Sstevel@tonic-gate 	struct	dir_component	*current_level = NULL, *next_level = NULL,
5757c478bd9Sstevel@tonic-gate 				*tmp_ptr;
5767c478bd9Sstevel@tonic-gate 	DIR			*dir_ptr;
5777c478bd9Sstevel@tonic-gate 	struct dirent		*dir_entry;
5787c478bd9Sstevel@tonic-gate 	struct rule		*begin_rule = NULL;
5797c478bd9Sstevel@tonic-gate 	int			ret;
5807c478bd9Sstevel@tonic-gate 	struct stat64		statb;
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	(void) snprintf(full_path, sizeof (full_path),
5837c478bd9Sstevel@tonic-gate 	    (rule[0] == '/') ? "%s%s" : "%s/%s", reloc_root, rule);
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	/*
5867c478bd9Sstevel@tonic-gate 	 * In the case of 'bart compare', don't validate
5877c478bd9Sstevel@tonic-gate 	 * the subtrees, since the machine running the
5887c478bd9Sstevel@tonic-gate 	 * comparison may not be the machine which generated
5897c478bd9Sstevel@tonic-gate 	 * the manifest.
5907c478bd9Sstevel@tonic-gate 	 */
5917c478bd9Sstevel@tonic-gate 	if (create == 0)
5927c478bd9Sstevel@tonic-gate 		return (add_single_rule(full_path));
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	/* Insert 'current_level' into the linked list */
5967c478bd9Sstevel@tonic-gate 	add_dir(&current_level, NULL);
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	/* Special case: occurs when -R is "/" and the subtree is "/" */
5997c478bd9Sstevel@tonic-gate 	if (strcmp(full_path, "/") == 0)
6007c478bd9Sstevel@tonic-gate 		(void) strcpy(current_level->dirname, "/");
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	beg_pattern = full_path;
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	while (beg_pattern != NULL) {
6057c478bd9Sstevel@tonic-gate 		/*
6067c478bd9Sstevel@tonic-gate 		 * Extract the pathname component starting at 'beg_pattern'.
6077c478bd9Sstevel@tonic-gate 		 * Take those chars and put them into 'pattern'.
6087c478bd9Sstevel@tonic-gate 		 */
6097c478bd9Sstevel@tonic-gate 		while (*beg_pattern == '/')
6107c478bd9Sstevel@tonic-gate 			beg_pattern++;
6117c478bd9Sstevel@tonic-gate 		if (*beg_pattern == '\0')	/* end of pathname */
6127c478bd9Sstevel@tonic-gate 			break;
6137c478bd9Sstevel@tonic-gate 		end_pattern = strchr(beg_pattern, '/');
6147c478bd9Sstevel@tonic-gate 		if (end_pattern != NULL)
6157c478bd9Sstevel@tonic-gate 			(void) strlcpy(pattern, beg_pattern,
6167c478bd9Sstevel@tonic-gate 			    end_pattern - beg_pattern + 1);
6177c478bd9Sstevel@tonic-gate 		else
6187c478bd9Sstevel@tonic-gate 			(void) strlcpy(pattern, beg_pattern, sizeof (pattern));
6197c478bd9Sstevel@tonic-gate 		beg_pattern = end_pattern;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 		/*
6227c478bd9Sstevel@tonic-gate 		 * At this point, search for 'pattern' as a *subdirectory* of
6237c478bd9Sstevel@tonic-gate 		 * the dirs in the linked list.
6247c478bd9Sstevel@tonic-gate 		 */
6257c478bd9Sstevel@tonic-gate 		while (current_level != NULL) {
6267c478bd9Sstevel@tonic-gate 			/* curr_dirname used to make the code more readable */
6277c478bd9Sstevel@tonic-gate 			curr_dirname = current_level->dirname;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 			/* Initialization case */
6307c478bd9Sstevel@tonic-gate 			if (strlen(curr_dirname) == 0)
6317c478bd9Sstevel@tonic-gate 				(void) strcpy(curr_dirname, "/");
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 			/* Open up the dir for this element in the list */
6347c478bd9Sstevel@tonic-gate 			dir_ptr = opendir(curr_dirname);
6357c478bd9Sstevel@tonic-gate 			dir_entry = NULL;
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 			if (dir_ptr == NULL) {
6387c478bd9Sstevel@tonic-gate 				perror(curr_dirname);
6397c478bd9Sstevel@tonic-gate 				*err_code = WARNING_EXIT;
6407c478bd9Sstevel@tonic-gate 			} else
6417c478bd9Sstevel@tonic-gate 				dir_entry = readdir(dir_ptr);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 			/*
6447c478bd9Sstevel@tonic-gate 			 * Now iterate through the subdirs of 'curr_dirname'
6457c478bd9Sstevel@tonic-gate 			 * In the case of a match against 'pattern',
6467c478bd9Sstevel@tonic-gate 			 * add the path to the next linked list, which
6477c478bd9Sstevel@tonic-gate 			 * will be matched on the next iteration.
6487c478bd9Sstevel@tonic-gate 			 */
6497c478bd9Sstevel@tonic-gate 			while (dir_entry != NULL) {
6507c478bd9Sstevel@tonic-gate 				/* Skip the dirs "." and ".." */
6517c478bd9Sstevel@tonic-gate 				if ((strcmp(dir_entry->d_name, ".") == 0) ||
6527c478bd9Sstevel@tonic-gate 				    (strcmp(dir_entry->d_name, "..") == 0)) {
6537c478bd9Sstevel@tonic-gate 					dir_entry = readdir(dir_ptr);
6547c478bd9Sstevel@tonic-gate 					continue;
6557c478bd9Sstevel@tonic-gate 				}
6567c478bd9Sstevel@tonic-gate 				if (fnmatch(pattern, dir_entry->d_name,
6577c478bd9Sstevel@tonic-gate 				    FNM_PATHNAME) == 0) {
6587c478bd9Sstevel@tonic-gate 					/*
6597c478bd9Sstevel@tonic-gate 					 * Build 'new_dirname' which will be
6607c478bd9Sstevel@tonic-gate 					 * examined on the next iteration.
6617c478bd9Sstevel@tonic-gate 					 */
6627c478bd9Sstevel@tonic-gate 					if (curr_dirname[strlen(curr_dirname)-1]
6637c478bd9Sstevel@tonic-gate 									!= '/')
6647c478bd9Sstevel@tonic-gate 						(void) snprintf(new_dirname,
6657c478bd9Sstevel@tonic-gate 						    sizeof (new_dirname),
6667c478bd9Sstevel@tonic-gate 						    "%s/%s", curr_dirname,
6677c478bd9Sstevel@tonic-gate 						    dir_entry->d_name);
6687c478bd9Sstevel@tonic-gate 					else
6697c478bd9Sstevel@tonic-gate 						(void) snprintf(new_dirname,
6707c478bd9Sstevel@tonic-gate 						    sizeof (new_dirname),
6717c478bd9Sstevel@tonic-gate 						    "%s%s", curr_dirname,
6727c478bd9Sstevel@tonic-gate 						    dir_entry->d_name);
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 					/* Add to the next lined list */
6757c478bd9Sstevel@tonic-gate 					add_dir(&next_level, new_dirname);
6767c478bd9Sstevel@tonic-gate 				}
6777c478bd9Sstevel@tonic-gate 				dir_entry = readdir(dir_ptr);
6787c478bd9Sstevel@tonic-gate 			}
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 			/* Close directory */
6817c478bd9Sstevel@tonic-gate 			if (dir_ptr != NULL)
6827c478bd9Sstevel@tonic-gate 				(void) closedir(dir_ptr);
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 			/* Free this entry and move on.... */
6857c478bd9Sstevel@tonic-gate 			tmp_ptr = current_level;
6867c478bd9Sstevel@tonic-gate 			current_level = current_level->next;
6877c478bd9Sstevel@tonic-gate 			free(tmp_ptr);
6887c478bd9Sstevel@tonic-gate 		}
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 		/*
6917c478bd9Sstevel@tonic-gate 		 * OK, done with this level.  Move to the next level and
6927c478bd9Sstevel@tonic-gate 		 * advance the ptrs which indicate the component name.
6937c478bd9Sstevel@tonic-gate 		 */
6947c478bd9Sstevel@tonic-gate 		current_level = next_level;
6957c478bd9Sstevel@tonic-gate 		next_level = NULL;
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	tmp_ptr = current_level;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	/* Error case: the subtree doesn't exist! */
7017c478bd9Sstevel@tonic-gate 	if (current_level == NULL) {
7027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, INVALID_SUBTREE, full_path);
7037c478bd9Sstevel@tonic-gate 		*err_code = WARNING_EXIT;
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	/*
7077c478bd9Sstevel@tonic-gate 	 * Iterate through all the dirnames which match the pattern and
7087c478bd9Sstevel@tonic-gate 	 * add them to to global list of subtrees which must be examined.
7097c478bd9Sstevel@tonic-gate 	 */
7107c478bd9Sstevel@tonic-gate 	while (current_level != NULL) {
7117c478bd9Sstevel@tonic-gate 		/*
7127c478bd9Sstevel@tonic-gate 		 * Sanity check for 'bart create', make sure the subtree
7137c478bd9Sstevel@tonic-gate 		 * points to a valid object.
7147c478bd9Sstevel@tonic-gate 		 */
7157c478bd9Sstevel@tonic-gate 		ret = lstat64(current_level->dirname, &statb);
7167c478bd9Sstevel@tonic-gate 		if (ret < 0) {
7177c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, INVALID_SUBTREE,
7187c478bd9Sstevel@tonic-gate 			    current_level->dirname);
7197c478bd9Sstevel@tonic-gate 			current_level = current_level->next;
7207c478bd9Sstevel@tonic-gate 			*err_code = WARNING_EXIT;
7217c478bd9Sstevel@tonic-gate 			continue;
7227c478bd9Sstevel@tonic-gate 		}
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 		if (begin_rule == NULL) {
7257c478bd9Sstevel@tonic-gate 			begin_rule =
7267c478bd9Sstevel@tonic-gate 			    add_single_rule(current_level->dirname);
7277c478bd9Sstevel@tonic-gate 		} else
7287c478bd9Sstevel@tonic-gate 			(void) add_single_rule(current_level->dirname);
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 		current_level = current_level->next;
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	/*
7347c478bd9Sstevel@tonic-gate 	 * Free up the memory and return a ptr to the first entry in the
7357c478bd9Sstevel@tonic-gate 	 * subtree block.  This is necessary for the parser, which may need
7367c478bd9Sstevel@tonic-gate 	 * to add modifier strings to all the elements in this block.
7377c478bd9Sstevel@tonic-gate 	 */
7387c478bd9Sstevel@tonic-gate 	dirs_cleanup(tmp_ptr);
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	return (begin_rule);
7417c478bd9Sstevel@tonic-gate }
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate /*
7457c478bd9Sstevel@tonic-gate  * Add a single entry to the linked list of rules to be read.  Does not do
7467c478bd9Sstevel@tonic-gate  * the wildcard expansion of 'add_subtree_rule', so is much simpler.
7477c478bd9Sstevel@tonic-gate  */
7487c478bd9Sstevel@tonic-gate static struct rule *
7497c478bd9Sstevel@tonic-gate add_single_rule(char *path)
7507c478bd9Sstevel@tonic-gate {
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	/*
7537c478bd9Sstevel@tonic-gate 	 * If the rules list does NOT exist, then create it.
7547c478bd9Sstevel@tonic-gate 	 * If the rules list does exist, then traverse the next element.
7557c478bd9Sstevel@tonic-gate 	 */
7567c478bd9Sstevel@tonic-gate 	if (first_rule == NULL) {
7577c478bd9Sstevel@tonic-gate 		first_rule = gen_rulestruct();
7587c478bd9Sstevel@tonic-gate 		current_rule = first_rule;
7597c478bd9Sstevel@tonic-gate 	} else {
7607c478bd9Sstevel@tonic-gate 		current_rule->next = gen_rulestruct();
7617c478bd9Sstevel@tonic-gate 		current_rule->next->prev = current_rule;
7627c478bd9Sstevel@tonic-gate 		current_rule = current_rule->next;
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	/* Setup the rule struct, handle relocatable roots, i.e. '-R' option */
7667c478bd9Sstevel@tonic-gate 	(void) strlcpy(current_rule->subtree, path,
7677c478bd9Sstevel@tonic-gate 	    sizeof (current_rule->subtree));
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	return (current_rule);
7707c478bd9Sstevel@tonic-gate }
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate /*
7737c478bd9Sstevel@tonic-gate  * Code stolen from filesync utility, used by read_rules() to read in the
7747c478bd9Sstevel@tonic-gate  * rulesfile.
7757c478bd9Sstevel@tonic-gate  */
7767c478bd9Sstevel@tonic-gate static char *
7777c478bd9Sstevel@tonic-gate lex(FILE *file)
7787c478bd9Sstevel@tonic-gate {
7797c478bd9Sstevel@tonic-gate 	char c, delim;
7807c478bd9Sstevel@tonic-gate 	char *p;
7817c478bd9Sstevel@tonic-gate 	char *s;
7827c478bd9Sstevel@tonic-gate 	static char *savep;
7837c478bd9Sstevel@tonic-gate 	static char namebuf[ BUF_SIZE ];
7847c478bd9Sstevel@tonic-gate 	static char inbuf[ BUF_SIZE ];
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	if (file) {			/* read a new line		*/
7877c478bd9Sstevel@tonic-gate 		p = inbuf + sizeof (inbuf);
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 		s = inbuf;
7907c478bd9Sstevel@tonic-gate 		/* read the next input line, with all continuations	*/
7917c478bd9Sstevel@tonic-gate 		while (savep = fgets(s, p - s, file)) {
7927c478bd9Sstevel@tonic-gate 			lex_linenum++;
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 			/* go find the last character of the input line	*/
7957c478bd9Sstevel@tonic-gate 			while (*s && s[1])
7967c478bd9Sstevel@tonic-gate 				s++;
7977c478bd9Sstevel@tonic-gate 			if (*s == '\n')
7987c478bd9Sstevel@tonic-gate 				s--;
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 			/* see whether or not we need a continuation	*/
8017c478bd9Sstevel@tonic-gate 			if (s < inbuf || *s != '\\')
8027c478bd9Sstevel@tonic-gate 				break;
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 			continue;
8057c478bd9Sstevel@tonic-gate 		}
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 		if (savep == NULL)
8087c478bd9Sstevel@tonic-gate 			return (0);
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 		s = inbuf;
8117c478bd9Sstevel@tonic-gate 	} else {			/* continue with old line	*/
8127c478bd9Sstevel@tonic-gate 		if (savep == NULL)
8137c478bd9Sstevel@tonic-gate 			return (0);
8147c478bd9Sstevel@tonic-gate 		s = savep;
8157c478bd9Sstevel@tonic-gate 	}
8167c478bd9Sstevel@tonic-gate 	savep = NULL;
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	/* skip over leading white space	*/
8197c478bd9Sstevel@tonic-gate 	while (isspace(*s))
8207c478bd9Sstevel@tonic-gate 		s++;
8217c478bd9Sstevel@tonic-gate 	if (*s == 0)
8227c478bd9Sstevel@tonic-gate 		return (0);
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	/* see if this is a quoted string	*/
8257c478bd9Sstevel@tonic-gate 	c = *s;
8267c478bd9Sstevel@tonic-gate 	if (c == '\'' || c == '"') {
8277c478bd9Sstevel@tonic-gate 		delim = c;
8287c478bd9Sstevel@tonic-gate 		s++;
8297c478bd9Sstevel@tonic-gate 	} else
8307c478bd9Sstevel@tonic-gate 		delim = 0;
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	/* copy the token into the buffer	*/
8337c478bd9Sstevel@tonic-gate 	for (p = namebuf; (c = *s) != 0; s++) {
8347c478bd9Sstevel@tonic-gate 		/* literal escape		*/
8357c478bd9Sstevel@tonic-gate 		if (c == '\\') {
8367c478bd9Sstevel@tonic-gate 			s++;
8377c478bd9Sstevel@tonic-gate 			*p++ = *s;
8387c478bd9Sstevel@tonic-gate 			continue;
8397c478bd9Sstevel@tonic-gate 		}
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 		/* closing delimiter		*/
8427c478bd9Sstevel@tonic-gate 		if (c == delim) {
8437c478bd9Sstevel@tonic-gate 			s++;
8447c478bd9Sstevel@tonic-gate 			break;
8457c478bd9Sstevel@tonic-gate 		}
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 		/* delimiting white space	*/
8487c478bd9Sstevel@tonic-gate 		if (delim == 0 && isspace(c))
8497c478bd9Sstevel@tonic-gate 			break;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 		/* ordinary characters		*/
8527c478bd9Sstevel@tonic-gate 		*p++ = *s;
8537c478bd9Sstevel@tonic-gate 	}
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	/* remember where we left off		*/
8577c478bd9Sstevel@tonic-gate 	savep = *s ? s : 0;
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	/* null terminate and return the buffer	*/
8607c478bd9Sstevel@tonic-gate 	*p = 0;
8617c478bd9Sstevel@tonic-gate 	return (namebuf);
8627c478bd9Sstevel@tonic-gate }
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate /*
8657c478bd9Sstevel@tonic-gate  * Iterate through the dir strcutures and free memory.
8667c478bd9Sstevel@tonic-gate  */
8677c478bd9Sstevel@tonic-gate static void
8687c478bd9Sstevel@tonic-gate dirs_cleanup(struct dir_component *dir)
8697c478bd9Sstevel@tonic-gate {
8707c478bd9Sstevel@tonic-gate 	struct	dir_component	*next;
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	while (dir != NULL) {
8737c478bd9Sstevel@tonic-gate 		next = dir->next;
8747c478bd9Sstevel@tonic-gate 		free(dir);
8757c478bd9Sstevel@tonic-gate 		dir = next;
8767c478bd9Sstevel@tonic-gate 	}
8777c478bd9Sstevel@tonic-gate }
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate /*
8807c478bd9Sstevel@tonic-gate  * Create and initialize a new dir structure.  Used by add_subtree_rule() when
8817c478bd9Sstevel@tonic-gate  * doing expansion of directory names caused by wildcards.
8827c478bd9Sstevel@tonic-gate  */
8837c478bd9Sstevel@tonic-gate static void
8847c478bd9Sstevel@tonic-gate add_dir(struct dir_component **dir, char *dirname)
8857c478bd9Sstevel@tonic-gate {
8867c478bd9Sstevel@tonic-gate 	struct	dir_component	*new, *next_dir;
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	new = gen_dir_component();
8897c478bd9Sstevel@tonic-gate 	if (dirname != NULL)
8907c478bd9Sstevel@tonic-gate 		(void) strlcpy(new->dirname, dirname, sizeof (new->dirname));
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	if (*dir == NULL)
8937c478bd9Sstevel@tonic-gate 		*dir = new;
8947c478bd9Sstevel@tonic-gate 	else {
8957c478bd9Sstevel@tonic-gate 		next_dir = *dir;
8967c478bd9Sstevel@tonic-gate 		while (next_dir->next != NULL)
8977c478bd9Sstevel@tonic-gate 			next_dir = next_dir->next;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 		next_dir->next = new;
9007c478bd9Sstevel@tonic-gate 	}
9017c478bd9Sstevel@tonic-gate }
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate /*
9047c478bd9Sstevel@tonic-gate  * Traverse the linked list of rules in a REVERSE order.
9057c478bd9Sstevel@tonic-gate  */
9067c478bd9Sstevel@tonic-gate static struct rule *
9077c478bd9Sstevel@tonic-gate get_last_entry(boolean_t reset)
9087c478bd9Sstevel@tonic-gate {
9097c478bd9Sstevel@tonic-gate 	static struct rule	*curr_root = NULL;
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	if (reset) {
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 		curr_root = first_rule;
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 		/* RESET: set cur_root to the end of the list */
9167c478bd9Sstevel@tonic-gate 		while (curr_root != NULL)
9177c478bd9Sstevel@tonic-gate 			if (curr_root->next == NULL)
9187c478bd9Sstevel@tonic-gate 				break;
9197c478bd9Sstevel@tonic-gate 			else
9207c478bd9Sstevel@tonic-gate 				curr_root = curr_root->next;
9217c478bd9Sstevel@tonic-gate 	} else
9227c478bd9Sstevel@tonic-gate 		curr_root = (curr_root->prev);
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	return (curr_root);
9257c478bd9Sstevel@tonic-gate }
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate /*
9287c478bd9Sstevel@tonic-gate  * Traverse the first entry, used by 'bart create' to iterate through
9297c478bd9Sstevel@tonic-gate  * subtrees or individual filenames.
9307c478bd9Sstevel@tonic-gate  */
9317c478bd9Sstevel@tonic-gate struct rule *
9327c478bd9Sstevel@tonic-gate get_first_subtree()
9337c478bd9Sstevel@tonic-gate {
9347c478bd9Sstevel@tonic-gate 	return (first_rule);
9357c478bd9Sstevel@tonic-gate }
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate /*
9387c478bd9Sstevel@tonic-gate  * Traverse the next entry, used by 'bart create' to iterate through
9397c478bd9Sstevel@tonic-gate  * subtrees or individual filenames.
9407c478bd9Sstevel@tonic-gate  */
9417c478bd9Sstevel@tonic-gate struct rule *
9427c478bd9Sstevel@tonic-gate get_next_subtree(struct rule *entry)
9437c478bd9Sstevel@tonic-gate {
9447c478bd9Sstevel@tonic-gate 	return (entry->next);
9457c478bd9Sstevel@tonic-gate }
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate char *
9487c478bd9Sstevel@tonic-gate safe_strdup(char *s)
9497c478bd9Sstevel@tonic-gate {
9507c478bd9Sstevel@tonic-gate 	char *ret;
9517c478bd9Sstevel@tonic-gate 	size_t len;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	len = strlen(s) + 1;
9547c478bd9Sstevel@tonic-gate 	ret = safe_calloc(len);
9557c478bd9Sstevel@tonic-gate 	(void) strlcpy(ret, s, len);
9567c478bd9Sstevel@tonic-gate 	return (ret);
9577c478bd9Sstevel@tonic-gate }
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate /*
9607c478bd9Sstevel@tonic-gate  * Function to match a filename against the subtrees in the link list
9617c478bd9Sstevel@tonic-gate  * of 'rule' strcutures.  Upon finding a matching rule, see if it should
9627c478bd9Sstevel@tonic-gate  * be excluded.  Keep going until a match is found OR all rules have been
9637c478bd9Sstevel@tonic-gate  * exhausted.
9647c478bd9Sstevel@tonic-gate  * NOTES: Rules are parsed in reverse;
9657c478bd9Sstevel@tonic-gate  * satisfies the spec that "Last rule wins".  Also, the default rule should
9667c478bd9Sstevel@tonic-gate  * always match, so this function should NEVER return NULL.
9677c478bd9Sstevel@tonic-gate  */
9687c478bd9Sstevel@tonic-gate struct rule *
9697c478bd9Sstevel@tonic-gate check_rules(const char *fname, char type)
9707c478bd9Sstevel@tonic-gate {
9717c478bd9Sstevel@tonic-gate 	struct rule		*root;
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	root = get_last_entry(B_TRUE);
9747c478bd9Sstevel@tonic-gate 	while (root != NULL) {
9757c478bd9Sstevel@tonic-gate 		if (match_subtree(fname, root->subtree)) {
9767c478bd9Sstevel@tonic-gate 			if (exclude_fname(fname, type, root) == 0)
9777c478bd9Sstevel@tonic-gate 				break;
9787c478bd9Sstevel@tonic-gate 		}
9797c478bd9Sstevel@tonic-gate 		root = get_last_entry(B_FALSE);
9807c478bd9Sstevel@tonic-gate 	}
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	return (root);
9837c478bd9Sstevel@tonic-gate }
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate /*
986*f7bbf134Shm123892  * Function to determine if an entry in a rules file (see bart_rules(4)) applies
987*f7bbf134Shm123892  * to a filename. We truncate "fname" such that it has the same number of
988*f7bbf134Shm123892  * components as "rule" and let fnmatch(3C) do the rest. A "component" is one
989*f7bbf134Shm123892  * part of an fname as delimited by slashes ('/'). So "/A/B/C/D" has four
990*f7bbf134Shm123892  * components: "A", "B", "C" and "D".
991*f7bbf134Shm123892  *
992*f7bbf134Shm123892  * For example:
993*f7bbf134Shm123892  *
994*f7bbf134Shm123892  * 1. the rule "/home/nickiso" applies to fname "/home/nickiso/src/foo.c" so
995*f7bbf134Shm123892  * should match.
996*f7bbf134Shm123892  *
997*f7bbf134Shm123892  * 2. the rule "/home/nickiso/temp/src" does not apply to fname
998*f7bbf134Shm123892  * "/home/nickiso/foo.c" so should not match.
9997c478bd9Sstevel@tonic-gate  */
10007c478bd9Sstevel@tonic-gate static int
10017c478bd9Sstevel@tonic-gate match_subtree(const char *fname, char *rule)
10027c478bd9Sstevel@tonic-gate {
1003*f7bbf134Shm123892 	int	match, num_rule_slash;
1004*f7bbf134Shm123892 	char	*ptr, fname_cp[PATH_MAX];
10057c478bd9Sstevel@tonic-gate 
1006*f7bbf134Shm123892 	/* If rule has more components than fname, it cannot match. */
1007*f7bbf134Shm123892 	if ((num_rule_slash = count_slashes(rule)) > count_slashes(fname))
1008*f7bbf134Shm123892 		return (0);
1009*f7bbf134Shm123892 
1010*f7bbf134Shm123892 	/* Create a copy of fname that we can truncate. */
1011*f7bbf134Shm123892 	(void) strlcpy(fname_cp, fname, sizeof (fname_cp));
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	/*
1014*f7bbf134Shm123892 	 * Truncate fname_cp such that it has the same number of components
1015*f7bbf134Shm123892 	 * as rule. If rule ends with '/', so should fname_cp. ie:
10167c478bd9Sstevel@tonic-gate 	 *
1017*f7bbf134Shm123892 	 * rule		fname			fname_cp	matches
1018*f7bbf134Shm123892 	 * ----		-----			--------	-------
1019*f7bbf134Shm123892 	 * /home/dir*	/home/dir0/dir1/fileA	/home/dir0	yes
1020*f7bbf134Shm123892 	 * /home/dir/	/home/dir0/dir1/fileA	/home/dir0/	no
10217c478bd9Sstevel@tonic-gate 	 */
1022*f7bbf134Shm123892 	for (ptr = fname_cp; num_rule_slash > 0; num_rule_slash--, ptr++)
1023*f7bbf134Shm123892 		ptr = strchr(ptr, '/');
1024*f7bbf134Shm123892 	if (*(rule + strlen(rule) - 1) != '/') {
1025*f7bbf134Shm123892 		while (*ptr != '\0') {
1026*f7bbf134Shm123892 			if (*ptr == '/')
1027*f7bbf134Shm123892 				break;
1028*f7bbf134Shm123892 			ptr++;
1029*f7bbf134Shm123892 		}
1030*f7bbf134Shm123892 	}
1031*f7bbf134Shm123892 	*ptr = '\0';
10327c478bd9Sstevel@tonic-gate 
1033*f7bbf134Shm123892 	/* OK, now see if they match. */
1034*f7bbf134Shm123892 	match = fnmatch(rule, fname_cp, FNM_PATHNAME);
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	/* No match, return failure */
10377c478bd9Sstevel@tonic-gate 	if (match != 0)
10387c478bd9Sstevel@tonic-gate 		return (0);
10397c478bd9Sstevel@tonic-gate 	else
10407c478bd9Sstevel@tonic-gate 		return (1);
10417c478bd9Sstevel@tonic-gate }
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate void
10447c478bd9Sstevel@tonic-gate process_glob_ignores(char *ignore_list, uint_t *flags)
10457c478bd9Sstevel@tonic-gate {
10467c478bd9Sstevel@tonic-gate 	char	*cp;
10477c478bd9Sstevel@tonic-gate 	struct attr_keyword *akp;
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	if (ignore_list == NULL)
10507c478bd9Sstevel@tonic-gate 		usage();
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	cp = strtok(ignore_list, ",");
10537c478bd9Sstevel@tonic-gate 	while (cp != NULL) {
10547c478bd9Sstevel@tonic-gate 		akp = attr_keylookup(cp);
10557c478bd9Sstevel@tonic-gate 		if (akp == NULL)
10567c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ERROR: Invalid keyword %s\n",
10577c478bd9Sstevel@tonic-gate 			    cp);
10587c478bd9Sstevel@tonic-gate 		else
10597c478bd9Sstevel@tonic-gate 			*flags &= ~akp->ak_flags;
10607c478bd9Sstevel@tonic-gate 		cp = strtok(NULL, ",");
10617c478bd9Sstevel@tonic-gate 	}
10627c478bd9Sstevel@tonic-gate }
1063