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