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