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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
230Sstevel@tonic-gate /*	  All Rights Reserved  	*/
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
26*1404Sns158690  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
27*1404Sns158690  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma	ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1 */
310Sstevel@tonic-gate /*LINTLIBRARY*/
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /*   5-20-92   newroot support added  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include <stdio.h>
360Sstevel@tonic-gate #include <limits.h>
370Sstevel@tonic-gate #include <ctype.h>
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate #include <string.h>
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <pkgstrct.h>
420Sstevel@tonic-gate #include <pkginfo.h>
430Sstevel@tonic-gate #include <pkglocs.h>
440Sstevel@tonic-gate #include <stdlib.h>
450Sstevel@tonic-gate #include <unistd.h>
460Sstevel@tonic-gate #include "libadm.h"
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #define	VALSIZ	128
490Sstevel@tonic-gate #define	NEWLINE	'\n'
500Sstevel@tonic-gate #define	ESCAPE	'\\'
510Sstevel@tonic-gate 
520Sstevel@tonic-gate static char sepset[] =	":=\n";
530Sstevel@tonic-gate static char qset[] = 	"'\"";
540Sstevel@tonic-gate static char *pkg_inst_root = NULL;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate char *pkgdir = NULL;
570Sstevel@tonic-gate char *pkgfile = NULL;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate static char Adm_pkgold[PATH_MAX] = { 0 }; /* added for newroot */
600Sstevel@tonic-gate static char Adm_pkgloc[PATH_MAX] = { 0 }; /* added for newroot */
610Sstevel@tonic-gate static char Adm_pkgadm[PATH_MAX] = { 0 }; /* added for newroot */
620Sstevel@tonic-gate 
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate  * This looks in a directory that might be the top level directory of a
650Sstevel@tonic-gate  * package. It tests a temporary install directory first and then for a
660Sstevel@tonic-gate  * standard directory. This looks a little confusing, so here's what's
670Sstevel@tonic-gate  * happening. If this pkginfo is being openned in a script during a pkgadd
680Sstevel@tonic-gate  * which is updating an existing package, the original pkginfo file is in a
690Sstevel@tonic-gate  * directory that has been renamed from <pkginst> to .save.<pkginst>. If the
700Sstevel@tonic-gate  * pkgadd fails it will be renamed back to <pkginst>. We are always interested
710Sstevel@tonic-gate  * in the OLD pkginfo data because the new pkginfo data is already in our
720Sstevel@tonic-gate  * environment. For that reason, we try to open the backup first - that has
730Sstevel@tonic-gate  * the old data. This returns the first accessible path in "path" and a "1"
740Sstevel@tonic-gate  * if an appropriate pkginfo file was found. It returns a 0 if no type of
750Sstevel@tonic-gate  * pkginfo was located.
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate int
780Sstevel@tonic-gate pkginfofind(char *path, char *pkg_dir, char *pkginst)
790Sstevel@tonic-gate {
80*1404Sns158690 	int len = 0;
81*1404Sns158690 
820Sstevel@tonic-gate 	/* Construct the temporary pkginfo file name. */
83*1404Sns158690 	len =  snprintf(path, PATH_MAX, "%s/.save.%s/pkginfo", pkg_dir,
84*1404Sns158690 	    pkginst);
85*1404Sns158690 	if (len > PATH_MAX)
86*1404Sns158690 		return (0);
870Sstevel@tonic-gate 	if (access(path, 0)) {
880Sstevel@tonic-gate 		/*
890Sstevel@tonic-gate 		 * This isn't a temporary directory, so we look for a
900Sstevel@tonic-gate 		 * regular one.
910Sstevel@tonic-gate 		 */
92*1404Sns158690 		len =  snprintf(path, PATH_MAX, "%s/%s/pkginfo", pkg_dir,
93*1404Sns158690 		    pkginst);
94*1404Sns158690 		if (len > PATH_MAX)
95*1404Sns158690 			return (0);
960Sstevel@tonic-gate 		if (access(path, 0))
970Sstevel@tonic-gate 			return (0); /* doesn't appear to be a package */
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	return (1);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * This opens the appropriate pkginfo file for a particular package.
1050Sstevel@tonic-gate  */
1060Sstevel@tonic-gate FILE *
1070Sstevel@tonic-gate pkginfopen(char *pkg_dir, char *pkginst)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate 	FILE *fp = NULL;
1100Sstevel@tonic-gate 	char temp[PATH_MAX];
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	if (pkginfofind(temp, pkg_dir, pkginst))
1130Sstevel@tonic-gate 		fp = fopen(temp, "r");
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	return (fp);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate char *
1200Sstevel@tonic-gate fpkgparam(FILE *fp, char *param)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	char	ch, buffer[VALSIZ];
1230Sstevel@tonic-gate 	char	*mempt, *copy;
1240Sstevel@tonic-gate 	int	c, n, escape, begline, quoted;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	if (param == NULL) {
1270Sstevel@tonic-gate 		errno = ENOENT;
1280Sstevel@tonic-gate 		return (NULL);
1290Sstevel@tonic-gate 	}
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	mempt = NULL;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	for (;;) {		/* for each entry in the file fp */
1340Sstevel@tonic-gate 		copy = buffer;
1350Sstevel@tonic-gate 		n = 0;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 		/* Get the next token. */
1380Sstevel@tonic-gate 		while ((c = getc(fp)) != EOF) {
139*1404Sns158690 			ch = (char)c;
1400Sstevel@tonic-gate 			if (strchr(sepset, ch))
1410Sstevel@tonic-gate 				break;
1420Sstevel@tonic-gate 			if (++n < VALSIZ)
1430Sstevel@tonic-gate 				*copy++ = ch;
1440Sstevel@tonic-gate 		}
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 		/* If it's the end of the file, exit the for() loop */
1470Sstevel@tonic-gate 		if (c == EOF) {
1480Sstevel@tonic-gate 			errno = EINVAL;
1490Sstevel@tonic-gate 			return (NULL); /* no more entries left */
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 		/* If it's end of line, look for the next parameter. */
1520Sstevel@tonic-gate 		} else if (c == NEWLINE)
1530Sstevel@tonic-gate 			continue;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 		/* At this point copy points to the end of a valid parameter. */
1560Sstevel@tonic-gate 		*copy = '\0';		/* Terminate the string. */
1570Sstevel@tonic-gate 		if (buffer[0] == '#')	/* If it's a comment, drop thru. */
1580Sstevel@tonic-gate 			copy = NULL;	/* Comments don't get buffered. */
1590Sstevel@tonic-gate 		else {
1600Sstevel@tonic-gate 			/* If parameter is NULL, we return whatever we got. */
1610Sstevel@tonic-gate 			if (param[0] == '\0') {
1620Sstevel@tonic-gate 				(void) strcpy(param, buffer);
1630Sstevel@tonic-gate 				copy = buffer;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 			/* If this doesn't match the parameter, drop thru. */
1660Sstevel@tonic-gate 			} else if (strcmp(param, buffer))
1670Sstevel@tonic-gate 				copy = NULL;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 			/* Otherwise, this is our boy. */
1700Sstevel@tonic-gate 			else
1710Sstevel@tonic-gate 				copy = buffer;
1720Sstevel@tonic-gate 		}
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 		n = quoted = escape = 0;
1750Sstevel@tonic-gate 		begline = 1;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 		/* Now read the parameter value. */
1780Sstevel@tonic-gate 		while ((c = getc(fp)) != EOF) {
179*1404Sns158690 			ch = (char)c;
1800Sstevel@tonic-gate 			if (begline && ((ch == ' ') || (ch == '\t')))
1810Sstevel@tonic-gate 				continue; /* ignore leading white space */
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 			if (ch == NEWLINE) {
1840Sstevel@tonic-gate 				if (!escape)
1850Sstevel@tonic-gate 					break; /* end of entry */
1860Sstevel@tonic-gate 				if (copy) {
1870Sstevel@tonic-gate 					if (escape) {
1880Sstevel@tonic-gate 						copy--; /* eat previous esc */
1890Sstevel@tonic-gate 						n--;
1900Sstevel@tonic-gate 					}
1910Sstevel@tonic-gate 					*copy++ = NEWLINE;
1920Sstevel@tonic-gate 				}
1930Sstevel@tonic-gate 				escape = 0;
1940Sstevel@tonic-gate 				begline = 1; /* new input line */
1950Sstevel@tonic-gate 			} else {
1960Sstevel@tonic-gate 				if (!escape && strchr(qset, ch)) {
1970Sstevel@tonic-gate 					/* handle quotes */
1980Sstevel@tonic-gate 					if (begline) {
1990Sstevel@tonic-gate 						quoted++;
2000Sstevel@tonic-gate 						begline = 0;
2010Sstevel@tonic-gate 						continue;
2020Sstevel@tonic-gate 					} else if (quoted) {
2030Sstevel@tonic-gate 						quoted = 0;
2040Sstevel@tonic-gate 						continue;
2050Sstevel@tonic-gate 					}
2060Sstevel@tonic-gate 				}
2070Sstevel@tonic-gate 				if (ch == ESCAPE)
2080Sstevel@tonic-gate 					escape++;
2090Sstevel@tonic-gate 				else if (escape)
2100Sstevel@tonic-gate 					escape = 0;
2110Sstevel@tonic-gate 				if (copy) *copy++ = ch;
2120Sstevel@tonic-gate 				begline = 0;
2130Sstevel@tonic-gate 			}
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 			if (copy && ((++n % VALSIZ) == 0)) {
2160Sstevel@tonic-gate 				if (mempt) {
2170Sstevel@tonic-gate 					mempt = realloc(mempt,
2180Sstevel@tonic-gate 						(n+VALSIZ)*sizeof (char));
2190Sstevel@tonic-gate 					if (!mempt)
2200Sstevel@tonic-gate 						return (NULL);
2210Sstevel@tonic-gate 				} else {
2220Sstevel@tonic-gate 					mempt = calloc((size_t)(2*VALSIZ),
2230Sstevel@tonic-gate 					    sizeof (char));
2240Sstevel@tonic-gate 					if (!mempt)
2250Sstevel@tonic-gate 						return (NULL);
2260Sstevel@tonic-gate 					(void) strncpy(mempt, buffer, n);
2270Sstevel@tonic-gate 				}
2280Sstevel@tonic-gate 				copy = &mempt[n];
2290Sstevel@tonic-gate 			}
2300Sstevel@tonic-gate 		}
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 		/*
2330Sstevel@tonic-gate 		 * Don't allow trailing white space.
2340Sstevel@tonic-gate 		 * NOTE : White space in the middle is OK, since this may
2350Sstevel@tonic-gate 		 * be a list. At some point it would be a good idea to let
2360Sstevel@tonic-gate 		 * this function know how to validate such a list. -- JST
2370Sstevel@tonic-gate 		 *
2380Sstevel@tonic-gate 		 * Now while there's a parametric value and it ends in a
2390Sstevel@tonic-gate 		 * space and the actual remaining string length is still
2400Sstevel@tonic-gate 		 * greater than 0, back over the space.
2410Sstevel@tonic-gate 		 */
2420Sstevel@tonic-gate 		while (copy && isspace((unsigned char)*(copy - 1)) && n-- > 0)
2430Sstevel@tonic-gate 			copy--;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 		if (quoted) {
2460Sstevel@tonic-gate 			if (mempt)
2470Sstevel@tonic-gate 				(void) free(mempt);
2480Sstevel@tonic-gate 			errno = EFAULT; /* missing closing quote */
2490Sstevel@tonic-gate 			return (NULL);
2500Sstevel@tonic-gate 		}
2510Sstevel@tonic-gate 		if (copy) {
2520Sstevel@tonic-gate 			*copy = '\0';
2530Sstevel@tonic-gate 			break;
2540Sstevel@tonic-gate 		}
2550Sstevel@tonic-gate 		if (c == EOF) {
2560Sstevel@tonic-gate 			errno = EINVAL; /* parameter not found */
2570Sstevel@tonic-gate 			return (NULL);
2580Sstevel@tonic-gate 		}
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	if (!mempt)
2620Sstevel@tonic-gate 		mempt = strdup(buffer);
2630Sstevel@tonic-gate 	else
2640Sstevel@tonic-gate 		mempt = realloc(mempt, (strlen(mempt)+1)*sizeof (char));
2650Sstevel@tonic-gate 	return (mempt);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate char *
2690Sstevel@tonic-gate pkgparam(char *pkg, char *param)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate 	static char lastfname[PATH_MAX];
2720Sstevel@tonic-gate 	static FILE *fp = NULL;
2730Sstevel@tonic-gate 	char *pt, *copy, *value, line[PATH_MAX];
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	if (!pkgdir)
2760Sstevel@tonic-gate 		pkgdir = get_PKGLOC();
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	if (!pkg) {
2790Sstevel@tonic-gate 		/* request to close file */
2800Sstevel@tonic-gate 		if (fp) {
2810Sstevel@tonic-gate 			(void) fclose(fp);
2820Sstevel@tonic-gate 			fp = NULL;
2830Sstevel@tonic-gate 		}
2840Sstevel@tonic-gate 		return (NULL);
2850Sstevel@tonic-gate 	}
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	if (!param) {
2880Sstevel@tonic-gate 		errno = ENOENT;
2890Sstevel@tonic-gate 		return (NULL);
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	if (pkgfile)
2930Sstevel@tonic-gate 		(void) strcpy(line, pkgfile); /* filename was passed */
2940Sstevel@tonic-gate 	else
2950Sstevel@tonic-gate 		(void) pkginfofind(line, pkgdir, pkg);
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	if (fp && strcmp(line, lastfname)) {
2980Sstevel@tonic-gate 		/* different filename implies need for different fp */
2990Sstevel@tonic-gate 		(void) fclose(fp);
3000Sstevel@tonic-gate 		fp = NULL;
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 	if (!fp) {
3030Sstevel@tonic-gate 		(void) strcpy(lastfname, line);
3040Sstevel@tonic-gate 		if ((fp = fopen(lastfname, "r")) == NULL)
3050Sstevel@tonic-gate 			return (NULL);
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	/*
3090Sstevel@tonic-gate 	 * if parameter is a null string, then the user is requesting us
3100Sstevel@tonic-gate 	 * to find the value of the next available parameter for this
3110Sstevel@tonic-gate 	 * package and to copy the parameter name into the provided string;
3120Sstevel@tonic-gate 	 * if it is not, then it is a request for a specified parameter, in
3130Sstevel@tonic-gate 	 * which case we rewind the file to start search from beginning
3140Sstevel@tonic-gate 	 */
3150Sstevel@tonic-gate 	if (param[0]) {
3160Sstevel@tonic-gate 		/* new parameter request, so reset file position */
3170Sstevel@tonic-gate 		if (fseek(fp, 0L, 0))
3180Sstevel@tonic-gate 			return (NULL);
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	if (pt = fpkgparam(fp, param)) {
3220Sstevel@tonic-gate 		if (strcmp(param, "ARCH") == NULL ||
3230Sstevel@tonic-gate 		    strcmp(param, "CATEGORY") == NULL) {
3240Sstevel@tonic-gate 			/* remove all whitespace from value */
3250Sstevel@tonic-gate 			value = copy = pt;
3260Sstevel@tonic-gate 			while (*value) {
3270Sstevel@tonic-gate 				if (!isspace((unsigned char)*value))
3280Sstevel@tonic-gate 					*copy++ = *value;
3290Sstevel@tonic-gate 				value++;
3300Sstevel@tonic-gate 			}
3310Sstevel@tonic-gate 			*copy = '\0';
3320Sstevel@tonic-gate 		}
3330Sstevel@tonic-gate 		return (pt);
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 	return (NULL);
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate /*
3380Sstevel@tonic-gate  * This routine sets adm_pkgloc and adm_pkgadm which are the
3390Sstevel@tonic-gate  * replacement location for PKGLOC and PKGADM.
3400Sstevel@tonic-gate  */
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate static void canonize_name(char *);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate void
3450Sstevel@tonic-gate set_PKGpaths(char *path)
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate 	if (path && *path) {
3480Sstevel@tonic-gate 		(void) sprintf(Adm_pkgloc, "%s%s", path, PKGLOC);
3490Sstevel@tonic-gate 		(void) sprintf(Adm_pkgold, "%s%s", path, PKGOLD);
3500Sstevel@tonic-gate 		(void) sprintf(Adm_pkgadm, "%s%s", path, PKGADM);
3510Sstevel@tonic-gate 		set_install_root(path);
3520Sstevel@tonic-gate 	} else {
3530Sstevel@tonic-gate 		(void) sprintf(Adm_pkgloc, "%s", PKGLOC);
3540Sstevel@tonic-gate 		(void) sprintf(Adm_pkgold, "%s", PKGOLD);
3550Sstevel@tonic-gate 		(void) sprintf(Adm_pkgadm, "%s", PKGADM);
3560Sstevel@tonic-gate 	}
3570Sstevel@tonic-gate 	canonize_name(Adm_pkgloc);
3580Sstevel@tonic-gate 	canonize_name(Adm_pkgold);
3590Sstevel@tonic-gate 	canonize_name(Adm_pkgadm);
3600Sstevel@tonic-gate 	pkgdir = Adm_pkgloc;
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate char *
3640Sstevel@tonic-gate get_PKGLOC(void)
3650Sstevel@tonic-gate {
3660Sstevel@tonic-gate 	if (Adm_pkgloc[0] == NULL)
3670Sstevel@tonic-gate 		return (PKGLOC);
3680Sstevel@tonic-gate 	else
3690Sstevel@tonic-gate 		return (Adm_pkgloc);
3700Sstevel@tonic-gate }
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate char *
3730Sstevel@tonic-gate get_PKGOLD(void)
3740Sstevel@tonic-gate {
3750Sstevel@tonic-gate 	if (Adm_pkgold[0] == NULL)
3760Sstevel@tonic-gate 		return (PKGOLD);
3770Sstevel@tonic-gate 	else
3780Sstevel@tonic-gate 		return (Adm_pkgold);
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate char *
3820Sstevel@tonic-gate get_PKGADM(void)
3830Sstevel@tonic-gate {
3840Sstevel@tonic-gate 	if (Adm_pkgadm[0] == NULL)
3850Sstevel@tonic-gate 		return (PKGADM);
3860Sstevel@tonic-gate 	else
3870Sstevel@tonic-gate 		return (Adm_pkgadm);
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate void
3910Sstevel@tonic-gate set_PKGADM(char *newpath)
3920Sstevel@tonic-gate {
3930Sstevel@tonic-gate 	(void) strcpy(Adm_pkgadm, newpath);
3940Sstevel@tonic-gate }
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate void
3970Sstevel@tonic-gate set_PKGLOC(char *newpath)
3980Sstevel@tonic-gate {
3990Sstevel@tonic-gate 	(void) strcpy(Adm_pkgloc, newpath);
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate #define	isdot(x)	((x[0] == '.')&&(!x[1]||(x[1] == '/')))
4030Sstevel@tonic-gate #define	isdotdot(x)	((x[0] == '.')&&(x[1] == '.')&&(!x[2]||(x[2] == '/')))
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate static void
4060Sstevel@tonic-gate canonize_name(char *file)
4070Sstevel@tonic-gate {
4080Sstevel@tonic-gate 	char *pt, *last;
4090Sstevel@tonic-gate 	int level;
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	/* Remove references such as "./" and "../" and "//" */
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	for (pt = file; *pt; ) {
4140Sstevel@tonic-gate 		if (isdot(pt))
4150Sstevel@tonic-gate 			(void) strcpy(pt, pt[1] ? pt+2 : pt+1);
4160Sstevel@tonic-gate 		else if (isdotdot(pt)) {
4170Sstevel@tonic-gate 			level = 0;
4180Sstevel@tonic-gate 			last = pt;
4190Sstevel@tonic-gate 			do {
4200Sstevel@tonic-gate 				level++;
4210Sstevel@tonic-gate 				last += 2;
4220Sstevel@tonic-gate 				if (*last)
4230Sstevel@tonic-gate 					last++;
4240Sstevel@tonic-gate 			} while (isdotdot(last));
4250Sstevel@tonic-gate 			--pt; /* point to previous '/' */
4260Sstevel@tonic-gate 			while (level--) {
4270Sstevel@tonic-gate 				if (pt <= file)
4280Sstevel@tonic-gate 					return;
4290Sstevel@tonic-gate 				while ((*--pt != '/') && (pt > file))
4300Sstevel@tonic-gate 					;
4310Sstevel@tonic-gate 			}
4320Sstevel@tonic-gate 			if (*pt == '/')
4330Sstevel@tonic-gate 				pt++;
4340Sstevel@tonic-gate 			(void) strcpy(pt, last);
4350Sstevel@tonic-gate 		} else {
4360Sstevel@tonic-gate 			while (*pt && (*pt != '/'))
4370Sstevel@tonic-gate 				pt++;
4380Sstevel@tonic-gate 			if (*pt == '/') {
4390Sstevel@tonic-gate 				while (pt[1] == '/')
4400Sstevel@tonic-gate 					(void) strcpy(pt, pt+1);
4410Sstevel@tonic-gate 				pt++;
4420Sstevel@tonic-gate 			}
4430Sstevel@tonic-gate 		}
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate 	if ((--pt > file) && (*pt == '/'))
4460Sstevel@tonic-gate 		*pt = '\0';
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate void
4500Sstevel@tonic-gate set_install_root(char *path)
4510Sstevel@tonic-gate {
4520Sstevel@tonic-gate 	pkg_inst_root = strdup(path);
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate char *
4560Sstevel@tonic-gate get_install_root()
4570Sstevel@tonic-gate {
4580Sstevel@tonic-gate 	return (pkg_inst_root);
4590Sstevel@tonic-gate }
460