xref: /titanic_44/usr/src/cmd/svr4pkg/libinst/eptstat.c (revision 5c51f1241dbbdf2656d0e10011981411ed0c9673)
1*5c51f124SMoriah Waterland /*
2*5c51f124SMoriah Waterland  * CDDL HEADER START
3*5c51f124SMoriah Waterland  *
4*5c51f124SMoriah Waterland  * The contents of this file are subject to the terms of the
5*5c51f124SMoriah Waterland  * Common Development and Distribution License (the "License").
6*5c51f124SMoriah Waterland  * You may not use this file except in compliance with the License.
7*5c51f124SMoriah Waterland  *
8*5c51f124SMoriah Waterland  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*5c51f124SMoriah Waterland  * or http://www.opensolaris.org/os/licensing.
10*5c51f124SMoriah Waterland  * See the License for the specific language governing permissions
11*5c51f124SMoriah Waterland  * and limitations under the License.
12*5c51f124SMoriah Waterland  *
13*5c51f124SMoriah Waterland  * When distributing Covered Code, include this CDDL HEADER in each
14*5c51f124SMoriah Waterland  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*5c51f124SMoriah Waterland  * If applicable, add the following below this CDDL HEADER, with the
16*5c51f124SMoriah Waterland  * fields enclosed by brackets "[]" replaced with your own identifying
17*5c51f124SMoriah Waterland  * information: Portions Copyright [yyyy] [name of copyright owner]
18*5c51f124SMoriah Waterland  *
19*5c51f124SMoriah Waterland  * CDDL HEADER END
20*5c51f124SMoriah Waterland  */
21*5c51f124SMoriah Waterland 
22*5c51f124SMoriah Waterland /*
23*5c51f124SMoriah Waterland  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*5c51f124SMoriah Waterland  * Use is subject to license terms.
25*5c51f124SMoriah Waterland  */
26*5c51f124SMoriah Waterland 
27*5c51f124SMoriah Waterland /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*5c51f124SMoriah Waterland /* All Rights Reserved */
29*5c51f124SMoriah Waterland 
30*5c51f124SMoriah Waterland 
31*5c51f124SMoriah Waterland #include <stdio.h>
32*5c51f124SMoriah Waterland #include <errno.h>
33*5c51f124SMoriah Waterland #include <stdlib.h>
34*5c51f124SMoriah Waterland #include <unistd.h>
35*5c51f124SMoriah Waterland #include <string.h>
36*5c51f124SMoriah Waterland #include <malloc.h>
37*5c51f124SMoriah Waterland #include <assert.h>
38*5c51f124SMoriah Waterland #include <sys/stat.h>
39*5c51f124SMoriah Waterland #include <pkgstrct.h>
40*5c51f124SMoriah Waterland #include <pkginfo.h>
41*5c51f124SMoriah Waterland #include <locale.h>
42*5c51f124SMoriah Waterland #include <libintl.h>
43*5c51f124SMoriah Waterland #include <pkglib.h>
44*5c51f124SMoriah Waterland #include <install.h>
45*5c51f124SMoriah Waterland #include <libinst.h>
46*5c51f124SMoriah Waterland #include <libadm.h>
47*5c51f124SMoriah Waterland 
48*5c51f124SMoriah Waterland #define	PINFOALLOC	200
49*5c51f124SMoriah Waterland 
50*5c51f124SMoriah Waterland #define	ERR_MEMORY	"memory allocation failure, errno=%d"
51*5c51f124SMoriah Waterland 
52*5c51f124SMoriah Waterland int	otherstoo;
53*5c51f124SMoriah Waterland char	*useclass;
54*5c51f124SMoriah Waterland 
55*5c51f124SMoriah Waterland static int pinfo_handle = -1;
56*5c51f124SMoriah Waterland 
57*5c51f124SMoriah Waterland /* Free all allocated package info structures. */
58*5c51f124SMoriah Waterland void
pinfo_free(void)59*5c51f124SMoriah Waterland pinfo_free(void)
60*5c51f124SMoriah Waterland {
61*5c51f124SMoriah Waterland 	bl_free(pinfo_handle);
62*5c51f124SMoriah Waterland }
63*5c51f124SMoriah Waterland 
64*5c51f124SMoriah Waterland /*
65*5c51f124SMoriah Waterland  * This function manipulates the pinfo entry corresponding to the package
66*5c51f124SMoriah Waterland  * indicated on the command line.
67*5c51f124SMoriah Waterland  */
68*5c51f124SMoriah Waterland struct pinfo *
eptstat(struct cfent * entry,char * pkg,char c)69*5c51f124SMoriah Waterland eptstat(struct cfent *entry, char *pkg, char c)
70*5c51f124SMoriah Waterland {
71*5c51f124SMoriah Waterland 	struct pinfo *pinfo, *last, *me, *myparent;
72*5c51f124SMoriah Waterland 
73*5c51f124SMoriah Waterland 	otherstoo = 0;
74*5c51f124SMoriah Waterland 	useclass = entry->pkg_class;
75*5c51f124SMoriah Waterland 
76*5c51f124SMoriah Waterland 	me = myparent = last = (struct pinfo *)0;
77*5c51f124SMoriah Waterland 
78*5c51f124SMoriah Waterland 	if (pinfo_handle == -1) {
79*5c51f124SMoriah Waterland 		pinfo_handle = bl_create(PINFOALLOC, sizeof (struct pinfo),
80*5c51f124SMoriah Waterland 		    "package data");
81*5c51f124SMoriah Waterland 	}
82*5c51f124SMoriah Waterland 
83*5c51f124SMoriah Waterland 	for (pinfo = entry->pinfo; pinfo; pinfo = pinfo->next) {
84*5c51f124SMoriah Waterland 		if (strcmp(pkg, pinfo->pkg) == 0) {
85*5c51f124SMoriah Waterland 			if (*pinfo->aclass)
86*5c51f124SMoriah Waterland 				useclass = pinfo->aclass;
87*5c51f124SMoriah Waterland 			myparent = last;
88*5c51f124SMoriah Waterland 			me = pinfo;
89*5c51f124SMoriah Waterland 		} else
90*5c51f124SMoriah Waterland 			otherstoo++;
91*5c51f124SMoriah Waterland 		last = pinfo;
92*5c51f124SMoriah Waterland 	}
93*5c51f124SMoriah Waterland 
94*5c51f124SMoriah Waterland 	if (c) {
95*5c51f124SMoriah Waterland 		/*
96*5c51f124SMoriah Waterland 		 * use a delete/add strategy to keep package list
97*5c51f124SMoriah Waterland 		 * ordered by modification time
98*5c51f124SMoriah Waterland 		 */
99*5c51f124SMoriah Waterland 		if (me) {
100*5c51f124SMoriah Waterland 			/* remove from list first */
101*5c51f124SMoriah Waterland 			if (myparent)
102*5c51f124SMoriah Waterland 				myparent->next = me->next;
103*5c51f124SMoriah Waterland 			else
104*5c51f124SMoriah Waterland 				entry->pinfo = me->next;
105*5c51f124SMoriah Waterland 			if (me == last)
106*5c51f124SMoriah Waterland 				last = myparent;
107*5c51f124SMoriah Waterland 			entry->npkgs--;
108*5c51f124SMoriah Waterland 			/* leave 'me' around until later! */
109*5c51f124SMoriah Waterland 		}
110*5c51f124SMoriah Waterland 		if ((c != STAT_NEXT) && (me || (c != RM_RDY))) {
111*5c51f124SMoriah Waterland 			/* need to add onto end */
112*5c51f124SMoriah Waterland 			entry->npkgs++;
113*5c51f124SMoriah Waterland 			if (me == NULL) {
114*5c51f124SMoriah Waterland 				/* LINTED pointer cast may result in impro... */
115*5c51f124SMoriah Waterland 				me = (struct pinfo *)
116*5c51f124SMoriah Waterland 				    bl_next_avail(pinfo_handle);
117*5c51f124SMoriah Waterland 				if (me == NULL) {
118*5c51f124SMoriah Waterland 					progerr(gettext(ERR_MEMORY), errno);
119*5c51f124SMoriah Waterland 					quit(99);
120*5c51f124SMoriah Waterland 				}
121*5c51f124SMoriah Waterland 			} else {
122*5c51f124SMoriah Waterland 				me->next = (struct pinfo *)NULL;
123*5c51f124SMoriah Waterland 				if (entry->npkgs == 1) {
124*5c51f124SMoriah Waterland 					if (me->aclass[0])
125*5c51f124SMoriah Waterland 						(void) strcpy(entry->pkg_class,
126*5c51f124SMoriah Waterland 							me->aclass);
127*5c51f124SMoriah Waterland 					useclass = entry->pkg_class;
128*5c51f124SMoriah Waterland 				} else
129*5c51f124SMoriah Waterland 					useclass = me->aclass;
130*5c51f124SMoriah Waterland 			}
131*5c51f124SMoriah Waterland 			(void) strncpy(me->pkg, pkg, PKGSIZ);
132*5c51f124SMoriah Waterland 
133*5c51f124SMoriah Waterland 			/*
134*5c51f124SMoriah Waterland 			 * Only change status for local objects.  Need
135*5c51f124SMoriah Waterland 			 * to maintain "shared" status for objects that
136*5c51f124SMoriah Waterland 			 * are provided from a server.
137*5c51f124SMoriah Waterland 			 */
138*5c51f124SMoriah Waterland 			if (me->status != SERVED_FILE)
139*5c51f124SMoriah Waterland 				me->status = ((c == DUP_ENTRY) ? '\0' : c);
140*5c51f124SMoriah Waterland 
141*5c51f124SMoriah Waterland 			if (last)
142*5c51f124SMoriah Waterland 				last->next = me; /* add to end */
143*5c51f124SMoriah Waterland 			else
144*5c51f124SMoriah Waterland 				entry->pinfo = me; /* only item */
145*5c51f124SMoriah Waterland 		} else {
146*5c51f124SMoriah Waterland 			/* just wanted to remove this package from list */
147*5c51f124SMoriah Waterland 			if (me) {
148*5c51f124SMoriah Waterland 				free(me);
149*5c51f124SMoriah Waterland 				me = (struct pinfo *)0;
150*5c51f124SMoriah Waterland 			}
151*5c51f124SMoriah Waterland 		}
152*5c51f124SMoriah Waterland 	}
153*5c51f124SMoriah Waterland 	return (me);
154*5c51f124SMoriah Waterland }
155