| 1 |
/****************************************************************/ |
| 2 |
/* */ |
| 3 |
/* putenv(3) */ |
| 4 |
/* */ |
| 5 |
/* Change or add an environment entry */ |
| 6 |
/* */ |
| 7 |
/****************************************************************/ |
| 8 |
/* origination 1987-Oct-7 T. Holm */ |
| 9 |
/****************************************************************/ |
| 10 |
|
| 11 |
/* |
| 12 |
Path: hoptoad!pacbell!ames!ll-xn!mit-eddie!uw-beaver!ssc-vax!uvicctr!tholm |
| 13 |
From: [email protected] (Terrence W. Holm) |
| 14 |
Newsgroups: comp.os.minix |
| 15 |
Subject: putenv(3) |
| 16 |
Message-ID: <[email protected]> |
| 17 |
Date: 5 May 88 06:40:52 GMT |
| 18 |
Organization: University of Victoria, Victoria B.C. Canada |
| 19 |
|
| 20 |
EFTH Minix report #2 - May 1988 - putenv(3) |
| 21 |
|
| 22 |
This is an implementation of putenv(3) that we |
| 23 |
wrote for Minix. Please consider this a public |
| 24 |
domain program. |
| 25 |
*/ |
| 26 |
|
| 27 |
#define NULL 0 |
| 28 |
#define PSIZE sizeof(char *) |
| 29 |
|
| 30 |
extern char **environ; |
| 31 |
|
| 32 |
char *strchr(); |
| 33 |
char *malloc(); |
| 34 |
|
| 35 |
/****************************************************************/ |
| 36 |
/* */ |
| 37 |
/* int */ |
| 38 |
/* putenv( entry ) */ |
| 39 |
/* */ |
| 40 |
/* The "entry" should follow the form */ |
| 41 |
/* "NAME=VALUE". This routine will search the */ |
| 42 |
/* user environment for "NAME" and replace its */ |
| 43 |
/* value with "VALUE". */ |
| 44 |
/* */ |
| 45 |
/* Note that "entry" is not copied, it is used */ |
| 46 |
/* as the environment entry. This means that it */ |
| 47 |
/* must not be unallocated or otherwise modifed */ |
| 48 |
/* by the caller, unless it is replaced by a */ |
| 49 |
/* subsequent putenv(). */ |
| 50 |
/* */ |
| 51 |
/* If the name is not found in the environment, */ |
| 52 |
/* then a new vector of pointers is allocated, */ |
| 53 |
/* "entry" is put at the end and the global */ |
| 54 |
/* variable "environ" is updated. */ |
| 55 |
/* */ |
| 56 |
/* This function normally returns 0, but -1 */ |
| 57 |
/* is returned if it can not allocate enough */ |
| 58 |
/* space using malloc(3), or "entry" does not */ |
| 59 |
/* contain a '='. */ |
| 60 |
/* */ |
| 61 |
/****************************************************************/ |
| 62 |
|
| 63 |
|
| 64 |
int |
| 65 |
putenv( entry ) |
| 66 |
char *entry; |
| 67 |
{ |
| 68 |
unsigned length; |
| 69 |
unsigned size; |
| 70 |
char *temp; |
| 71 |
char **p; |
| 72 |
char **new_environ; |
| 73 |
|
| 74 |
/* Find the length of the "NAME=" */ |
| 75 |
|
| 76 |
temp = strchr(entry,'='); |
| 77 |
if ( temp == 0 ) |
| 78 |
return( -1 ); |
| 79 |
|
| 80 |
length = (unsigned) (temp - entry + 1); |
| 81 |
|
| 82 |
|
| 83 |
/* Scan through the environment looking for "NAME=" */ |
| 84 |
|
| 85 |
for ( p=environ; *p != 0 ; p++ ) |
| 86 |
if ( strncmp( entry, *p, length ) == 0 ) |
| 87 |
{ |
| 88 |
*p = entry; |
| 89 |
return( 0 ); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
/* The name was not found, build a bigger environment */ |
| 94 |
|
| 95 |
size = p - environ; |
| 96 |
|
| 97 |
new_environ = (char **) malloc( (size+2)*PSIZE ); |
| 98 |
|
| 99 |
if ( new_environ == (char **) NULL ) |
| 100 |
return( -1 ); |
| 101 |
|
| 102 |
memcpy ((char *) new_environ, (char *) environ, size*PSIZE ); |
| 103 |
|
| 104 |
new_environ[size] = entry; |
| 105 |
new_environ[size+1] = NULL; |
| 106 |
|
| 107 |
environ = new_environ; |
| 108 |
|
| 109 |
return(0); |
| 110 |
} |