xref: /onnv-gate/usr/src/cmd/svr4pkg/libinst/pathdup.c (revision 9781:ccf49524d5dc)
1*9781SMoriah.Waterland@Sun.COM /*
2*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER START
3*9781SMoriah.Waterland@Sun.COM  *
4*9781SMoriah.Waterland@Sun.COM  * The contents of this file are subject to the terms of the
5*9781SMoriah.Waterland@Sun.COM  * Common Development and Distribution License (the "License").
6*9781SMoriah.Waterland@Sun.COM  * You may not use this file except in compliance with the License.
7*9781SMoriah.Waterland@Sun.COM  *
8*9781SMoriah.Waterland@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*9781SMoriah.Waterland@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*9781SMoriah.Waterland@Sun.COM  * See the License for the specific language governing permissions
11*9781SMoriah.Waterland@Sun.COM  * and limitations under the License.
12*9781SMoriah.Waterland@Sun.COM  *
13*9781SMoriah.Waterland@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*9781SMoriah.Waterland@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*9781SMoriah.Waterland@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*9781SMoriah.Waterland@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*9781SMoriah.Waterland@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*9781SMoriah.Waterland@Sun.COM  *
19*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER END
20*9781SMoriah.Waterland@Sun.COM  */
21*9781SMoriah.Waterland@Sun.COM 
22*9781SMoriah.Waterland@Sun.COM /*
23*9781SMoriah.Waterland@Sun.COM  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*9781SMoriah.Waterland@Sun.COM  * Use is subject to license terms.
25*9781SMoriah.Waterland@Sun.COM  */
26*9781SMoriah.Waterland@Sun.COM 
27*9781SMoriah.Waterland@Sun.COM /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*9781SMoriah.Waterland@Sun.COM /* All Rights Reserved */
29*9781SMoriah.Waterland@Sun.COM 
30*9781SMoriah.Waterland@Sun.COM 
31*9781SMoriah.Waterland@Sun.COM 
32*9781SMoriah.Waterland@Sun.COM #include <limits.h>
33*9781SMoriah.Waterland@Sun.COM #include <string.h>
34*9781SMoriah.Waterland@Sun.COM #include <stdlib.h>
35*9781SMoriah.Waterland@Sun.COM #include <unistd.h>
36*9781SMoriah.Waterland@Sun.COM #include <errno.h>
37*9781SMoriah.Waterland@Sun.COM #include <locale.h>
38*9781SMoriah.Waterland@Sun.COM #include <libintl.h>
39*9781SMoriah.Waterland@Sun.COM #include <pkglib.h>
40*9781SMoriah.Waterland@Sun.COM #include <libadm.h>
41*9781SMoriah.Waterland@Sun.COM 
42*9781SMoriah.Waterland@Sun.COM #define	ERR_MEMORY	"memory allocation failure, errno=%d"
43*9781SMoriah.Waterland@Sun.COM 
44*9781SMoriah.Waterland@Sun.COM /*
45*9781SMoriah.Waterland@Sun.COM  * using factor of eight limits maximum
46*9781SMoriah.Waterland@Sun.COM  * memory fragmentation to 12.5%
47*9781SMoriah.Waterland@Sun.COM  */
48*9781SMoriah.Waterland@Sun.COM #define	MEMSIZ	PATH_MAX*8
49*9781SMoriah.Waterland@Sun.COM #define	NULL	0
50*9781SMoriah.Waterland@Sun.COM 
51*9781SMoriah.Waterland@Sun.COM struct dup {
52*9781SMoriah.Waterland@Sun.COM 	char	mem[MEMSIZ];
53*9781SMoriah.Waterland@Sun.COM 	struct dup *next;
54*9781SMoriah.Waterland@Sun.COM };
55*9781SMoriah.Waterland@Sun.COM 
56*9781SMoriah.Waterland@Sun.COM static struct dup *head, *tail, *new;
57*9781SMoriah.Waterland@Sun.COM 
58*9781SMoriah.Waterland@Sun.COM static int	size, initialized;
59*9781SMoriah.Waterland@Sun.COM static void	pathinit();
60*9781SMoriah.Waterland@Sun.COM static void	growstore();
61*9781SMoriah.Waterland@Sun.COM 
62*9781SMoriah.Waterland@Sun.COM /*
63*9781SMoriah.Waterland@Sun.COM  * These functions allocate space for all the path names required
64*9781SMoriah.Waterland@Sun.COM  * in the packaging code. They are all allocated here so as to reduce
65*9781SMoriah.Waterland@Sun.COM  * memory fragmentation.
66*9781SMoriah.Waterland@Sun.COM  */
67*9781SMoriah.Waterland@Sun.COM 
68*9781SMoriah.Waterland@Sun.COM /* Initialize storage area. */
69*9781SMoriah.Waterland@Sun.COM static void
pathinit()70*9781SMoriah.Waterland@Sun.COM pathinit()
71*9781SMoriah.Waterland@Sun.COM {
72*9781SMoriah.Waterland@Sun.COM 	if (head == NULL)
73*9781SMoriah.Waterland@Sun.COM 		size = (-1);
74*9781SMoriah.Waterland@Sun.COM 	else {
75*9781SMoriah.Waterland@Sun.COM 		/* free all memory used except initial structure */
76*9781SMoriah.Waterland@Sun.COM 		tail = head->next;
77*9781SMoriah.Waterland@Sun.COM 		while (tail) {
78*9781SMoriah.Waterland@Sun.COM 			new = tail->next;
79*9781SMoriah.Waterland@Sun.COM 			free(tail);
80*9781SMoriah.Waterland@Sun.COM 			tail = new;
81*9781SMoriah.Waterland@Sun.COM 		}
82*9781SMoriah.Waterland@Sun.COM 		tail = head;
83*9781SMoriah.Waterland@Sun.COM 		size = MEMSIZ;
84*9781SMoriah.Waterland@Sun.COM 	}
85*9781SMoriah.Waterland@Sun.COM 
86*9781SMoriah.Waterland@Sun.COM 	initialized = 1;
87*9781SMoriah.Waterland@Sun.COM }
88*9781SMoriah.Waterland@Sun.COM 
89*9781SMoriah.Waterland@Sun.COM /* Allocate additional space for storage area. */
90*9781SMoriah.Waterland@Sun.COM static void
growstore()91*9781SMoriah.Waterland@Sun.COM growstore()
92*9781SMoriah.Waterland@Sun.COM {
93*9781SMoriah.Waterland@Sun.COM 	/* need more memory */
94*9781SMoriah.Waterland@Sun.COM 	new = calloc(1, sizeof (struct dup));
95*9781SMoriah.Waterland@Sun.COM 	if (new == NULL) {
96*9781SMoriah.Waterland@Sun.COM 		progerr(gettext(ERR_MEMORY), errno);
97*9781SMoriah.Waterland@Sun.COM 		quit(99);
98*9781SMoriah.Waterland@Sun.COM 	}
99*9781SMoriah.Waterland@Sun.COM 	if (head == NULL)
100*9781SMoriah.Waterland@Sun.COM 		head = new;
101*9781SMoriah.Waterland@Sun.COM 	else
102*9781SMoriah.Waterland@Sun.COM 		tail->next = new;
103*9781SMoriah.Waterland@Sun.COM 	tail = new;
104*9781SMoriah.Waterland@Sun.COM 	size = MEMSIZ;
105*9781SMoriah.Waterland@Sun.COM }
106*9781SMoriah.Waterland@Sun.COM 
107*9781SMoriah.Waterland@Sun.COM /* Allocate and return a pointer. If n == 0, initialize. */
108*9781SMoriah.Waterland@Sun.COM char *
pathalloc(int n)109*9781SMoriah.Waterland@Sun.COM pathalloc(int n)
110*9781SMoriah.Waterland@Sun.COM {
111*9781SMoriah.Waterland@Sun.COM 	char	*pt;
112*9781SMoriah.Waterland@Sun.COM 
113*9781SMoriah.Waterland@Sun.COM 	if (n <= 0) {
114*9781SMoriah.Waterland@Sun.COM 		pathinit();
115*9781SMoriah.Waterland@Sun.COM 		pt = NULL;
116*9781SMoriah.Waterland@Sun.COM 	} else {
117*9781SMoriah.Waterland@Sun.COM 		if (!initialized)
118*9781SMoriah.Waterland@Sun.COM 			pathinit();
119*9781SMoriah.Waterland@Sun.COM 
120*9781SMoriah.Waterland@Sun.COM 		n++;	/* Account for terminating null. */
121*9781SMoriah.Waterland@Sun.COM 
122*9781SMoriah.Waterland@Sun.COM 		if (size < n)
123*9781SMoriah.Waterland@Sun.COM 			growstore();
124*9781SMoriah.Waterland@Sun.COM 
125*9781SMoriah.Waterland@Sun.COM 		pt = &tail->mem[MEMSIZ-size];
126*9781SMoriah.Waterland@Sun.COM 		size -= n;
127*9781SMoriah.Waterland@Sun.COM 	}
128*9781SMoriah.Waterland@Sun.COM 
129*9781SMoriah.Waterland@Sun.COM 	return (pt);
130*9781SMoriah.Waterland@Sun.COM }
131*9781SMoriah.Waterland@Sun.COM 
132*9781SMoriah.Waterland@Sun.COM /* Allocate and insert a pathname returning a pointer to the new string. */
133*9781SMoriah.Waterland@Sun.COM char *
pathdup(char * s)134*9781SMoriah.Waterland@Sun.COM pathdup(char *s)
135*9781SMoriah.Waterland@Sun.COM {
136*9781SMoriah.Waterland@Sun.COM 	char	*pt;
137*9781SMoriah.Waterland@Sun.COM 	int	n;
138*9781SMoriah.Waterland@Sun.COM 
139*9781SMoriah.Waterland@Sun.COM 	if (s == NULL) {
140*9781SMoriah.Waterland@Sun.COM 		pathinit();
141*9781SMoriah.Waterland@Sun.COM 		pt = NULL;
142*9781SMoriah.Waterland@Sun.COM 	} else {
143*9781SMoriah.Waterland@Sun.COM 		if (!initialized)
144*9781SMoriah.Waterland@Sun.COM 			pathinit();
145*9781SMoriah.Waterland@Sun.COM 
146*9781SMoriah.Waterland@Sun.COM 		n = strlen(s) + 1;	/* string + null terminator */
147*9781SMoriah.Waterland@Sun.COM 
148*9781SMoriah.Waterland@Sun.COM 		if (size < n)
149*9781SMoriah.Waterland@Sun.COM 			growstore();
150*9781SMoriah.Waterland@Sun.COM 
151*9781SMoriah.Waterland@Sun.COM 		pt = &tail->mem[MEMSIZ-size];
152*9781SMoriah.Waterland@Sun.COM 		size -= n;
153*9781SMoriah.Waterland@Sun.COM 
154*9781SMoriah.Waterland@Sun.COM 		(void) strcpy(pt, s);
155*9781SMoriah.Waterland@Sun.COM 	}
156*9781SMoriah.Waterland@Sun.COM 
157*9781SMoriah.Waterland@Sun.COM 	return (pt);
158*9781SMoriah.Waterland@Sun.COM }
159