9b9831c43e50adf492a5a1c944e51e16a5dd0631
[openafs.git] / src / login / 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 char *_findenv();
28
29 /*
30  * setenv --
31  *      Set the value of the environmental variable "name" to be
32  *      "value".  If rewrite is set, replace any current value.
33  */
34 setenv(name, value, rewrite)
35      register char *name, *value;
36      int rewrite;
37 {
38     extern char **environ;
39     static int alloced;         /* if allocated space before */
40     register char *C;
41     int l_value, offset;
42     char *malloc(), *realloc();
43
44     if (*value == '=')          /* no `=' in value */
45         ++value;
46     l_value = strlen(value);
47     if ((C = _findenv(name, &offset))) {        /* find if already exists */
48         if (!rewrite)
49             return (0);
50         if (strlen(C) >= l_value) {     /* old larger; copy over */
51             while (*C++ = *value++);
52             return (0);
53         }
54     } else {                    /* create new slot */
55         register int cnt;
56         register char **P;
57
58         for (P = environ, cnt = 0; *P; ++P, ++cnt);
59         if (alloced) {          /* just increase size */
60             environ =
61                 (char **)realloc((char *)environ,
62                                  (u_int) (sizeof(char *) * (cnt + 2)));
63             if (!environ)
64                 return (-1);
65         } else {                /* get new space */
66             alloced = 1;        /* copy old entries into it */
67             P = (char **)malloc((u_int) (sizeof(char *) * (cnt + 2)));
68             if (!P)
69                 return (-1);
70             memcpy(P, environ, cnt * sizeof(char *));
71             environ = P;
72         }
73         environ[cnt + 1] = NULL;
74         offset = cnt;
75     }
76     for (C = name; *C && *C != '='; ++C);       /* no `=' in name */
77     if (!(environ[offset] =     /* name + `=' + value */
78           malloc((u_int) ((int)(C - name) + l_value + 2))))
79         return (-1);
80     for (C = environ[offset]; (*C = *name++) && *C != '='; ++C);
81     for (*C++ = '='; *C++ = *value++;);
82     return (0);
83 }
84
85 /*
86  * unsetenv(name) --
87  *      Delete environmental variable "name".
88  */
89 void
90 unsetenv(name)
91      char *name;
92 {
93     extern char **environ;
94     register char **P;
95     int offset;
96
97     while (_findenv(name, &offset))     /* if set multiple times */
98         for (P = &environ[offset];; ++P)
99             if (!(*P = *(P + 1)))
100                 break;
101 }