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