xref: /titanic_54/usr/src/lib/libadm/common/pkgparam.c (revision 4f5c01f9a280183d32b11a58cc2701be45f1a8aa)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
26*4f5c01f9Sns158690  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
27*4f5c01f9Sns158690  * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma	ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1 */
317c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*   5-20-92   newroot support added  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <limits.h>
377c478bd9Sstevel@tonic-gate #include <ctype.h>
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <pkgstrct.h>
427c478bd9Sstevel@tonic-gate #include <pkginfo.h>
437c478bd9Sstevel@tonic-gate #include <pkglocs.h>
447c478bd9Sstevel@tonic-gate #include <stdlib.h>
457c478bd9Sstevel@tonic-gate #include <unistd.h>
467c478bd9Sstevel@tonic-gate #include "libadm.h"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	VALSIZ	128
497c478bd9Sstevel@tonic-gate #define	NEWLINE	'\n'
507c478bd9Sstevel@tonic-gate #define	ESCAPE	'\\'
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static char sepset[] =	":=\n";
537c478bd9Sstevel@tonic-gate static char qset[] = 	"'\"";
547c478bd9Sstevel@tonic-gate static char *pkg_inst_root = NULL;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate char *pkgdir = NULL;
577c478bd9Sstevel@tonic-gate char *pkgfile = NULL;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static char Adm_pkgold[PATH_MAX] = { 0 }; /* added for newroot */
607c478bd9Sstevel@tonic-gate static char Adm_pkgloc[PATH_MAX] = { 0 }; /* added for newroot */
617c478bd9Sstevel@tonic-gate static char Adm_pkgadm[PATH_MAX] = { 0 }; /* added for newroot */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  * This looks in a directory that might be the top level directory of a
657c478bd9Sstevel@tonic-gate  * package. It tests a temporary install directory first and then for a
667c478bd9Sstevel@tonic-gate  * standard directory. This looks a little confusing, so here's what's
677c478bd9Sstevel@tonic-gate  * happening. If this pkginfo is being openned in a script during a pkgadd
687c478bd9Sstevel@tonic-gate  * which is updating an existing package, the original pkginfo file is in a
697c478bd9Sstevel@tonic-gate  * directory that has been renamed from <pkginst> to .save.<pkginst>. If the
707c478bd9Sstevel@tonic-gate  * pkgadd fails it will be renamed back to <pkginst>. We are always interested
717c478bd9Sstevel@tonic-gate  * in the OLD pkginfo data because the new pkginfo data is already in our
727c478bd9Sstevel@tonic-gate  * environment. For that reason, we try to open the backup first - that has
737c478bd9Sstevel@tonic-gate  * the old data. This returns the first accessible path in "path" and a "1"
747c478bd9Sstevel@tonic-gate  * if an appropriate pkginfo file was found. It returns a 0 if no type of
757c478bd9Sstevel@tonic-gate  * pkginfo was located.
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate int
787c478bd9Sstevel@tonic-gate pkginfofind(char *path, char *pkg_dir, char *pkginst)
797c478bd9Sstevel@tonic-gate {
80*4f5c01f9Sns158690 	int len = 0;
81*4f5c01f9Sns158690 
827c478bd9Sstevel@tonic-gate 	/* Construct the temporary pkginfo file name. */
83*4f5c01f9Sns158690 	len =  snprintf(path, PATH_MAX, "%s/.save.%s/pkginfo", pkg_dir,
84*4f5c01f9Sns158690 	    pkginst);
85*4f5c01f9Sns158690 	if (len > PATH_MAX)
86*4f5c01f9Sns158690 		return (0);
877c478bd9Sstevel@tonic-gate 	if (access(path, 0)) {
887c478bd9Sstevel@tonic-gate 		/*
897c478bd9Sstevel@tonic-gate 		 * This isn't a temporary directory, so we look for a
907c478bd9Sstevel@tonic-gate 		 * regular one.
917c478bd9Sstevel@tonic-gate 		 */
92*4f5c01f9Sns158690 		len =  snprintf(path, PATH_MAX, "%s/%s/pkginfo", pkg_dir,
93*4f5c01f9Sns158690 		    pkginst);
94*4f5c01f9Sns158690 		if (len > PATH_MAX)
95*4f5c01f9Sns158690 			return (0);
967c478bd9Sstevel@tonic-gate 		if (access(path, 0))
977c478bd9Sstevel@tonic-gate 			return (0); /* doesn't appear to be a package */
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	return (1);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate  * This opens the appropriate pkginfo file for a particular package.
1057c478bd9Sstevel@tonic-gate  */
1067c478bd9Sstevel@tonic-gate FILE *
1077c478bd9Sstevel@tonic-gate pkginfopen(char *pkg_dir, char *pkginst)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	FILE *fp = NULL;
1107c478bd9Sstevel@tonic-gate 	char temp[PATH_MAX];
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	if (pkginfofind(temp, pkg_dir, pkginst))
1137c478bd9Sstevel@tonic-gate 		fp = fopen(temp, "r");
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	return (fp);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate char *
1207c478bd9Sstevel@tonic-gate fpkgparam(FILE *fp, char *param)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	char	ch, buffer[VALSIZ];
1237c478bd9Sstevel@tonic-gate 	char	*mempt, *copy;
1247c478bd9Sstevel@tonic-gate 	int	c, n, escape, begline, quoted;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	if (param == NULL) {
1277c478bd9Sstevel@tonic-gate 		errno = ENOENT;
1287c478bd9Sstevel@tonic-gate 		return (NULL);
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	mempt = NULL;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	for (;;) {		/* for each entry in the file fp */
1347c478bd9Sstevel@tonic-gate 		copy = buffer;
1357c478bd9Sstevel@tonic-gate 		n = 0;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 		/* Get the next token. */
1387c478bd9Sstevel@tonic-gate 		while ((c = getc(fp)) != EOF) {
1397c478bd9Sstevel@tonic-gate 			ch = (char)c;
1407c478bd9Sstevel@tonic-gate 			if (strchr(sepset, ch))
1417c478bd9Sstevel@tonic-gate 				break;
1427c478bd9Sstevel@tonic-gate 			if (++n < VALSIZ)
1437c478bd9Sstevel@tonic-gate 				*copy++ = ch;
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 		/* If it's the end of the file, exit the for() loop */
1477c478bd9Sstevel@tonic-gate 		if (c == EOF) {
1487c478bd9Sstevel@tonic-gate 			errno = EINVAL;
1497c478bd9Sstevel@tonic-gate 			return (NULL); /* no more entries left */
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 		/* If it's end of line, look for the next parameter. */
1527c478bd9Sstevel@tonic-gate 		} else if (c == NEWLINE)
1537c478bd9Sstevel@tonic-gate 			continue;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 		/* At this point copy points to the end of a valid parameter. */
1567c478bd9Sstevel@tonic-gate 		*copy = '\0';		/* Terminate the string. */
1577c478bd9Sstevel@tonic-gate 		if (buffer[0] == '#')	/* If it's a comment, drop thru. */
1587c478bd9Sstevel@tonic-gate 			copy = NULL;	/* Comments don't get buffered. */
1597c478bd9Sstevel@tonic-gate 		else {
1607c478bd9Sstevel@tonic-gate 			/* If parameter is NULL, we return whatever we got. */
1617c478bd9Sstevel@tonic-gate 			if (param[0] == '\0') {
1627c478bd9Sstevel@tonic-gate 				(void) strcpy(param, buffer);
1637c478bd9Sstevel@tonic-gate 				copy = buffer;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 			/* If this doesn't match the parameter, drop thru. */
1667c478bd9Sstevel@tonic-gate 			} else if (strcmp(param, buffer))
1677c478bd9Sstevel@tonic-gate 				copy = NULL;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 			/* Otherwise, this is our boy. */
1707c478bd9Sstevel@tonic-gate 			else
1717c478bd9Sstevel@tonic-gate 				copy = buffer;
1727c478bd9Sstevel@tonic-gate 		}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 		n = quoted = escape = 0;
1757c478bd9Sstevel@tonic-gate 		begline = 1;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 		/* Now read the parameter value. */
1787c478bd9Sstevel@tonic-gate 		while ((c = getc(fp)) != EOF) {
1797c478bd9Sstevel@tonic-gate 			ch = (char)c;
1807c478bd9Sstevel@tonic-gate 			if (begline && ((ch == ' ') || (ch == '\t')))
1817c478bd9Sstevel@tonic-gate 				continue; /* ignore leading white space */
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 			if (ch == NEWLINE) {
1847c478bd9Sstevel@tonic-gate 				if (!escape)
1857c478bd9Sstevel@tonic-gate 					break; /* end of entry */
1867c478bd9Sstevel@tonic-gate 				if (copy) {
1877c478bd9Sstevel@tonic-gate 					if (escape) {
1887c478bd9Sstevel@tonic-gate 						copy--; /* eat previous esc */
1897c478bd9Sstevel@tonic-gate 						n--;
1907c478bd9Sstevel@tonic-gate 					}
1917c478bd9Sstevel@tonic-gate 					*copy++ = NEWLINE;
1927c478bd9Sstevel@tonic-gate 				}
1937c478bd9Sstevel@tonic-gate 				escape = 0;
1947c478bd9Sstevel@tonic-gate 				begline = 1; /* new input line */
1957c478bd9Sstevel@tonic-gate 			} else {
1967c478bd9Sstevel@tonic-gate 				if (!escape && strchr(qset, ch)) {
1977c478bd9Sstevel@tonic-gate 					/* handle quotes */
1987c478bd9Sstevel@tonic-gate 					if (begline) {
1997c478bd9Sstevel@tonic-gate 						quoted++;
2007c478bd9Sstevel@tonic-gate 						begline = 0;
2017c478bd9Sstevel@tonic-gate 						continue;
2027c478bd9Sstevel@tonic-gate 					} else if (quoted) {
2037c478bd9Sstevel@tonic-gate 						quoted = 0;
2047c478bd9Sstevel@tonic-gate 						continue;
2057c478bd9Sstevel@tonic-gate 					}
2067c478bd9Sstevel@tonic-gate 				}
2077c478bd9Sstevel@tonic-gate 				if (ch == ESCAPE)
2087c478bd9Sstevel@tonic-gate 					escape++;
2097c478bd9Sstevel@tonic-gate 				else if (escape)
2107c478bd9Sstevel@tonic-gate 					escape = 0;
2117c478bd9Sstevel@tonic-gate 				if (copy) *copy++ = ch;
2127c478bd9Sstevel@tonic-gate 				begline = 0;
2137c478bd9Sstevel@tonic-gate 			}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 			if (copy && ((++n % VALSIZ) == 0)) {
2167c478bd9Sstevel@tonic-gate 				if (mempt) {
2177c478bd9Sstevel@tonic-gate 					mempt = realloc(mempt,
2187c478bd9Sstevel@tonic-gate 						(n+VALSIZ)*sizeof (char));
2197c478bd9Sstevel@tonic-gate 					if (!mempt)
2207c478bd9Sstevel@tonic-gate 						return (NULL);
2217c478bd9Sstevel@tonic-gate 				} else {
2227c478bd9Sstevel@tonic-gate 					mempt = calloc((size_t)(2*VALSIZ),
2237c478bd9Sstevel@tonic-gate 					    sizeof (char));
2247c478bd9Sstevel@tonic-gate 					if (!mempt)
2257c478bd9Sstevel@tonic-gate 						return (NULL);
2267c478bd9Sstevel@tonic-gate 					(void) strncpy(mempt, buffer, n);
2277c478bd9Sstevel@tonic-gate 				}
2287c478bd9Sstevel@tonic-gate 				copy = &mempt[n];
2297c478bd9Sstevel@tonic-gate 			}
2307c478bd9Sstevel@tonic-gate 		}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		/*
2337c478bd9Sstevel@tonic-gate 		 * Don't allow trailing white space.
2347c478bd9Sstevel@tonic-gate 		 * NOTE : White space in the middle is OK, since this may
2357c478bd9Sstevel@tonic-gate 		 * be a list. At some point it would be a good idea to let
2367c478bd9Sstevel@tonic-gate 		 * this function know how to validate such a list. -- JST
2377c478bd9Sstevel@tonic-gate 		 *
2387c478bd9Sstevel@tonic-gate 		 * Now while there's a parametric value and it ends in a
2397c478bd9Sstevel@tonic-gate 		 * space and the actual remaining string length is still
2407c478bd9Sstevel@tonic-gate 		 * greater than 0, back over the space.
2417c478bd9Sstevel@tonic-gate 		 */
2427c478bd9Sstevel@tonic-gate 		while (copy && isspace((unsigned char)*(copy - 1)) && n-- > 0)
2437c478bd9Sstevel@tonic-gate 			copy--;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 		if (quoted) {
2467c478bd9Sstevel@tonic-gate 			if (mempt)
2477c478bd9Sstevel@tonic-gate 				(void) free(mempt);
2487c478bd9Sstevel@tonic-gate 			errno = EFAULT; /* missing closing quote */
2497c478bd9Sstevel@tonic-gate 			return (NULL);
2507c478bd9Sstevel@tonic-gate 		}
2517c478bd9Sstevel@tonic-gate 		if (copy) {
2527c478bd9Sstevel@tonic-gate 			*copy = '\0';
2537c478bd9Sstevel@tonic-gate 			break;
2547c478bd9Sstevel@tonic-gate 		}
2557c478bd9Sstevel@tonic-gate 		if (c == EOF) {
2567c478bd9Sstevel@tonic-gate 			errno = EINVAL; /* parameter not found */
2577c478bd9Sstevel@tonic-gate 			return (NULL);
2587c478bd9Sstevel@tonic-gate 		}
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if (!mempt)
2627c478bd9Sstevel@tonic-gate 		mempt = strdup(buffer);
2637c478bd9Sstevel@tonic-gate 	else
2647c478bd9Sstevel@tonic-gate 		mempt = realloc(mempt, (strlen(mempt)+1)*sizeof (char));
2657c478bd9Sstevel@tonic-gate 	return (mempt);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate char *
2697c478bd9Sstevel@tonic-gate pkgparam(char *pkg, char *param)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 	static char lastfname[PATH_MAX];
2727c478bd9Sstevel@tonic-gate 	static FILE *fp = NULL;
2737c478bd9Sstevel@tonic-gate 	char *pt, *copy, *value, line[PATH_MAX];
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	if (!pkgdir)
2767c478bd9Sstevel@tonic-gate 		pkgdir = get_PKGLOC();
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	if (!pkg) {
2797c478bd9Sstevel@tonic-gate 		/* request to close file */
2807c478bd9Sstevel@tonic-gate 		if (fp) {
2817c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
2827c478bd9Sstevel@tonic-gate 			fp = NULL;
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 		return (NULL);
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	if (!param) {
2887c478bd9Sstevel@tonic-gate 		errno = ENOENT;
2897c478bd9Sstevel@tonic-gate 		return (NULL);
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	if (pkgfile)
2937c478bd9Sstevel@tonic-gate 		(void) strcpy(line, pkgfile); /* filename was passed */
2947c478bd9Sstevel@tonic-gate 	else
2957c478bd9Sstevel@tonic-gate 		(void) pkginfofind(line, pkgdir, pkg);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	if (fp && strcmp(line, lastfname)) {
2987c478bd9Sstevel@tonic-gate 		/* different filename implies need for different fp */
2997c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
3007c478bd9Sstevel@tonic-gate 		fp = NULL;
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 	if (!fp) {
3037c478bd9Sstevel@tonic-gate 		(void) strcpy(lastfname, line);
3047c478bd9Sstevel@tonic-gate 		if ((fp = fopen(lastfname, "r")) == NULL)
3057c478bd9Sstevel@tonic-gate 			return (NULL);
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	/*
3097c478bd9Sstevel@tonic-gate 	 * if parameter is a null string, then the user is requesting us
3107c478bd9Sstevel@tonic-gate 	 * to find the value of the next available parameter for this
3117c478bd9Sstevel@tonic-gate 	 * package and to copy the parameter name into the provided string;
3127c478bd9Sstevel@tonic-gate 	 * if it is not, then it is a request for a specified parameter, in
3137c478bd9Sstevel@tonic-gate 	 * which case we rewind the file to start search from beginning
3147c478bd9Sstevel@tonic-gate 	 */
3157c478bd9Sstevel@tonic-gate 	if (param[0]) {
3167c478bd9Sstevel@tonic-gate 		/* new parameter request, so reset file position */
3177c478bd9Sstevel@tonic-gate 		if (fseek(fp, 0L, 0))
3187c478bd9Sstevel@tonic-gate 			return (NULL);
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	if (pt = fpkgparam(fp, param)) {
3227c478bd9Sstevel@tonic-gate 		if (strcmp(param, "ARCH") == NULL ||
3237c478bd9Sstevel@tonic-gate 		    strcmp(param, "CATEGORY") == NULL) {
3247c478bd9Sstevel@tonic-gate 			/* remove all whitespace from value */
3257c478bd9Sstevel@tonic-gate 			value = copy = pt;
3267c478bd9Sstevel@tonic-gate 			while (*value) {
3277c478bd9Sstevel@tonic-gate 				if (!isspace((unsigned char)*value))
3287c478bd9Sstevel@tonic-gate 					*copy++ = *value;
3297c478bd9Sstevel@tonic-gate 				value++;
3307c478bd9Sstevel@tonic-gate 			}
3317c478bd9Sstevel@tonic-gate 			*copy = '\0';
3327c478bd9Sstevel@tonic-gate 		}
3337c478bd9Sstevel@tonic-gate 		return (pt);
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 	return (NULL);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate /*
3387c478bd9Sstevel@tonic-gate  * This routine sets adm_pkgloc and adm_pkgadm which are the
3397c478bd9Sstevel@tonic-gate  * replacement location for PKGLOC and PKGADM.
3407c478bd9Sstevel@tonic-gate  */
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate static void canonize_name(char *);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate void
3457c478bd9Sstevel@tonic-gate set_PKGpaths(char *path)
3467c478bd9Sstevel@tonic-gate {
3477c478bd9Sstevel@tonic-gate 	if (path && *path) {
3487c478bd9Sstevel@tonic-gate 		(void) sprintf(Adm_pkgloc, "%s%s", path, PKGLOC);
3497c478bd9Sstevel@tonic-gate 		(void) sprintf(Adm_pkgold, "%s%s", path, PKGOLD);
3507c478bd9Sstevel@tonic-gate 		(void) sprintf(Adm_pkgadm, "%s%s", path, PKGADM);
3517c478bd9Sstevel@tonic-gate 		set_install_root(path);
3527c478bd9Sstevel@tonic-gate 	} else {
3537c478bd9Sstevel@tonic-gate 		(void) sprintf(Adm_pkgloc, "%s", PKGLOC);
3547c478bd9Sstevel@tonic-gate 		(void) sprintf(Adm_pkgold, "%s", PKGOLD);
3557c478bd9Sstevel@tonic-gate 		(void) sprintf(Adm_pkgadm, "%s", PKGADM);
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 	canonize_name(Adm_pkgloc);
3587c478bd9Sstevel@tonic-gate 	canonize_name(Adm_pkgold);
3597c478bd9Sstevel@tonic-gate 	canonize_name(Adm_pkgadm);
3607c478bd9Sstevel@tonic-gate 	pkgdir = Adm_pkgloc;
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate char *
3647c478bd9Sstevel@tonic-gate get_PKGLOC(void)
3657c478bd9Sstevel@tonic-gate {
3667c478bd9Sstevel@tonic-gate 	if (Adm_pkgloc[0] == NULL)
3677c478bd9Sstevel@tonic-gate 		return (PKGLOC);
3687c478bd9Sstevel@tonic-gate 	else
3697c478bd9Sstevel@tonic-gate 		return (Adm_pkgloc);
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate char *
3737c478bd9Sstevel@tonic-gate get_PKGOLD(void)
3747c478bd9Sstevel@tonic-gate {
3757c478bd9Sstevel@tonic-gate 	if (Adm_pkgold[0] == NULL)
3767c478bd9Sstevel@tonic-gate 		return (PKGOLD);
3777c478bd9Sstevel@tonic-gate 	else
3787c478bd9Sstevel@tonic-gate 		return (Adm_pkgold);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate char *
3827c478bd9Sstevel@tonic-gate get_PKGADM(void)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	if (Adm_pkgadm[0] == NULL)
3857c478bd9Sstevel@tonic-gate 		return (PKGADM);
3867c478bd9Sstevel@tonic-gate 	else
3877c478bd9Sstevel@tonic-gate 		return (Adm_pkgadm);
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate void
3917c478bd9Sstevel@tonic-gate set_PKGADM(char *newpath)
3927c478bd9Sstevel@tonic-gate {
3937c478bd9Sstevel@tonic-gate 	(void) strcpy(Adm_pkgadm, newpath);
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate void
3977c478bd9Sstevel@tonic-gate set_PKGLOC(char *newpath)
3987c478bd9Sstevel@tonic-gate {
3997c478bd9Sstevel@tonic-gate 	(void) strcpy(Adm_pkgloc, newpath);
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate #define	isdot(x)	((x[0] == '.')&&(!x[1]||(x[1] == '/')))
4037c478bd9Sstevel@tonic-gate #define	isdotdot(x)	((x[0] == '.')&&(x[1] == '.')&&(!x[2]||(x[2] == '/')))
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate static void
4067c478bd9Sstevel@tonic-gate canonize_name(char *file)
4077c478bd9Sstevel@tonic-gate {
4087c478bd9Sstevel@tonic-gate 	char *pt, *last;
4097c478bd9Sstevel@tonic-gate 	int level;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	/* Remove references such as "./" and "../" and "//" */
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	for (pt = file; *pt; ) {
4147c478bd9Sstevel@tonic-gate 		if (isdot(pt))
4157c478bd9Sstevel@tonic-gate 			(void) strcpy(pt, pt[1] ? pt+2 : pt+1);
4167c478bd9Sstevel@tonic-gate 		else if (isdotdot(pt)) {
4177c478bd9Sstevel@tonic-gate 			level = 0;
4187c478bd9Sstevel@tonic-gate 			last = pt;
4197c478bd9Sstevel@tonic-gate 			do {
4207c478bd9Sstevel@tonic-gate 				level++;
4217c478bd9Sstevel@tonic-gate 				last += 2;
4227c478bd9Sstevel@tonic-gate 				if (*last)
4237c478bd9Sstevel@tonic-gate 					last++;
4247c478bd9Sstevel@tonic-gate 			} while (isdotdot(last));
4257c478bd9Sstevel@tonic-gate 			--pt; /* point to previous '/' */
4267c478bd9Sstevel@tonic-gate 			while (level--) {
4277c478bd9Sstevel@tonic-gate 				if (pt <= file)
4287c478bd9Sstevel@tonic-gate 					return;
4297c478bd9Sstevel@tonic-gate 				while ((*--pt != '/') && (pt > file))
4307c478bd9Sstevel@tonic-gate 					;
4317c478bd9Sstevel@tonic-gate 			}
4327c478bd9Sstevel@tonic-gate 			if (*pt == '/')
4337c478bd9Sstevel@tonic-gate 				pt++;
4347c478bd9Sstevel@tonic-gate 			(void) strcpy(pt, last);
4357c478bd9Sstevel@tonic-gate 		} else {
4367c478bd9Sstevel@tonic-gate 			while (*pt && (*pt != '/'))
4377c478bd9Sstevel@tonic-gate 				pt++;
4387c478bd9Sstevel@tonic-gate 			if (*pt == '/') {
4397c478bd9Sstevel@tonic-gate 				while (pt[1] == '/')
4407c478bd9Sstevel@tonic-gate 					(void) strcpy(pt, pt+1);
4417c478bd9Sstevel@tonic-gate 				pt++;
4427c478bd9Sstevel@tonic-gate 			}
4437c478bd9Sstevel@tonic-gate 		}
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 	if ((--pt > file) && (*pt == '/'))
4467c478bd9Sstevel@tonic-gate 		*pt = '\0';
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate void
4507c478bd9Sstevel@tonic-gate set_install_root(char *path)
4517c478bd9Sstevel@tonic-gate {
4527c478bd9Sstevel@tonic-gate 	pkg_inst_root = strdup(path);
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate char *
4567c478bd9Sstevel@tonic-gate get_install_root()
4577c478bd9Sstevel@tonic-gate {
4587c478bd9Sstevel@tonic-gate 	return (pkg_inst_root);
4597c478bd9Sstevel@tonic-gate }
460