xref: /illumos-gate/usr/src/tools/protocmp/depend.c (revision f6f0fcc0)
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*f6f0fcc0SRainer Orth  * Common Development and Distribution License (the "License").
6*f6f0fcc0SRainer Orth  * 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  */
21*f6f0fcc0SRainer Orth 
227c478bd9Sstevel@tonic-gate /*
23*f6f0fcc0SRainer Orth  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <fcntl.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <strings.h>
337c478bd9Sstevel@tonic-gate #include <errno.h>
347c478bd9Sstevel@tonic-gate #include <dirent.h>
357c478bd9Sstevel@tonic-gate #include <ctype.h>
367c478bd9Sstevel@tonic-gate #include <sys/stat.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include "list.h"
397c478bd9Sstevel@tonic-gate #include "protodir.h"
407c478bd9Sstevel@tonic-gate #include "arch.h"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate static pkg_list *packages[HASH_SIZE];
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #define	HASH(name) (hash(name) % HASH_SIZE)
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate int
processed_package(const char * pkgname)477c478bd9Sstevel@tonic-gate processed_package(const char *pkgname)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	int	bucket;
507c478bd9Sstevel@tonic-gate 	pkg_list *tmp;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	bucket = HASH(pkgname);
537c478bd9Sstevel@tonic-gate 	for (tmp = packages[bucket]; tmp != NULL; tmp = tmp->next) {
547c478bd9Sstevel@tonic-gate 		if (strcmp(tmp->pkg_name, pkgname) == 0)
557c478bd9Sstevel@tonic-gate 			return (1);
567c478bd9Sstevel@tonic-gate 	}
577c478bd9Sstevel@tonic-gate 	return (0);
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate void
mark_processed(const char * pkgname)617c478bd9Sstevel@tonic-gate mark_processed(const char *pkgname)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	int	bucket;
647c478bd9Sstevel@tonic-gate 	pkg_list *tmp;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	bucket = HASH(pkgname);
677c478bd9Sstevel@tonic-gate 	tmp = malloc(sizeof (pkg_list));
687c478bd9Sstevel@tonic-gate 	bzero(tmp, sizeof (pkg_list));
697c478bd9Sstevel@tonic-gate 	(void) strcpy(tmp->pkg_name, pkgname);
707c478bd9Sstevel@tonic-gate 	tmp->next = packages[bucket];
717c478bd9Sstevel@tonic-gate 	packages[bucket] = tmp;
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static pkg_list *
add_dependency(pkg_list * dependlist,const char * pkgname)757c478bd9Sstevel@tonic-gate add_dependency(pkg_list *dependlist, const char *pkgname)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	pkg_list *tmp;
787c478bd9Sstevel@tonic-gate 	pkg_list *pkg;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	pkg = malloc(sizeof (pkg_list));
817c478bd9Sstevel@tonic-gate 	bzero(pkg, sizeof (pkg_list));
827c478bd9Sstevel@tonic-gate 	(void) strcpy(pkg->pkg_name, pkgname);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	/* easy case */
857c478bd9Sstevel@tonic-gate 	if (dependlist == NULL)
867c478bd9Sstevel@tonic-gate 		return (pkg);
877c478bd9Sstevel@tonic-gate 	/* insert at end, since the order matters */
887c478bd9Sstevel@tonic-gate 	for (tmp = dependlist; tmp->next != NULL; tmp = tmp->next) {
897c478bd9Sstevel@tonic-gate 		/* NULL */
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 	tmp->next = pkg;
927c478bd9Sstevel@tonic-gate 	return (dependlist);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static void
free_dependency_list(pkg_list * dependlist)967c478bd9Sstevel@tonic-gate free_dependency_list(pkg_list *dependlist)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	pkg_list *tmp;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	while (dependlist) {
1017c478bd9Sstevel@tonic-gate 		tmp = dependlist;
1027c478bd9Sstevel@tonic-gate 		dependlist = dependlist->next;
1037c478bd9Sstevel@tonic-gate 		tmp->next = NULL;
1047c478bd9Sstevel@tonic-gate 		free(tmp);
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate #ifdef DEBUG
1097c478bd9Sstevel@tonic-gate void
print_dependencies(const char * pkgname,pkg_list * dependlist)1107c478bd9Sstevel@tonic-gate print_dependencies(const char *pkgname, pkg_list *dependlist)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	pkg_list *tmp;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s:", pkgname);
1157c478bd9Sstevel@tonic-gate 	for (tmp = dependlist; tmp != NULL; tmp = tmp->next)
1167c478bd9Sstevel@tonic-gate 		fprintf(stderr, " %s", tmp->pkg_name);
1177c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\n");
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate #endif
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate static char *suffix_list[] = {
1227c478bd9Sstevel@tonic-gate #if defined(__i386)
1237c478bd9Sstevel@tonic-gate 	".i",
1247c478bd9Sstevel@tonic-gate #elif defined(__sparc)
1257c478bd9Sstevel@tonic-gate 	".c",
1267c478bd9Sstevel@tonic-gate 	".d",
1277c478bd9Sstevel@tonic-gate 	".m",
1287c478bd9Sstevel@tonic-gate 	".u",
129*f6f0fcc0SRainer Orth 	".v",
1307c478bd9Sstevel@tonic-gate #else
1317c478bd9Sstevel@tonic-gate #error "Unknown architecture."
1327c478bd9Sstevel@tonic-gate #endif
1337c478bd9Sstevel@tonic-gate 	NULL,
1347c478bd9Sstevel@tonic-gate };
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate static pkg_list *
find_dependencies(const char * pkgname,const char * parentdir)1377c478bd9Sstevel@tonic-gate find_dependencies(const char *pkgname, const char *parentdir)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	char	dependfile[MAXPATHLEN + 1];
1407c478bd9Sstevel@tonic-gate 	char	pkgdir[MAXPATHLEN + 1];
1417c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ];
1427c478bd9Sstevel@tonic-gate 	char	deppkg[MAXNAME];
1437c478bd9Sstevel@tonic-gate 	char	archpkg[MAXNAME];
1447c478bd9Sstevel@tonic-gate 	struct stat sbuf;
1457c478bd9Sstevel@tonic-gate 	FILE	*fp;
1467c478bd9Sstevel@tonic-gate 	pkg_list *dependlist = NULL;
1477c478bd9Sstevel@tonic-gate 	char	**suffixes;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	(void) sprintf(dependfile, "%s/%s/depend", parentdir, pkgname);
1507c478bd9Sstevel@tonic-gate 	fp = fopen(dependfile, "r");
1517c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
1527c478bd9Sstevel@tonic-gate 		/*
1537c478bd9Sstevel@tonic-gate 		 * depend won't exist in ON packages until a build
1547c478bd9Sstevel@tonic-gate 		 * has been done, but it would be nice if you didn't have
1557c478bd9Sstevel@tonic-gate 		 * to do that. So try the generic depend file that those
1567c478bd9Sstevel@tonic-gate 		 * packages would copy in during the build.
1577c478bd9Sstevel@tonic-gate 		 */
1587c478bd9Sstevel@tonic-gate 		(void) sprintf(dependfile, "%s/common_files/depend", parentdir);
1597c478bd9Sstevel@tonic-gate 		fp = fopen(dependfile, "r");
1607c478bd9Sstevel@tonic-gate 		if (fp == NULL)
1617c478bd9Sstevel@tonic-gate 			return (NULL);
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 	while (fgets(buf, BUFSIZ, fp) != NULL) {
1647c478bd9Sstevel@tonic-gate 		if ((buf[0] == '\0') || (buf[0] == '#') || isspace(buf[0]))
1657c478bd9Sstevel@tonic-gate 			continue;
1667c478bd9Sstevel@tonic-gate 		/* we only care about prerequisites */
1677c478bd9Sstevel@tonic-gate 		if (buf[0] != 'P')
1687c478bd9Sstevel@tonic-gate 			continue;
1697c478bd9Sstevel@tonic-gate 		(void) sscanf(buf, "P %s", deppkg);
1707c478bd9Sstevel@tonic-gate 		/*
1717c478bd9Sstevel@tonic-gate 		 * We have to be careful with some of the packages that are
1727c478bd9Sstevel@tonic-gate 		 * listed as dependencies but exist under a different name -
1737c478bd9Sstevel@tonic-gate 		 * SUNWcar is good, because it's actually SUNWcar.{c,d,i,m,u}.
1747c478bd9Sstevel@tonic-gate 		 * What do we do there? We can't just go for all the '.'
1757c478bd9Sstevel@tonic-gate 		 * packages, since on x86 we only want the .i one, and on sparc
1767c478bd9Sstevel@tonic-gate 		 * we want everything _but_ .i. Maybe
1777c478bd9Sstevel@tonic-gate 		 *
1787c478bd9Sstevel@tonic-gate 		 * I think perhaps what we do is, if we don't find a package
1797c478bd9Sstevel@tonic-gate 		 * dependency, on intel we append '.i' and try for that, and on
1807c478bd9Sstevel@tonic-gate 		 * sparc we try the other extensions. Any we find get added.
1817c478bd9Sstevel@tonic-gate 		 *
1827c478bd9Sstevel@tonic-gate 		 * Note also we're quiet on failures. This is because you might
1837c478bd9Sstevel@tonic-gate 		 * be dependant on some outside package.
1847c478bd9Sstevel@tonic-gate 		 */
1857c478bd9Sstevel@tonic-gate 		(void) sprintf(pkgdir, "%s/%s", parentdir, deppkg);
1867c478bd9Sstevel@tonic-gate 		if (stat(pkgdir, &sbuf) == -1) {
1877c478bd9Sstevel@tonic-gate 			if (errno != ENOENT) {
1887c478bd9Sstevel@tonic-gate 				continue;
1897c478bd9Sstevel@tonic-gate 			}
1907c478bd9Sstevel@tonic-gate 			for (suffixes = &suffix_list[0]; *suffixes != NULL;
1917c478bd9Sstevel@tonic-gate 			    suffixes++) {
1927c478bd9Sstevel@tonic-gate 				(void) sprintf(archpkg, "%s%s", deppkg,
1937c478bd9Sstevel@tonic-gate 				    *suffixes);
1947c478bd9Sstevel@tonic-gate 				(void) sprintf(pkgdir, "%s/%s", parentdir,
1957c478bd9Sstevel@tonic-gate 				    archpkg);
1967c478bd9Sstevel@tonic-gate 				if (stat(pkgdir, &sbuf) == -1) {
1977c478bd9Sstevel@tonic-gate 					continue;
1987c478bd9Sstevel@tonic-gate 				}
1997c478bd9Sstevel@tonic-gate 				if (!S_ISDIR(sbuf.st_mode)) {
2007c478bd9Sstevel@tonic-gate 					continue;
2017c478bd9Sstevel@tonic-gate 				}
2027c478bd9Sstevel@tonic-gate 				/* found one */
2037c478bd9Sstevel@tonic-gate 				dependlist = add_dependency(dependlist,
2047c478bd9Sstevel@tonic-gate 				    archpkg);
2057c478bd9Sstevel@tonic-gate 			}
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 		if (!S_ISDIR(sbuf.st_mode)) {
2087c478bd9Sstevel@tonic-gate 			continue;
2097c478bd9Sstevel@tonic-gate 		}
2107c478bd9Sstevel@tonic-gate 		dependlist = add_dependency(dependlist, deppkg);
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
2137c478bd9Sstevel@tonic-gate 	return (dependlist);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate int
process_dependencies(const char * pkgname,const char * parentdir,elem_list * list,int verbose)2177c478bd9Sstevel@tonic-gate process_dependencies(const char *pkgname, const char *parentdir,
2187c478bd9Sstevel@tonic-gate     elem_list *list, int verbose)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	int	count = 0;
2217c478bd9Sstevel@tonic-gate 	char	pkgdir[MAXPATHLEN + 1];
2227c478bd9Sstevel@tonic-gate 	pkg_list *dependlist;
2237c478bd9Sstevel@tonic-gate 	pkg_list *tmp;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	dependlist = find_dependencies(pkgname, parentdir);
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate  *	print_dependencies(pkgname, dependlist);
2287c478bd9Sstevel@tonic-gate  */
2297c478bd9Sstevel@tonic-gate 	if (dependlist == NULL)
2307c478bd9Sstevel@tonic-gate 		return (0);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	for (tmp = dependlist; tmp != NULL; tmp = tmp->next) {
2337c478bd9Sstevel@tonic-gate 		(void) sprintf(pkgdir, "%s/%s", parentdir, tmp->pkg_name);
2347c478bd9Sstevel@tonic-gate 		count += process_package_dir(tmp->pkg_name, pkgdir, list,
2357c478bd9Sstevel@tonic-gate 		    verbose);
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	free_dependency_list(dependlist);
2397c478bd9Sstevel@tonic-gate 	return (count);
2407c478bd9Sstevel@tonic-gate }
241