xref: /onnv-gate/usr/src/tools/protocmp/protodir.c (revision 2868:c6c11cf3e587)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2868Sdduvall  * Common Development and Distribution License (the "License").
6*2868Sdduvall  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*2868Sdduvall 
220Sstevel@tonic-gate /*
23*2868Sdduvall  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <fcntl.h>
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <strings.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate #include <dirent.h>
370Sstevel@tonic-gate #include <sys/stat.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include "list.h"
400Sstevel@tonic-gate #include "protodir.h"
410Sstevel@tonic-gate #include "arch.h"
420Sstevel@tonic-gate #include "exception_list.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #define	FS	" \t\n"
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static char *
resolve_relative(const char * source,const char * link)470Sstevel@tonic-gate resolve_relative(const char *source, const char *link)
480Sstevel@tonic-gate {
490Sstevel@tonic-gate 	char	*p;
500Sstevel@tonic-gate 	char	*l_next;
510Sstevel@tonic-gate 	char	*l_pos;
520Sstevel@tonic-gate 	static char	curpath[MAXPATHLEN];
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	/* if absolute path - no relocation required */
550Sstevel@tonic-gate 	if (link[0] == '/')
560Sstevel@tonic-gate 		return (strcpy(curpath, link));
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 	(void) strcpy(curpath, source);
590Sstevel@tonic-gate 	p = rindex(curpath, '/');
600Sstevel@tonic-gate 	*p = '\0';
610Sstevel@tonic-gate 	l_pos = (char *)link;
620Sstevel@tonic-gate 	do {
630Sstevel@tonic-gate 		l_next = index(l_pos, '/');
640Sstevel@tonic-gate 		if (strncmp(l_pos, "../", 3) == 0) {
650Sstevel@tonic-gate 			if ((p = rindex(curpath, '/')) != NULL)
660Sstevel@tonic-gate 				*p = '\0';
670Sstevel@tonic-gate 			else
680Sstevel@tonic-gate 				curpath[0] = '\0';
690Sstevel@tonic-gate 		} else if (strncmp(l_pos, "./", 2)) {
700Sstevel@tonic-gate 			/* if not . then we process */
710Sstevel@tonic-gate 			if (curpath[0])
720Sstevel@tonic-gate 				(void) strcat(curpath, "/");
730Sstevel@tonic-gate 			if (l_next) {
740Sstevel@tonic-gate 				(void) strncat(curpath, l_pos,
750Sstevel@tonic-gate 				    (l_next - l_pos));
760Sstevel@tonic-gate 			} else
770Sstevel@tonic-gate 				(void) strcat(curpath, l_pos);
780Sstevel@tonic-gate 		}
790Sstevel@tonic-gate 		l_pos = l_next + 1;
800Sstevel@tonic-gate 	} while (l_next);
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	return (curpath);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 
860Sstevel@tonic-gate static int
parse_proto_line(const char * basedir,char * line,elem_list * list,short arch,const char * pkgname)870Sstevel@tonic-gate parse_proto_line(const char *basedir, char *line, elem_list *list, short arch,
880Sstevel@tonic-gate     const char *pkgname)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate 	char	*type, *class, *file, *src, *maj, *min, *perm, *owner, *group;
910Sstevel@tonic-gate 	char	p_line[BUFSIZ];
920Sstevel@tonic-gate 	elem	*dup;
930Sstevel@tonic-gate 	static elem *e = NULL;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	(void) strcpy(p_line, line);
960Sstevel@tonic-gate 	if (!e)
970Sstevel@tonic-gate 		e = (elem *)calloc(1, sizeof (elem));
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	e->flag = 0;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	if (!(type  = strtok(p_line, FS))) {
1020Sstevel@tonic-gate 		(void) fprintf(stderr, "error: bad line(type) : %s\n", line);
1030Sstevel@tonic-gate 		return (-1);
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	e->file_type = type[0];
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	if ((class = strtok(NULL, FS)) == NULL) {
1090Sstevel@tonic-gate 		(void) fprintf(stderr, "error: bad line(class) : %s\n", line);
1100Sstevel@tonic-gate 		return (-1);
1110Sstevel@tonic-gate 	}
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	/*
1140Sstevel@tonic-gate 	 * Just ignore 'legacy' entries.  These are not present in the proto
1150Sstevel@tonic-gate 	 * area at all.  They're phantoms.
1160Sstevel@tonic-gate 	 */
1170Sstevel@tonic-gate 	if (strcmp(class, "legacy") == 0)
1180Sstevel@tonic-gate 		return (0);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	if (!(file  = strtok(NULL, FS))) {
1210Sstevel@tonic-gate 		(void) fprintf(stderr, "error: bad line(file_name) : %s\n",
1220Sstevel@tonic-gate 		    line);
1230Sstevel@tonic-gate 		return (-1);
1240Sstevel@tonic-gate 	}
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	e->symsrc = NULL;
1270Sstevel@tonic-gate 	if ((src = index(file, '=')) != NULL) {
1280Sstevel@tonic-gate 		/*
1290Sstevel@tonic-gate 		 * The '=' operator is subtly different for link and non-link
1300Sstevel@tonic-gate 		 * entries.  For the hard or soft link case, the left hand side
131*2868Sdduvall 		 * exists in the proto area and is created by the package.
132*2868Sdduvall 		 *
133*2868Sdduvall 		 * When the file is an editable file, it's very likely that the
134*2868Sdduvall 		 * right hand side is only a fragment of that file, which is
135*2868Sdduvall 		 * delivered by multiple packages in the consolidation.  Thus it
136*2868Sdduvall 		 * can't exist in the proto area, and because we can't really
137*2868Sdduvall 		 * know where the file's root directory is, we should skip the
138*2868Sdduvall 		 * file.
139*2868Sdduvall 		 *
140*2868Sdduvall 		 * For all other filetypes, assume the right hand side is in the
141*2868Sdduvall 		 * proto area.
1420Sstevel@tonic-gate 		 */
1430Sstevel@tonic-gate 		if (e->file_type == SYM_LINK_T || e->file_type == LINK_T) {
1440Sstevel@tonic-gate 			*src++ = '\0';
1450Sstevel@tonic-gate 			e->symsrc = strdup(src);
146*2868Sdduvall 		} else if (e->file_type == EDIT_T) {
147*2868Sdduvall 			return (0);
1480Sstevel@tonic-gate 		} else {
1490Sstevel@tonic-gate 			file = src + 1;
1500Sstevel@tonic-gate 		}
1510Sstevel@tonic-gate 	}
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	/*
1540Sstevel@tonic-gate 	 * if a basedir has a value, prepend it to the filename
1550Sstevel@tonic-gate 	 */
1560Sstevel@tonic-gate 	if (basedir[0])
1570Sstevel@tonic-gate 		(void) strcat(strcat(strcpy(e->name, basedir), "/"), file);
1580Sstevel@tonic-gate 	else
1590Sstevel@tonic-gate 		(void) strcpy(e->name, file);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	if (e->file_type != SYM_LINK_T) {
1620Sstevel@tonic-gate 		if ((e->file_type == CHAR_DEV_T) ||
1630Sstevel@tonic-gate 		    (e->file_type == BLOCK_DEV_T)) {
1640Sstevel@tonic-gate 			if (!(maj = strtok(NULL, FS))) {
1650Sstevel@tonic-gate 				(void) fprintf(stderr,
1660Sstevel@tonic-gate 				    "error: bad line(major number) : %s\n",
1670Sstevel@tonic-gate 				    line);
1680Sstevel@tonic-gate 				return (-1);
1690Sstevel@tonic-gate 			}
1700Sstevel@tonic-gate 			e->major = atoi(maj);
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 			if (!(min = strtok(NULL, FS))) {
1730Sstevel@tonic-gate 				(void) fprintf(stderr,
1740Sstevel@tonic-gate 				    "error: bad line(minor number) : %s\n",
1750Sstevel@tonic-gate 				    line);
1760Sstevel@tonic-gate 				return (-1);
1770Sstevel@tonic-gate 			}
1780Sstevel@tonic-gate 			e->minor = atoi(min);
1790Sstevel@tonic-gate 		} else {
1800Sstevel@tonic-gate 			e->major = -1;
1810Sstevel@tonic-gate 			e->minor = -1;
1820Sstevel@tonic-gate 		}
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 		if (!(perm = strtok(NULL, FS))) {
1850Sstevel@tonic-gate 			(void) fprintf(stderr,
1860Sstevel@tonic-gate 			    "error: bad line(permission) : %s\n", line);
1870Sstevel@tonic-gate 			return (-1);
1880Sstevel@tonic-gate 		}
1890Sstevel@tonic-gate 		e->perm = strtol(perm, NULL, 8);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 		if (!(owner = strtok(NULL, FS))) {
1920Sstevel@tonic-gate 			(void) fprintf(stderr,
1930Sstevel@tonic-gate 			    "error: bad line(owner) : %s\n", line);
1940Sstevel@tonic-gate 			return (-1);
1950Sstevel@tonic-gate 		}
1960Sstevel@tonic-gate 		(void) strcpy(e->owner, owner);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 		if (!(group = strtok(NULL, FS))) {
1990Sstevel@tonic-gate 			(void) fprintf(stderr,
2000Sstevel@tonic-gate 			    "error: bad line(group) : %s\n", line);
2010Sstevel@tonic-gate 			return (-1);
2020Sstevel@tonic-gate 		}
2030Sstevel@tonic-gate 		(void) strcpy(e->group, group);
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	e->inode = 0;
2070Sstevel@tonic-gate 	e->ref_cnt = 1;
2080Sstevel@tonic-gate 	e->arch = arch;
2090Sstevel@tonic-gate 	e->link_parent = NULL;
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	if (!(dup = find_elem(list, e, FOLLOW_LINK))) {
2120Sstevel@tonic-gate 		e->pkgs = add_pkg(NULL, pkgname); /* init pkgs list */
2130Sstevel@tonic-gate 		add_elem(list, e);
2140Sstevel@tonic-gate 		e = NULL;
2150Sstevel@tonic-gate 		return (1);
2160Sstevel@tonic-gate 	} else if (dup->file_type == DIR_T) {
2170Sstevel@tonic-gate 		if (!(dup->pkgs = add_pkg(dup->pkgs, pkgname))) {
2180Sstevel@tonic-gate 			/* add entry to pkgs */
2190Sstevel@tonic-gate 			(void) fprintf(stderr,
2200Sstevel@tonic-gate 			    "warning: %s: Duplicate entry for %s\n",
2210Sstevel@tonic-gate 			    pkgname, dup->name);
2220Sstevel@tonic-gate 			return (-1);
2230Sstevel@tonic-gate 		}
2240Sstevel@tonic-gate 		if (e->perm != dup->perm) {
2250Sstevel@tonic-gate 			(void) fprintf(stderr,
2260Sstevel@tonic-gate 			    "warning: %s: permissions %#o of %s do not match "
2270Sstevel@tonic-gate 			    "previous permissions %#o\n",
2280Sstevel@tonic-gate 			    pkgname, e->perm, dup->name, dup->perm);
2290Sstevel@tonic-gate 		}
2300Sstevel@tonic-gate 		if (strcmp(e->owner, dup->owner) != 0) {
2310Sstevel@tonic-gate 			(void) fprintf(stderr,
2320Sstevel@tonic-gate 			    "warning: %s: owner \"%s\" of %s does not match "
2330Sstevel@tonic-gate 			    "previous owner \"%s\"\n",
2340Sstevel@tonic-gate 			    pkgname, e->owner, dup->name, dup->owner);
2350Sstevel@tonic-gate 		}
2360Sstevel@tonic-gate 		if (strcmp(e->group, dup->group) != 0) {
2370Sstevel@tonic-gate 			(void) fprintf(stderr,
2380Sstevel@tonic-gate 			    "warning: %s: group \"%s\" of %s does not match "
2390Sstevel@tonic-gate 			    "previous group \"%s\"\n",
2400Sstevel@tonic-gate 			    pkgname, e->group, dup->name, dup->group);
2410Sstevel@tonic-gate 		}
2420Sstevel@tonic-gate 	} else {
2430Sstevel@tonic-gate 		/*
2440Sstevel@tonic-gate 		 * Signal an error only if this is something that's not on the
2450Sstevel@tonic-gate 		 * exception list.
2460Sstevel@tonic-gate 		 */
2470Sstevel@tonic-gate 		(void) strcpy(e->name, file);
2480Sstevel@tonic-gate 		if (find_elem(&exception_list, e, FOLLOW_LINK) == NULL) {
2490Sstevel@tonic-gate 			(void) fprintf(stderr,
2500Sstevel@tonic-gate 			    "warning: %s: duplicate entry for %s - ignored\n",
2510Sstevel@tonic-gate 			    pkgname, e->name);
2520Sstevel@tonic-gate 			return (-1);
2530Sstevel@tonic-gate 		}
2540Sstevel@tonic-gate 	}
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	return (0);
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate static int
parse_proto_link(const char * basedir,char * line,elem_list * list,short arch,const char * pkgname)2600Sstevel@tonic-gate parse_proto_link(const char *basedir, char *line, elem_list *list, short arch,
2610Sstevel@tonic-gate     const char *pkgname)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate 	char	*type, *file, *src;
2640Sstevel@tonic-gate 	char	p_line[BUFSIZ];
2650Sstevel@tonic-gate 	elem	*p, *dup;
2660Sstevel@tonic-gate 	elem	key;
2670Sstevel@tonic-gate 	static elem	*e = NULL;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	(void) strcpy(p_line, line);
2710Sstevel@tonic-gate 	if (!e)
2720Sstevel@tonic-gate 		e = (elem *)calloc(1, sizeof (elem));
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	e->flag = 0;
2750Sstevel@tonic-gate 	type = strtok(p_line, FS);
2760Sstevel@tonic-gate 	e->arch = arch;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	e->file_type = type[0];
2790Sstevel@tonic-gate 	(void) strtok(NULL, FS);   /* burn class */
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	file = strtok(NULL, FS);
2820Sstevel@tonic-gate 	if ((src = index(file, '=')) != NULL) {
2830Sstevel@tonic-gate 		*src++ = '\0';
2840Sstevel@tonic-gate 		e->symsrc = strdup(src);
2850Sstevel@tonic-gate 	} else {
2860Sstevel@tonic-gate 		(void) fprintf(stderr,
2870Sstevel@tonic-gate 		    "error: %s: hard link does not have destination (%s)\n",
2880Sstevel@tonic-gate 		    pkgname, file);
2890Sstevel@tonic-gate 		return (0);
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	/*
2930Sstevel@tonic-gate 	 * if a basedir has a value, prepend it to the filename
2940Sstevel@tonic-gate 	 */
2950Sstevel@tonic-gate 	if (basedir[0])
2960Sstevel@tonic-gate 		(void) strcat(strcat(strcpy(e->name, basedir), "/"), file);
2970Sstevel@tonic-gate 	else
2980Sstevel@tonic-gate 		(void) strcpy(e->name, file);
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	/*
3010Sstevel@tonic-gate 	 * now we need to find the file that we link to - to do this
3020Sstevel@tonic-gate 	 * we build a key.
3030Sstevel@tonic-gate 	 */
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	src = resolve_relative(e->name, e->symsrc);
3060Sstevel@tonic-gate 	(void) strcpy(key.name, src);
3070Sstevel@tonic-gate 	key.arch = e->arch;
3080Sstevel@tonic-gate 	if ((p = find_elem(list, &key, NO_FOLLOW_LINK)) == NULL) {
3090Sstevel@tonic-gate 		(void) fprintf(stderr,
3100Sstevel@tonic-gate 		    "error: %s: hardlink to non-existent file: %s=%s\n",
3110Sstevel@tonic-gate 		    pkgname, e->name, e->symsrc);
3120Sstevel@tonic-gate 		return (0);
3130Sstevel@tonic-gate 	}
3140Sstevel@tonic-gate 	if ((p->file_type == SYM_LINK_T) || (p->file_type == LINK_T)) {
3150Sstevel@tonic-gate 		(void) fprintf(stderr,
3160Sstevel@tonic-gate 		    "error: %s: hardlink must link to a file or directory "
3170Sstevel@tonic-gate 		    "(not other links): %s=%s\n", pkgname, e->name, p->name);
3180Sstevel@tonic-gate 		return (0);
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate 	e->link_parent = p;
3210Sstevel@tonic-gate 	e->link_sib = p->link_sib;
3220Sstevel@tonic-gate 	p->link_sib = e;
3230Sstevel@tonic-gate 	p->ref_cnt++;
3240Sstevel@tonic-gate 	e->inode = p->inode;
3250Sstevel@tonic-gate 	e->perm = p->perm;
3260Sstevel@tonic-gate 	e->ref_cnt = p->ref_cnt;
3270Sstevel@tonic-gate 	e->major = p->major;
3280Sstevel@tonic-gate 	e->minor = p->minor;
3290Sstevel@tonic-gate 	(void) strcpy(e->owner, p->owner);
3300Sstevel@tonic-gate 	(void) strcpy(e->group, p->owner);
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	if (!(dup = find_elem(list, e, NO_FOLLOW_LINK))) {
3330Sstevel@tonic-gate 		e->pkgs = add_pkg(NULL, pkgname); /* init pkgs list */
3340Sstevel@tonic-gate 		e->link_sib = NULL;
3350Sstevel@tonic-gate 		add_elem(list, e);
3360Sstevel@tonic-gate 		e = NULL;
3370Sstevel@tonic-gate 		return (1);
3380Sstevel@tonic-gate 	} else {
3390Sstevel@tonic-gate 		/*
3400Sstevel@tonic-gate 		 * Signal an error only if this is something that's not on the
3410Sstevel@tonic-gate 		 * exception list.
3420Sstevel@tonic-gate 		 */
3430Sstevel@tonic-gate 		(void) strcpy(e->name, file);
3440Sstevel@tonic-gate 		if (find_elem(&exception_list, e, FOLLOW_LINK) == NULL) {
3450Sstevel@tonic-gate 			(void) fprintf(stderr,
3460Sstevel@tonic-gate 			    "warning: %s: duplicate entry for %s - ignored\n",
3470Sstevel@tonic-gate 			    pkgname, e->name);
3480Sstevel@tonic-gate 			return (-1);
3490Sstevel@tonic-gate 		}
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	return (0);
3530Sstevel@tonic-gate }
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate /*
3570Sstevel@tonic-gate  * open up the pkginfo file and find the ARCH= and the BASEDIR= macros.
3580Sstevel@tonic-gate  * I will set the arch and basedir variables based on these fields.
3590Sstevel@tonic-gate  */
3600Sstevel@tonic-gate static void
read_pkginfo(const char * protodir,short * arch,char * basedir)3610Sstevel@tonic-gate read_pkginfo(const char *protodir, short *arch, char *basedir)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 	char	pkginfofile[MAXPATHLEN];
3640Sstevel@tonic-gate 	char	architecture[MAXPATHLEN];
3650Sstevel@tonic-gate 	char	buf[BUFSIZ];
3660Sstevel@tonic-gate 	FILE	*pkginfo_fp;
3670Sstevel@tonic-gate 	int	hits = 0;
3680Sstevel@tonic-gate 	int	i;
3690Sstevel@tonic-gate 	int	index;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	architecture[0] = '\0';
3730Sstevel@tonic-gate 	basedir[0] = '\0';
3740Sstevel@tonic-gate 	*arch = P_ISA;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	/*
3770Sstevel@tonic-gate 	 * determine whether the pkginfo file is a pkginfo.tmpl or
3780Sstevel@tonic-gate 	 * a pkginfo file
3790Sstevel@tonic-gate 	 */
3800Sstevel@tonic-gate 	(void) strcat(strcat(strcpy(pkginfofile, protodir), "/"),
3810Sstevel@tonic-gate 	    "pkginfo.tmpl");
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	if ((pkginfo_fp = fopen(pkginfofile, "r")) == NULL) {
3840Sstevel@tonic-gate 		(void) strcat(strcat(strcpy(pkginfofile, protodir), "/"),
3850Sstevel@tonic-gate 		    "pkginfo");
3860Sstevel@tonic-gate 		if ((pkginfo_fp = fopen(pkginfofile, "r")) == NULL) {
3870Sstevel@tonic-gate 			perror(pkginfofile);
3880Sstevel@tonic-gate 			return;
3890Sstevel@tonic-gate 		}
3900Sstevel@tonic-gate 	}
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	while (fgets(buf, BUFSIZ, pkginfo_fp) && (hits != 3)) {
3940Sstevel@tonic-gate 		if (strncmp(buf, "ARCH=", 5) == 0) {
3950Sstevel@tonic-gate 			index = 0;
3960Sstevel@tonic-gate 			/*
3970Sstevel@tonic-gate 			 * remove any '"' in the ARCH field.
3980Sstevel@tonic-gate 			 */
3990Sstevel@tonic-gate 			for (i = 5; buf[i]; i++) {
4000Sstevel@tonic-gate 				if (buf[i] != '"')
4010Sstevel@tonic-gate 					architecture[index++] = buf[i];
4020Sstevel@tonic-gate 			}
4030Sstevel@tonic-gate 			/* -1 because above copy included '\n' */
4040Sstevel@tonic-gate 			architecture[index-1] = '\0';
4050Sstevel@tonic-gate 			hits += 1;
4060Sstevel@tonic-gate 		} else if (strncmp(buf, "BASEDIR=", 8) == 0) {
4070Sstevel@tonic-gate 			index = 0;
4080Sstevel@tonic-gate 			/*
4090Sstevel@tonic-gate 			 * remove any '"' in the BASEDIR field, and
4100Sstevel@tonic-gate 			 * strip off a leading '/' if present.
4110Sstevel@tonic-gate 			 */
4120Sstevel@tonic-gate 			for (i = 8; buf[i]; i++) {
4130Sstevel@tonic-gate 				if (buf[i] != '"' &&
4140Sstevel@tonic-gate 				    (buf[i] != '/' || index != 0)) {
4150Sstevel@tonic-gate 					buf[index++] = buf[i];
4160Sstevel@tonic-gate 				}
4170Sstevel@tonic-gate 			}
4180Sstevel@tonic-gate 			/* -1 because above copy included '\n' */
4190Sstevel@tonic-gate 			buf[index-1] = '\0';
4200Sstevel@tonic-gate 			(void) strcpy(basedir, &buf[0]);
4210Sstevel@tonic-gate 			hits += 2;
4220Sstevel@tonic-gate 		}
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate 	(void) fclose(pkginfo_fp);
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	if (architecture[0])
4270Sstevel@tonic-gate 		if ((*arch = assign_arch(architecture)) == NULL) {
4280Sstevel@tonic-gate 			(void) fprintf(stderr,
4290Sstevel@tonic-gate 			    "warning: Unknown architecture %s found in %s\n",
4300Sstevel@tonic-gate 			    architecture, pkginfofile);
4310Sstevel@tonic-gate 		}
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate /*
4350Sstevel@tonic-gate  * The first pass through the prototype file goes through and reads
4360Sstevel@tonic-gate  * in all the entries except 'hard links'.  Those must be processed
4370Sstevel@tonic-gate  * in a second pass.
4380Sstevel@tonic-gate  *
4390Sstevel@tonic-gate  * If any !includes are found in the prototype file this routine
4400Sstevel@tonic-gate  * will be called recursively.
4410Sstevel@tonic-gate  *
4420Sstevel@tonic-gate  * Args:
4430Sstevel@tonic-gate  *   protofile - full pathname to prototype file to be processed.
4440Sstevel@tonic-gate  *   protodir  - directory in which prototype file resides.
4450Sstevel@tonic-gate  *   list      - list to which elements will be added
4460Sstevel@tonic-gate  *   arch      - architecture of current prototype
4470Sstevel@tonic-gate  *   basedir   - basedir for package
4480Sstevel@tonic-gate  *   pkgname   - name of package
4490Sstevel@tonic-gate  *
4500Sstevel@tonic-gate  * Returns:
4510Sstevel@tonic-gate  *   returns number of items added to list.
4520Sstevel@tonic-gate  *
4530Sstevel@tonic-gate  */
4540Sstevel@tonic-gate static int
first_pass_prototype(const char * protofile,const char * protodir,elem_list * list,short arch,const char * basedir,const char * pkgname)4550Sstevel@tonic-gate first_pass_prototype(const char *protofile, const char *protodir,
4560Sstevel@tonic-gate     elem_list *list, short arch, const char *basedir, const char *pkgname)
4570Sstevel@tonic-gate {
4580Sstevel@tonic-gate 	int	elem_count = 0;
4590Sstevel@tonic-gate 	FILE	*proto_fp;
4600Sstevel@tonic-gate 	char	include_file[MAXPATHLEN];
4610Sstevel@tonic-gate 	char	buf[BUFSIZ];
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	if ((proto_fp = fopen(protofile, "r")) == NULL) {
4640Sstevel@tonic-gate 		perror(protofile);
4650Sstevel@tonic-gate 		return (0);
4660Sstevel@tonic-gate 	}
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	/*
4690Sstevel@tonic-gate 	 * first pass through file - process everything but
4700Sstevel@tonic-gate 	 * hard links.
4710Sstevel@tonic-gate 	 */
4720Sstevel@tonic-gate 	while (fgets(buf, BUFSIZ, proto_fp)) {
4730Sstevel@tonic-gate 		int	rc;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 		switch (buf[0]) {
4760Sstevel@tonic-gate 		case FILE_T:
4770Sstevel@tonic-gate 		case EDIT_T:
4780Sstevel@tonic-gate 		case VOLATILE_T:
4790Sstevel@tonic-gate 		case DIR_T:
4800Sstevel@tonic-gate 		case SYM_LINK_T:
4810Sstevel@tonic-gate 		case CHAR_DEV_T:
4820Sstevel@tonic-gate 		case BLOCK_DEV_T:
4830Sstevel@tonic-gate 			if ((rc = parse_proto_line(basedir, buf, list, arch,
4840Sstevel@tonic-gate 			    pkgname)) >= 0) {
4850Sstevel@tonic-gate 				elem_count += rc;
4860Sstevel@tonic-gate 			} else {
4870Sstevel@tonic-gate 				(void) fprintf(stderr,
4880Sstevel@tonic-gate 				    "error: Errors found in %s\n", protofile);
4890Sstevel@tonic-gate 			}
4900Sstevel@tonic-gate 			break;
4910Sstevel@tonic-gate 		case LINK_T:
4920Sstevel@tonic-gate 		case 'i':
4930Sstevel@tonic-gate 		case '#':
4940Sstevel@tonic-gate 		case '\n':
4950Sstevel@tonic-gate 			break;
4960Sstevel@tonic-gate 		case '!':
4970Sstevel@tonic-gate 			/* Is this an include statement - if so process */
4980Sstevel@tonic-gate 			if (strncmp(buf, "!include", 8) == 0) {
4990Sstevel@tonic-gate 				char *inc_file = (char *)(buf + 9);
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 				/* burn white space */
5020Sstevel@tonic-gate 				while ((*inc_file == ' ') ||
5030Sstevel@tonic-gate 				    (*inc_file == '\t'))
5040Sstevel@tonic-gate 					inc_file++;
5050Sstevel@tonic-gate 				if (*inc_file) {
5060Sstevel@tonic-gate 					/* remove trailing \n */
5070Sstevel@tonic-gate 					inc_file[strlen(inc_file) - 1] = '\0';
5080Sstevel@tonic-gate 					(void) strcat(strcat(strcpy(
5090Sstevel@tonic-gate 					    include_file, protodir), "/"),
5100Sstevel@tonic-gate 					    inc_file);
5110Sstevel@tonic-gate 					elem_count +=
5120Sstevel@tonic-gate 					    first_pass_prototype(include_file,
5130Sstevel@tonic-gate 					    protodir, list, arch, basedir,
5140Sstevel@tonic-gate 					    pkgname);
5150Sstevel@tonic-gate 				} else {
5160Sstevel@tonic-gate 					(void) fprintf(stderr,
5170Sstevel@tonic-gate 					    "warning: bad !include statement "
5180Sstevel@tonic-gate 					    "in prototype %s : %s\n",
5190Sstevel@tonic-gate 					    protofile, buf);
5200Sstevel@tonic-gate 				}
5210Sstevel@tonic-gate 			} else {
5220Sstevel@tonic-gate 				(void) fprintf(stderr,
5230Sstevel@tonic-gate 				    "warning: unexpected ! notation in "
5240Sstevel@tonic-gate 				    "prototype %s : %s\n", protofile, buf);
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 			}
5270Sstevel@tonic-gate 			break;
5280Sstevel@tonic-gate 		default:
5290Sstevel@tonic-gate 			(void) fprintf(stderr,
5300Sstevel@tonic-gate 			    "warning: unexpected line in prototype %s : %s\n",
5310Sstevel@tonic-gate 			    protofile, buf);
5320Sstevel@tonic-gate 			break;
5330Sstevel@tonic-gate 		}
5340Sstevel@tonic-gate 	}
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	(void) fclose(proto_fp);
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	return (elem_count);
5390Sstevel@tonic-gate }
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate /*
5420Sstevel@tonic-gate  * The second pass through the prototype file goes through and reads
5430Sstevel@tonic-gate  * and processes only the 'hard links' in the prototype file.  These
5440Sstevel@tonic-gate  * are resolved and added accordingly to the elements list(list).
5450Sstevel@tonic-gate  *
5460Sstevel@tonic-gate  * If any !includes are found in the prototype file this routine
5470Sstevel@tonic-gate  * will be called recursively.
5480Sstevel@tonic-gate  *
5490Sstevel@tonic-gate  * Args:
5500Sstevel@tonic-gate  *   protofile - full pathname to prototype file to be processed.
5510Sstevel@tonic-gate  *   protodir  - directory in which prototype file resides.
5520Sstevel@tonic-gate  *   list      - list to which elements will be added
5530Sstevel@tonic-gate  *   arch      - architecture of current prototype
5540Sstevel@tonic-gate  *   basedir   - basedir for package
5550Sstevel@tonic-gate  *   pkgname   - package name
5560Sstevel@tonic-gate  *
5570Sstevel@tonic-gate  * Returns:
5580Sstevel@tonic-gate  *   returns number of items added to list.
5590Sstevel@tonic-gate  *
5600Sstevel@tonic-gate  */
5610Sstevel@tonic-gate static int
second_pass_prototype(const char * protofile,const char * protodir,elem_list * list,short arch,const char * basedir,const char * pkgname)5620Sstevel@tonic-gate second_pass_prototype(const char *protofile, const char *protodir,
5630Sstevel@tonic-gate     elem_list *list, short arch, const char *basedir, const char *pkgname)
5640Sstevel@tonic-gate {
5650Sstevel@tonic-gate 	FILE	*proto_fp;
5660Sstevel@tonic-gate 	int	elem_count = 0;
5670Sstevel@tonic-gate 	char	include_file[MAXPATHLEN];
5680Sstevel@tonic-gate 	char	buf[BUFSIZ];
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	if ((proto_fp = fopen(protofile, "r")) == NULL) {
5710Sstevel@tonic-gate 		perror(protofile);
5720Sstevel@tonic-gate 		return (0);
5730Sstevel@tonic-gate 	}
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	/*
5760Sstevel@tonic-gate 	 * second pass through prototype file - process the hard links
5770Sstevel@tonic-gate 	 * now.
5780Sstevel@tonic-gate 	 */
5790Sstevel@tonic-gate 	while (fgets(buf, BUFSIZ, proto_fp))
5800Sstevel@tonic-gate 		if (buf[0] == LINK_T) {
5810Sstevel@tonic-gate 			int	rc;
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 			if ((rc = parse_proto_link(basedir, buf, list, arch,
5840Sstevel@tonic-gate 			    pkgname)) >= 0) {
5850Sstevel@tonic-gate 				elem_count += rc;
5860Sstevel@tonic-gate 			} else {
5870Sstevel@tonic-gate 				(void) fprintf(stderr,
5880Sstevel@tonic-gate 				    "error: Errors found in %s\n", protofile);
5890Sstevel@tonic-gate 			}
5900Sstevel@tonic-gate 		} else if (strncmp(buf, "!include", 8) == 0) {
5910Sstevel@tonic-gate 			/*
5920Sstevel@tonic-gate 			 * This is a file to include
5930Sstevel@tonic-gate 			 */
5940Sstevel@tonic-gate 			char *inc_file = (char *)(buf + 9);
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 			/* burn white space */
5970Sstevel@tonic-gate 			while ((*inc_file == ' ') || (*inc_file == '\t'))
5980Sstevel@tonic-gate 				inc_file++;
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 			if (*inc_file) {
6010Sstevel@tonic-gate 				/* remove trailing \n */
6020Sstevel@tonic-gate 				inc_file[strlen(inc_file) - 1] = '\0';
6030Sstevel@tonic-gate 				/* build up include file name to be opened. */
6040Sstevel@tonic-gate 				(void) strcat(strcat(strcpy(include_file,
6050Sstevel@tonic-gate 				    protodir), "/"), inc_file);
6060Sstevel@tonic-gate 				/*
6070Sstevel@tonic-gate 				 * recursively call this routine to process the
6080Sstevel@tonic-gate 				 * !include file.
6090Sstevel@tonic-gate 				 */
6100Sstevel@tonic-gate 				elem_count +=
6110Sstevel@tonic-gate 				    second_pass_prototype(include_file,
6120Sstevel@tonic-gate 				    protodir, list, arch, basedir, pkgname);
6130Sstevel@tonic-gate 			} else {
6140Sstevel@tonic-gate 				(void) fprintf(stderr,
6150Sstevel@tonic-gate 				    "warning: Bad !include statement in "
6160Sstevel@tonic-gate 				    "prototype %s : %s\n", protofile, buf);
6170Sstevel@tonic-gate 			}
6180Sstevel@tonic-gate 		}
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 		(void) fclose(proto_fp);
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 		return (elem_count);
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate /*
6260Sstevel@tonic-gate  * Args:
6270Sstevel@tonic-gate  *    pkgname  - name of package being processed
6280Sstevel@tonic-gate  *    protodir - pathname to package defs directory
6290Sstevel@tonic-gate  *    list     - List of elements read in, elements are added to this
6300Sstevel@tonic-gate  *		 as they are read in.
6310Sstevel@tonic-gate  *    verbose  - verbose output
6320Sstevel@tonic-gate  *
6330Sstevel@tonic-gate  * Returns:
6340Sstevel@tonic-gate  *    number of elements added to list
6350Sstevel@tonic-gate  */
6360Sstevel@tonic-gate int
process_package_dir(const char * pkgname,const char * protodir,elem_list * list,int verbose)6370Sstevel@tonic-gate process_package_dir(const char *pkgname, const char *protodir,
6380Sstevel@tonic-gate     elem_list *list, int verbose)
6390Sstevel@tonic-gate {
6400Sstevel@tonic-gate 	struct stat	st_buf;
6410Sstevel@tonic-gate 	char		protofile[MAXPATHLEN];
6420Sstevel@tonic-gate 	char		basedir[MAXPATHLEN];
6430Sstevel@tonic-gate 	short		arch;
6440Sstevel@tonic-gate 	int		count = 0;
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	/*
6480Sstevel@tonic-gate 	 * skip any packages we've already handled (because of
6490Sstevel@tonic-gate 	 * dependencies)
6500Sstevel@tonic-gate 	 */
6510Sstevel@tonic-gate 	if (processed_package(pkgname)) {
6520Sstevel@tonic-gate 		return (0);
6530Sstevel@tonic-gate 	}
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	/*
6560Sstevel@tonic-gate 	 * find the prototype file.  Legal forms of the name are:
6570Sstevel@tonic-gate 	 *		prototype
6580Sstevel@tonic-gate 	 *		prototype_<mach> (where mach == (sparc || i386 || ppc)
6590Sstevel@tonic-gate 	 */
6600Sstevel@tonic-gate 	(void) strcat(strcat(strcpy(protofile, protodir), "/"), "prototype");
6610Sstevel@tonic-gate 	if (stat(protofile, &st_buf) < 0) {
6620Sstevel@tonic-gate 		if (errno == ENOENT) {
6630Sstevel@tonic-gate 			(void) strcat(strcat(strcat(strcpy(protofile,
6640Sstevel@tonic-gate 			    protodir), "/"), "prototype"), PROTO_EXT);
6650Sstevel@tonic-gate 			if (stat(protofile, &st_buf) < 0) {
6660Sstevel@tonic-gate 				if (errno == ENOENT) {
6670Sstevel@tonic-gate 					if (verbose) {
6680Sstevel@tonic-gate 						(void) fprintf(stderr,
6690Sstevel@tonic-gate 						    "warning: no prototype "
6700Sstevel@tonic-gate 						    "file found in %s, "
6710Sstevel@tonic-gate 						    "skipping...\n",
6720Sstevel@tonic-gate 						    protodir);
6730Sstevel@tonic-gate 					}
6740Sstevel@tonic-gate 				} else
6750Sstevel@tonic-gate 					perror(protofile);
6760Sstevel@tonic-gate 				return (0);
6770Sstevel@tonic-gate 			}
6780Sstevel@tonic-gate 		} else {
6790Sstevel@tonic-gate 			perror(protofile);
6800Sstevel@tonic-gate 			return (0);
6810Sstevel@tonic-gate 		}
6820Sstevel@tonic-gate 	}
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	mark_processed(pkgname);
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	read_pkginfo(protodir, &arch, basedir);
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	count += first_pass_prototype(protofile, protodir, list, arch,
6890Sstevel@tonic-gate 	    basedir, pkgname);
6900Sstevel@tonic-gate 	count += second_pass_prototype(protofile, protodir, list, arch,
6910Sstevel@tonic-gate 	    basedir, pkgname);
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	/* print_list(list); */
6940Sstevel@tonic-gate 	return (count);
6950Sstevel@tonic-gate }
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate int
read_in_protodir(const char * dir_name,elem_list * list,int verbose)6980Sstevel@tonic-gate read_in_protodir(const char *dir_name, elem_list *list, int verbose)
6990Sstevel@tonic-gate {
7000Sstevel@tonic-gate 	DIR		*p_dir;
7010Sstevel@tonic-gate 	struct dirent	*dp;
7020Sstevel@tonic-gate 	char		protodir[MAXPATHLEN];
7030Sstevel@tonic-gate 	struct stat	st_buf;
7040Sstevel@tonic-gate 	int		count = 0;
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	if ((p_dir = opendir(dir_name)) == NULL) {
7070Sstevel@tonic-gate 		perror(dir_name);
7080Sstevel@tonic-gate 		exit(1);
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	list->type = PROTODIR_LIST;
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	while ((dp = readdir(p_dir)) != NULL) {
7140Sstevel@tonic-gate 		/*
7150Sstevel@tonic-gate 		 * let's not check "." and ".." - I don't really like this
7160Sstevel@tonic-gate 		 * but I wasn't really sure you could be sure that they
7170Sstevel@tonic-gate 		 * are always the first two entries in the directory
7180Sstevel@tonic-gate 		 * structure  - so I put it in the loop.
7190Sstevel@tonic-gate 		 *
7200Sstevel@tonic-gate 		 * Also - we skip all directories that are names .del-*.
7210Sstevel@tonic-gate 		 * and any SCCS directories too.
7220Sstevel@tonic-gate 		 */
7230Sstevel@tonic-gate 		if ((strcmp(dp->d_name, ".") == 0) ||
7240Sstevel@tonic-gate 		    (strcmp(dp->d_name, "..") == 0) ||
7250Sstevel@tonic-gate 		    (strncmp(dp->d_name, ".del-", 5) == 0) ||
7260Sstevel@tonic-gate 		    (strcmp(dp->d_name, "SCCS") == 0))
7270Sstevel@tonic-gate 			continue;
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 		(void) strcat(strcat(strcpy(protodir, dir_name), "/"),
7300Sstevel@tonic-gate 		    dp->d_name);
7310Sstevel@tonic-gate 		if (stat(protodir, &st_buf) < 0) {
7320Sstevel@tonic-gate 			perror(protodir);
7330Sstevel@tonic-gate 			continue;
7340Sstevel@tonic-gate 		}
7350Sstevel@tonic-gate 		if (!S_ISDIR(st_buf.st_mode)) {
7360Sstevel@tonic-gate 			if (verbose) {
7370Sstevel@tonic-gate 				(void) fprintf(stderr,
7380Sstevel@tonic-gate 				    "warning: %s not a directory\n", protodir);
7390Sstevel@tonic-gate 			}
7400Sstevel@tonic-gate 			continue;
7410Sstevel@tonic-gate 		}
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 		count += process_dependencies(dp->d_name, dir_name, list,
7440Sstevel@tonic-gate 		    verbose);
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 		count += process_package_dir(dp->d_name, protodir, list,
7470Sstevel@tonic-gate 		    verbose);
7480Sstevel@tonic-gate 	}
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	if (verbose)
7510Sstevel@tonic-gate 		(void) printf("read in %d lines\n", count);
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	(void) closedir(p_dir);
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	return (count);
7560Sstevel@tonic-gate }
757