source: trunk/third/sysinfo/setenv.c @ 12269

Revision 12269, 2.3 KB checked in by ghudson, 26 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r12268, which included commits to RCS files with non-trunk default branches.
Line 
1/*
2 * Copyright (c) 1992-1998 Michael A. Cooper.
3 * This software may be freely used and distributed provided it is not
4 * sold for profit or used in part or in whole for commercial gain
5 * without prior written agreement, and the author is credited
6 * appropriately.
7 */
8
9#if defined(LIBC_SCCS) && !defined(lint)
10static char sccsid[] = "@(#)setenv.c    5.6 (Berkeley) 6/4/91";
11#endif /* LIBC_SCCS and not lint */
12
13#include <stddef.h>
14#include <stdlib.h>
15#include <string.h>
16
17char *_findenv();
18
19/*
20 * setenv --
21 *      Set the value of the environmental variable "name" to be
22 *      "value".  If rewrite is set, replace any current value.
23 */
24setenv(name, value, rewrite)
25        register const char *name;
26        register const char *value;
27        int rewrite;
28{
29        extern char **environ;
30        static int alloced;                     /* if allocated space before */
31        register char *C;
32        int l_value, offset;
33
34        if (*value == '=')                      /* no `=' in value */
35                ++value;
36        l_value = strlen(value);
37        if ((C = _findenv(name, &offset))) {    /* find if already exists */
38                if (!rewrite)
39                        return (0);
40                if (strlen(C) >= l_value) {     /* old larger; copy over */
41                        while (*C++ = *value++);
42                        return (0);
43                }
44        } else {                                        /* create new slot */
45                register int    cnt;
46                register char   **P;
47
48                for (P = environ, cnt = 0; *P; ++P, ++cnt);
49                if (alloced) {                  /* just increase size */
50                        environ = (char **)realloc((char *)environ,
51                            (size_t)(sizeof(char *) * (cnt + 2)));
52                        if (!environ)
53                                return (-1);
54                }
55                else {                          /* get new space */
56                        alloced = 1;            /* copy old entries into it */
57                        P = (char **)malloc((size_t)(sizeof(char *) *
58                            (cnt + 2)));
59                        if (!P)
60                                return (-1);
61                        memcpy(P, environ, cnt * sizeof(char *));
62                        environ = P;
63                }
64                environ[cnt + 1] = NULL;
65                offset = cnt;
66        }
67        for (C = (char *)name; *C && *C != '='; ++C);   /* no `=' in name */
68        if (!(environ[offset] =                 /* name + `=' + value */
69            malloc((size_t)((int)(C - name) + l_value + 2))))
70                return (-1);
71        for (C = environ[offset]; (*C = *name++) && *C != '='; ++C)
72                ;
73        for (*C++ = '='; *C++ = *value++; )
74                ;
75        return (0);
76}
77
78/*
79 * unsetenv(name) --
80 *      Delete environmental variable "name".
81 */
82void
83unsetenv(name)
84        const char      *name;
85{
86        extern char **environ;
87        register char **P;
88        int offset;
89
90        while (_findenv(name, &offset))         /* if set multiple times */
91                for (P = &environ[offset];; ++P)
92                        if (!(*P = *(P + 1)))
93                                break;
94}
Note: See TracBrowser for help on using the repository browser.