4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 1987 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
33 /* putenv - change environment variables
35 * input - char *change = a pointer to a string of the form
38 * output - 0, if successful
45 extern char **environ
; /* pointer to enviroment */
46 static int reall
; /* flag to reallocate space, if putenv is called
48 static int find(char *);
49 static int match(char *, char *);
54 char **newenv
; /* points to new environment */
55 int which
; /* index of variable to replace */
57 if ((which
= find(change
)) < 0) {
58 /* if a new variable */
59 /* which is negative of table size, so invert and
63 /* we have expanded environ before */
64 newenv
= (char **)realloc(environ
,
65 which
*sizeof(char *));
66 if (newenv
== NULL
) return (-1);
67 /* now that we have space, change environ */
70 /* environ points to the original space */
72 newenv
= (char **)malloc(which
*sizeof(char *));
73 if (newenv
== NULL
) return (-1);
74 (void)memcpy((char *)newenv
, (char *)environ
,
75 (int)(which
*sizeof(char *)));
78 environ
[which
-2] = change
;
79 environ
[which
-1] = NULL
;
81 /* we are replacing an old variable */
82 environ
[which
] = change
;
87 /* find - find where s2 is in environ
89 * input - str = string of form name=value
91 * output - index of name in environ that matches "name"
92 * -size of table, if none exists
97 int ct
= 0; /* index into environ */
99 while(environ
[ct
] != NULL
) {
100 if (match(environ
[ct
], str
) != 0)
107 * s1 is either name, or name=value
109 * if names match, return value of 1,
114 match(char *s1
, char *s2
)
116 while(*s1
== *s2
++) {