new beta-0.90.0
[luatex.git] / source / texk / kpathsea / putenv.c
blob34cbd3add896f0de2d88900afbfa7d71d386bab9
1 /****************************************************************/
2 /* */
3 /* putenv(3) */
4 /* */
5 /* Change or add an environment entry */
6 /* */
7 /****************************************************************/
8 /* origination 1987-Oct-7 T. Holm */
9 /****************************************************************/
12 Path: hoptoad!pacbell!ames!ll-xn!mit-eddie!uw-beaver!ssc-vax!uvicctr!tholm
13 From: tholm@uvicctr.UUCP (Terrence W. Holm)
14 Newsgroups: comp.os.minix
15 Subject: putenv(3)
16 Message-ID: <395@uvicctr.UUCP>
17 Date: 5 May 88 06:40:52 GMT
18 Organization: University of Victoria, Victoria B.C. Canada
20 EFTH Minix report #2 - May 1988 - putenv(3)
22 This is an implementation of putenv(3) that we
23 wrote for Minix. Please consider this a public
24 domain program.
27 #define NULL 0
28 #define PSIZE sizeof(char *)
30 extern char **environ;
32 char *strchr();
33 char *malloc();
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 /****************************************************************/
64 int
65 putenv( entry )
66 char *entry;
68 unsigned length;
69 unsigned size;
70 char *temp;
71 char **p;
72 char **new_environ;
74 /* Find the length of the "NAME=" */
76 temp = strchr(entry,'=');
77 if ( temp == 0 )
78 return( -1 );
80 length = (unsigned) (temp - entry + 1);
83 /* Scan through the environment looking for "NAME=" */
85 for ( p=environ; *p != 0 ; p++ )
86 if ( strncmp( entry, *p, length ) == 0 )
88 *p = entry;
89 return( 0 );
93 /* The name was not found, build a bigger environment */
95 size = p - environ;
97 new_environ = (char **) malloc( (size+2)*PSIZE );
99 if ( new_environ == (char **) NULL )
100 return( -1 );
102 memcpy ((char *) new_environ, (char *) environ, size*PSIZE );
104 new_environ[size] = entry;
105 new_environ[size+1] = NULL;
107 environ = new_environ;
109 return(0);