10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*5208Sny155746  * Common Development and Distribution License (the "License").
6*5208Sny155746  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
220Sstevel@tonic-gate /*	  All Rights Reserved  	*/
230Sstevel@tonic-gate 
240Sstevel@tonic-gate /*
25*5208Sny155746  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
261404Sns158690  * Use is subject to license terms.
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
29*5208Sny155746 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1 */
300Sstevel@tonic-gate /*LINTLIBRARY*/
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*   5-20-92   newroot support added  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <stdio.h>
350Sstevel@tonic-gate #include <limits.h>
360Sstevel@tonic-gate #include <ctype.h>
370Sstevel@tonic-gate #include <errno.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <pkgstrct.h>
410Sstevel@tonic-gate #include <pkginfo.h>
420Sstevel@tonic-gate #include <pkglocs.h>
430Sstevel@tonic-gate #include <stdlib.h>
440Sstevel@tonic-gate #include <unistd.h>
450Sstevel@tonic-gate #include "libadm.h"
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #define	VALSIZ	128
480Sstevel@tonic-gate #define	NEWLINE	'\n'
490Sstevel@tonic-gate #define	ESCAPE	'\\'
500Sstevel@tonic-gate 
510Sstevel@tonic-gate static char sepset[] =	":=\n";
520Sstevel@tonic-gate static char qset[] = 	"'\"";
530Sstevel@tonic-gate static char *pkg_inst_root = NULL;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate char *pkgdir = NULL;
560Sstevel@tonic-gate char *pkgfile = NULL;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate static char Adm_pkgold[PATH_MAX] = { 0 }; /* added for newroot */
590Sstevel@tonic-gate static char Adm_pkgloc[PATH_MAX] = { 0 }; /* added for newroot */
600Sstevel@tonic-gate static char Adm_pkgadm[PATH_MAX] = { 0 }; /* added for newroot */
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate  * This looks in a directory that might be the top level directory of a
640Sstevel@tonic-gate  * package. It tests a temporary install directory first and then for a
650Sstevel@tonic-gate  * standard directory. This looks a little confusing, so here's what's
660Sstevel@tonic-gate  * happening. If this pkginfo is being openned in a script during a pkgadd
670Sstevel@tonic-gate  * which is updating an existing package, the original pkginfo file is in a
680Sstevel@tonic-gate  * directory that has been renamed from <pkginst> to .save.<pkginst>. If the
690Sstevel@tonic-gate  * pkgadd fails it will be renamed back to <pkginst>. We are always interested
700Sstevel@tonic-gate  * in the OLD pkginfo data because the new pkginfo data is already in our
710Sstevel@tonic-gate  * environment. For that reason, we try to open the backup first - that has
720Sstevel@tonic-gate  * the old data. This returns the first accessible path in "path" and a "1"
730Sstevel@tonic-gate  * if an appropriate pkginfo file was found. It returns a 0 if no type of
740Sstevel@tonic-gate  * pkginfo was located.
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate int
770Sstevel@tonic-gate pkginfofind(char *path, char *pkg_dir, char *pkginst)
780Sstevel@tonic-gate {
791404Sns158690 	int len = 0;
801404Sns158690 
810Sstevel@tonic-gate 	/* Construct the temporary pkginfo file name. */
821404Sns158690 	len =  snprintf(path, PATH_MAX, "%s/.save.%s/pkginfo", pkg_dir,
831404Sns158690 	    pkginst);
841404Sns158690 	if (len > PATH_MAX)
851404Sns158690 		return (0);
860Sstevel@tonic-gate 	if (access(path, 0)) {
870Sstevel@tonic-gate 		/*
880Sstevel@tonic-gate 		 * This isn't a temporary directory, so we look for a
890Sstevel@tonic-gate 		 * regular one.
900Sstevel@tonic-gate 		 */
911404Sns158690 		len =  snprintf(path, PATH_MAX, "%s/%s/pkginfo", pkg_dir,
921404Sns158690 		    pkginst);
931404Sns158690 		if (len > PATH_MAX)
941404Sns158690 			return (0);
950Sstevel@tonic-gate 		if (access(path, 0))
960Sstevel@tonic-gate 			return (0); /* doesn't appear to be a package */
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	return (1);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate  * This opens the appropriate pkginfo file for a particular package.
1040Sstevel@tonic-gate  */
1050Sstevel@tonic-gate FILE *
1060Sstevel@tonic-gate pkginfopen(char *pkg_dir, char *pkginst)
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate 	FILE *fp = NULL;
1090Sstevel@tonic-gate 	char temp[PATH_MAX];
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	if (pkginfofind(temp, pkg_dir, pkginst))
1120Sstevel@tonic-gate 		fp = fopen(temp, "r");
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	return (fp);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate char *
1190Sstevel@tonic-gate fpkgparam(FILE *fp, char *param)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate 	char	ch, buffer[VALSIZ];
1220Sstevel@tonic-gate 	char	*mempt, *copy;
123*5208Sny155746 	int	c, n;
124*5208Sny155746 	boolean_t check_end_quote = B_FALSE;
125*5208Sny155746 	boolean_t begline, quoted, escape;
126*5208Sny155746 	int idx = 0;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	if (param == NULL) {
1290Sstevel@tonic-gate 		errno = ENOENT;
1300Sstevel@tonic-gate 		return (NULL);
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	mempt = NULL;
1340Sstevel@tonic-gate 
135*5208Sny155746 	for (;;) {		/* For each entry in the file fp */
1360Sstevel@tonic-gate 		copy = buffer;
1370Sstevel@tonic-gate 		n = 0;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 		/* Get the next token. */
1400Sstevel@tonic-gate 		while ((c = getc(fp)) != EOF) {
1411404Sns158690 			ch = (char)c;
1420Sstevel@tonic-gate 			if (strchr(sepset, ch))
1430Sstevel@tonic-gate 				break;
1440Sstevel@tonic-gate 			if (++n < VALSIZ)
1450Sstevel@tonic-gate 				*copy++ = ch;
1460Sstevel@tonic-gate 		}
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 		/* If it's the end of the file, exit the for() loop */
1490Sstevel@tonic-gate 		if (c == EOF) {
1500Sstevel@tonic-gate 			errno = EINVAL;
151*5208Sny155746 			return (NULL); /* No more entries left */
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 		/* If it's end of line, look for the next parameter. */
1540Sstevel@tonic-gate 		} else if (c == NEWLINE)
1550Sstevel@tonic-gate 			continue;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 		/* At this point copy points to the end of a valid parameter. */
1580Sstevel@tonic-gate 		*copy = '\0';		/* Terminate the string. */
1590Sstevel@tonic-gate 		if (buffer[0] == '#')	/* If it's a comment, drop thru. */
1600Sstevel@tonic-gate 			copy = NULL;	/* Comments don't get buffered. */
1610Sstevel@tonic-gate 		else {
1620Sstevel@tonic-gate 			/* If parameter is NULL, we return whatever we got. */
1630Sstevel@tonic-gate 			if (param[0] == '\0') {
1640Sstevel@tonic-gate 				(void) strcpy(param, buffer);
1650Sstevel@tonic-gate 				copy = buffer;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 			/* If this doesn't match the parameter, drop thru. */
1680Sstevel@tonic-gate 			} else if (strcmp(param, buffer))
1690Sstevel@tonic-gate 				copy = NULL;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 			/* Otherwise, this is our boy. */
1720Sstevel@tonic-gate 			else
1730Sstevel@tonic-gate 				copy = buffer;
1740Sstevel@tonic-gate 		}
1750Sstevel@tonic-gate 
176*5208Sny155746 		n = 0;
177*5208Sny155746 		quoted = escape = B_FALSE;
178*5208Sny155746 		begline = B_TRUE; /* Value's line begins */
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 		/* Now read the parameter value. */
1810Sstevel@tonic-gate 		while ((c = getc(fp)) != EOF) {
1821404Sns158690 			ch = (char)c;
183*5208Sny155746 
1840Sstevel@tonic-gate 			if (begline && ((ch == ' ') || (ch == '\t')))
185*5208Sny155746 				continue; /* Ignore leading white space */
186*5208Sny155746 
187*5208Sny155746 			/*
188*5208Sny155746 			 * Take last end quote 'verbatim' if anything
189*5208Sny155746 			 * other than space, newline and escape.
190*5208Sny155746 			 * Example:
191*5208Sny155746 			 * PARAM1="zonename="test-zone""
192*5208Sny155746 			 *	Here in this example the letter 't' inside
193*5208Sny155746 			 *	the value is followed by '"', this makes
194*5208Sny155746 			 *	the previous end quote candidate '"',
195*5208Sny155746 			 *	a part of value and the end quote
196*5208Sny155746 			 *	disqualfies. Reset check_end_quote.
197*5208Sny155746 			 * PARAM2="value"<== newline here
198*5208Sny155746 			 * PARAM3="value"\
199*5208Sny155746 			 * "continued"<== newline here.
200*5208Sny155746 			 *	Check for end quote continues.
201*5208Sny155746 			 */
202*5208Sny155746 			if (ch != NEWLINE && ch != ' ' && ch != ESCAPE &&
203*5208Sny155746 			    ch != '\t' && check_end_quote)
204*5208Sny155746 				check_end_quote = B_FALSE;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 			if (ch == NEWLINE) {
207*5208Sny155746 				if (!escape) {
208*5208Sny155746 					/*
209*5208Sny155746 					 * The end quote candidate qualifies.
210*5208Sny155746 					 * Eat any trailing spaces.
211*5208Sny155746 					 */
212*5208Sny155746 					if (check_end_quote) {
213*5208Sny155746 						copy -= n - idx;
214*5208Sny155746 						n = idx;
215*5208Sny155746 						check_end_quote = B_FALSE;
216*5208Sny155746 						quoted = B_FALSE;
2170Sstevel@tonic-gate 					}
218*5208Sny155746 					break; /* End of entry */
2190Sstevel@tonic-gate 				}
220*5208Sny155746 				/*
221*5208Sny155746 				 * The end quote if exists, doesn't qualify.
222*5208Sny155746 				 * Eat end quote and trailing spaces if any.
223*5208Sny155746 				 * Value spans to next line.
224*5208Sny155746 				 */
225*5208Sny155746 				if (check_end_quote) {
226*5208Sny155746 					copy -= n - idx;
227*5208Sny155746 					n = idx;
228*5208Sny155746 					check_end_quote = B_FALSE;
229*5208Sny155746 				} else if (copy) {
230*5208Sny155746 					copy--; /* Eat previous esc */
231*5208Sny155746 					n--;
232*5208Sny155746 				}
233*5208Sny155746 				escape = B_FALSE;
234*5208Sny155746 				begline = B_TRUE; /* New input line */
235*5208Sny155746 				continue;
2360Sstevel@tonic-gate 			} else {
2370Sstevel@tonic-gate 				if (!escape && strchr(qset, ch)) {
238*5208Sny155746 					/* Handle quotes */
2390Sstevel@tonic-gate 					if (begline) {
240*5208Sny155746 						/* Starting quote */
241*5208Sny155746 						quoted = B_TRUE;
242*5208Sny155746 						begline = B_FALSE;
2430Sstevel@tonic-gate 						continue;
2440Sstevel@tonic-gate 					} else if (quoted) {
245*5208Sny155746 						/*
246*5208Sny155746 						 * This is the candidate
247*5208Sny155746 						 * for end quote. Check
248*5208Sny155746 						 * to see it qualifies.
249*5208Sny155746 						 */
250*5208Sny155746 						check_end_quote = B_TRUE;
251*5208Sny155746 						idx = n;
2520Sstevel@tonic-gate 					}
2530Sstevel@tonic-gate 				}
2540Sstevel@tonic-gate 				if (ch == ESCAPE)
255*5208Sny155746 					escape = B_TRUE;
2560Sstevel@tonic-gate 				else if (escape)
257*5208Sny155746 					escape = B_FALSE;
2580Sstevel@tonic-gate 				if (copy) *copy++ = ch;
259*5208Sny155746 				begline = B_FALSE;
2600Sstevel@tonic-gate 			}
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 			if (copy && ((++n % VALSIZ) == 0)) {
2630Sstevel@tonic-gate 				if (mempt) {
2640Sstevel@tonic-gate 					mempt = realloc(mempt,
265*5208Sny155746 					    (n+VALSIZ)*sizeof (char));
2660Sstevel@tonic-gate 					if (!mempt)
2670Sstevel@tonic-gate 						return (NULL);
2680Sstevel@tonic-gate 				} else {
2690Sstevel@tonic-gate 					mempt = calloc((size_t)(2*VALSIZ),
2700Sstevel@tonic-gate 					    sizeof (char));
2710Sstevel@tonic-gate 					if (!mempt)
2720Sstevel@tonic-gate 						return (NULL);
2730Sstevel@tonic-gate 					(void) strncpy(mempt, buffer, n);
2740Sstevel@tonic-gate 				}
2750Sstevel@tonic-gate 				copy = &mempt[n];
2760Sstevel@tonic-gate 			}
2770Sstevel@tonic-gate 		}
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 		/*
2800Sstevel@tonic-gate 		 * Don't allow trailing white space.
2810Sstevel@tonic-gate 		 * NOTE : White space in the middle is OK, since this may
2820Sstevel@tonic-gate 		 * be a list. At some point it would be a good idea to let
2830Sstevel@tonic-gate 		 * this function know how to validate such a list. -- JST
2840Sstevel@tonic-gate 		 *
2850Sstevel@tonic-gate 		 * Now while there's a parametric value and it ends in a
2860Sstevel@tonic-gate 		 * space and the actual remaining string length is still
2870Sstevel@tonic-gate 		 * greater than 0, back over the space.
2880Sstevel@tonic-gate 		 */
2890Sstevel@tonic-gate 		while (copy && isspace((unsigned char)*(copy - 1)) && n-- > 0)
2900Sstevel@tonic-gate 			copy--;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 		if (quoted) {
2930Sstevel@tonic-gate 			if (mempt)
2940Sstevel@tonic-gate 				(void) free(mempt);
2950Sstevel@tonic-gate 			errno = EFAULT; /* missing closing quote */
2960Sstevel@tonic-gate 			return (NULL);
2970Sstevel@tonic-gate 		}
2980Sstevel@tonic-gate 		if (copy) {
2990Sstevel@tonic-gate 			*copy = '\0';
3000Sstevel@tonic-gate 			break;
3010Sstevel@tonic-gate 		}
3020Sstevel@tonic-gate 		if (c == EOF) {
3030Sstevel@tonic-gate 			errno = EINVAL; /* parameter not found */
3040Sstevel@tonic-gate 			return (NULL);
3050Sstevel@tonic-gate 		}
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	if (!mempt)
3090Sstevel@tonic-gate 		mempt = strdup(buffer);
3100Sstevel@tonic-gate 	else
3110Sstevel@tonic-gate 		mempt = realloc(mempt, (strlen(mempt)+1)*sizeof (char));
3120Sstevel@tonic-gate 	return (mempt);
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate char *
3160Sstevel@tonic-gate pkgparam(char *pkg, char *param)
3170Sstevel@tonic-gate {
3180Sstevel@tonic-gate 	static char lastfname[PATH_MAX];
3190Sstevel@tonic-gate 	static FILE *fp = NULL;
3200Sstevel@tonic-gate 	char *pt, *copy, *value, line[PATH_MAX];
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	if (!pkgdir)
3230Sstevel@tonic-gate 		pkgdir = get_PKGLOC();
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	if (!pkg) {
3260Sstevel@tonic-gate 		/* request to close file */
3270Sstevel@tonic-gate 		if (fp) {
3280Sstevel@tonic-gate 			(void) fclose(fp);
3290Sstevel@tonic-gate 			fp = NULL;
3300Sstevel@tonic-gate 		}
3310Sstevel@tonic-gate 		return (NULL);
3320Sstevel@tonic-gate 	}
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	if (!param) {
3350Sstevel@tonic-gate 		errno = ENOENT;
3360Sstevel@tonic-gate 		return (NULL);
3370Sstevel@tonic-gate 	}
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	if (pkgfile)
3400Sstevel@tonic-gate 		(void) strcpy(line, pkgfile); /* filename was passed */
3410Sstevel@tonic-gate 	else
3420Sstevel@tonic-gate 		(void) pkginfofind(line, pkgdir, pkg);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	if (fp && strcmp(line, lastfname)) {
3450Sstevel@tonic-gate 		/* different filename implies need for different fp */
3460Sstevel@tonic-gate 		(void) fclose(fp);
3470Sstevel@tonic-gate 		fp = NULL;
3480Sstevel@tonic-gate 	}
3490Sstevel@tonic-gate 	if (!fp) {
3500Sstevel@tonic-gate 		(void) strcpy(lastfname, line);
3510Sstevel@tonic-gate 		if ((fp = fopen(lastfname, "r")) == NULL)
3520Sstevel@tonic-gate 			return (NULL);
3530Sstevel@tonic-gate 	}
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	/*
3560Sstevel@tonic-gate 	 * if parameter is a null string, then the user is requesting us
3570Sstevel@tonic-gate 	 * to find the value of the next available parameter for this
3580Sstevel@tonic-gate 	 * package and to copy the parameter name into the provided string;
3590Sstevel@tonic-gate 	 * if it is not, then it is a request for a specified parameter, in
3600Sstevel@tonic-gate 	 * which case we rewind the file to start search from beginning
3610Sstevel@tonic-gate 	 */
3620Sstevel@tonic-gate 	if (param[0]) {
3630Sstevel@tonic-gate 		/* new parameter request, so reset file position */
3640Sstevel@tonic-gate 		if (fseek(fp, 0L, 0))
3650Sstevel@tonic-gate 			return (NULL);
3660Sstevel@tonic-gate 	}
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	if (pt = fpkgparam(fp, param)) {
3690Sstevel@tonic-gate 		if (strcmp(param, "ARCH") == NULL ||
3700Sstevel@tonic-gate 		    strcmp(param, "CATEGORY") == NULL) {
3710Sstevel@tonic-gate 			/* remove all whitespace from value */
3720Sstevel@tonic-gate 			value = copy = pt;
3730Sstevel@tonic-gate 			while (*value) {
3740Sstevel@tonic-gate 				if (!isspace((unsigned char)*value))
3750Sstevel@tonic-gate 					*copy++ = *value;
3760Sstevel@tonic-gate 				value++;
3770Sstevel@tonic-gate 			}
3780Sstevel@tonic-gate 			*copy = '\0';
3790Sstevel@tonic-gate 		}
3800Sstevel@tonic-gate 		return (pt);
3810Sstevel@tonic-gate 	}
3820Sstevel@tonic-gate 	return (NULL);
3830Sstevel@tonic-gate }
3840Sstevel@tonic-gate /*
3850Sstevel@tonic-gate  * This routine sets adm_pkgloc and adm_pkgadm which are the
3860Sstevel@tonic-gate  * replacement location for PKGLOC and PKGADM.
3870Sstevel@tonic-gate  */
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate static void canonize_name(char *);
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate void
3920Sstevel@tonic-gate set_PKGpaths(char *path)
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	if (path && *path) {
3950Sstevel@tonic-gate 		(void) sprintf(Adm_pkgloc, "%s%s", path, PKGLOC);
3960Sstevel@tonic-gate 		(void) sprintf(Adm_pkgold, "%s%s", path, PKGOLD);
3970Sstevel@tonic-gate 		(void) sprintf(Adm_pkgadm, "%s%s", path, PKGADM);
3980Sstevel@tonic-gate 		set_install_root(path);
3990Sstevel@tonic-gate 	} else {
4000Sstevel@tonic-gate 		(void) sprintf(Adm_pkgloc, "%s", PKGLOC);
4010Sstevel@tonic-gate 		(void) sprintf(Adm_pkgold, "%s", PKGOLD);
4020Sstevel@tonic-gate 		(void) sprintf(Adm_pkgadm, "%s", PKGADM);
4030Sstevel@tonic-gate 	}
4040Sstevel@tonic-gate 	canonize_name(Adm_pkgloc);
4050Sstevel@tonic-gate 	canonize_name(Adm_pkgold);
4060Sstevel@tonic-gate 	canonize_name(Adm_pkgadm);
4070Sstevel@tonic-gate 	pkgdir = Adm_pkgloc;
4080Sstevel@tonic-gate }
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate char *
4110Sstevel@tonic-gate get_PKGLOC(void)
4120Sstevel@tonic-gate {
4130Sstevel@tonic-gate 	if (Adm_pkgloc[0] == NULL)
4140Sstevel@tonic-gate 		return (PKGLOC);
4150Sstevel@tonic-gate 	else
4160Sstevel@tonic-gate 		return (Adm_pkgloc);
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate char *
4200Sstevel@tonic-gate get_PKGOLD(void)
4210Sstevel@tonic-gate {
4220Sstevel@tonic-gate 	if (Adm_pkgold[0] == NULL)
4230Sstevel@tonic-gate 		return (PKGOLD);
4240Sstevel@tonic-gate 	else
4250Sstevel@tonic-gate 		return (Adm_pkgold);
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate char *
4290Sstevel@tonic-gate get_PKGADM(void)
4300Sstevel@tonic-gate {
4310Sstevel@tonic-gate 	if (Adm_pkgadm[0] == NULL)
4320Sstevel@tonic-gate 		return (PKGADM);
4330Sstevel@tonic-gate 	else
4340Sstevel@tonic-gate 		return (Adm_pkgadm);
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate void
4380Sstevel@tonic-gate set_PKGADM(char *newpath)
4390Sstevel@tonic-gate {
4400Sstevel@tonic-gate 	(void) strcpy(Adm_pkgadm, newpath);
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate void
4440Sstevel@tonic-gate set_PKGLOC(char *newpath)
4450Sstevel@tonic-gate {
4460Sstevel@tonic-gate 	(void) strcpy(Adm_pkgloc, newpath);
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate #define	isdot(x)	((x[0] == '.')&&(!x[1]||(x[1] == '/')))
4500Sstevel@tonic-gate #define	isdotdot(x)	((x[0] == '.')&&(x[1] == '.')&&(!x[2]||(x[2] == '/')))
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate static void
4530Sstevel@tonic-gate canonize_name(char *file)
4540Sstevel@tonic-gate {
4550Sstevel@tonic-gate 	char *pt, *last;
4560Sstevel@tonic-gate 	int level;
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	/* Remove references such as "./" and "../" and "//" */
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	for (pt = file; *pt; ) {
4610Sstevel@tonic-gate 		if (isdot(pt))
4620Sstevel@tonic-gate 			(void) strcpy(pt, pt[1] ? pt+2 : pt+1);
4630Sstevel@tonic-gate 		else if (isdotdot(pt)) {
4640Sstevel@tonic-gate 			level = 0;
4650Sstevel@tonic-gate 			last = pt;
4660Sstevel@tonic-gate 			do {
4670Sstevel@tonic-gate 				level++;
4680Sstevel@tonic-gate 				last += 2;
4690Sstevel@tonic-gate 				if (*last)
4700Sstevel@tonic-gate 					last++;
4710Sstevel@tonic-gate 			} while (isdotdot(last));
4720Sstevel@tonic-gate 			--pt; /* point to previous '/' */
4730Sstevel@tonic-gate 			while (level--) {
4740Sstevel@tonic-gate 				if (pt <= file)
4750Sstevel@tonic-gate 					return;
4760Sstevel@tonic-gate 				while ((*--pt != '/') && (pt > file))
4770Sstevel@tonic-gate 					;
4780Sstevel@tonic-gate 			}
4790Sstevel@tonic-gate 			if (*pt == '/')
4800Sstevel@tonic-gate 				pt++;
4810Sstevel@tonic-gate 			(void) strcpy(pt, last);
4820Sstevel@tonic-gate 		} else {
4830Sstevel@tonic-gate 			while (*pt && (*pt != '/'))
4840Sstevel@tonic-gate 				pt++;
4850Sstevel@tonic-gate 			if (*pt == '/') {
4860Sstevel@tonic-gate 				while (pt[1] == '/')
4870Sstevel@tonic-gate 					(void) strcpy(pt, pt+1);
4880Sstevel@tonic-gate 				pt++;
4890Sstevel@tonic-gate 			}
4900Sstevel@tonic-gate 		}
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate 	if ((--pt > file) && (*pt == '/'))
4930Sstevel@tonic-gate 		*pt = '\0';
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate void
4970Sstevel@tonic-gate set_install_root(char *path)
4980Sstevel@tonic-gate {
4990Sstevel@tonic-gate 	pkg_inst_root = strdup(path);
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate char *
5030Sstevel@tonic-gate get_install_root()
5040Sstevel@tonic-gate {
5050Sstevel@tonic-gate 	return (pkg_inst_root);
5060Sstevel@tonic-gate }
507