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 
32*5c51f124SMoriah Waterland #include <limits.h>
33*5c51f124SMoriah Waterland #include <string.h>
34*5c51f124SMoriah Waterland #include <stdlib.h>
35*5c51f124SMoriah Waterland #include <unistd.h>
36*5c51f124SMoriah Waterland #include <errno.h>
37*5c51f124SMoriah Waterland #include <locale.h>
38*5c51f124SMoriah Waterland #include <libintl.h>
39*5c51f124SMoriah Waterland #include <pkglib.h>
40*5c51f124SMoriah Waterland #include <libadm.h>
41*5c51f124SMoriah Waterland 
42*5c51f124SMoriah Waterland #define	ERR_MEMORY	"memory allocation failure, errno=%d"
43*5c51f124SMoriah Waterland 
44*5c51f124SMoriah Waterland /*
45*5c51f124SMoriah Waterland  * using factor of eight limits maximum
46*5c51f124SMoriah Waterland  * memory fragmentation to 12.5%
47*5c51f124SMoriah Waterland  */
48*5c51f124SMoriah Waterland #define	MEMSIZ	PATH_MAX*8
49*5c51f124SMoriah Waterland 
50*5c51f124SMoriah Waterland struct dup {
51*5c51f124SMoriah Waterland 	char	mem[MEMSIZ];
52*5c51f124SMoriah Waterland 	struct dup *next;
53*5c51f124SMoriah Waterland };
54*5c51f124SMoriah Waterland 
55*5c51f124SMoriah Waterland static struct dup *head, *tail, *new;
56*5c51f124SMoriah Waterland 
57*5c51f124SMoriah Waterland static int	size, initialized;
58*5c51f124SMoriah Waterland static void	pathinit();
59*5c51f124SMoriah Waterland static void	growstore();
60*5c51f124SMoriah Waterland 
61*5c51f124SMoriah Waterland /*
62*5c51f124SMoriah Waterland  * These functions allocate space for all the path names required
63*5c51f124SMoriah Waterland  * in the packaging code. They are all allocated here so as to reduce
64*5c51f124SMoriah Waterland  * memory fragmentation.
65*5c51f124SMoriah Waterland  */
66*5c51f124SMoriah Waterland 
67*5c51f124SMoriah Waterland /* Initialize storage area. */
68*5c51f124SMoriah Waterland static void
pathinit()69*5c51f124SMoriah Waterland pathinit()
70*5c51f124SMoriah Waterland {
71*5c51f124SMoriah Waterland 	if (head == NULL)
72*5c51f124SMoriah Waterland 		size = (-1);
73*5c51f124SMoriah Waterland 	else {
74*5c51f124SMoriah Waterland 		/* free all memory used except initial structure */
75*5c51f124SMoriah Waterland 		tail = head->next;
76*5c51f124SMoriah Waterland 		while (tail) {
77*5c51f124SMoriah Waterland 			new = tail->next;
78*5c51f124SMoriah Waterland 			free(tail);
79*5c51f124SMoriah Waterland 			tail = new;
80*5c51f124SMoriah Waterland 		}
81*5c51f124SMoriah Waterland 		tail = head;
82*5c51f124SMoriah Waterland 		size = MEMSIZ;
83*5c51f124SMoriah Waterland 	}
84*5c51f124SMoriah Waterland 
85*5c51f124SMoriah Waterland 	initialized = 1;
86*5c51f124SMoriah Waterland }
87*5c51f124SMoriah Waterland 
88*5c51f124SMoriah Waterland /* Allocate additional space for storage area. */
89*5c51f124SMoriah Waterland static void
growstore()90*5c51f124SMoriah Waterland growstore()
91*5c51f124SMoriah Waterland {
92*5c51f124SMoriah Waterland 	/* need more memory */
93*5c51f124SMoriah Waterland 	new = calloc(1, sizeof (struct dup));
94*5c51f124SMoriah Waterland 	if (new == NULL) {
95*5c51f124SMoriah Waterland 		progerr(gettext(ERR_MEMORY), errno);
96*5c51f124SMoriah Waterland 		quit(99);
97*5c51f124SMoriah Waterland 	}
98*5c51f124SMoriah Waterland 	if (head == NULL)
99*5c51f124SMoriah Waterland 		head = new;
100*5c51f124SMoriah Waterland 	else
101*5c51f124SMoriah Waterland 		tail->next = new;
102*5c51f124SMoriah Waterland 	tail = new;
103*5c51f124SMoriah Waterland 	size = MEMSIZ;
104*5c51f124SMoriah Waterland }
105*5c51f124SMoriah Waterland 
106*5c51f124SMoriah Waterland /* Allocate and return a pointer. If n == 0, initialize. */
107*5c51f124SMoriah Waterland char *
pathalloc(int n)108*5c51f124SMoriah Waterland pathalloc(int n)
109*5c51f124SMoriah Waterland {
110*5c51f124SMoriah Waterland 	char	*pt;
111*5c51f124SMoriah Waterland 
112*5c51f124SMoriah Waterland 	if (n <= 0) {
113*5c51f124SMoriah Waterland 		pathinit();
114*5c51f124SMoriah Waterland 		pt = NULL;
115*5c51f124SMoriah Waterland 	} else {
116*5c51f124SMoriah Waterland 		if (!initialized)
117*5c51f124SMoriah Waterland 			pathinit();
118*5c51f124SMoriah Waterland 
119*5c51f124SMoriah Waterland 		n++;	/* Account for terminating null. */
120*5c51f124SMoriah Waterland 
121*5c51f124SMoriah Waterland 		if (size < n)
122*5c51f124SMoriah Waterland 			growstore();
123*5c51f124SMoriah Waterland 
124*5c51f124SMoriah Waterland 		pt = &tail->mem[MEMSIZ-size];
125*5c51f124SMoriah Waterland 		size -= n;
126*5c51f124SMoriah Waterland 	}
127*5c51f124SMoriah Waterland 
128*5c51f124SMoriah Waterland 	return (pt);
129*5c51f124SMoriah Waterland }
130*5c51f124SMoriah Waterland 
131*5c51f124SMoriah Waterland /* Allocate and insert a pathname returning a pointer to the new string. */
132*5c51f124SMoriah Waterland char *
pathdup(char * s)133*5c51f124SMoriah Waterland pathdup(char *s)
134*5c51f124SMoriah Waterland {
135*5c51f124SMoriah Waterland 	char	*pt;
136*5c51f124SMoriah Waterland 	int	n;
137*5c51f124SMoriah Waterland 
138*5c51f124SMoriah Waterland 	if (s == NULL) {
139*5c51f124SMoriah Waterland 		pathinit();
140*5c51f124SMoriah Waterland 		pt = NULL;
141*5c51f124SMoriah Waterland 	} else {
142*5c51f124SMoriah Waterland 		if (!initialized)
143*5c51f124SMoriah Waterland 			pathinit();
144*5c51f124SMoriah Waterland 
145*5c51f124SMoriah Waterland 		n = strlen(s) + 1;	/* string + null terminator */
146*5c51f124SMoriah Waterland 
147*5c51f124SMoriah Waterland 		if (size < n)
148*5c51f124SMoriah Waterland 			growstore();
149*5c51f124SMoriah Waterland 
150*5c51f124SMoriah Waterland 		pt = &tail->mem[MEMSIZ-size];
151*5c51f124SMoriah Waterland 		size -= n;
152*5c51f124SMoriah Waterland 
153*5c51f124SMoriah Waterland 		(void) strcpy(pt, s);
154*5c51f124SMoriah Waterland 	}
155*5c51f124SMoriah Waterland 
156*5c51f124SMoriah Waterland 	return (pt);
157*5c51f124SMoriah Waterland }