1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate /*
26*0Sstevel@tonic-gate  * Copyright (c) 1995-1998 by Sun Microsystems, Inc.
27*0Sstevel@tonic-gate  * All rights reserved.
28*0Sstevel@tonic-gate  */
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma	ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1 */
31*0Sstevel@tonic-gate /*LINTLIBRARY*/
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*   5-20-92   newroot support added  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <stdio.h>
36*0Sstevel@tonic-gate #include <limits.h>
37*0Sstevel@tonic-gate #include <ctype.h>
38*0Sstevel@tonic-gate #include <errno.h>
39*0Sstevel@tonic-gate #include <string.h>
40*0Sstevel@tonic-gate #include <sys/types.h>
41*0Sstevel@tonic-gate #include <pkgstrct.h>
42*0Sstevel@tonic-gate #include <pkginfo.h>
43*0Sstevel@tonic-gate #include <pkglocs.h>
44*0Sstevel@tonic-gate #include <stdlib.h>
45*0Sstevel@tonic-gate #include <unistd.h>
46*0Sstevel@tonic-gate #include "libadm.h"
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #define	VALSIZ	128
49*0Sstevel@tonic-gate #define	NEWLINE	'\n'
50*0Sstevel@tonic-gate #define	ESCAPE	'\\'
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate static char sepset[] =	":=\n";
53*0Sstevel@tonic-gate static char qset[] = 	"'\"";
54*0Sstevel@tonic-gate static char *pkg_inst_root = NULL;
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate char *pkgdir = NULL;
57*0Sstevel@tonic-gate char *pkgfile = NULL;
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate static char Adm_pkgold[PATH_MAX] = { 0 }; /* added for newroot */
60*0Sstevel@tonic-gate static char Adm_pkgloc[PATH_MAX] = { 0 }; /* added for newroot */
61*0Sstevel@tonic-gate static char Adm_pkgadm[PATH_MAX] = { 0 }; /* added for newroot */
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate /*
64*0Sstevel@tonic-gate  * This looks in a directory that might be the top level directory of a
65*0Sstevel@tonic-gate  * package. It tests a temporary install directory first and then for a
66*0Sstevel@tonic-gate  * standard directory. This looks a little confusing, so here's what's
67*0Sstevel@tonic-gate  * happening. If this pkginfo is being openned in a script during a pkgadd
68*0Sstevel@tonic-gate  * which is updating an existing package, the original pkginfo file is in a
69*0Sstevel@tonic-gate  * directory that has been renamed from <pkginst> to .save.<pkginst>. If the
70*0Sstevel@tonic-gate  * pkgadd fails it will be renamed back to <pkginst>. We are always interested
71*0Sstevel@tonic-gate  * in the OLD pkginfo data because the new pkginfo data is already in our
72*0Sstevel@tonic-gate  * environment. For that reason, we try to open the backup first - that has
73*0Sstevel@tonic-gate  * the old data. This returns the first accessible path in "path" and a "1"
74*0Sstevel@tonic-gate  * if an appropriate pkginfo file was found. It returns a 0 if no type of
75*0Sstevel@tonic-gate  * pkginfo was located.
76*0Sstevel@tonic-gate  */
77*0Sstevel@tonic-gate int
78*0Sstevel@tonic-gate pkginfofind(char *path, char *pkg_dir, char *pkginst)
79*0Sstevel@tonic-gate {
80*0Sstevel@tonic-gate 	/* Construct the temporary pkginfo file name. */
81*0Sstevel@tonic-gate 	(void) sprintf(path, "%s/.save.%s/pkginfo", pkg_dir, pkginst);
82*0Sstevel@tonic-gate 	if (access(path, 0)) {
83*0Sstevel@tonic-gate 		/*
84*0Sstevel@tonic-gate 		 * This isn't a temporary directory, so we look for a
85*0Sstevel@tonic-gate 		 * regular one.
86*0Sstevel@tonic-gate 		 */
87*0Sstevel@tonic-gate 		(void) sprintf(path, "%s/%s/pkginfo", pkg_dir, pkginst);
88*0Sstevel@tonic-gate 		if (access(path, 0))
89*0Sstevel@tonic-gate 			return (0); /* doesn't appear to be a package */
90*0Sstevel@tonic-gate 	}
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	return (1);
93*0Sstevel@tonic-gate }
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate /*
96*0Sstevel@tonic-gate  * This opens the appropriate pkginfo file for a particular package.
97*0Sstevel@tonic-gate  */
98*0Sstevel@tonic-gate FILE *
99*0Sstevel@tonic-gate pkginfopen(char *pkg_dir, char *pkginst)
100*0Sstevel@tonic-gate {
101*0Sstevel@tonic-gate 	FILE *fp = NULL;
102*0Sstevel@tonic-gate 	char temp[PATH_MAX];
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	if (pkginfofind(temp, pkg_dir, pkginst))
105*0Sstevel@tonic-gate 		fp = fopen(temp, "r");
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	return (fp);
108*0Sstevel@tonic-gate }
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate char *
112*0Sstevel@tonic-gate fpkgparam(FILE *fp, char *param)
113*0Sstevel@tonic-gate {
114*0Sstevel@tonic-gate 	char	ch, buffer[VALSIZ];
115*0Sstevel@tonic-gate 	char	*mempt, *copy;
116*0Sstevel@tonic-gate 	int	c, n, escape, begline, quoted;
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	if (param == NULL) {
119*0Sstevel@tonic-gate 		errno = ENOENT;
120*0Sstevel@tonic-gate 		return (NULL);
121*0Sstevel@tonic-gate 	}
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	mempt = NULL;
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	for (;;) {		/* for each entry in the file fp */
126*0Sstevel@tonic-gate 		copy = buffer;
127*0Sstevel@tonic-gate 		n = 0;
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 		/* Get the next token. */
130*0Sstevel@tonic-gate 		while ((c = getc(fp)) != EOF) {
131*0Sstevel@tonic-gate 			ch = (char) c;
132*0Sstevel@tonic-gate 			if (strchr(sepset, ch))
133*0Sstevel@tonic-gate 				break;
134*0Sstevel@tonic-gate 			if (++n < VALSIZ)
135*0Sstevel@tonic-gate 				*copy++ = ch;
136*0Sstevel@tonic-gate 		}
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 		/* If it's the end of the file, exit the for() loop */
139*0Sstevel@tonic-gate 		if (c == EOF) {
140*0Sstevel@tonic-gate 			errno = EINVAL;
141*0Sstevel@tonic-gate 			return (NULL); /* no more entries left */
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 		/* If it's end of line, look for the next parameter. */
144*0Sstevel@tonic-gate 		} else if (c == NEWLINE)
145*0Sstevel@tonic-gate 			continue;
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 		/* At this point copy points to the end of a valid parameter. */
148*0Sstevel@tonic-gate 		*copy = '\0';		/* Terminate the string. */
149*0Sstevel@tonic-gate 		if (buffer[0] == '#')	/* If it's a comment, drop thru. */
150*0Sstevel@tonic-gate 			copy = NULL;	/* Comments don't get buffered. */
151*0Sstevel@tonic-gate 		else {
152*0Sstevel@tonic-gate 			/* If parameter is NULL, we return whatever we got. */
153*0Sstevel@tonic-gate 			if (param[0] == '\0') {
154*0Sstevel@tonic-gate 				(void) strcpy(param, buffer);
155*0Sstevel@tonic-gate 				copy = buffer;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 			/* If this doesn't match the parameter, drop thru. */
158*0Sstevel@tonic-gate 			} else if (strcmp(param, buffer))
159*0Sstevel@tonic-gate 				copy = NULL;
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 			/* Otherwise, this is our boy. */
162*0Sstevel@tonic-gate 			else
163*0Sstevel@tonic-gate 				copy = buffer;
164*0Sstevel@tonic-gate 		}
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 		n = quoted = escape = 0;
167*0Sstevel@tonic-gate 		begline = 1;
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 		/* Now read the parameter value. */
170*0Sstevel@tonic-gate 		while ((c = getc(fp)) != EOF) {
171*0Sstevel@tonic-gate 			ch = (char) c;
172*0Sstevel@tonic-gate 			if (begline && ((ch == ' ') || (ch == '\t')))
173*0Sstevel@tonic-gate 				continue; /* ignore leading white space */
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 			if (ch == NEWLINE) {
176*0Sstevel@tonic-gate 				if (!escape)
177*0Sstevel@tonic-gate 					break; /* end of entry */
178*0Sstevel@tonic-gate 				if (copy) {
179*0Sstevel@tonic-gate 					if (escape) {
180*0Sstevel@tonic-gate 						copy--; /* eat previous esc */
181*0Sstevel@tonic-gate 						n--;
182*0Sstevel@tonic-gate 					}
183*0Sstevel@tonic-gate 					*copy++ = NEWLINE;
184*0Sstevel@tonic-gate 				}
185*0Sstevel@tonic-gate 				escape = 0;
186*0Sstevel@tonic-gate 				begline = 1; /* new input line */
187*0Sstevel@tonic-gate 			} else {
188*0Sstevel@tonic-gate 				if (!escape && strchr(qset, ch)) {
189*0Sstevel@tonic-gate 					/* handle quotes */
190*0Sstevel@tonic-gate 					if (begline) {
191*0Sstevel@tonic-gate 						quoted++;
192*0Sstevel@tonic-gate 						begline = 0;
193*0Sstevel@tonic-gate 						continue;
194*0Sstevel@tonic-gate 					} else if (quoted) {
195*0Sstevel@tonic-gate 						quoted = 0;
196*0Sstevel@tonic-gate 						continue;
197*0Sstevel@tonic-gate 					}
198*0Sstevel@tonic-gate 				}
199*0Sstevel@tonic-gate 				if (ch == ESCAPE)
200*0Sstevel@tonic-gate 					escape++;
201*0Sstevel@tonic-gate 				else if (escape)
202*0Sstevel@tonic-gate 					escape = 0;
203*0Sstevel@tonic-gate 				if (copy) *copy++ = ch;
204*0Sstevel@tonic-gate 				begline = 0;
205*0Sstevel@tonic-gate 			}
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 			if (copy && ((++n % VALSIZ) == 0)) {
208*0Sstevel@tonic-gate 				if (mempt) {
209*0Sstevel@tonic-gate 					mempt = realloc(mempt,
210*0Sstevel@tonic-gate 						(n+VALSIZ)*sizeof (char));
211*0Sstevel@tonic-gate 					if (!mempt)
212*0Sstevel@tonic-gate 						return (NULL);
213*0Sstevel@tonic-gate 				} else {
214*0Sstevel@tonic-gate 					mempt = calloc((size_t)(2*VALSIZ),
215*0Sstevel@tonic-gate 					    sizeof (char));
216*0Sstevel@tonic-gate 					if (!mempt)
217*0Sstevel@tonic-gate 						return (NULL);
218*0Sstevel@tonic-gate 					(void) strncpy(mempt, buffer, n);
219*0Sstevel@tonic-gate 				}
220*0Sstevel@tonic-gate 				copy = &mempt[n];
221*0Sstevel@tonic-gate 			}
222*0Sstevel@tonic-gate 		}
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 		/*
225*0Sstevel@tonic-gate 		 * Don't allow trailing white space.
226*0Sstevel@tonic-gate 		 * NOTE : White space in the middle is OK, since this may
227*0Sstevel@tonic-gate 		 * be a list. At some point it would be a good idea to let
228*0Sstevel@tonic-gate 		 * this function know how to validate such a list. -- JST
229*0Sstevel@tonic-gate 		 *
230*0Sstevel@tonic-gate 		 * Now while there's a parametric value and it ends in a
231*0Sstevel@tonic-gate 		 * space and the actual remaining string length is still
232*0Sstevel@tonic-gate 		 * greater than 0, back over the space.
233*0Sstevel@tonic-gate 		 */
234*0Sstevel@tonic-gate 		while (copy && isspace((unsigned char)*(copy - 1)) && n-- > 0)
235*0Sstevel@tonic-gate 			copy--;
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 		if (quoted) {
238*0Sstevel@tonic-gate 			if (mempt)
239*0Sstevel@tonic-gate 				(void) free(mempt);
240*0Sstevel@tonic-gate 			errno = EFAULT; /* missing closing quote */
241*0Sstevel@tonic-gate 			return (NULL);
242*0Sstevel@tonic-gate 		}
243*0Sstevel@tonic-gate 		if (copy) {
244*0Sstevel@tonic-gate 			*copy = '\0';
245*0Sstevel@tonic-gate 			break;
246*0Sstevel@tonic-gate 		}
247*0Sstevel@tonic-gate 		if (c == EOF) {
248*0Sstevel@tonic-gate 			errno = EINVAL; /* parameter not found */
249*0Sstevel@tonic-gate 			return (NULL);
250*0Sstevel@tonic-gate 		}
251*0Sstevel@tonic-gate 	}
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 	if (!mempt)
254*0Sstevel@tonic-gate 		mempt = strdup(buffer);
255*0Sstevel@tonic-gate 	else
256*0Sstevel@tonic-gate 		mempt = realloc(mempt, (strlen(mempt)+1)*sizeof (char));
257*0Sstevel@tonic-gate 	return (mempt);
258*0Sstevel@tonic-gate }
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate char *
261*0Sstevel@tonic-gate pkgparam(char *pkg, char *param)
262*0Sstevel@tonic-gate {
263*0Sstevel@tonic-gate 	static char lastfname[PATH_MAX];
264*0Sstevel@tonic-gate 	static FILE *fp = NULL;
265*0Sstevel@tonic-gate 	char *pt, *copy, *value, line[PATH_MAX];
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate 	if (!pkgdir)
268*0Sstevel@tonic-gate 		pkgdir = get_PKGLOC();
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	if (!pkg) {
271*0Sstevel@tonic-gate 		/* request to close file */
272*0Sstevel@tonic-gate 		if (fp) {
273*0Sstevel@tonic-gate 			(void) fclose(fp);
274*0Sstevel@tonic-gate 			fp = NULL;
275*0Sstevel@tonic-gate 		}
276*0Sstevel@tonic-gate 		return (NULL);
277*0Sstevel@tonic-gate 	}
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 	if (!param) {
280*0Sstevel@tonic-gate 		errno = ENOENT;
281*0Sstevel@tonic-gate 		return (NULL);
282*0Sstevel@tonic-gate 	}
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	if (pkgfile)
285*0Sstevel@tonic-gate 		(void) strcpy(line, pkgfile); /* filename was passed */
286*0Sstevel@tonic-gate 	else
287*0Sstevel@tonic-gate 		(void) pkginfofind(line, pkgdir, pkg);
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate 	if (fp && strcmp(line, lastfname)) {
290*0Sstevel@tonic-gate 		/* different filename implies need for different fp */
291*0Sstevel@tonic-gate 		(void) fclose(fp);
292*0Sstevel@tonic-gate 		fp = NULL;
293*0Sstevel@tonic-gate 	}
294*0Sstevel@tonic-gate 	if (!fp) {
295*0Sstevel@tonic-gate 		(void) strcpy(lastfname, line);
296*0Sstevel@tonic-gate 		if ((fp = fopen(lastfname, "r")) == NULL)
297*0Sstevel@tonic-gate 			return (NULL);
298*0Sstevel@tonic-gate 	}
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	/*
301*0Sstevel@tonic-gate 	 * if parameter is a null string, then the user is requesting us
302*0Sstevel@tonic-gate 	 * to find the value of the next available parameter for this
303*0Sstevel@tonic-gate 	 * package and to copy the parameter name into the provided string;
304*0Sstevel@tonic-gate 	 * if it is not, then it is a request for a specified parameter, in
305*0Sstevel@tonic-gate 	 * which case we rewind the file to start search from beginning
306*0Sstevel@tonic-gate 	 */
307*0Sstevel@tonic-gate 	if (param[0]) {
308*0Sstevel@tonic-gate 		/* new parameter request, so reset file position */
309*0Sstevel@tonic-gate 		if (fseek(fp, 0L, 0))
310*0Sstevel@tonic-gate 			return (NULL);
311*0Sstevel@tonic-gate 	}
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 	if (pt = fpkgparam(fp, param)) {
314*0Sstevel@tonic-gate 		if (strcmp(param, "ARCH") == NULL ||
315*0Sstevel@tonic-gate 		    strcmp(param, "CATEGORY") == NULL) {
316*0Sstevel@tonic-gate 			/* remove all whitespace from value */
317*0Sstevel@tonic-gate 			value = copy = pt;
318*0Sstevel@tonic-gate 			while (*value) {
319*0Sstevel@tonic-gate 				if (!isspace((unsigned char)*value))
320*0Sstevel@tonic-gate 					*copy++ = *value;
321*0Sstevel@tonic-gate 				value++;
322*0Sstevel@tonic-gate 			}
323*0Sstevel@tonic-gate 			*copy = '\0';
324*0Sstevel@tonic-gate 		}
325*0Sstevel@tonic-gate 		return (pt);
326*0Sstevel@tonic-gate 	}
327*0Sstevel@tonic-gate 	return (NULL);
328*0Sstevel@tonic-gate }
329*0Sstevel@tonic-gate /*
330*0Sstevel@tonic-gate  * This routine sets adm_pkgloc and adm_pkgadm which are the
331*0Sstevel@tonic-gate  * replacement location for PKGLOC and PKGADM.
332*0Sstevel@tonic-gate  */
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate static void canonize_name(char *);
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate void
337*0Sstevel@tonic-gate set_PKGpaths(char *path)
338*0Sstevel@tonic-gate {
339*0Sstevel@tonic-gate 	if (path && *path) {
340*0Sstevel@tonic-gate 		(void) sprintf(Adm_pkgloc, "%s%s", path, PKGLOC);
341*0Sstevel@tonic-gate 		(void) sprintf(Adm_pkgold, "%s%s", path, PKGOLD);
342*0Sstevel@tonic-gate 		(void) sprintf(Adm_pkgadm, "%s%s", path, PKGADM);
343*0Sstevel@tonic-gate 		set_install_root(path);
344*0Sstevel@tonic-gate 	} else {
345*0Sstevel@tonic-gate 		(void) sprintf(Adm_pkgloc, "%s", PKGLOC);
346*0Sstevel@tonic-gate 		(void) sprintf(Adm_pkgold, "%s", PKGOLD);
347*0Sstevel@tonic-gate 		(void) sprintf(Adm_pkgadm, "%s", PKGADM);
348*0Sstevel@tonic-gate 	}
349*0Sstevel@tonic-gate 	canonize_name(Adm_pkgloc);
350*0Sstevel@tonic-gate 	canonize_name(Adm_pkgold);
351*0Sstevel@tonic-gate 	canonize_name(Adm_pkgadm);
352*0Sstevel@tonic-gate 	pkgdir = Adm_pkgloc;
353*0Sstevel@tonic-gate }
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate char *
356*0Sstevel@tonic-gate get_PKGLOC(void)
357*0Sstevel@tonic-gate {
358*0Sstevel@tonic-gate 	if (Adm_pkgloc[0] == NULL)
359*0Sstevel@tonic-gate 		return (PKGLOC);
360*0Sstevel@tonic-gate 	else
361*0Sstevel@tonic-gate 		return (Adm_pkgloc);
362*0Sstevel@tonic-gate }
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate char *
365*0Sstevel@tonic-gate get_PKGOLD(void)
366*0Sstevel@tonic-gate {
367*0Sstevel@tonic-gate 	if (Adm_pkgold[0] == NULL)
368*0Sstevel@tonic-gate 		return (PKGOLD);
369*0Sstevel@tonic-gate 	else
370*0Sstevel@tonic-gate 		return (Adm_pkgold);
371*0Sstevel@tonic-gate }
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate char *
374*0Sstevel@tonic-gate get_PKGADM(void)
375*0Sstevel@tonic-gate {
376*0Sstevel@tonic-gate 	if (Adm_pkgadm[0] == NULL)
377*0Sstevel@tonic-gate 		return (PKGADM);
378*0Sstevel@tonic-gate 	else
379*0Sstevel@tonic-gate 		return (Adm_pkgadm);
380*0Sstevel@tonic-gate }
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate void
383*0Sstevel@tonic-gate set_PKGADM(char *newpath)
384*0Sstevel@tonic-gate {
385*0Sstevel@tonic-gate 	(void) strcpy(Adm_pkgadm, newpath);
386*0Sstevel@tonic-gate }
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate void
389*0Sstevel@tonic-gate set_PKGLOC(char *newpath)
390*0Sstevel@tonic-gate {
391*0Sstevel@tonic-gate 	(void) strcpy(Adm_pkgloc, newpath);
392*0Sstevel@tonic-gate }
393*0Sstevel@tonic-gate 
394*0Sstevel@tonic-gate #define	isdot(x)	((x[0] == '.')&&(!x[1]||(x[1] == '/')))
395*0Sstevel@tonic-gate #define	isdotdot(x)	((x[0] == '.')&&(x[1] == '.')&&(!x[2]||(x[2] == '/')))
396*0Sstevel@tonic-gate 
397*0Sstevel@tonic-gate static void
398*0Sstevel@tonic-gate canonize_name(char *file)
399*0Sstevel@tonic-gate {
400*0Sstevel@tonic-gate 	char *pt, *last;
401*0Sstevel@tonic-gate 	int level;
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 	/* Remove references such as "./" and "../" and "//" */
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate 	for (pt = file; *pt; ) {
406*0Sstevel@tonic-gate 		if (isdot(pt))
407*0Sstevel@tonic-gate 			(void) strcpy(pt, pt[1] ? pt+2 : pt+1);
408*0Sstevel@tonic-gate 		else if (isdotdot(pt)) {
409*0Sstevel@tonic-gate 			level = 0;
410*0Sstevel@tonic-gate 			last = pt;
411*0Sstevel@tonic-gate 			do {
412*0Sstevel@tonic-gate 				level++;
413*0Sstevel@tonic-gate 				last += 2;
414*0Sstevel@tonic-gate 				if (*last)
415*0Sstevel@tonic-gate 					last++;
416*0Sstevel@tonic-gate 			} while (isdotdot(last));
417*0Sstevel@tonic-gate 			--pt; /* point to previous '/' */
418*0Sstevel@tonic-gate 			while (level--) {
419*0Sstevel@tonic-gate 				if (pt <= file)
420*0Sstevel@tonic-gate 					return;
421*0Sstevel@tonic-gate 				while ((*--pt != '/') && (pt > file))
422*0Sstevel@tonic-gate 					;
423*0Sstevel@tonic-gate 			}
424*0Sstevel@tonic-gate 			if (*pt == '/')
425*0Sstevel@tonic-gate 				pt++;
426*0Sstevel@tonic-gate 			(void) strcpy(pt, last);
427*0Sstevel@tonic-gate 		} else {
428*0Sstevel@tonic-gate 			while (*pt && (*pt != '/'))
429*0Sstevel@tonic-gate 				pt++;
430*0Sstevel@tonic-gate 			if (*pt == '/') {
431*0Sstevel@tonic-gate 				while (pt[1] == '/')
432*0Sstevel@tonic-gate 					(void) strcpy(pt, pt+1);
433*0Sstevel@tonic-gate 				pt++;
434*0Sstevel@tonic-gate 			}
435*0Sstevel@tonic-gate 		}
436*0Sstevel@tonic-gate 	}
437*0Sstevel@tonic-gate 	if ((--pt > file) && (*pt == '/'))
438*0Sstevel@tonic-gate 		*pt = '\0';
439*0Sstevel@tonic-gate }
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate void
442*0Sstevel@tonic-gate set_install_root(char *path)
443*0Sstevel@tonic-gate {
444*0Sstevel@tonic-gate 	pkg_inst_root = strdup(path);
445*0Sstevel@tonic-gate }
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate char *
448*0Sstevel@tonic-gate get_install_root()
449*0Sstevel@tonic-gate {
450*0Sstevel@tonic-gate 	return (pkg_inst_root);
451*0Sstevel@tonic-gate }
452