Home | History | Annotate | Download | only in stdlib
      1 /*	$OpenBSD: setenv.c,v 1.9 2005/08/08 08:05:37 espie Exp $ */
      2 /*
      3  * Copyright (c) 1987 Regents of the University of California.
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. Neither the name of the University nor the names of its contributors
     15  *    may be used to endorse or promote products derived from this software
     16  *    without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include <stdlib.h>
     32 #include <string.h>
     33 
     34 char *__findenv(const char *name, int *offset);
     35 
     36 extern char **environ;
     37 
     38 /*
     39  * setenv --
     40  *	Set the value of the environmental variable "name" to be
     41  *	"value".  If rewrite is set, replace any current value.
     42  */
     43 int
     44 setenv(const char *name, const char *value, int rewrite)
     45 {
     46 	static char **lastenv;			/* last value of environ */
     47 	char *C;
     48 	int l_value, offset;
     49 
     50 	if (*value == '=')			/* no `=' in value */
     51 		++value;
     52 	l_value = strlen(value);
     53 	if ((C = __findenv(name, &offset))) {	/* find if already exists */
     54 		if (!rewrite)
     55 			return (0);
     56 		if ((int)strlen(C) >= l_value) {	/* old larger; copy over */
     57 			while ((*C++ = *value++))
     58 				;
     59 			return (0);
     60 		}
     61 	} else {					/* create new slot */
     62 		size_t cnt;
     63 		char **P;
     64 
     65 		for (P = environ; *P != NULL; P++)
     66 			;
     67 		cnt = P - environ;
     68         P = (char **)realloc(lastenv, sizeof(char *) * (cnt + 2));
     69 		if (!P)
     70 			return (-1);
     71 		if (lastenv != environ)
     72 			memcpy(P, environ, cnt * sizeof(char *));
     73 		lastenv = environ = P;
     74 		offset = cnt;
     75 		environ[cnt + 1] = NULL;
     76 	}
     77 	for (C = (char *)name; *C && *C != '='; ++C)
     78 		;				/* no `=' in name */
     79 	if (!(environ[offset] =			/* name + `=' + value */
     80 	    malloc((size_t)((int)(C - name) + l_value + 2))))
     81 		return (-1);
     82 	for (C = environ[offset]; (*C = *name++) && *C != '='; ++C)
     83 		;
     84 	for (*C++ = '='; (*C++ = *value++); )
     85 		;
     86 	return (0);
     87 }
     88 
     89 /*
     90  * unsetenv(name) --
     91  *	Delete environmental variable "name".
     92  */
     93 int
     94 unsetenv(const char *name)
     95 {
     96 	char **P;
     97 	int offset;
     98 
     99 	while (__findenv(name, &offset))	/* if set multiple times */
    100 		for (P = &environ[offset];; ++P)
    101 			if (!(*P = *(P + 1)))
    102 				break;
    103 
    104         return 0;
    105 }
    106