8354 sync regcomp(3C) with upstream (fix make catalog)
[unleashed/tickless.git] / usr / src / cmd / svr4pkg / libinst / pathdup.c
blob39c92ce2ce54111b6946af8bbec88f052362d36a
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
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>
42 #define ERR_MEMORY "memory allocation failure, errno=%d"
45 * using factor of eight limits maximum
46 * memory fragmentation to 12.5%
48 #define MEMSIZ PATH_MAX*8
50 struct dup {
51 char mem[MEMSIZ];
52 struct dup *next;
55 static struct dup *head, *tail, *new;
57 static int size, initialized;
58 static void pathinit();
59 static void growstore();
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.
67 /* Initialize storage area. */
68 static void
69 pathinit()
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;
81 tail = head;
82 size = MEMSIZ;
85 initialized = 1;
88 /* Allocate additional space for storage area. */
89 static void
90 growstore()
92 /* need more memory */
93 new = calloc(1, sizeof (struct dup));
94 if (new == NULL) {
95 progerr(gettext(ERR_MEMORY), errno);
96 quit(99);
98 if (head == NULL)
99 head = new;
100 else
101 tail->next = new;
102 tail = new;
103 size = MEMSIZ;
106 /* Allocate and return a pointer. If n == 0, initialize. */
107 char *
108 pathalloc(int n)
110 char *pt;
112 if (n <= 0) {
113 pathinit();
114 pt = NULL;
115 } else {
116 if (!initialized)
117 pathinit();
119 n++; /* Account for terminating null. */
121 if (size < n)
122 growstore();
124 pt = &tail->mem[MEMSIZ-size];
125 size -= n;
128 return (pt);
131 /* Allocate and insert a pathname returning a pointer to the new string. */
132 char *
133 pathdup(char *s)
135 char *pt;
136 int n;
138 if (s == NULL) {
139 pathinit();
140 pt = NULL;
141 } else {
142 if (!initialized)
143 pathinit();
145 n = strlen(s) + 1; /* string + null terminator */
147 if (size < n)
148 growstore();
150 pt = &tail->mem[MEMSIZ-size];
151 size -= n;
153 (void) strcpy(pt, s);
156 return (pt);