convert-from-bsd-to-posix-string-and-memory-functions-20010807
[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("$Header$");
22
23 #include <sys/types.h>
24 #include <stdio.h>
25
26 char *_findenv();
27
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         char *malloc(), *realloc();
42
43         if (*value == '=')                      /* no `=' in value */
44                 ++value;
45         l_value = strlen(value);
46         if ((C = _findenv(name, &offset))) {    /* find if already exists */
47                 if (!rewrite)
48                         return(0);
49                 if (strlen(C) >= l_value) {     /* old larger; copy over */
50                         while (*C++ = *value++);
51                         return(0);
52                 }
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 = (char **)realloc((char *)environ,
61                             (u_int)(sizeof(char *) * (cnt + 2)));
62                         if (!environ)
63                                 return(-1);
64                 }
65                 else {                          /* get new space */
66                         alloced = 1;            /* copy old entries into it */
67                         P = (char **)malloc((u_int)(sizeof(char *) *
68                             (cnt + 2)));
69                         if (!P)
70                                 return(-1);
71                         memcpy(P, environ, cnt * sizeof(char *));
72                         environ = P;
73                 }
74                 environ[cnt + 1] = NULL;
75                 offset = cnt;
76         }
77         for (C = name; *C && *C != '='; ++C);   /* no `=' in name */
78         if (!(environ[offset] =                 /* name + `=' + value */
79             malloc((u_int)((int)(C - name) + l_value + 2))))
80                 return(-1);
81         for (C = environ[offset]; (*C = *name++) && *C != '='; ++C);
82         for (*C++ = '='; *C++ = *value++;);
83         return(0);
84 }
85
86 /*
87  * unsetenv(name) --
88  *      Delete environmental variable "name".
89  */
90 void
91 unsetenv(name)
92         char    *name;
93 {
94         extern  char    **environ;
95         register char   **P;
96         int     offset;
97
98         while (_findenv(name, &offset))         /* if set multiple times */
99                 for (P = &environ[offset];; ++P)
100                         if (!(*P = *(P + 1)))
101                                 break;
102 }