xref: /onnv-gate/usr/src/lib/libadm/common/pkginfo.c (revision 0:68f95e015346)
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 /*
27*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28*0Sstevel@tonic-gate  * Use is subject to license terms.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.2 */
32*0Sstevel@tonic-gate /*LINTLIBRARY*/
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate /*  5-20-92   added newroot functions  */
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <stdio.h>
37*0Sstevel@tonic-gate #include <limits.h>
38*0Sstevel@tonic-gate #include <stdarg.h>
39*0Sstevel@tonic-gate #include <unistd.h>
40*0Sstevel@tonic-gate #include <stdlib.h>
41*0Sstevel@tonic-gate #include <ctype.h>
42*0Sstevel@tonic-gate #include <string.h>
43*0Sstevel@tonic-gate #include <sys/types.h>
44*0Sstevel@tonic-gate #include <sys/stat.h>
45*0Sstevel@tonic-gate #include <dirent.h>
46*0Sstevel@tonic-gate #include <pkginfo.h>
47*0Sstevel@tonic-gate #include <pkgstrct.h>
48*0Sstevel@tonic-gate #include <pkglocs.h>
49*0Sstevel@tonic-gate #include <errno.h>
50*0Sstevel@tonic-gate #include "libadm.h"
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate static void	initpkg(struct pkginfo *);
53*0Sstevel@tonic-gate static char	*svr4inst(char *);
54*0Sstevel@tonic-gate static int	rdconfig(struct pkginfo *, char *, char *);
55*0Sstevel@tonic-gate static int	svr4info(struct pkginfo *, char *, char *);
56*0Sstevel@tonic-gate static int	ckinfo(char *, char *, char *);
57*0Sstevel@tonic-gate static int	ckinst(char *, char *, char *, char *, char *);
58*0Sstevel@tonic-gate static int	verscmp(char *, char *);
59*0Sstevel@tonic-gate static int	archcmp(char *, char *);
60*0Sstevel@tonic-gate static int	compver(char *, char *);
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate /*
63*0Sstevel@tonic-gate  * Globals:
64*0Sstevel@tonic-gate  *	pkgdir - specifies the directory where information about packages
65*0Sstevel@tonic-gate  *	    resides, i.e. the pkginfo file is located in a subdirectory
66*0Sstevel@tonic-gate  *
67*0Sstevel@tonic-gate  * Caveats:
68*0Sstevel@tonic-gate  *	The structure provided via "info" will contain malloc'd information;
69*0Sstevel@tonic-gate  *	    this will be free'd upon the next call to pkginfo with this
70*0Sstevel@tonic-gate  *	    same structure.  Application calls must make sure this structure
71*0Sstevel@tonic-gate  *	    is null on the first call, or else we'll free static memory areas
72*0Sstevel@tonic-gate  *	If the "pkg" argument is a wildcard specification, the next found
73*0Sstevel@tonic-gate  *	    instance available which matches the request will be returned
74*0Sstevel@tonic-gate  *	If the "pkg" argument is a NULL pointer, the structure pointed to
75*0Sstevel@tonic-gate  *	    via "info" will have its elements deallocated and all files
76*0Sstevel@tonic-gate  *	    associated with this routine will be closed
77*0Sstevel@tonic-gate  *
78*0Sstevel@tonic-gate  * Return codes:
79*0Sstevel@tonic-gate  *	A non-zero exit code indicates error with "errno" appropriately set:
80*0Sstevel@tonic-gate  *	    EINVAL - invalid argument
81*0Sstevel@tonic-gate  *	    ESRCH - there are no more instances of this package around
82*0Sstevel@tonic-gate  *	    EACCESS - unable to access files which should have been there
83*0Sstevel@tonic-gate  */
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate /*VARARGS*/
86*0Sstevel@tonic-gate int
pkginfo(struct pkginfo * info,char * pkginst,...)87*0Sstevel@tonic-gate pkginfo(struct pkginfo *info, char *pkginst, ...)
88*0Sstevel@tonic-gate {
89*0Sstevel@tonic-gate 	char	*ckarch, *ckvers;
90*0Sstevel@tonic-gate 	int	check;
91*0Sstevel@tonic-gate 	va_list ap;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	va_start(ap, pkginst);
94*0Sstevel@tonic-gate 	if (info == NULL) {
95*0Sstevel@tonic-gate 		errno = EINVAL;
96*0Sstevel@tonic-gate 		return (-1);
97*0Sstevel@tonic-gate 	}
98*0Sstevel@tonic-gate 	if (pkginst == NULL) {
99*0Sstevel@tonic-gate 		info->pkginst = NULL;
100*0Sstevel@tonic-gate 		(void) fpkginfo(info, NULL);
101*0Sstevel@tonic-gate 		(void) fpkginst(NULL);
102*0Sstevel@tonic-gate 		return (0);
103*0Sstevel@tonic-gate 	}
104*0Sstevel@tonic-gate 	ckarch = va_arg(ap, char *);
105*0Sstevel@tonic-gate 	ckvers = va_arg(ap, char *);
106*0Sstevel@tonic-gate 	va_end(ap);
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	check = 0;
109*0Sstevel@tonic-gate 	if (pkgnmchk(pkginst, "all", 1)) {
110*0Sstevel@tonic-gate 		/* wild card specification */
111*0Sstevel@tonic-gate 		pkginst = fpkginst(pkginst, ckarch, ckvers);
112*0Sstevel@tonic-gate 		if (pkginst == NULL)
113*0Sstevel@tonic-gate 			return (-1);
114*0Sstevel@tonic-gate 	} else {
115*0Sstevel@tonic-gate 		/* request to check indicated instance */
116*0Sstevel@tonic-gate 		if (ckarch || ckvers)
117*0Sstevel@tonic-gate 			check++;
118*0Sstevel@tonic-gate 	}
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	info->pkginst = NULL;
121*0Sstevel@tonic-gate 	if (fpkginfo(info, pkginst))
122*0Sstevel@tonic-gate 		return (-1);
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	if (check) {
125*0Sstevel@tonic-gate 		/*
126*0Sstevel@tonic-gate 		 * verify that the provided instance matches
127*0Sstevel@tonic-gate 		 * any arch & vers specs that were provided
128*0Sstevel@tonic-gate 		 */
129*0Sstevel@tonic-gate 		if (ckinst(pkginst, info->arch, info->version, ckarch,
130*0Sstevel@tonic-gate 		    ckvers)) {
131*0Sstevel@tonic-gate 			errno = ESRCH;
132*0Sstevel@tonic-gate 			return (-1);
133*0Sstevel@tonic-gate 		}
134*0Sstevel@tonic-gate 	}
135*0Sstevel@tonic-gate 	return (0);
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate /*ARGSUSED*/
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate int
fpkginfo(struct pkginfo * info,char * pkginst)140*0Sstevel@tonic-gate fpkginfo(struct pkginfo *info, char *pkginst)
141*0Sstevel@tonic-gate {
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	if (info == NULL) {
144*0Sstevel@tonic-gate 		errno = EINVAL;
145*0Sstevel@tonic-gate 		return (-1);
146*0Sstevel@tonic-gate 	}
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	initpkg(info);
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	if (pkginst == NULL)
151*0Sstevel@tonic-gate 		return (0);
152*0Sstevel@tonic-gate 	else if (pkgnmchk(pkginst, "all", 1)) {
153*0Sstevel@tonic-gate 		errno = EINVAL; /* not an instance identifier */
154*0Sstevel@tonic-gate 		return (-1);
155*0Sstevel@tonic-gate 	}
156*0Sstevel@tonic-gate 	if (pkgdir == NULL)
157*0Sstevel@tonic-gate 		pkgdir = get_PKGLOC();
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	if (rdconfig(info, pkginst, NULL)) {
160*0Sstevel@tonic-gate 		initpkg(info);
161*0Sstevel@tonic-gate 		return (-1);
162*0Sstevel@tonic-gate 	}
163*0Sstevel@tonic-gate 	return (0);
164*0Sstevel@tonic-gate }
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate static void
initpkg(struct pkginfo * info)167*0Sstevel@tonic-gate initpkg(struct pkginfo *info)
168*0Sstevel@tonic-gate {
169*0Sstevel@tonic-gate 	/* free previously allocated space */
170*0Sstevel@tonic-gate 	if (info->pkginst) {
171*0Sstevel@tonic-gate 		free(info->pkginst);
172*0Sstevel@tonic-gate 		if (info->arch)
173*0Sstevel@tonic-gate 			free(info->arch);
174*0Sstevel@tonic-gate 		if (info->version)
175*0Sstevel@tonic-gate 			free(info->version);
176*0Sstevel@tonic-gate 		if (info->basedir)
177*0Sstevel@tonic-gate 			free(info->basedir);
178*0Sstevel@tonic-gate 		if (info->name)
179*0Sstevel@tonic-gate 			free(info->name);
180*0Sstevel@tonic-gate 		if (info->vendor)
181*0Sstevel@tonic-gate 			free(info->vendor);
182*0Sstevel@tonic-gate 		if (info->catg)
183*0Sstevel@tonic-gate 			free(info->catg);
184*0Sstevel@tonic-gate 	}
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	info->pkginst = NULL;
187*0Sstevel@tonic-gate 	info->arch = info->version = NULL;
188*0Sstevel@tonic-gate 	info->basedir = info->name = NULL;
189*0Sstevel@tonic-gate 	info->vendor = info->catg = NULL;
190*0Sstevel@tonic-gate 	info->status = PI_UNKNOWN;
191*0Sstevel@tonic-gate }
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate static int
rdconfig(struct pkginfo * info,char * pkginst,char * ckvers)194*0Sstevel@tonic-gate rdconfig(struct pkginfo *info, char *pkginst, char *ckvers)
195*0Sstevel@tonic-gate {
196*0Sstevel@tonic-gate 	FILE	*fp;
197*0Sstevel@tonic-gate 	char	temp[256];
198*0Sstevel@tonic-gate 	char	*value, *pt, *copy, **memloc;
199*0Sstevel@tonic-gate 	int	count;
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	if ((fp = pkginfopen(pkgdir, pkginst)) == NULL) {
202*0Sstevel@tonic-gate 		if ((errno == ENOENT) && strcmp(pkgdir, get_PKGLOC()) == 0)
203*0Sstevel@tonic-gate 			return (svr4info(info, pkginst, ckvers));
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 		errno = EACCES;
206*0Sstevel@tonic-gate 		return (-1);
207*0Sstevel@tonic-gate 	}
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 	*temp = '\0';
210*0Sstevel@tonic-gate 	count = 0;
211*0Sstevel@tonic-gate 	while (value = fpkgparam(fp, temp)) {
212*0Sstevel@tonic-gate 		if (strcmp(temp, "ARCH") == 0 ||
213*0Sstevel@tonic-gate 		    strcmp(temp, "CATEGORY") == 0) {
214*0Sstevel@tonic-gate 			/* remove all whitespace from value */
215*0Sstevel@tonic-gate 			pt = copy = value;
216*0Sstevel@tonic-gate 			while (*pt) {
217*0Sstevel@tonic-gate 				if (!isspace((unsigned char)*pt))
218*0Sstevel@tonic-gate 					*copy++ = *pt;
219*0Sstevel@tonic-gate 				pt++;
220*0Sstevel@tonic-gate 			}
221*0Sstevel@tonic-gate 			*copy = '\0';
222*0Sstevel@tonic-gate 		}
223*0Sstevel@tonic-gate 		count++;
224*0Sstevel@tonic-gate 		memloc = NULL;
225*0Sstevel@tonic-gate 		if (strcmp(temp, "NAME") == 0)
226*0Sstevel@tonic-gate 			memloc = &info->name;
227*0Sstevel@tonic-gate 		else if (strcmp(temp, "VERSION") == 0)
228*0Sstevel@tonic-gate 			memloc = &info->version;
229*0Sstevel@tonic-gate 		else if (strcmp(temp, "ARCH") == 0)
230*0Sstevel@tonic-gate 			memloc = &info->arch;
231*0Sstevel@tonic-gate 		else if (strcmp(temp, "VENDOR") == 0)
232*0Sstevel@tonic-gate 			memloc = &info->vendor;
233*0Sstevel@tonic-gate 		else if (strcmp(temp, "BASEDIR") == 0)
234*0Sstevel@tonic-gate 			memloc = &info->basedir;
235*0Sstevel@tonic-gate 		else if (strcmp(temp, "CATEGORY") == 0)
236*0Sstevel@tonic-gate 			memloc = &info->catg;
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 		temp[0] = '\0';
239*0Sstevel@tonic-gate 		if (memloc == NULL)
240*0Sstevel@tonic-gate 			continue; /* not a parameter we're looking for */
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 		*memloc = strdup(value);
243*0Sstevel@tonic-gate 		if (!*memloc) {
244*0Sstevel@tonic-gate 			(void) fclose(fp);
245*0Sstevel@tonic-gate 			errno = ENOMEM;
246*0Sstevel@tonic-gate 			return (-1); /* malloc from strdup failed */
247*0Sstevel@tonic-gate 		}
248*0Sstevel@tonic-gate 	}
249*0Sstevel@tonic-gate 	(void) fclose(fp);
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	if (!count) {
252*0Sstevel@tonic-gate 		errno = ESRCH;
253*0Sstevel@tonic-gate 		return (-1);
254*0Sstevel@tonic-gate 	}
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	info->status = (strcmp(pkgdir, get_PKGLOC()) ? PI_SPOOLED :
257*0Sstevel@tonic-gate 	    PI_INSTALLED);
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate 	if (info->status == PI_INSTALLED) {
260*0Sstevel@tonic-gate 		(void) sprintf(temp, "%s/%s/!I-Lock!", pkgdir, pkginst);
261*0Sstevel@tonic-gate 		if (access(temp, 0) == 0)
262*0Sstevel@tonic-gate 			info->status = PI_PARTIAL;
263*0Sstevel@tonic-gate 		else {
264*0Sstevel@tonic-gate 			(void) sprintf(temp, "%s/%s/!R-Lock!", pkgdir, pkginst);
265*0Sstevel@tonic-gate 			if (access(temp, 0) == 0)
266*0Sstevel@tonic-gate 				info->status = PI_PARTIAL;
267*0Sstevel@tonic-gate 		}
268*0Sstevel@tonic-gate 	}
269*0Sstevel@tonic-gate 	info->pkginst = strdup(pkginst);
270*0Sstevel@tonic-gate 	return (0);
271*0Sstevel@tonic-gate }
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate static int
svr4info(struct pkginfo * info,char * pkginst,char * ckvers)274*0Sstevel@tonic-gate svr4info(struct pkginfo *info, char *pkginst, char *ckvers)
275*0Sstevel@tonic-gate {
276*0Sstevel@tonic-gate 	static DIR *pdirfp;
277*0Sstevel@tonic-gate 	struct stat64 status;
278*0Sstevel@tonic-gate 	FILE *fp;
279*0Sstevel@tonic-gate 	char *pt, path[128], line[128];
280*0Sstevel@tonic-gate 	char	temp[PKGSIZ+1];
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	if (strcmp(pkginst, "all")) {
283*0Sstevel@tonic-gate 		if (pdirfp) {
284*0Sstevel@tonic-gate 			(void) closedir(pdirfp);
285*0Sstevel@tonic-gate 			pdirfp = NULL;
286*0Sstevel@tonic-gate 		}
287*0Sstevel@tonic-gate 		/* determine pkginst - remove '.*' extension, if any */
288*0Sstevel@tonic-gate 		(void) strncpy(temp, pkginst, PKGSIZ);
289*0Sstevel@tonic-gate 		if (((pt = strchr(temp, '.')) != NULL) && strcmp(pt, ".*") == 0)
290*0Sstevel@tonic-gate 			*pt = '\0';
291*0Sstevel@tonic-gate 	}
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	/* look in /usr/options direcotry for 'name' file */
294*0Sstevel@tonic-gate 	(void) sprintf(path, "%s/%s.name", get_PKGOLD(), temp);
295*0Sstevel@tonic-gate 	if (lstat64(path, &status)) {
296*0Sstevel@tonic-gate 		errno = (errno == ENOENT) ? ESRCH : EACCES;
297*0Sstevel@tonic-gate 		return (-1);
298*0Sstevel@tonic-gate 	}
299*0Sstevel@tonic-gate 	if ((status.st_mode & S_IFMT) != S_IFREG) {
300*0Sstevel@tonic-gate 		errno = ESRCH;
301*0Sstevel@tonic-gate 		return (-1);
302*0Sstevel@tonic-gate 	}
303*0Sstevel@tonic-gate 	if ((fp = fopen(path, "r")) == NULL) {
304*0Sstevel@tonic-gate 		errno = (errno == ENOENT) ? ESRCH : EACCES;
305*0Sstevel@tonic-gate 		return (-1);
306*0Sstevel@tonic-gate 	}
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 	/* /usr/options/xxx.name exists */
309*0Sstevel@tonic-gate 	(void) fgets(line, 128, fp);
310*0Sstevel@tonic-gate 	(void) fclose(fp);
311*0Sstevel@tonic-gate 	if (pt = strchr(line, '\n'))
312*0Sstevel@tonic-gate 		*pt = '\0'; /* remove trailing newline */
313*0Sstevel@tonic-gate 	if (pt = strchr(line, ':'))
314*0Sstevel@tonic-gate 		*pt++ = '\0'; /* assumed version specification */
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 	if (info) {
317*0Sstevel@tonic-gate 		info->name = strdup(line);
318*0Sstevel@tonic-gate 		info->pkginst = strdup(temp);
319*0Sstevel@tonic-gate 		if (!info->name || !info->pkginst) {
320*0Sstevel@tonic-gate 			errno = ENOMEM;
321*0Sstevel@tonic-gate 			return (-1);
322*0Sstevel@tonic-gate 		}
323*0Sstevel@tonic-gate 		info->status = PI_PRESVR4;
324*0Sstevel@tonic-gate 		info->version = NULL;
325*0Sstevel@tonic-gate 	}
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 	if (pt) {
328*0Sstevel@tonic-gate 		/* eat leading space off of version spec */
329*0Sstevel@tonic-gate 		while (isspace((unsigned char)*pt))
330*0Sstevel@tonic-gate 			pt++;
331*0Sstevel@tonic-gate 	}
332*0Sstevel@tonic-gate 	if (ckvers && verscmp(ckvers, pt)) {
333*0Sstevel@tonic-gate 		errno = ESRCH;
334*0Sstevel@tonic-gate 		return (-1);
335*0Sstevel@tonic-gate 	}
336*0Sstevel@tonic-gate 	if (info && *pt)
337*0Sstevel@tonic-gate 		info->version = strdup(pt);
338*0Sstevel@tonic-gate 	return (0);
339*0Sstevel@tonic-gate }
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate static int
ckinst(char * pkginst,char * pkgarch,char * pkgvers,char * ckarch,char * ckvers)342*0Sstevel@tonic-gate ckinst(char *pkginst, char *pkgarch, char *pkgvers, char *ckarch, char *ckvers)
343*0Sstevel@tonic-gate {
344*0Sstevel@tonic-gate 	if (ckarch && archcmp(ckarch, pkgarch))
345*0Sstevel@tonic-gate 		return (-1);
346*0Sstevel@tonic-gate 	if (ckvers) {
347*0Sstevel@tonic-gate 		/* Check for exact version match */
348*0Sstevel@tonic-gate 		if (verscmp(ckvers, pkgvers)) {
349*0Sstevel@tonic-gate 			/* Check for compatable version */
350*0Sstevel@tonic-gate 			if (compver(pkginst, ckvers))
351*0Sstevel@tonic-gate 				return (-1);
352*0Sstevel@tonic-gate 		}
353*0Sstevel@tonic-gate 	}
354*0Sstevel@tonic-gate 	return (0);
355*0Sstevel@tonic-gate }
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate /*VARARGS*/
358*0Sstevel@tonic-gate char *
fpkginst(char * pkg,...)359*0Sstevel@tonic-gate fpkginst(char *pkg, ...)
360*0Sstevel@tonic-gate {
361*0Sstevel@tonic-gate 	static char pkginst[PKGSIZ+1];
362*0Sstevel@tonic-gate 	static DIR *pdirfp;
363*0Sstevel@tonic-gate 	struct dirent64 *dp;
364*0Sstevel@tonic-gate 	char	*pt, *ckarch, *ckvers;
365*0Sstevel@tonic-gate 	va_list	ap;
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 	va_start(ap, pkg);
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 	if (pkg == NULL) {
370*0Sstevel@tonic-gate 		/* request to close or rewind the file */
371*0Sstevel@tonic-gate 		if (pdirfp) {
372*0Sstevel@tonic-gate 			(void) closedir(pdirfp);
373*0Sstevel@tonic-gate 			pdirfp = NULL;
374*0Sstevel@tonic-gate 		}
375*0Sstevel@tonic-gate 		(void) svr4inst(NULL); /* close any files used here */
376*0Sstevel@tonic-gate 		return (NULL);
377*0Sstevel@tonic-gate 	}
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 	ckarch = va_arg(ap, char *);
380*0Sstevel@tonic-gate 	ckvers = va_arg(ap, char *);
381*0Sstevel@tonic-gate 	va_end(ap);
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate 	if (!pkgdir)
384*0Sstevel@tonic-gate 		pkgdir = get_PKGLOC();
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate 	if (!pdirfp && ((pdirfp = opendir(pkgdir)) == NULL)) {
387*0Sstevel@tonic-gate 		errno = EACCES;
388*0Sstevel@tonic-gate 		return (NULL);
389*0Sstevel@tonic-gate 	}
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 	while ((dp = readdir64(pdirfp)) != NULL) {
392*0Sstevel@tonic-gate 		if (dp->d_name[0] == '.')
393*0Sstevel@tonic-gate 			continue;
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate 		if (pkgnmchk(dp->d_name, pkg, 0))
396*0Sstevel@tonic-gate 			continue; /* ignore invalid SVR4 package names */
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 		if (ckinfo(dp->d_name, ckarch, ckvers))
399*0Sstevel@tonic-gate 			continue;
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 		/*
402*0Sstevel@tonic-gate 		 * Leave directory open in case user requests another
403*0Sstevel@tonic-gate 		 * instance.
404*0Sstevel@tonic-gate 		 */
405*0Sstevel@tonic-gate 		(void) strcpy(pkginst, dp->d_name);
406*0Sstevel@tonic-gate 		return (pkginst);
407*0Sstevel@tonic-gate 	}
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 	/*
410*0Sstevel@tonic-gate 	 * If we are searching the directory which contains info about
411*0Sstevel@tonic-gate 	 * installed packages, check the pre-svr4 directory for an instance
412*0Sstevel@tonic-gate 	 * and be sure it matches any version specification provided to us
413*0Sstevel@tonic-gate 	 */
414*0Sstevel@tonic-gate 	if (strcmp(pkgdir, get_PKGLOC()) == 0 && (ckarch == NULL)) {
415*0Sstevel@tonic-gate 		/* search for pre-SVR4 instance */
416*0Sstevel@tonic-gate 		if (pt = svr4inst(pkg))
417*0Sstevel@tonic-gate 			return (pt);
418*0Sstevel@tonic-gate 	}
419*0Sstevel@tonic-gate 	errno = ESRCH;
420*0Sstevel@tonic-gate 	/* close any file we might have open */
421*0Sstevel@tonic-gate 	(void) closedir(pdirfp);
422*0Sstevel@tonic-gate 	pdirfp = NULL;
423*0Sstevel@tonic-gate 	return (NULL);
424*0Sstevel@tonic-gate }
425*0Sstevel@tonic-gate /*ARGSUSED*/
426*0Sstevel@tonic-gate 
427*0Sstevel@tonic-gate static char *
svr4inst(char * pkg)428*0Sstevel@tonic-gate svr4inst(char *pkg)
429*0Sstevel@tonic-gate {
430*0Sstevel@tonic-gate 	static char pkginst[PKGSIZ];
431*0Sstevel@tonic-gate 	static DIR *pdirfp;
432*0Sstevel@tonic-gate 	struct dirent64 *dp;
433*0Sstevel@tonic-gate 	struct stat64	status;	/* file status buffer */
434*0Sstevel@tonic-gate 	char	*pt;
435*0Sstevel@tonic-gate 	char	path[PATH_MAX];
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate 	if (pkg == NULL) {
438*0Sstevel@tonic-gate 		if (pdirfp) {
439*0Sstevel@tonic-gate 			(void) closedir(pdirfp);
440*0Sstevel@tonic-gate 			pdirfp = NULL;
441*0Sstevel@tonic-gate 		}
442*0Sstevel@tonic-gate 		return (NULL);
443*0Sstevel@tonic-gate 	}
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate 	if (!pdirfp && ((pdirfp = opendir(get_PKGOLD())) == NULL))
446*0Sstevel@tonic-gate 		return (NULL);
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 	while ((dp = readdir64(pdirfp)) != NULL) {
449*0Sstevel@tonic-gate 		if (dp->d_name[0] == '.')
450*0Sstevel@tonic-gate 			continue;
451*0Sstevel@tonic-gate 		pt = strchr(dp->d_name, '.');
452*0Sstevel@tonic-gate 		if (pt && strcmp(pt, ".name") == 0) {
453*0Sstevel@tonic-gate 			/* the pkgnmchk function works on .name extensions */
454*0Sstevel@tonic-gate 			if (pkgnmchk(dp->d_name, pkg, 1))
455*0Sstevel@tonic-gate 				continue;
456*0Sstevel@tonic-gate 			(void) sprintf(path, "%s/%s", get_PKGOLD(), dp->d_name);
457*0Sstevel@tonic-gate 			if (lstat64(path, &status))
458*0Sstevel@tonic-gate 				continue;
459*0Sstevel@tonic-gate 			if ((status.st_mode & S_IFMT) != S_IFREG)
460*0Sstevel@tonic-gate 				continue;
461*0Sstevel@tonic-gate 			*pt = '\0';
462*0Sstevel@tonic-gate 			(void) strcpy(pkginst, dp->d_name);
463*0Sstevel@tonic-gate 			return (pkginst);
464*0Sstevel@tonic-gate 		}
465*0Sstevel@tonic-gate 	}
466*0Sstevel@tonic-gate 	(void) closedir(pdirfp);
467*0Sstevel@tonic-gate 	pdirfp = NULL;
468*0Sstevel@tonic-gate 	return (NULL);
469*0Sstevel@tonic-gate }
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate static int
verscmp(char * request,char * actual)472*0Sstevel@tonic-gate verscmp(char *request, char *actual)
473*0Sstevel@tonic-gate {
474*0Sstevel@tonic-gate 	/* eat leading white space */
475*0Sstevel@tonic-gate 	while (isspace((unsigned char)*actual))
476*0Sstevel@tonic-gate 		actual++;
477*0Sstevel@tonic-gate 	while (isspace((unsigned char)*request))
478*0Sstevel@tonic-gate 		request++;
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate 	while (*request || *actual) {
481*0Sstevel@tonic-gate 		/*
482*0Sstevel@tonic-gate 		 * Once the pointers don't match, return an error condition.
483*0Sstevel@tonic-gate 		 */
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 		if (*request++ != *actual++)
486*0Sstevel@tonic-gate 			return (-1);
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate 		/* eat white space if any in both the strings */
489*0Sstevel@tonic-gate 		if (isspace((unsigned char)*request)) {
490*0Sstevel@tonic-gate 			if (*actual && !isspace((unsigned char)*actual))
491*0Sstevel@tonic-gate 				return (-1);
492*0Sstevel@tonic-gate 			while (isspace((unsigned char)*request))
493*0Sstevel@tonic-gate 				request++;
494*0Sstevel@tonic-gate 			while (isspace((unsigned char)*actual))
495*0Sstevel@tonic-gate 				actual++;
496*0Sstevel@tonic-gate 		}
497*0Sstevel@tonic-gate 	}
498*0Sstevel@tonic-gate 
499*0Sstevel@tonic-gate 	return (0);
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate }
502*0Sstevel@tonic-gate 
503*0Sstevel@tonic-gate static int
compver(char * pkginst,char * version)504*0Sstevel@tonic-gate compver(char *pkginst, char *version)
505*0Sstevel@tonic-gate {
506*0Sstevel@tonic-gate 	FILE *fp;
507*0Sstevel@tonic-gate 	char temp[256];
508*0Sstevel@tonic-gate 
509*0Sstevel@tonic-gate 	(void) sprintf(temp, "%s/%s/install/compver", get_PKGLOC(), pkginst);
510*0Sstevel@tonic-gate 	if ((fp = fopen(temp, "r")) == NULL)
511*0Sstevel@tonic-gate 		return (-1);
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate 	while (fgets(temp, 256, fp)) {
514*0Sstevel@tonic-gate 		if (*temp == '#')
515*0Sstevel@tonic-gate 			continue;
516*0Sstevel@tonic-gate 		if (verscmp(temp, version) == 0) {
517*0Sstevel@tonic-gate 			(void) fclose(fp);
518*0Sstevel@tonic-gate 			return (0);
519*0Sstevel@tonic-gate 		}
520*0Sstevel@tonic-gate 	}
521*0Sstevel@tonic-gate 	(void) fclose(fp);
522*0Sstevel@tonic-gate 	return (-1);
523*0Sstevel@tonic-gate }
524*0Sstevel@tonic-gate 
525*0Sstevel@tonic-gate static int
archcmp(char * arch,char * archlist)526*0Sstevel@tonic-gate archcmp(char *arch, char *archlist)
527*0Sstevel@tonic-gate {
528*0Sstevel@tonic-gate 	char *pt;
529*0Sstevel@tonic-gate 
530*0Sstevel@tonic-gate 	if (arch == NULL)
531*0Sstevel@tonic-gate 		return (0);
532*0Sstevel@tonic-gate 
533*0Sstevel@tonic-gate 	/* arch and archlist must not contain whitespace! */
534*0Sstevel@tonic-gate 
535*0Sstevel@tonic-gate 	while (*archlist) {
536*0Sstevel@tonic-gate 		for (pt = arch; *pt && (*pt == *archlist); )
537*0Sstevel@tonic-gate 			pt++, archlist++;
538*0Sstevel@tonic-gate 		if (!*pt && (!*archlist || (*archlist == ',')))
539*0Sstevel@tonic-gate 			return (0);
540*0Sstevel@tonic-gate 		while (*archlist) {
541*0Sstevel@tonic-gate 			if (*archlist++ == ',')
542*0Sstevel@tonic-gate 				break;
543*0Sstevel@tonic-gate 		}
544*0Sstevel@tonic-gate 	}
545*0Sstevel@tonic-gate 	return (-1);
546*0Sstevel@tonic-gate }
547*0Sstevel@tonic-gate 
548*0Sstevel@tonic-gate static int
ckinfo(char * inst,char * arch,char * vers)549*0Sstevel@tonic-gate ckinfo(char *inst, char *arch, char *vers)
550*0Sstevel@tonic-gate {
551*0Sstevel@tonic-gate 	FILE	*fp;
552*0Sstevel@tonic-gate 	char	temp[128];
553*0Sstevel@tonic-gate 	char	file[PATH_MAX];
554*0Sstevel@tonic-gate 	char	*pt, *copy, *value, *myarch, *myvers;
555*0Sstevel@tonic-gate 	int	errflg;
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate 	(void) sprintf(file, "%s/%s/pkginfo", pkgdir, inst);
558*0Sstevel@tonic-gate 	if ((fp = fopen(file, "r")) == NULL)
559*0Sstevel@tonic-gate 		return (1);
560*0Sstevel@tonic-gate 
561*0Sstevel@tonic-gate 	if ((arch == NULL) && (vers == NULL)) {
562*0Sstevel@tonic-gate 		(void) fclose(fp);
563*0Sstevel@tonic-gate 		return (0);
564*0Sstevel@tonic-gate 	}
565*0Sstevel@tonic-gate 	temp[0] = '\0';
566*0Sstevel@tonic-gate 	myarch = myvers = NULL;
567*0Sstevel@tonic-gate 	while (value = fpkgparam(fp, temp)) {
568*0Sstevel@tonic-gate 		if (strcmp(temp, "ARCH") == 0) {
569*0Sstevel@tonic-gate 			/* remove all whitespace from value */
570*0Sstevel@tonic-gate 			pt = copy = value;
571*0Sstevel@tonic-gate 			while (*pt) {
572*0Sstevel@tonic-gate 				if (!isspace((unsigned char)*pt))
573*0Sstevel@tonic-gate 					*copy++ = *pt;
574*0Sstevel@tonic-gate 				pt++;
575*0Sstevel@tonic-gate 			}
576*0Sstevel@tonic-gate 			*copy = '\0';
577*0Sstevel@tonic-gate 			myarch = value;
578*0Sstevel@tonic-gate 			if (myvers)
579*0Sstevel@tonic-gate 				break;
580*0Sstevel@tonic-gate 		} else if (strcmp(temp, "VERSION") == 0) {
581*0Sstevel@tonic-gate 			myvers = value;
582*0Sstevel@tonic-gate 			if (myarch)
583*0Sstevel@tonic-gate 				break;
584*0Sstevel@tonic-gate 		} else
585*0Sstevel@tonic-gate 			free(value);
586*0Sstevel@tonic-gate 		temp[0] = '\0';
587*0Sstevel@tonic-gate 	}
588*0Sstevel@tonic-gate 	(void) fclose(fp);
589*0Sstevel@tonic-gate 	errflg = 0;
590*0Sstevel@tonic-gate 
591*0Sstevel@tonic-gate 	if (ckinst(inst, myarch, myvers, arch, vers))
592*0Sstevel@tonic-gate 		errflg++;
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate 	if (myarch)
595*0Sstevel@tonic-gate 		free(myarch);
596*0Sstevel@tonic-gate 	if (myvers)
597*0Sstevel@tonic-gate 		free(myvers);
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 	return (errflg);
600*0Sstevel@tonic-gate }
601