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