]> Git Repo - binutils.git/blob - gdb/putenv.c
* TODO: Add suggestions about structure passing tests.
[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 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 #include <stdio.h>
28 #include <string.h>
29
30 #define  PSIZE  sizeof(char *)
31
32 extern  char  **environ;
33
34 char  *strchr();
35 char  *malloc();
36
37 /****************************************************************/
38 /*                                                              */
39 /*      int                                                     */
40 /*      putenv( entry )                                         */
41 /*                                                              */
42 /*              The "entry" should follow the form              */
43 /*              "NAME=VALUE". This routine will search the      */
44 /*              user environment for "NAME" and replace its     */
45 /*              value with "VALUE".                             */
46 /*                                                              */
47 /*              Note that "entry" is not copied, it is used     */
48 /*              as the environment entry. This means that it    */
49 /*              must not be unallocated or otherwise modifed    */
50 /*              by the caller, unless it is replaced by a       */
51 /*              subsequent putenv().                            */
52 /*                                                              */
53 /*              If the name is not found in the environment,    */
54 /*              then a new vector of pointers is allocated,     */
55 /*              "entry" is put at the end and the global        */
56 /*              variable "environ" is updated.                  */
57 /*                                                              */
58 /*              This function normally returns NULL, but -1     */
59 /*              is returned if it can not allocate enough       */
60 /*              space using malloc(3), or "entry" does not      */
61 /*              contain a '='.                                  */
62 /*                                                              */
63 /****************************************************************/
64
65
66 int
67 putenv( entry )
68   char *entry;
69 {
70   unsigned length;
71   unsigned size;
72   char     *temp;
73   char     **p;
74   char     **new_environ;
75
76   /*  Find the length of the "NAME="  */
77
78   temp = strchr(entry,'=');
79   if ( temp == 0 )
80     return( -1 );
81
82   length = (unsigned) (temp - entry + 1);
83
84
85   /*  Scan through the environment looking for "NAME="  */
86
87   for ( p=environ; *p != 0 ; p++ )
88     if ( strncmp( entry, *p, length ) == 0 )
89       {
90       *p = entry;
91       return( 0 );
92       }
93
94
95   /*  The name was not found, build a bigger environment  */
96
97   size = p - environ;
98
99   new_environ = (char **) malloc( (size+2)*PSIZE );
100
101   if ( new_environ == (char **) NULL )
102     return( -1 );
103
104   memcpy ((char *) new_environ, (char *) environ, size*PSIZE );
105
106   new_environ[size]   = entry;
107   new_environ[size+1] = NULL;
108
109   environ = new_environ;
110
111   return(0);
112 }
This page took 0.027359 seconds and 4 git commands to generate.