remove-rx-2tier-freepacketq-20050403
[openafs.git] / src / inetd / setenv.c
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17
18 #include <afsconfig.h>
19 #include <afs/param.h>
20
21 RCSID
22     ("$Header$");
23
24 #include <sys/types.h>
25 #include <stdio.h>
26
27 extern char *malloc(), *realloc(), *_findenv();
28 /*
29  * setenv --
30  *      Set the value of the environmental variable "name" to be
31  *      "value".  If rewrite is set, replace any current value.
32  */
33 setenv(name, value, rewrite)
34      register char *name, *value;
35      int rewrite;
36 {
37     extern char **environ;
38     static int alloced;         /* if allocated space before */
39     register char *C;
40     int l_value, offset;
41
42     if (*value == '=')          /* no `=' in value */
43         ++value;
44     l_value = strlen(value);
45     if ((C = _findenv(name, &offset))) {        /* find if already exists */
46         if (!rewrite)
47             return (0);
48         if (strlen(C) >= l_value) {     /* old larger; copy over */
49             while (*C++ = *value++);
50             return (0);
51         }
52     } else {                    /* create new slot */
53         register int cnt;
54         register char **P;
55
56         for (P = environ, cnt = 0; *P; ++P, ++cnt);
57         if (alloced) {          /* just increase size */
58             environ =
59                 (char **)realloc((char *)environ,
60                                  (u_int) (sizeof(char *) * (cnt + 2)));
61             if (!environ)
62                 return (-1);
63         } else {                /* get new space */
64             alloced = 1;        /* copy old entries into it */
65             P = (char **)malloc((u_int) (sizeof(char *) * (cnt + 2)));
66             if (!P)
67                 return (-1);
68             memcpy(P, environ, cnt * sizeof(char *));
69             environ = P;
70         }
71         environ[cnt + 1] = NULL;
72         offset = cnt;
73     }
74     for (C = name; *C && *C != '='; ++C);       /* no `=' in name */
75     if (!(environ[offset] =     /* name + `=' + value */
76           malloc((u_int) ((int)(C - name) + l_value + 2))))
77         return (-1);
78     for (C = environ[offset]; (*C = *name++) && *C != '='; ++C);
79     for (*C++ = '='; *C++ = *value++;);
80     return (0);
81 }
82
83 /*
84  * unsetenv(name) --
85  *      Delete environmental variable "name".
86  */
87 void
88 unsetenv(name)
89      char *name;
90 {
91     extern char **environ;
92     register char **P;
93     int offset;
94
95     while (_findenv(name, &offset))     /* if set multiple times */
96         for (P = &environ[offset];; ++P)
97             if (!(*P = *(P + 1)))
98                 break;
99 }