xref: /illumos-gate/usr/src/tools/protocmp/depend.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #include <stdio.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
32*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
33*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
34*7c478bd9Sstevel@tonic-gate #include <strings.h>
35*7c478bd9Sstevel@tonic-gate #include <errno.h>
36*7c478bd9Sstevel@tonic-gate #include <dirent.h>
37*7c478bd9Sstevel@tonic-gate #include <ctype.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #include "list.h"
41*7c478bd9Sstevel@tonic-gate #include "protodir.h"
42*7c478bd9Sstevel@tonic-gate #include "arch.h"
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate static pkg_list *packages[HASH_SIZE];
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate #define	HASH(name) (hash(name) % HASH_SIZE)
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate int
49*7c478bd9Sstevel@tonic-gate processed_package(const char *pkgname)
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate 	int	bucket;
52*7c478bd9Sstevel@tonic-gate 	pkg_list *tmp;
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 	bucket = HASH(pkgname);
55*7c478bd9Sstevel@tonic-gate 	for (tmp = packages[bucket]; tmp != NULL; tmp = tmp->next) {
56*7c478bd9Sstevel@tonic-gate 		if (strcmp(tmp->pkg_name, pkgname) == 0)
57*7c478bd9Sstevel@tonic-gate 			return (1);
58*7c478bd9Sstevel@tonic-gate 	}
59*7c478bd9Sstevel@tonic-gate 	return (0);
60*7c478bd9Sstevel@tonic-gate }
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate void
63*7c478bd9Sstevel@tonic-gate mark_processed(const char *pkgname)
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate 	int	bucket;
66*7c478bd9Sstevel@tonic-gate 	pkg_list *tmp;
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	bucket = HASH(pkgname);
69*7c478bd9Sstevel@tonic-gate 	tmp = malloc(sizeof (pkg_list));
70*7c478bd9Sstevel@tonic-gate 	bzero(tmp, sizeof (pkg_list));
71*7c478bd9Sstevel@tonic-gate 	(void) strcpy(tmp->pkg_name, pkgname);
72*7c478bd9Sstevel@tonic-gate 	tmp->next = packages[bucket];
73*7c478bd9Sstevel@tonic-gate 	packages[bucket] = tmp;
74*7c478bd9Sstevel@tonic-gate }
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate static pkg_list *
77*7c478bd9Sstevel@tonic-gate add_dependency(pkg_list *dependlist, const char *pkgname)
78*7c478bd9Sstevel@tonic-gate {
79*7c478bd9Sstevel@tonic-gate 	pkg_list *tmp;
80*7c478bd9Sstevel@tonic-gate 	pkg_list *pkg;
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	pkg = malloc(sizeof (pkg_list));
83*7c478bd9Sstevel@tonic-gate 	bzero(pkg, sizeof (pkg_list));
84*7c478bd9Sstevel@tonic-gate 	(void) strcpy(pkg->pkg_name, pkgname);
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	/* easy case */
87*7c478bd9Sstevel@tonic-gate 	if (dependlist == NULL)
88*7c478bd9Sstevel@tonic-gate 		return (pkg);
89*7c478bd9Sstevel@tonic-gate 	/* insert at end, since the order matters */
90*7c478bd9Sstevel@tonic-gate 	for (tmp = dependlist; tmp->next != NULL; tmp = tmp->next) {
91*7c478bd9Sstevel@tonic-gate 		/* NULL */
92*7c478bd9Sstevel@tonic-gate 	}
93*7c478bd9Sstevel@tonic-gate 	tmp->next = pkg;
94*7c478bd9Sstevel@tonic-gate 	return (dependlist);
95*7c478bd9Sstevel@tonic-gate }
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate static void
98*7c478bd9Sstevel@tonic-gate free_dependency_list(pkg_list *dependlist)
99*7c478bd9Sstevel@tonic-gate {
100*7c478bd9Sstevel@tonic-gate 	pkg_list *tmp;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	while (dependlist) {
103*7c478bd9Sstevel@tonic-gate 		tmp = dependlist;
104*7c478bd9Sstevel@tonic-gate 		dependlist = dependlist->next;
105*7c478bd9Sstevel@tonic-gate 		tmp->next = NULL;
106*7c478bd9Sstevel@tonic-gate 		free(tmp);
107*7c478bd9Sstevel@tonic-gate 	}
108*7c478bd9Sstevel@tonic-gate }
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
111*7c478bd9Sstevel@tonic-gate void
112*7c478bd9Sstevel@tonic-gate print_dependencies(const char *pkgname, pkg_list *dependlist)
113*7c478bd9Sstevel@tonic-gate {
114*7c478bd9Sstevel@tonic-gate 	pkg_list *tmp;
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s:", pkgname);
117*7c478bd9Sstevel@tonic-gate 	for (tmp = dependlist; tmp != NULL; tmp = tmp->next)
118*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, " %s", tmp->pkg_name);
119*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\n");
120*7c478bd9Sstevel@tonic-gate }
121*7c478bd9Sstevel@tonic-gate #endif
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate static char *suffix_list[] = {
124*7c478bd9Sstevel@tonic-gate #if defined(__i386)
125*7c478bd9Sstevel@tonic-gate 	".i",
126*7c478bd9Sstevel@tonic-gate #elif defined(__sparc)
127*7c478bd9Sstevel@tonic-gate 	".c",
128*7c478bd9Sstevel@tonic-gate 	".d",
129*7c478bd9Sstevel@tonic-gate 	".m",
130*7c478bd9Sstevel@tonic-gate 	".u",
131*7c478bd9Sstevel@tonic-gate #else
132*7c478bd9Sstevel@tonic-gate #error "Unknown architecture."
133*7c478bd9Sstevel@tonic-gate #endif
134*7c478bd9Sstevel@tonic-gate 	NULL,
135*7c478bd9Sstevel@tonic-gate };
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate static pkg_list *
138*7c478bd9Sstevel@tonic-gate find_dependencies(const char *pkgname, const char *parentdir)
139*7c478bd9Sstevel@tonic-gate {
140*7c478bd9Sstevel@tonic-gate 	char	dependfile[MAXPATHLEN + 1];
141*7c478bd9Sstevel@tonic-gate 	char	pkgdir[MAXPATHLEN + 1];
142*7c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ];
143*7c478bd9Sstevel@tonic-gate 	char	deppkg[MAXNAME];
144*7c478bd9Sstevel@tonic-gate 	char	archpkg[MAXNAME];
145*7c478bd9Sstevel@tonic-gate 	struct stat sbuf;
146*7c478bd9Sstevel@tonic-gate 	FILE	*fp;
147*7c478bd9Sstevel@tonic-gate 	pkg_list *dependlist = NULL;
148*7c478bd9Sstevel@tonic-gate 	char	**suffixes;
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	(void) sprintf(dependfile, "%s/%s/depend", parentdir, pkgname);
151*7c478bd9Sstevel@tonic-gate 	fp = fopen(dependfile, "r");
152*7c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
153*7c478bd9Sstevel@tonic-gate 		/*
154*7c478bd9Sstevel@tonic-gate 		 * depend won't exist in ON packages until a build
155*7c478bd9Sstevel@tonic-gate 		 * has been done, but it would be nice if you didn't have
156*7c478bd9Sstevel@tonic-gate 		 * to do that. So try the generic depend file that those
157*7c478bd9Sstevel@tonic-gate 		 * packages would copy in during the build.
158*7c478bd9Sstevel@tonic-gate 		 */
159*7c478bd9Sstevel@tonic-gate 		(void) sprintf(dependfile, "%s/common_files/depend", parentdir);
160*7c478bd9Sstevel@tonic-gate 		fp = fopen(dependfile, "r");
161*7c478bd9Sstevel@tonic-gate 		if (fp == NULL)
162*7c478bd9Sstevel@tonic-gate 			return (NULL);
163*7c478bd9Sstevel@tonic-gate 	}
164*7c478bd9Sstevel@tonic-gate 	while (fgets(buf, BUFSIZ, fp) != NULL) {
165*7c478bd9Sstevel@tonic-gate 		if ((buf[0] == '\0') || (buf[0] == '#') || isspace(buf[0]))
166*7c478bd9Sstevel@tonic-gate 			continue;
167*7c478bd9Sstevel@tonic-gate 		/* we only care about prerequisites */
168*7c478bd9Sstevel@tonic-gate 		if (buf[0] != 'P')
169*7c478bd9Sstevel@tonic-gate 			continue;
170*7c478bd9Sstevel@tonic-gate 		(void) sscanf(buf, "P %s", deppkg);
171*7c478bd9Sstevel@tonic-gate 		/*
172*7c478bd9Sstevel@tonic-gate 		 * We have to be careful with some of the packages that are
173*7c478bd9Sstevel@tonic-gate 		 * listed as dependencies but exist under a different name -
174*7c478bd9Sstevel@tonic-gate 		 * SUNWcar is good, because it's actually SUNWcar.{c,d,i,m,u}.
175*7c478bd9Sstevel@tonic-gate 		 * What do we do there? We can't just go for all the '.'
176*7c478bd9Sstevel@tonic-gate 		 * packages, since on x86 we only want the .i one, and on sparc
177*7c478bd9Sstevel@tonic-gate 		 * we want everything _but_ .i. Maybe
178*7c478bd9Sstevel@tonic-gate 		 *
179*7c478bd9Sstevel@tonic-gate 		 * I think perhaps what we do is, if we don't find a package
180*7c478bd9Sstevel@tonic-gate 		 * dependency, on intel we append '.i' and try for that, and on
181*7c478bd9Sstevel@tonic-gate 		 * sparc we try the other extensions. Any we find get added.
182*7c478bd9Sstevel@tonic-gate 		 *
183*7c478bd9Sstevel@tonic-gate 		 * Note also we're quiet on failures. This is because you might
184*7c478bd9Sstevel@tonic-gate 		 * be dependant on some outside package.
185*7c478bd9Sstevel@tonic-gate 		 */
186*7c478bd9Sstevel@tonic-gate 		(void) sprintf(pkgdir, "%s/%s", parentdir, deppkg);
187*7c478bd9Sstevel@tonic-gate 		if (stat(pkgdir, &sbuf) == -1) {
188*7c478bd9Sstevel@tonic-gate 			if (errno != ENOENT) {
189*7c478bd9Sstevel@tonic-gate 				continue;
190*7c478bd9Sstevel@tonic-gate 			}
191*7c478bd9Sstevel@tonic-gate 			for (suffixes = &suffix_list[0]; *suffixes != NULL;
192*7c478bd9Sstevel@tonic-gate 			    suffixes++) {
193*7c478bd9Sstevel@tonic-gate 				(void) sprintf(archpkg, "%s%s", deppkg,
194*7c478bd9Sstevel@tonic-gate 				    *suffixes);
195*7c478bd9Sstevel@tonic-gate 				(void) sprintf(pkgdir, "%s/%s", parentdir,
196*7c478bd9Sstevel@tonic-gate 				    archpkg);
197*7c478bd9Sstevel@tonic-gate 				if (stat(pkgdir, &sbuf) == -1) {
198*7c478bd9Sstevel@tonic-gate 					continue;
199*7c478bd9Sstevel@tonic-gate 				}
200*7c478bd9Sstevel@tonic-gate 				if (!S_ISDIR(sbuf.st_mode)) {
201*7c478bd9Sstevel@tonic-gate 					continue;
202*7c478bd9Sstevel@tonic-gate 				}
203*7c478bd9Sstevel@tonic-gate 				/* found one */
204*7c478bd9Sstevel@tonic-gate 				dependlist = add_dependency(dependlist,
205*7c478bd9Sstevel@tonic-gate 				    archpkg);
206*7c478bd9Sstevel@tonic-gate 			}
207*7c478bd9Sstevel@tonic-gate 		}
208*7c478bd9Sstevel@tonic-gate 		if (!S_ISDIR(sbuf.st_mode)) {
209*7c478bd9Sstevel@tonic-gate 			continue;
210*7c478bd9Sstevel@tonic-gate 		}
211*7c478bd9Sstevel@tonic-gate 		dependlist = add_dependency(dependlist, deppkg);
212*7c478bd9Sstevel@tonic-gate 	}
213*7c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
214*7c478bd9Sstevel@tonic-gate 	return (dependlist);
215*7c478bd9Sstevel@tonic-gate }
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate int
218*7c478bd9Sstevel@tonic-gate process_dependencies(const char *pkgname, const char *parentdir,
219*7c478bd9Sstevel@tonic-gate     elem_list *list, int verbose)
220*7c478bd9Sstevel@tonic-gate {
221*7c478bd9Sstevel@tonic-gate 	int	count = 0;
222*7c478bd9Sstevel@tonic-gate 	char	pkgdir[MAXPATHLEN + 1];
223*7c478bd9Sstevel@tonic-gate 	pkg_list *dependlist;
224*7c478bd9Sstevel@tonic-gate 	pkg_list *tmp;
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 	dependlist = find_dependencies(pkgname, parentdir);
227*7c478bd9Sstevel@tonic-gate /*
228*7c478bd9Sstevel@tonic-gate  *	print_dependencies(pkgname, dependlist);
229*7c478bd9Sstevel@tonic-gate  */
230*7c478bd9Sstevel@tonic-gate 	if (dependlist == NULL)
231*7c478bd9Sstevel@tonic-gate 		return (0);
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 	for (tmp = dependlist; tmp != NULL; tmp = tmp->next) {
234*7c478bd9Sstevel@tonic-gate 		(void) sprintf(pkgdir, "%s/%s", parentdir, tmp->pkg_name);
235*7c478bd9Sstevel@tonic-gate 		count += process_package_dir(tmp->pkg_name, pkgdir, list,
236*7c478bd9Sstevel@tonic-gate 		    verbose);
237*7c478bd9Sstevel@tonic-gate 	}
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 	free_dependency_list(dependlist);
240*7c478bd9Sstevel@tonic-gate 	return (count);
241*7c478bd9Sstevel@tonic-gate }
242