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