earlybrowserreborn - Blame information for rev 3

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>
3 roytam 26 //extern char *malloc();
27 extern char *_findenv(register char *name,int *offset);
1 roytam 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         }
53         else {                                  /* create new slot */
54                 register int    cnt;
55                 register char   **P;
56  
57                 for (P = environ, cnt = 0; *P; ++P, ++cnt);
58                 if (alloced) {                  /* just increase size */
59                         environ = (char **)realloc((char *)environ,
60                             (size_t)(sizeof(char *) * (cnt + 2)));
61                         if (!environ)
62                                 return(-1);
63                 }
64                 else {                          /* get new space */
65                         alloced = 1;            /* copy old entries into it */
66                         P = (char **)malloc((size_t)(sizeof(char *) *
67                             (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((size_t)((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 }