blob: b52a99c2ccd2356bbde868a6bdf5b6708924a89c [file] [log] [blame]
Darren Tucker063ba742005-11-10 10:38:45 +11001/* $OpenBSD: setenv.c,v 1.9 2005/08/08 08:05:37 espie Exp $ */
Damien Miller2c9279f2000-03-26 12:12:34 +10002/*
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.
Damien Miller329638e2003-06-03 12:12:50 +100014 * 3. Neither the name of the University nor the names of its contributors
Damien Miller2c9279f2000-03-26 12:12:34 +100015 * 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
Darren Tucker7f24a0e2005-11-10 16:18:56 +110031/* OPENBSD ORIGINAL: lib/libc/stdlib/setenv.c */
32
Ben Lindstromdd21fe92002-06-27 18:23:20 +000033#include "includes.h"
Darren Tucker86c093d2004-03-08 22:59:03 +110034#if !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV)
Damien Miller2c9279f2000-03-26 12:12:34 +100035
Damien Miller2c9279f2000-03-26 12:12:34 +100036#include <stdlib.h>
37#include <string.h>
38
Darren Tucker063ba742005-11-10 10:38:45 +110039extern char **environ;
Darren Tuckerb8c89d12005-11-10 10:10:10 +110040
Darren Tucker063ba742005-11-10 10:38:45 +110041/* OpenSSH Portable: __findenv is from getenv.c rev 1.8, made static */
Damien Miller2c9279f2000-03-26 12:12:34 +100042/*
43 * __findenv --
44 * Returns pointer to value associated with name, if any, else NULL.
45 * Sets offset to be the offset of the name/value combination in the
46 * environmental array, for use by setenv(3) and unsetenv(3).
47 * Explicitly removes '=' in argument name.
Damien Miller2c9279f2000-03-26 12:12:34 +100048 */
Darren Tucker32b53102005-11-10 10:13:06 +110049static char *
Darren Tuckerb8c89d12005-11-10 10:10:10 +110050__findenv(const char *name, int *offset)
Damien Miller2c9279f2000-03-26 12:12:34 +100051{
52 extern char **environ;
Darren Tuckerb8c89d12005-11-10 10:10:10 +110053 int len, i;
54 const char *np;
55 char **p, *cp;
Damien Miller2c9279f2000-03-26 12:12:34 +100056
57 if (name == NULL || environ == NULL)
58 return (NULL);
59 for (np = name; *np && *np != '='; ++np)
60 ;
61 len = np - name;
62 for (p = environ; (cp = *p) != NULL; ++p) {
63 for (np = name, i = len; i && *cp; i--)
64 if (*cp++ != *np++)
65 break;
66 if (i == 0 && *cp++ == '=') {
67 *offset = p - environ;
68 return (cp);
69 }
70 }
71 return (NULL);
72}
73
Darren Tucker86c093d2004-03-08 22:59:03 +110074#ifndef HAVE_SETENV
Damien Miller2c9279f2000-03-26 12:12:34 +100075/*
76 * setenv --
77 * Set the value of the environmental variable "name" to be
78 * "value". If rewrite is set, replace any current value.
79 */
80int
Darren Tucker063ba742005-11-10 10:38:45 +110081setenv(const char *name, const char *value, int rewrite)
Damien Miller2c9279f2000-03-26 12:12:34 +100082{
Darren Tucker063ba742005-11-10 10:38:45 +110083 static char **lastenv; /* last value of environ */
84 char *C;
Damien Miller2c9279f2000-03-26 12:12:34 +100085 int l_value, offset;
Damien Miller2c9279f2000-03-26 12:12:34 +100086
87 if (*value == '=') /* no `=' in value */
88 ++value;
89 l_value = strlen(value);
90 if ((C = __findenv(name, &offset))) { /* find if already exists */
91 if (!rewrite)
92 return (0);
93 if (strlen(C) >= l_value) { /* old larger; copy over */
Damien Miller8e3bdca2002-02-13 16:00:15 +110094 while ((*C++ = *value++))
95 ;
Damien Miller2c9279f2000-03-26 12:12:34 +100096 return (0);
97 }
98 } else { /* create new slot */
Darren Tucker063ba742005-11-10 10:38:45 +110099 size_t cnt;
100 char **P;
Damien Miller2c9279f2000-03-26 12:12:34 +1000101
Darren Tucker063ba742005-11-10 10:38:45 +1100102 for (P = environ; *P != NULL; P++)
103 ;
104 cnt = P - environ;
105 P = (char **)realloc(lastenv, sizeof(char *) * (cnt + 2));
106 if (!P)
107 return (-1);
108 if (lastenv != environ)
109 memcpy(P, environ, cnt * sizeof(char *));
110 lastenv = environ = P;
Damien Miller2c9279f2000-03-26 12:12:34 +1000111 offset = cnt;
Darren Tucker063ba742005-11-10 10:38:45 +1100112 environ[cnt + 1] = NULL;
Damien Miller2c9279f2000-03-26 12:12:34 +1000113 }
Darren Tucker063ba742005-11-10 10:38:45 +1100114 for (C = (char *)name; *C && *C != '='; ++C)
115 ; /* no `=' in name */
Damien Miller2c9279f2000-03-26 12:12:34 +1000116 if (!(environ[offset] = /* name + `=' + value */
117 malloc((size_t)((int)(C - name) + l_value + 2))))
118 return (-1);
119 for (C = environ[offset]; (*C = *name++) && *C != '='; ++C)
120 ;
121 for (*C++ = '='; (*C++ = *value++); )
122 ;
123 return (0);
124}
Darren Tucker86c093d2004-03-08 22:59:03 +1100125#endif /* HAVE_SETENV */
Damien Miller2c9279f2000-03-26 12:12:34 +1000126
Darren Tucker86c093d2004-03-08 22:59:03 +1100127#ifndef HAVE_UNSETENV
Damien Miller2c9279f2000-03-26 12:12:34 +1000128/*
129 * unsetenv(name) --
130 * Delete environmental variable "name".
131 */
132void
Darren Tucker063ba742005-11-10 10:38:45 +1100133unsetenv(const char *name)
Damien Miller2c9279f2000-03-26 12:12:34 +1000134{
Darren Tucker063ba742005-11-10 10:38:45 +1100135 char **P;
Damien Miller2c9279f2000-03-26 12:12:34 +1000136 int offset;
Damien Miller2c9279f2000-03-26 12:12:34 +1000137
Darren Tucker063ba742005-11-10 10:38:45 +1100138 while (__findenv(name, &offset)) /* if set multiple times */
Damien Miller2c9279f2000-03-26 12:12:34 +1000139 for (P = &environ[offset];; ++P)
140 if (!(*P = *(P + 1)))
141 break;
142}
Darren Tucker86c093d2004-03-08 22:59:03 +1100143#endif /* HAVE_UNSETENV */
Damien Miller2c9279f2000-03-26 12:12:34 +1000144
Darren Tucker86c093d2004-03-08 22:59:03 +1100145#endif /* !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV) */