17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*ae115bc7Smrj  * Common Development and Distribution License (the "License").
6*ae115bc7Smrj  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*ae115bc7Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <fcntl.h>
307c478bd9Sstevel@tonic-gate #include <strings.h>
317c478bd9Sstevel@tonic-gate #include <ctype.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include "list.h"
357c478bd9Sstevel@tonic-gate #include "proto_list.h"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #define	FS	" \t\n"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate static void
407c478bd9Sstevel@tonic-gate error(const char *msg, int lc)
417c478bd9Sstevel@tonic-gate {
427c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "warning: line %d - %s\n", lc, msg);
437c478bd9Sstevel@tonic-gate }
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * int is_num()
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * returns 1 if the string is entirely numeric - if not it returns 0
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate static int
527c478bd9Sstevel@tonic-gate is_num(const char *str)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	int i;
557c478bd9Sstevel@tonic-gate 	int len = strlen(str);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	if (len < 1)
587c478bd9Sstevel@tonic-gate 		return (0);
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++)
617c478bd9Sstevel@tonic-gate 		if (!isdigit(str[i]))
627c478bd9Sstevel@tonic-gate 			return (0);
637c478bd9Sstevel@tonic-gate 	return (1);
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate  * void check_line()
687c478bd9Sstevel@tonic-gate  *
697c478bd9Sstevel@tonic-gate  * try and do some sanity/syntax checking against the line just
707c478bd9Sstevel@tonic-gate  * read in - print warning messages as errors are encountered.
717c478bd9Sstevel@tonic-gate  *
727c478bd9Sstevel@tonic-gate  * these are simple checks, but then they catch the simple errors-:)
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate static void
767c478bd9Sstevel@tonic-gate check_line(char *v[], int lc)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	if ((!v[NAME]) || ((int)strlen(v[NAME]) < 1))
797c478bd9Sstevel@tonic-gate 		error("bad name", lc);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	if ((!v[SRC]) || ((int)strlen(v[SRC])) < 1)
827c478bd9Sstevel@tonic-gate 		error("bad source/symbolic line", lc);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if ((!v[PERM]) || ((int)strlen(v[PERM]) < 3) || (!is_num(v[PERM])))
857c478bd9Sstevel@tonic-gate 		error("bad permissions", lc);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	if ((!v[OWNR]) || ((int)strlen(v[OWNR]) < 2))
887c478bd9Sstevel@tonic-gate 		error("bad owner", lc);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	if ((!v[GRP]) || ((int)strlen(v[GRP]) < 2))
917c478bd9Sstevel@tonic-gate 		error("bad group", lc);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	if ((!v[INO]) || (!is_num(v[INO])))
947c478bd9Sstevel@tonic-gate 		error("bad i-node", lc);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	if ((!v[LCNT]) || (!is_num(v[LCNT])))
977c478bd9Sstevel@tonic-gate 		error("bad link-count", lc);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if ((!v[CODE]) || ((*v[CODE] != 'f') && (*v[CODE] != 'c') &&
1007c478bd9Sstevel@tonic-gate 	    (*v[CODE] != 'd') && (*v[CODE] != 'b') &&
1017c478bd9Sstevel@tonic-gate 	    (*v[CODE] != 'v') && (*v[CODE] != 'e') &&
1027c478bd9Sstevel@tonic-gate 	    (*v[CODE] != 's')) || ((int)strlen(v[CODE]) > 1))
1037c478bd9Sstevel@tonic-gate 		error("bad type", lc);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if ((!v[MAJOR]) || ((!is_num(v[MAJOR])) && (*v[MAJOR] != '-')))
1067c478bd9Sstevel@tonic-gate 		error("bad major number", lc);
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	if ((!v[MINOR]) || ((!is_num(v[MINOR])) && (*v[MINOR] != '-')))
1097c478bd9Sstevel@tonic-gate 		error("bad minor number", lc);
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate static char **
1137c478bd9Sstevel@tonic-gate get_line(FILE *fp, char *v[])
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	char	*rc;
1167c478bd9Sstevel@tonic-gate 	char	*p;
1177c478bd9Sstevel@tonic-gate 	int	len;
1187c478bd9Sstevel@tonic-gate 	int	cont = 1;
1197c478bd9Sstevel@tonic-gate 	static char	buf[BUFSIZ];
1207c478bd9Sstevel@tonic-gate 	static int	line_count = 0;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	p = buf;
1237c478bd9Sstevel@tonic-gate 	p[0] = '\0';
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	do {
1267c478bd9Sstevel@tonic-gate 		rc = fgets(p, BUFSIZ, fp);
1277c478bd9Sstevel@tonic-gate 		line_count ++;
1287c478bd9Sstevel@tonic-gate 		/*
1297c478bd9Sstevel@tonic-gate 		 * check for continuation marks at the end of the
1307c478bd9Sstevel@tonic-gate 		 * line - if it exists then append the next line at the
1317c478bd9Sstevel@tonic-gate 		 * end of this one.
1327c478bd9Sstevel@tonic-gate 		 */
1337c478bd9Sstevel@tonic-gate 		if (buf[0] == '#') {
1347c478bd9Sstevel@tonic-gate 			/*
1357c478bd9Sstevel@tonic-gate 			 * skip comments.
1367c478bd9Sstevel@tonic-gate 			 */
1377c478bd9Sstevel@tonic-gate 			continue;
1387c478bd9Sstevel@tonic-gate 		} else if ((rc != NULL) && ((len = strlen(p)) > 1) &&
1397c478bd9Sstevel@tonic-gate 		    (p[len - 2] == '\\')) {
1407c478bd9Sstevel@tonic-gate 			/*
1417c478bd9Sstevel@tonic-gate 			 * check for continuation marks at the end of the
1427c478bd9Sstevel@tonic-gate 			 * line - if it exists then append the next line at the
1437c478bd9Sstevel@tonic-gate 			 * end of this one.
1447c478bd9Sstevel@tonic-gate 			 */
1457c478bd9Sstevel@tonic-gate 			p += len - 2;
1467c478bd9Sstevel@tonic-gate 		} else
1477c478bd9Sstevel@tonic-gate 			cont = 0;
1487c478bd9Sstevel@tonic-gate 	} while (cont);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	if (rc == NULL)
1517c478bd9Sstevel@tonic-gate 		return (NULL);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	/*
1547c478bd9Sstevel@tonic-gate 	 * breakup the line into the various fields.
1557c478bd9Sstevel@tonic-gate 	 */
1567c478bd9Sstevel@tonic-gate 	v[PROTOS] = index(buf, ';');
1577c478bd9Sstevel@tonic-gate 	if (v[PROTOS])
1587c478bd9Sstevel@tonic-gate 		*v[PROTOS]++ = '\0';
1597c478bd9Sstevel@tonic-gate 	v[0]  = strtok(buf, FS);
1607c478bd9Sstevel@tonic-gate 	for (cont = 1; cont < FIELDS - 1; cont++)
1617c478bd9Sstevel@tonic-gate 		v[cont] = strtok(NULL, FS);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	check_line(v, line_count);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	return (v);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate static void
1697c478bd9Sstevel@tonic-gate parse_line(char **v, elem *e)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	e->flag = 0;
1727c478bd9Sstevel@tonic-gate 	e->pkgs = NULL;
1737c478bd9Sstevel@tonic-gate 	e->arch = P_ISA;
1747c478bd9Sstevel@tonic-gate 	(void) strcpy(e->name, v[NAME]);
1757c478bd9Sstevel@tonic-gate 	e->perm = strtol(v[PERM], NULL, 8);
1767c478bd9Sstevel@tonic-gate 	(void) strcpy(e->owner, v[OWNR]);
1777c478bd9Sstevel@tonic-gate 	(void) strcpy(e->group, v[GRP]);
1787c478bd9Sstevel@tonic-gate 	e->inode = atoi(v[INO]);
1797c478bd9Sstevel@tonic-gate 	e->ref_cnt = atoi(v[LCNT]);
1807c478bd9Sstevel@tonic-gate 	e->file_type = *v[CODE];
1817c478bd9Sstevel@tonic-gate 	if ((v[MAJOR][0] == '-') && (v[MAJOR][1] == '\0'))
1827c478bd9Sstevel@tonic-gate 		e->major = -1;
1837c478bd9Sstevel@tonic-gate 	else
1847c478bd9Sstevel@tonic-gate 		e->major = atoi(v[MAJOR]);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if ((v[MINOR][0] == '-') && (v[MINOR][1] == '\0'))
1877c478bd9Sstevel@tonic-gate 		e->minor = -1;
1887c478bd9Sstevel@tonic-gate 	else
1897c478bd9Sstevel@tonic-gate 		e->minor = atoi(v[MINOR]);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	if ((v[SYM][0] == '-') && (v[SYM][1] == '\0'))
1927c478bd9Sstevel@tonic-gate 		e->symsrc = NULL;
1937c478bd9Sstevel@tonic-gate 	else {
1947c478bd9Sstevel@tonic-gate 		e->symsrc = malloc(strlen(v[SYM]) + 1);
1957c478bd9Sstevel@tonic-gate 		(void) strcpy(e->symsrc, v[SYM]);
1967c478bd9Sstevel@tonic-gate 			if (e->file_type != SYM_LINK_T)
197*ae115bc7Smrj #if defined(__sparc)
1987c478bd9Sstevel@tonic-gate 			if (strncmp(e->symsrc, "sun4/", 5) == 0)
1997c478bd9Sstevel@tonic-gate 				e->arch = P_SUN4;
2007c478bd9Sstevel@tonic-gate 			else if (strncmp(e->symsrc, "sun4c/", 6) == 0)
2017c478bd9Sstevel@tonic-gate 				e->arch = P_SUN4c;
2027c478bd9Sstevel@tonic-gate 			else if (strncmp(e->symsrc, "sun4u/", 6) == 0)
2037c478bd9Sstevel@tonic-gate 				e->arch = P_SUN4u;
2047c478bd9Sstevel@tonic-gate 			else if (strncmp(e->symsrc, "sun4d/", 6) == 0)
2057c478bd9Sstevel@tonic-gate 				e->arch = P_SUN4d;
2067c478bd9Sstevel@tonic-gate 			else if (strncmp(e->symsrc, "sun4e/", 6) == 0)
2077c478bd9Sstevel@tonic-gate 				e->arch = P_SUN4e;
2087c478bd9Sstevel@tonic-gate 			else if (strncmp(e->symsrc, "sun4m/", 6) == 0)
2097c478bd9Sstevel@tonic-gate 				e->arch = P_SUN4m;
2107c478bd9Sstevel@tonic-gate 			else if (strncmp(e->symsrc, "sun4v/", 6) == 0)
2117c478bd9Sstevel@tonic-gate 				e->arch = P_SUN4v;
212*ae115bc7Smrj #elif defined(__i386)
2137c478bd9Sstevel@tonic-gate 			if (strncmp(e->symsrc, "i86pc/", 6) == 0)
2147c478bd9Sstevel@tonic-gate 				e->arch = P_I86PC;
2157c478bd9Sstevel@tonic-gate #elif defined(__ppc)
2167c478bd9Sstevel@tonic-gate 			if (strncmp(e->symsrc, "prep/", 5) == 0)
2177c478bd9Sstevel@tonic-gate 				e->arch = P_PREP;
2187c478bd9Sstevel@tonic-gate #else
2197c478bd9Sstevel@tonic-gate #error "Unknown instruction set"
2207c478bd9Sstevel@tonic-gate #endif
2217c478bd9Sstevel@tonic-gate 			else {
2227c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2237c478bd9Sstevel@tonic-gate 				    "warning: Unknown relocation architecture "
2247c478bd9Sstevel@tonic-gate 				    "for %s\n", e->symsrc);
2257c478bd9Sstevel@tonic-gate 			}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate int
2317c478bd9Sstevel@tonic-gate read_in_protolist(const char *pname, elem_list *list, int verbose)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	FILE	*proto_fp;
2347c478bd9Sstevel@tonic-gate 	char	*line_vec[FIELDS];
2357c478bd9Sstevel@tonic-gate 	int	count = 0;
2367c478bd9Sstevel@tonic-gate 	static elem	*e = NULL;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	list->type = PROTOLIST_LIST;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	if ((proto_fp = fopen(pname, "r")) == NULL) {
2417c478bd9Sstevel@tonic-gate 		perror(pname);
2427c478bd9Sstevel@tonic-gate 		exit(1);
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if (verbose)
2467c478bd9Sstevel@tonic-gate 		(void) printf("reading in proto_list(%s)...\n", pname);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	count = 0;
2497c478bd9Sstevel@tonic-gate 	while (get_line(proto_fp, line_vec)) {
2507c478bd9Sstevel@tonic-gate 		if (!e)
2517c478bd9Sstevel@tonic-gate 			e = (elem *)calloc(1, sizeof (elem));
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 		parse_line(line_vec, e);
2547c478bd9Sstevel@tonic-gate 		if (!find_elem(list, e, FOLLOW_LINK)) {
2557c478bd9Sstevel@tonic-gate 			add_elem(list, e);
2567c478bd9Sstevel@tonic-gate 			e = NULL;
2577c478bd9Sstevel@tonic-gate 			count++;
2587c478bd9Sstevel@tonic-gate 		}
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if (verbose)
2627c478bd9Sstevel@tonic-gate 		(void) printf("read in %d lines\n", count);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	(void) fclose(proto_fp);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	return (count);
2677c478bd9Sstevel@tonic-gate }
268