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