earlybrowserreborn - Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 roytam 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: (1) source distributions retain this entire copyright
7  * notice and comment, and (2) distributions including binaries display
8  * the following acknowledgement:  ``This product includes software
9  * developed by the University of California, Berkeley and its contributors''
10  * in the documentation or other materials provided with the distribution
11  * and in all advertising materials mentioning features or use of this
12  * software. Neither the name of the University nor the names of its
13  * contributors may be used to endorse or promote products derived
14  * from this software without specific prior written permission.
15  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18  */
19  
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static char sccsid[] = "@(#)setenv.c    5.4 (Berkeley) 6/1/90";
22 #endif /* LIBC_SCCS and not lint */
23  
24 #include <stdio.h>
25 #include <sys/types.h>
26 extern char *malloc();
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 *_findenv();
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                             (size_t)(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((size_t)(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((size_t)((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 }