xref: /illumos-gate/usr/src/cmd/bart/compare.c (revision 5f273229)
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 /*
237c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <unistd.h>
287c478bd9Sstevel@tonic-gate #include "bart.h"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate static int compare_manifests(FILE *rulesfile, char *control, char *test,
317c478bd9Sstevel@tonic-gate     boolean_t prog_fmt, uint_t flags);
327c478bd9Sstevel@tonic-gate static void extract_fname_ftype(char *line, char *fname, char *type);
337c478bd9Sstevel@tonic-gate static int report_add(char *fname, char *type);
347c478bd9Sstevel@tonic-gate static int report_delete(char *fname, char *type);
357c478bd9Sstevel@tonic-gate static int evaluate_differences(char *control_line, char *test_line,
367c478bd9Sstevel@tonic-gate     boolean_t prog_fmt, int flags);
377c478bd9Sstevel@tonic-gate static void report_error(char *fname, char *type, char *ctrl_val,
387c478bd9Sstevel@tonic-gate     char *test_val, boolean_t prog_fmt);
397c478bd9Sstevel@tonic-gate static int read_manifest_line(FILE *fd, char *buf, int buf_size, int start_pos,
407c478bd9Sstevel@tonic-gate     char **line, char *fname);
417c478bd9Sstevel@tonic-gate static void parse_line(char *line, char *fname, char *type, char *size,
427c478bd9Sstevel@tonic-gate     char *mode, char *acl, char *mtime, char *uid, char *gid, char *contents,
437c478bd9Sstevel@tonic-gate     char *devnode, char *dest);
447c478bd9Sstevel@tonic-gate static void init_default_flags(uint_t *flags);
457c478bd9Sstevel@tonic-gate static void get_token(char *line, int *curr_pos, int line_len, char *buf,
467c478bd9Sstevel@tonic-gate     int buf_size);
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate int
bart_compare(int argc,char ** argv)497c478bd9Sstevel@tonic-gate bart_compare(int argc, char **argv)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	char			*control_fname, *test_fname;
527c478bd9Sstevel@tonic-gate 	int			c;
537c478bd9Sstevel@tonic-gate 	FILE			*rules_fd = NULL;
54*5f273229SToomas Soome 	uint_t			glob_flags;
557c478bd9Sstevel@tonic-gate 	boolean_t		prog_fmt = B_FALSE;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	init_default_flags(&glob_flags);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "pr:i:")) != EOF) {
607c478bd9Sstevel@tonic-gate 		switch (c) {
617c478bd9Sstevel@tonic-gate 		case 'p':
627c478bd9Sstevel@tonic-gate 			prog_fmt = B_TRUE;
637c478bd9Sstevel@tonic-gate 			break;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 		case 'r':
667c478bd9Sstevel@tonic-gate 			if (optarg == NULL)
677c478bd9Sstevel@tonic-gate 				usage();
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 			if (strcmp(optarg, "-") == 0)
707c478bd9Sstevel@tonic-gate 				rules_fd = stdin;
717c478bd9Sstevel@tonic-gate 			else
727c478bd9Sstevel@tonic-gate 				rules_fd = fopen(optarg, "r");
737c478bd9Sstevel@tonic-gate 			if (rules_fd == NULL) {
747c478bd9Sstevel@tonic-gate 				perror(optarg);
757c478bd9Sstevel@tonic-gate 				usage();
767c478bd9Sstevel@tonic-gate 			}
777c478bd9Sstevel@tonic-gate 			break;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 		case 'i':
807c478bd9Sstevel@tonic-gate 			process_glob_ignores(optarg, &glob_flags);
817c478bd9Sstevel@tonic-gate 			break;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 		case '?':
847c478bd9Sstevel@tonic-gate 		default:
857c478bd9Sstevel@tonic-gate 			usage();
867c478bd9Sstevel@tonic-gate 		}
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	/* Make sure we have the right number of args */
907c478bd9Sstevel@tonic-gate 	if ((optind + 2) != argc)
917c478bd9Sstevel@tonic-gate 		usage();
927c478bd9Sstevel@tonic-gate 	argv += optind;
937c478bd9Sstevel@tonic-gate 	control_fname = argv[0];
947c478bd9Sstevel@tonic-gate 	test_fname = argv[1];
957c478bd9Sstevel@tonic-gate 	/* At this point, the filenames are sane, so do the comparison */
967c478bd9Sstevel@tonic-gate 	return (compare_manifests(rules_fd, control_fname, test_fname,
977c478bd9Sstevel@tonic-gate 	    prog_fmt, glob_flags));
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static int
compare_manifests(FILE * rulesfile,char * control,char * test,boolean_t prog_fmt,uint_t flags)1017c478bd9Sstevel@tonic-gate compare_manifests(FILE *rulesfile, char *control, char *test,
102*5f273229SToomas Soome     boolean_t prog_fmt, uint_t flags)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	FILE	*control_fd, *test_fd;
1057c478bd9Sstevel@tonic-gate 	char	*control_line, *test_line, control_buf[BUF_SIZE],
106*5f273229SToomas Soome 	    test_buf[BUF_SIZE], control_fname[PATH_MAX],
107*5f273229SToomas Soome 	    control_type[TYPE_SIZE], test_fname[PATH_MAX],
108*5f273229SToomas Soome 	    test_type[TYPE_SIZE];
1097c478bd9Sstevel@tonic-gate 	int	control_pos, test_pos, ret, fname_cmp, return_status;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	return_status = EXIT;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	return_status = read_rules(rulesfile, "", flags, 0);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	control_fd = fopen(control, "r");
1167c478bd9Sstevel@tonic-gate 	if (control_fd == NULL) {
1177c478bd9Sstevel@tonic-gate 		perror(control);
1187c478bd9Sstevel@tonic-gate 		return (FATAL_EXIT);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	test_fd = fopen(test, "r");
1227c478bd9Sstevel@tonic-gate 	if (test_fd == NULL) {
1237c478bd9Sstevel@tonic-gate 		perror(test);
1247c478bd9Sstevel@tonic-gate 		return (FATAL_EXIT);
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	control_pos = read_manifest_line(control_fd, control_buf,
1287c478bd9Sstevel@tonic-gate 	    BUF_SIZE, 0, &control_line, control);
1297c478bd9Sstevel@tonic-gate 	test_pos = read_manifest_line(test_fd, test_buf, BUF_SIZE, 0,
1307c478bd9Sstevel@tonic-gate 	    &test_line, test);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	while ((control_pos != -1) && (test_pos != -1)) {
1337c478bd9Sstevel@tonic-gate 		ret = strcmp(control_line, test_line);
1347c478bd9Sstevel@tonic-gate 		if (ret == 0) {
1357c478bd9Sstevel@tonic-gate 			/* Lines compare OK, just read the next lines.... */
1367c478bd9Sstevel@tonic-gate 			control_pos = read_manifest_line(control_fd,
1377c478bd9Sstevel@tonic-gate 			    control_buf, BUF_SIZE, control_pos, &control_line,
1387c478bd9Sstevel@tonic-gate 			    control);
1397c478bd9Sstevel@tonic-gate 			test_pos = read_manifest_line(test_fd, test_buf,
1407c478bd9Sstevel@tonic-gate 			    BUF_SIZE, test_pos, &test_line, test);
1417c478bd9Sstevel@tonic-gate 			continue;
1427c478bd9Sstevel@tonic-gate 		}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 		/*
1457c478bd9Sstevel@tonic-gate 		 * Something didn't compare properly.
1467c478bd9Sstevel@tonic-gate 		 */
1477c478bd9Sstevel@tonic-gate 		extract_fname_ftype(control_line, control_fname, control_type);
1487c478bd9Sstevel@tonic-gate 		extract_fname_ftype(test_line, test_fname, test_type);
1497c478bd9Sstevel@tonic-gate 		fname_cmp = strcmp(control_fname, test_fname);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 		if (fname_cmp == 0) {
1527c478bd9Sstevel@tonic-gate 			/*
1537c478bd9Sstevel@tonic-gate 			 * Filenames were the same, see what was
1547c478bd9Sstevel@tonic-gate 			 * different and continue.
1557c478bd9Sstevel@tonic-gate 			 */
1567c478bd9Sstevel@tonic-gate 			if (evaluate_differences(control_line, test_line,
1577c478bd9Sstevel@tonic-gate 			    prog_fmt, flags) != 0)
1587c478bd9Sstevel@tonic-gate 				return_status = WARNING_EXIT;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 			control_pos = read_manifest_line(control_fd,
1617c478bd9Sstevel@tonic-gate 			    control_buf, BUF_SIZE, control_pos, &control_line,
1627c478bd9Sstevel@tonic-gate 			    control);
1637c478bd9Sstevel@tonic-gate 			test_pos = read_manifest_line(test_fd, test_buf,
1647c478bd9Sstevel@tonic-gate 			    BUF_SIZE, test_pos, &test_line, test);
1657c478bd9Sstevel@tonic-gate 		} else if (fname_cmp > 0) {
1667c478bd9Sstevel@tonic-gate 			/* Filenames were different, a files was ADDED */
1677c478bd9Sstevel@tonic-gate 			if (report_add(test_fname, test_type)) {
1687c478bd9Sstevel@tonic-gate 				report_error(test_fname, ADD_KEYWORD, NULL,
1697c478bd9Sstevel@tonic-gate 				    NULL, prog_fmt);
1707c478bd9Sstevel@tonic-gate 				return_status = WARNING_EXIT;
1717c478bd9Sstevel@tonic-gate 			}
1727c478bd9Sstevel@tonic-gate 			test_pos = read_manifest_line(test_fd, test_buf,
1737c478bd9Sstevel@tonic-gate 			    BUF_SIZE, test_pos, &test_line, test);
1747c478bd9Sstevel@tonic-gate 		} else if (fname_cmp < 0) {
1757c478bd9Sstevel@tonic-gate 			/* Filenames were different, a files was DELETED */
1767c478bd9Sstevel@tonic-gate 			if (report_delete(control_fname, control_type)) {
1777c478bd9Sstevel@tonic-gate 				report_error(control_fname, DELETE_KEYWORD,
1787c478bd9Sstevel@tonic-gate 				    NULL, NULL, prog_fmt);
1797c478bd9Sstevel@tonic-gate 				return_status = WARNING_EXIT;
1807c478bd9Sstevel@tonic-gate 			}
1817c478bd9Sstevel@tonic-gate 			control_pos = read_manifest_line(control_fd,
1827c478bd9Sstevel@tonic-gate 			    control_buf, BUF_SIZE, control_pos, &control_line,
1837c478bd9Sstevel@tonic-gate 			    control);
1847c478bd9Sstevel@tonic-gate 		}
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	/*
1887c478bd9Sstevel@tonic-gate 	 * Entering this while loop means files were DELETED from the test
1897c478bd9Sstevel@tonic-gate 	 * manifest.
1907c478bd9Sstevel@tonic-gate 	 */
1917c478bd9Sstevel@tonic-gate 	while (control_pos != -1) {
1927c478bd9Sstevel@tonic-gate 		(void) sscanf(control_line, "%1023s", control_fname);
1937c478bd9Sstevel@tonic-gate 		if (report_delete(control_fname, control_type)) {
1947c478bd9Sstevel@tonic-gate 			report_error(control_fname, DELETE_KEYWORD, NULL,
1957c478bd9Sstevel@tonic-gate 			    NULL, prog_fmt);
1967c478bd9Sstevel@tonic-gate 			return_status = WARNING_EXIT;
1977c478bd9Sstevel@tonic-gate 		}
1987c478bd9Sstevel@tonic-gate 		control_pos = read_manifest_line(control_fd, control_buf,
1997c478bd9Sstevel@tonic-gate 		    BUF_SIZE, control_pos, &control_line, control);
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	/*
2037c478bd9Sstevel@tonic-gate 	 * Entering this while loop means files were ADDED to the test
2047c478bd9Sstevel@tonic-gate 	 * manifest.
2057c478bd9Sstevel@tonic-gate 	 */
2067c478bd9Sstevel@tonic-gate 	while (test_pos != -1) {
2077c478bd9Sstevel@tonic-gate 		(void) sscanf(test_line, "%1023s", test_fname);
2087c478bd9Sstevel@tonic-gate 		if (report_add(test_fname, test_type)) {
2097c478bd9Sstevel@tonic-gate 			report_error(test_fname, ADD_KEYWORD, NULL,
2107c478bd9Sstevel@tonic-gate 			    NULL, prog_fmt);
2117c478bd9Sstevel@tonic-gate 			return_status = WARNING_EXIT;
2127c478bd9Sstevel@tonic-gate 		}
2137c478bd9Sstevel@tonic-gate 		test_pos = read_manifest_line(test_fd, test_buf,
2147c478bd9Sstevel@tonic-gate 		    BUF_SIZE, test_pos, &test_line, test);
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	(void) fclose(control_fd);
2187c478bd9Sstevel@tonic-gate 	(void) fclose(test_fd);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	/* For programmatic mode, add a newline for cosmetic reasons */
2217c478bd9Sstevel@tonic-gate 	if (prog_fmt && (return_status != 0))
2227c478bd9Sstevel@tonic-gate 		(void) printf("\n");
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	return (return_status);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate static void
parse_line(char * line,char * fname,char * type,char * size,char * mode,char * acl,char * mtime,char * uid,char * gid,char * contents,char * devnode,char * dest)2287c478bd9Sstevel@tonic-gate parse_line(char *line, char *fname, char *type, char *size, char *mode,
229*5f273229SToomas Soome     char *acl, char *mtime, char *uid, char *gid, char *contents, char *devnode,
230*5f273229SToomas Soome     char *dest)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	int		pos, line_len;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	line_len = strlen(line);
2357c478bd9Sstevel@tonic-gate 	pos = 0;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	get_token(line, &pos, line_len, fname, PATH_MAX);
2387c478bd9Sstevel@tonic-gate 	get_token(line, &pos, line_len, type, TYPE_SIZE);
2397c478bd9Sstevel@tonic-gate 	get_token(line, &pos, line_len, size, MISC_SIZE);
2407c478bd9Sstevel@tonic-gate 	get_token(line, &pos, line_len, mode, MISC_SIZE);
2417c478bd9Sstevel@tonic-gate 	get_token(line, &pos, line_len, acl, ACL_SIZE);
2427c478bd9Sstevel@tonic-gate 	get_token(line, &pos, line_len, mtime, MISC_SIZE);
2437c478bd9Sstevel@tonic-gate 	get_token(line, &pos, line_len, uid, MISC_SIZE);
2447c478bd9Sstevel@tonic-gate 	get_token(line, &pos, line_len, gid, MISC_SIZE);
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	/* Reset these fields... */
2477c478bd9Sstevel@tonic-gate 
248*5f273229SToomas Soome 	*contents = '\0';
2497c478bd9Sstevel@tonic-gate 	*devnode = '\0';
2507c478bd9Sstevel@tonic-gate 	*dest = '\0';
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/* Handle filetypes which have a last field..... */
2537c478bd9Sstevel@tonic-gate 	if (type[0] == 'F')
2547c478bd9Sstevel@tonic-gate 		get_token(line, &pos, line_len, contents, PATH_MAX);
2557c478bd9Sstevel@tonic-gate 	else if ((type[0] == 'B') || (type[0] == 'C'))
2567c478bd9Sstevel@tonic-gate 		get_token(line, &pos, line_len, devnode, PATH_MAX);
2577c478bd9Sstevel@tonic-gate 	else if (type[0] == 'L')
2587c478bd9Sstevel@tonic-gate 		get_token(line, &pos, line_len, dest, PATH_MAX);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate static void
get_token(char * line,int * curr_pos,int line_len,char * buf,int buf_size)2627c478bd9Sstevel@tonic-gate get_token(char *line, int *curr_pos, int line_len, char *buf, int buf_size)
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate 	int	cnt = 0;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	while (isspace(line[*curr_pos]) && (*curr_pos < line_len))
2677c478bd9Sstevel@tonic-gate 		(*curr_pos)++;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	while (!isspace(line[*curr_pos]) &&
2707c478bd9Sstevel@tonic-gate 	    (*curr_pos < line_len) && (cnt < (buf_size-1))) {
2717c478bd9Sstevel@tonic-gate 		buf[cnt] = line[*curr_pos];
2727c478bd9Sstevel@tonic-gate 		(*curr_pos)++;
2737c478bd9Sstevel@tonic-gate 		cnt++;
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 	buf[cnt] = '\0';
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate /*
2797c478bd9Sstevel@tonic-gate  * Utility function: extract fname and type from this line
2807c478bd9Sstevel@tonic-gate  */
2817c478bd9Sstevel@tonic-gate static void
extract_fname_ftype(char * line,char * fname,char * type)2827c478bd9Sstevel@tonic-gate extract_fname_ftype(char *line, char *fname, char *type)
2837c478bd9Sstevel@tonic-gate {
2847c478bd9Sstevel@tonic-gate 	int		line_len, pos;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	pos = 0;
2877c478bd9Sstevel@tonic-gate 	line_len = strlen(line);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	get_token(line, &pos, line_len, fname, PATH_MAX);
2907c478bd9Sstevel@tonic-gate 	get_token(line, &pos, line_len, type, TYPE_SIZE);
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate /*
2947c478bd9Sstevel@tonic-gate  * Utility function: tells us whether or not this addition should be reported
2957c478bd9Sstevel@tonic-gate  *
2967c478bd9Sstevel@tonic-gate  * Returns 0 if the discrepancy is ignored, non-zero if the discrepancy is
2977c478bd9Sstevel@tonic-gate  * reported.
2987c478bd9Sstevel@tonic-gate  */
2997c478bd9Sstevel@tonic-gate static int
report_add(char * fname,char * type)3007c478bd9Sstevel@tonic-gate report_add(char *fname, char *type)
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate 	struct rule	*rule_ptr;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	rule_ptr = check_rules(fname, type[0]);
3057c478bd9Sstevel@tonic-gate 	if ((rule_ptr != NULL) && (rule_ptr->attr_list & ATTR_ADD))
3067c478bd9Sstevel@tonic-gate 		return (1);
3077c478bd9Sstevel@tonic-gate 	else
3087c478bd9Sstevel@tonic-gate 		return (0);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate /*
3127c478bd9Sstevel@tonic-gate  * Utility function: tells us whether or not this deletion should be reported
3137c478bd9Sstevel@tonic-gate  *
3147c478bd9Sstevel@tonic-gate  * Returns 0 if the discrepancy is ignored, non-zero if the discrepancy is
3157c478bd9Sstevel@tonic-gate  * reported.
3167c478bd9Sstevel@tonic-gate  */
3177c478bd9Sstevel@tonic-gate static int
report_delete(char * fname,char * type)3187c478bd9Sstevel@tonic-gate report_delete(char *fname, char *type)
3197c478bd9Sstevel@tonic-gate {
3207c478bd9Sstevel@tonic-gate 	struct rule	*rule_ptr;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	rule_ptr = check_rules(fname, type[0]);
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	if ((rule_ptr != NULL) && (rule_ptr->attr_list & ATTR_DELETE))
3257c478bd9Sstevel@tonic-gate 		return (1);
3267c478bd9Sstevel@tonic-gate 	else
3277c478bd9Sstevel@tonic-gate 		return (0);
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate /*
3317c478bd9Sstevel@tonic-gate  * This function takes in the two entries, which have been flagged as
3327c478bd9Sstevel@tonic-gate  * different, breaks them up and reports discrepancies.  Note, discrepancies
3337c478bd9Sstevel@tonic-gate  * are affected by the 'CHECK' and 'IGNORE' stanzas which may apply to
3347c478bd9Sstevel@tonic-gate  * these entries.
3357c478bd9Sstevel@tonic-gate  *
3367c478bd9Sstevel@tonic-gate  * Returns the number of discrepancies reported.
3377c478bd9Sstevel@tonic-gate  */
3387c478bd9Sstevel@tonic-gate static int
evaluate_differences(char * control_line,char * test_line,boolean_t prog_fmt,int flags)3397c478bd9Sstevel@tonic-gate evaluate_differences(char *control_line, char *test_line,
3407c478bd9Sstevel@tonic-gate     boolean_t prog_fmt, int flags)
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate 	char		ctrl_fname[PATH_MAX], test_fname[PATH_MAX],
343*5f273229SToomas Soome 	    ctrl_type[TYPE_SIZE], test_type[TYPE_SIZE],
344*5f273229SToomas Soome 	    ctrl_size[MISC_SIZE], ctrl_mode[MISC_SIZE],
345*5f273229SToomas Soome 	    ctrl_acl[ACL_SIZE], ctrl_mtime[MISC_SIZE],
346*5f273229SToomas Soome 	    ctrl_uid[MISC_SIZE], ctrl_gid[MISC_SIZE],
347*5f273229SToomas Soome 	    ctrl_dest[PATH_MAX], ctrl_contents[PATH_MAX],
348*5f273229SToomas Soome 	    ctrl_devnode[PATH_MAX], test_size[MISC_SIZE],
349*5f273229SToomas Soome 	    test_mode[MISC_SIZE], test_acl[ACL_SIZE],
350*5f273229SToomas Soome 	    test_mtime[MISC_SIZE], test_uid[MISC_SIZE],
351*5f273229SToomas Soome 	    test_gid[MISC_SIZE], test_dest[PATH_MAX],
352*5f273229SToomas Soome 	    test_contents[PATH_MAX], test_devnode[PATH_MAX],
353*5f273229SToomas Soome 	    *tag;
3547c478bd9Sstevel@tonic-gate 	int		ret_val;
3557c478bd9Sstevel@tonic-gate 	struct rule	*rule_ptr;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	ret_val = 0;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	parse_line(control_line, ctrl_fname, ctrl_type, ctrl_size, ctrl_mode,
3607c478bd9Sstevel@tonic-gate 	    ctrl_acl, ctrl_mtime, ctrl_uid, ctrl_gid, ctrl_contents,
3617c478bd9Sstevel@tonic-gate 	    ctrl_devnode, ctrl_dest);
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	/*
3647c478bd9Sstevel@tonic-gate 	 * Now we know the fname and type, let's get the rule that matches this
3657c478bd9Sstevel@tonic-gate 	 * manifest entry.  If there is a match, make sure to setup the
3667c478bd9Sstevel@tonic-gate 	 * correct reporting flags.
3677c478bd9Sstevel@tonic-gate 	 */
3687c478bd9Sstevel@tonic-gate 	rule_ptr = check_rules(ctrl_fname, ctrl_type[0]);
3697c478bd9Sstevel@tonic-gate 	if (rule_ptr != NULL)
3707c478bd9Sstevel@tonic-gate 		flags = rule_ptr->attr_list;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	parse_line(test_line, test_fname, test_type, test_size, test_mode,
3737c478bd9Sstevel@tonic-gate 	    test_acl, test_mtime, test_uid, test_gid, test_contents,
3747c478bd9Sstevel@tonic-gate 	    test_devnode, test_dest);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	/*
3777c478bd9Sstevel@tonic-gate 	 * Report the errors based upon which keywords have been set by
3787c478bd9Sstevel@tonic-gate 	 * the user.
3797c478bd9Sstevel@tonic-gate 	 */
3807c478bd9Sstevel@tonic-gate 	if ((flags & ATTR_TYPE) && (ctrl_type[0] != test_type[0])) {
3817c478bd9Sstevel@tonic-gate 		report_error(ctrl_fname, TYPE_KEYWORD, ctrl_type,
3827c478bd9Sstevel@tonic-gate 		    test_type, prog_fmt);
3837c478bd9Sstevel@tonic-gate 		ret_val++;
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	if ((flags & ATTR_SIZE) && (strcmp(ctrl_size, test_size) != 0)) {
3877c478bd9Sstevel@tonic-gate 		report_error(ctrl_fname, SIZE_KEYWORD, ctrl_size,
3887c478bd9Sstevel@tonic-gate 		    test_size, prog_fmt);
3897c478bd9Sstevel@tonic-gate 		ret_val++;
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	if ((flags & ATTR_MODE) && (strcmp(ctrl_mode, test_mode) != 0)) {
3937c478bd9Sstevel@tonic-gate 		report_error(ctrl_fname, MODE_KEYWORD, ctrl_mode,
3947c478bd9Sstevel@tonic-gate 		    test_mode, prog_fmt);
3957c478bd9Sstevel@tonic-gate 		ret_val++;
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	if ((flags & ATTR_ACL) && (strcmp(ctrl_acl, test_acl) != 0)) {
3997c478bd9Sstevel@tonic-gate 		report_error(ctrl_fname, ACL_KEYWORD, ctrl_acl,
4007c478bd9Sstevel@tonic-gate 		    test_acl, prog_fmt);
4017c478bd9Sstevel@tonic-gate 		ret_val++;
4027c478bd9Sstevel@tonic-gate 	}
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	if ((flags & ATTR_MTIME) && (ctrl_type[0] == test_type[0])) {
4057c478bd9Sstevel@tonic-gate 		if (strcmp(ctrl_mtime, test_mtime) != 0) {
4067c478bd9Sstevel@tonic-gate 			switch (ctrl_type[0]) {
4077c478bd9Sstevel@tonic-gate 			case 'D':
4087c478bd9Sstevel@tonic-gate 				tag = "dirmtime";
4097c478bd9Sstevel@tonic-gate 				break;
4107c478bd9Sstevel@tonic-gate 			case 'L':
4117c478bd9Sstevel@tonic-gate 				tag = "lnmtime";
4127c478bd9Sstevel@tonic-gate 				break;
4137c478bd9Sstevel@tonic-gate 			default:
4147c478bd9Sstevel@tonic-gate 				tag = "mtime";
4157c478bd9Sstevel@tonic-gate 				break;
4167c478bd9Sstevel@tonic-gate 			}
4177c478bd9Sstevel@tonic-gate 			if (flags == 0) {
4187c478bd9Sstevel@tonic-gate 				report_error(ctrl_fname, tag, ctrl_mtime,
4197c478bd9Sstevel@tonic-gate 				    test_mtime, prog_fmt);
4207c478bd9Sstevel@tonic-gate 			ret_val++;
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	if ((ctrl_type[0] == 'F') && (flags & ATTR_MTIME) &&
4257c478bd9Sstevel@tonic-gate 	    (strcmp(ctrl_mtime, test_mtime) != 0)) {
4267c478bd9Sstevel@tonic-gate 		report_error(ctrl_fname, MTIME_KEYWORD, ctrl_mtime, test_mtime,
4277c478bd9Sstevel@tonic-gate 		    prog_fmt);
4287c478bd9Sstevel@tonic-gate 		ret_val++;
4297c478bd9Sstevel@tonic-gate 	}
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	if ((ctrl_type[0] == 'D') && (flags & ATTR_DIRMTIME) &&
4327c478bd9Sstevel@tonic-gate 	    (strcmp(ctrl_mtime, test_mtime) != 0)) {
4337c478bd9Sstevel@tonic-gate 		report_error(ctrl_fname, DIRMTIME_KEYWORD, ctrl_mtime,
4347c478bd9Sstevel@tonic-gate 		    test_mtime, prog_fmt);
4357c478bd9Sstevel@tonic-gate 		ret_val++;
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	if ((ctrl_type[0] == 'L') && (flags & ATTR_LNMTIME) &&
4397c478bd9Sstevel@tonic-gate 	    (strcmp(ctrl_mtime, test_mtime) != 0)) {
4407c478bd9Sstevel@tonic-gate 		report_error(ctrl_fname, LNMTIME_KEYWORD, ctrl_mtime,
4417c478bd9Sstevel@tonic-gate 		    test_mtime, prog_fmt);
4427c478bd9Sstevel@tonic-gate 		ret_val++;
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 	} else if ((flags & ATTR_MTIME) &&
4457c478bd9Sstevel@tonic-gate 	    (strcmp(ctrl_mtime, test_mtime) != 0)) {
4467c478bd9Sstevel@tonic-gate 		report_error(ctrl_fname, MTIME_KEYWORD, ctrl_mtime,
4477c478bd9Sstevel@tonic-gate 		    test_mtime, prog_fmt);
4487c478bd9Sstevel@tonic-gate 		ret_val++;
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	if ((flags & ATTR_UID) && (strcmp(ctrl_uid, test_uid) != 0)) {
4527c478bd9Sstevel@tonic-gate 		report_error(ctrl_fname, UID_KEYWORD, ctrl_uid,
4537c478bd9Sstevel@tonic-gate 		    test_uid, prog_fmt);
4547c478bd9Sstevel@tonic-gate 		ret_val++;
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	if ((flags & ATTR_GID) && (strcmp(ctrl_gid, test_gid) != 0)) {
4587c478bd9Sstevel@tonic-gate 		report_error(ctrl_fname, GID_KEYWORD, ctrl_gid,
4597c478bd9Sstevel@tonic-gate 		    test_gid, prog_fmt);
4607c478bd9Sstevel@tonic-gate 		ret_val++;
4617c478bd9Sstevel@tonic-gate 	}
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	if ((flags & ATTR_DEVNODE) &&
4647c478bd9Sstevel@tonic-gate 	    (strcmp(ctrl_devnode, test_devnode) != 0)) {
4657c478bd9Sstevel@tonic-gate 		report_error(ctrl_fname, DEVNODE_KEYWORD, ctrl_devnode,
4667c478bd9Sstevel@tonic-gate 		    test_devnode, prog_fmt);
4677c478bd9Sstevel@tonic-gate 		ret_val++;
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	if ((flags & ATTR_DEST) && (strcmp(ctrl_dest, test_dest) != 0)) {
4717c478bd9Sstevel@tonic-gate 		report_error(ctrl_fname, DEST_KEYWORD, ctrl_dest,
4727c478bd9Sstevel@tonic-gate 		    test_dest, prog_fmt);
4737c478bd9Sstevel@tonic-gate 		ret_val++;
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	if ((flags & ATTR_CONTENTS) &&
4777c478bd9Sstevel@tonic-gate 	    (strcmp(ctrl_contents, test_contents)) != 0) {
4787c478bd9Sstevel@tonic-gate 		report_error(ctrl_fname, CONTENTS_KEYWORD, ctrl_contents,
4797c478bd9Sstevel@tonic-gate 		    test_contents, prog_fmt);
4807c478bd9Sstevel@tonic-gate 		ret_val++;
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	return (ret_val);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate /*
4877c478bd9Sstevel@tonic-gate  * Function responsible for reporting errors.
4887c478bd9Sstevel@tonic-gate  */
4897c478bd9Sstevel@tonic-gate static void
report_error(char * fname,char * type,char * ctrl_val,char * test_val,boolean_t prog_fmt)4907c478bd9Sstevel@tonic-gate report_error(char *fname, char *type, char *ctrl_val, char *test_val,
4917c478bd9Sstevel@tonic-gate     boolean_t prog_fmt)
4927c478bd9Sstevel@tonic-gate {
4937c478bd9Sstevel@tonic-gate 	static char	last_fname[PATH_MAX] = "";
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	if (!prog_fmt) {
4967c478bd9Sstevel@tonic-gate 		/* Verbose mode */
4977c478bd9Sstevel@tonic-gate 		if (strcmp(fname, last_fname) != 0) {
4987c478bd9Sstevel@tonic-gate 			(void) printf("%s:\n", fname);
4997c478bd9Sstevel@tonic-gate 			(void) strlcpy(last_fname, fname, sizeof (last_fname));
5007c478bd9Sstevel@tonic-gate 		}
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 		if (strcmp(type, ADD_KEYWORD) == 0 ||
5037c478bd9Sstevel@tonic-gate 		    strcmp(type, DELETE_KEYWORD) == 0)
5047c478bd9Sstevel@tonic-gate 			(void) printf("  %s\n", type);
5057c478bd9Sstevel@tonic-gate 		else
5067c478bd9Sstevel@tonic-gate 			(void) printf("  %s  control:%s  test:%s\n", type,
5077c478bd9Sstevel@tonic-gate 			    ctrl_val, test_val);
5087c478bd9Sstevel@tonic-gate 	} else {
5097c478bd9Sstevel@tonic-gate 		/* Programmatic mode */
5107c478bd9Sstevel@tonic-gate 		if (strcmp(fname, last_fname) != 0) {
5117c478bd9Sstevel@tonic-gate 			/* Ensure a line is not printed for the initial case */
5127c478bd9Sstevel@tonic-gate 			if (strlen(last_fname) != 0)
5137c478bd9Sstevel@tonic-gate 				(void) printf("\n");
5147c478bd9Sstevel@tonic-gate 			(void) strlcpy(last_fname, fname, sizeof (last_fname));
5157c478bd9Sstevel@tonic-gate 			(void) printf("%s ", fname);
5167c478bd9Sstevel@tonic-gate 		}
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 		(void) printf("%s ", type);
5197c478bd9Sstevel@tonic-gate 		if (strcmp(type, ADD_KEYWORD) != 0 &&
5207c478bd9Sstevel@tonic-gate 		    strcmp(type, DELETE_KEYWORD) != 0) {
5217c478bd9Sstevel@tonic-gate 			(void) printf("%s ", ctrl_val);
5227c478bd9Sstevel@tonic-gate 			(void) printf("%s ", test_val);
5237c478bd9Sstevel@tonic-gate 		}
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate /*
5287c478bd9Sstevel@tonic-gate  * Function responsible for reading in a line from the manifest.
5297c478bd9Sstevel@tonic-gate  * Takes in the file ptr and a buffer, parses the buffer  and sets the 'line'
5307c478bd9Sstevel@tonic-gate  * ptr correctly.  In the case when the buffer is fully parsed, this function
5317c478bd9Sstevel@tonic-gate  * reads more data from the file ptr and refills the buffer.
5327c478bd9Sstevel@tonic-gate  */
5337c478bd9Sstevel@tonic-gate static int
read_manifest_line(FILE * fd,char * buf,int buf_size,int start_pos,char ** line,char * fname)5347c478bd9Sstevel@tonic-gate read_manifest_line(FILE *fd, char *buf, int buf_size, int start_pos,
5357c478bd9Sstevel@tonic-gate     char **line, char *fname)
5367c478bd9Sstevel@tonic-gate {
5377c478bd9Sstevel@tonic-gate 	int	end_pos, len, iscomment = 0, filepos;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	/*
5407c478bd9Sstevel@tonic-gate 	 * Initialization case: make sure the manifest version is OK
5417c478bd9Sstevel@tonic-gate 	 */
5427c478bd9Sstevel@tonic-gate 	if (start_pos == 0) {
5437c478bd9Sstevel@tonic-gate 		end_pos = 0;
5447c478bd9Sstevel@tonic-gate 		buf[0] = '\0';
5457c478bd9Sstevel@tonic-gate 		filepos = ftell(fd);
5467c478bd9Sstevel@tonic-gate 		(void) fread((void *) buf, (size_t)buf_size, (size_t)1, fd);
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 		*line = buf;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 		if (filepos == 0) {
5517c478bd9Sstevel@tonic-gate 			if (strncmp(buf, MANIFEST_VER,
5527c478bd9Sstevel@tonic-gate 			    strlen(MANIFEST_VER)) != 0)
5537c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, MISSING_VER, fname);
5547c478bd9Sstevel@tonic-gate 			if ((*line[0] == '!') || (*line[0] == '#'))
5557c478bd9Sstevel@tonic-gate 				iscomment++;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 			while (iscomment) {
5587c478bd9Sstevel@tonic-gate 				while ((buf[end_pos] != '\n') &&
5597c478bd9Sstevel@tonic-gate 				    (buf[end_pos] != '\0') &&
5607c478bd9Sstevel@tonic-gate 				    (end_pos < buf_size))
5617c478bd9Sstevel@tonic-gate 					end_pos++;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 				if (end_pos >= buf_size)
5647c478bd9Sstevel@tonic-gate 					return (-1);
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 				end_pos++;
5677c478bd9Sstevel@tonic-gate 				*line = &(buf[end_pos]);
5687c478bd9Sstevel@tonic-gate 				iscomment = 0;
5697c478bd9Sstevel@tonic-gate 				if ((*line[0] == '!') || (*line[0] == '#'))
5707c478bd9Sstevel@tonic-gate 					iscomment++;
5717c478bd9Sstevel@tonic-gate 			}
5727c478bd9Sstevel@tonic-gate 		}
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 		while ((buf[end_pos] != '\n') && (buf[end_pos] != '\0') &&
5757c478bd9Sstevel@tonic-gate 		    (end_pos < buf_size))
5767c478bd9Sstevel@tonic-gate 			end_pos++;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 		if (end_pos < buf_size) {
5797c478bd9Sstevel@tonic-gate 			if (buf[end_pos] == '\n') {
5807c478bd9Sstevel@tonic-gate 				buf[end_pos] = '\0';
5817c478bd9Sstevel@tonic-gate 				return (end_pos);
5827c478bd9Sstevel@tonic-gate 			}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 			if (buf[end_pos] == '\0')
5857c478bd9Sstevel@tonic-gate 				return (-1);
5867c478bd9Sstevel@tonic-gate 		}
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MANIFEST_ERR);
5897c478bd9Sstevel@tonic-gate 		exit(FATAL_EXIT);
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	end_pos = (start_pos+1);
5937c478bd9Sstevel@tonic-gate 	*line = &(buf[end_pos]);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	/* Read the buffer until EOL or the buffer is empty */
5967c478bd9Sstevel@tonic-gate 	while ((buf[end_pos] != '\n') && (buf[end_pos] != '\0') &&
5977c478bd9Sstevel@tonic-gate 	    (end_pos < buf_size))
5987c478bd9Sstevel@tonic-gate 		end_pos++;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if (end_pos < buf_size) {
6017c478bd9Sstevel@tonic-gate 		/* Found the end of the line, normal exit */
6027c478bd9Sstevel@tonic-gate 		if (buf[end_pos] == '\n') {
6037c478bd9Sstevel@tonic-gate 			buf[end_pos] = '\0';
6047c478bd9Sstevel@tonic-gate 			return (end_pos);
6057c478bd9Sstevel@tonic-gate 		}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 		/* No more input to read */
6087c478bd9Sstevel@tonic-gate 		if (buf[end_pos] == '\0')
6097c478bd9Sstevel@tonic-gate 			return (-1);
6107c478bd9Sstevel@tonic-gate 	}
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	/*
6137c478bd9Sstevel@tonic-gate 	 * The following code takes the remainder of the buffer and
6147c478bd9Sstevel@tonic-gate 	 * puts it at the beginning.  The space after the remainder, which
6157c478bd9Sstevel@tonic-gate 	 * is now at the beginning, is blanked.
6167c478bd9Sstevel@tonic-gate 	 * At this point, read in more data and continue to find the EOL....
6177c478bd9Sstevel@tonic-gate 	 */
6187c478bd9Sstevel@tonic-gate 	len = end_pos - (start_pos + 1);
6197c478bd9Sstevel@tonic-gate 	(void) memcpy(buf, &(buf[start_pos+1]), (size_t)len);
6207c478bd9Sstevel@tonic-gate 	(void) memset(&buf[len], '\0', (buf_size - len));
6217c478bd9Sstevel@tonic-gate 	(void) fread((void *) &buf[len], (size_t)(buf_size-len), (size_t)1, fd);
6227c478bd9Sstevel@tonic-gate 	*line = buf;
6237c478bd9Sstevel@tonic-gate 	end_pos = len;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	/* Read the buffer until EOL or the buffer is empty */
6267c478bd9Sstevel@tonic-gate 	while ((buf[end_pos] != '\n') && (buf[end_pos] != '\0') &&
6277c478bd9Sstevel@tonic-gate 	    (end_pos < buf_size))
6287c478bd9Sstevel@tonic-gate 		end_pos++;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	if (end_pos < buf_size) {
6317c478bd9Sstevel@tonic-gate 		/* Found the end of the line, normal exit */
6327c478bd9Sstevel@tonic-gate 		if (buf[end_pos] == '\n') {
6337c478bd9Sstevel@tonic-gate 			buf[end_pos] = '\0';
6347c478bd9Sstevel@tonic-gate 			return (end_pos);
6357c478bd9Sstevel@tonic-gate 		}
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 		/* No more input to read */
6387c478bd9Sstevel@tonic-gate 		if (buf[end_pos] == '\0')
6397c478bd9Sstevel@tonic-gate 			return (-1);
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, MANIFEST_ERR);
6437c478bd9Sstevel@tonic-gate 	exit(FATAL_EXIT);
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate static void
init_default_flags(uint_t * flags)6497c478bd9Sstevel@tonic-gate init_default_flags(uint_t *flags)
6507c478bd9Sstevel@tonic-gate {
6517c478bd9Sstevel@tonic-gate 	/* Default behavior: everything is checked *except* dirmtime */
6527c478bd9Sstevel@tonic-gate 	*flags = ATTR_ALL & ~(ATTR_DIRMTIME);
6537c478bd9Sstevel@tonic-gate }
654