10Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint) 2*11038SRao.Shoaib@Sun.COM static const char sccsid[] = "@(#)setenv.c 8.1 (Berkeley) 6/4/93"; 3*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: setenv.c,v 1.2 2005/04/27 04:56:11 sra Exp $"; 40Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */ 50Sstevel@tonic-gate 60Sstevel@tonic-gate /* 70Sstevel@tonic-gate * Copyright (c) 1987, 1993 80Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 90Sstevel@tonic-gate * 100Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 110Sstevel@tonic-gate * modification, are permitted provided that the following conditions 120Sstevel@tonic-gate * are met: 130Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 140Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 150Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 160Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 170Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 180Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 190Sstevel@tonic-gate * must display the following acknowledgement: 200Sstevel@tonic-gate * This product includes software developed by the University of 210Sstevel@tonic-gate * California, Berkeley and its contributors. 220Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors 230Sstevel@tonic-gate * may be used to endorse or promote products derived from this software 240Sstevel@tonic-gate * without specific prior written permission. 250Sstevel@tonic-gate * 260Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 270Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 280Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 290Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 300Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 310Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 320Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 330Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 340Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 350Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 360Sstevel@tonic-gate * SUCH DAMAGE. 370Sstevel@tonic-gate */ 380Sstevel@tonic-gate 390Sstevel@tonic-gate #include "port_before.h" 400Sstevel@tonic-gate 410Sstevel@tonic-gate #include <stddef.h> 420Sstevel@tonic-gate #include <stdlib.h> 430Sstevel@tonic-gate #include <string.h> 440Sstevel@tonic-gate 450Sstevel@tonic-gate #include "port_after.h" 460Sstevel@tonic-gate 470Sstevel@tonic-gate #if !defined(NEED_SETENV) 480Sstevel@tonic-gate int __bindcompat_setenv; 490Sstevel@tonic-gate #else 500Sstevel@tonic-gate 510Sstevel@tonic-gate extern char **environ; 520Sstevel@tonic-gate 530Sstevel@tonic-gate static char *findenv(const char *name, int *offset); 540Sstevel@tonic-gate 55*11038SRao.Shoaib@Sun.COM /*% 560Sstevel@tonic-gate * setenv -- 570Sstevel@tonic-gate * Set the value of the environmental variable "name" to be 580Sstevel@tonic-gate * "value". If rewrite is set, replace any current value. 590Sstevel@tonic-gate */ 600Sstevel@tonic-gate setenv(const char *name, const char *value, int rewrite) { 610Sstevel@tonic-gate extern char **environ; 62*11038SRao.Shoaib@Sun.COM static int alloced; /*%< if allocated space before */ 630Sstevel@tonic-gate char *c; 640Sstevel@tonic-gate int l_value, offset; 650Sstevel@tonic-gate 66*11038SRao.Shoaib@Sun.COM if (*value == '=') /*%< no `=' in value */ 670Sstevel@tonic-gate ++value; 680Sstevel@tonic-gate l_value = strlen(value); 69*11038SRao.Shoaib@Sun.COM if ((c = findenv(name, &offset))) { /*%< find if already exists */ 700Sstevel@tonic-gate if (!rewrite) 710Sstevel@tonic-gate return (0); 72*11038SRao.Shoaib@Sun.COM if (strlen(c) >= l_value) { /*%< old larger; copy over */ 730Sstevel@tonic-gate while (*c++ = *value++); 740Sstevel@tonic-gate return (0); 750Sstevel@tonic-gate } 76*11038SRao.Shoaib@Sun.COM } else { /*%< create new slot */ 770Sstevel@tonic-gate int cnt; 780Sstevel@tonic-gate char **p; 790Sstevel@tonic-gate 800Sstevel@tonic-gate for (p = environ, cnt = 0; *p; ++p, ++cnt); 81*11038SRao.Shoaib@Sun.COM if (alloced) { /*%< just increase size */ 820Sstevel@tonic-gate environ = (char **)realloc((char *)environ, 830Sstevel@tonic-gate (size_t)(sizeof(char *) * (cnt + 2))); 840Sstevel@tonic-gate if (!environ) 850Sstevel@tonic-gate return (-1); 860Sstevel@tonic-gate } 87*11038SRao.Shoaib@Sun.COM else { /*%< get new space */ 88*11038SRao.Shoaib@Sun.COM alloced = 1; /*%< copy old entries into it */ 890Sstevel@tonic-gate p = malloc((size_t)(sizeof(char *) * (cnt + 2))); 900Sstevel@tonic-gate if (!p) 910Sstevel@tonic-gate return (-1); 920Sstevel@tonic-gate memcpy(p, environ, cnt * sizeof(char *)); 930Sstevel@tonic-gate environ = p; 940Sstevel@tonic-gate } 950Sstevel@tonic-gate environ[cnt + 1] = NULL; 960Sstevel@tonic-gate offset = cnt; 970Sstevel@tonic-gate } 98*11038SRao.Shoaib@Sun.COM for (c = (char *)name; *c && *c != '='; ++c); /*%< no `=' in name */ 99*11038SRao.Shoaib@Sun.COM if (!(environ[offset] = /*%< name + `=' + value */ 1000Sstevel@tonic-gate malloc((size_t)((int)(c - name) + l_value + 2)))) 1010Sstevel@tonic-gate return (-1); 1020Sstevel@tonic-gate for (c = environ[offset]; (*c = *name++) && *c != '='; ++c); 1030Sstevel@tonic-gate for (*c++ = '='; *c++ = *value++;); 1040Sstevel@tonic-gate return (0); 1050Sstevel@tonic-gate } 1060Sstevel@tonic-gate 107*11038SRao.Shoaib@Sun.COM /*% 1080Sstevel@tonic-gate * unsetenv(name) -- 1090Sstevel@tonic-gate * Delete environmental variable "name". 1100Sstevel@tonic-gate */ 1110Sstevel@tonic-gate void 1120Sstevel@tonic-gate unsetenv(const char *name) { 1130Sstevel@tonic-gate char **p; 1140Sstevel@tonic-gate int offset; 1150Sstevel@tonic-gate 116*11038SRao.Shoaib@Sun.COM while (findenv(name, &offset)) /*%< if set multiple times */ 1170Sstevel@tonic-gate for (p = &environ[offset];; ++p) 1180Sstevel@tonic-gate if (!(*p = *(p + 1))) 1190Sstevel@tonic-gate break; 1200Sstevel@tonic-gate } 1210Sstevel@tonic-gate 122*11038SRao.Shoaib@Sun.COM /*% 1230Sstevel@tonic-gate * findenv -- 1240Sstevel@tonic-gate * Returns pointer to value associated with name, if any, else NULL. 1250Sstevel@tonic-gate * Sets offset to be the offset of the name/value combination in the 1260Sstevel@tonic-gate * environmental array, for use by setenv(3) and unsetenv(3). 1270Sstevel@tonic-gate * Explicitly removes '=' in argument name. 1280Sstevel@tonic-gate * 1290Sstevel@tonic-gate * This routine *should* be a static; don't use it. 1300Sstevel@tonic-gate */ 1310Sstevel@tonic-gate static char * 1320Sstevel@tonic-gate findenv(const char *name, int *offset) { 1330Sstevel@tonic-gate const char *np; 1340Sstevel@tonic-gate char **p, *c; 1350Sstevel@tonic-gate int len; 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate if (name == NULL || environ == NULL) 1380Sstevel@tonic-gate return (NULL); 1390Sstevel@tonic-gate for (np = name; *np && *np != '='; ++np) 1400Sstevel@tonic-gate continue; 1410Sstevel@tonic-gate len = np - name; 1420Sstevel@tonic-gate for (p = environ; (c = *p) != NULL; ++p) 1430Sstevel@tonic-gate if (strncmp(c, name, len) == 0 && c[len] == '=') { 1440Sstevel@tonic-gate *offset = p - environ; 1450Sstevel@tonic-gate return (c + len + 1); 1460Sstevel@tonic-gate } 1470Sstevel@tonic-gate return (NULL); 1480Sstevel@tonic-gate } 1490Sstevel@tonic-gate #endif 150*11038SRao.Shoaib@Sun.COM 151*11038SRao.Shoaib@Sun.COM /*! \file */ 152