4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
34 * xsetenv, xgetenv, Xgetenv - manage an alternate environment space
37 * int ret = xsetenv(file)
38 * char *x = xgetenv("FOO");
39 * char *x = Xgetenv("FOO");
42 * xsetenv() reads the given file into an internal buffer
43 * and sets up an alternate environment.
45 * Return values: 1 - OKAY
46 * 0 - troubles reading the file
47 * -1 - troubles opening the file
49 * xgetenv() returns the environment value from the
50 * alternate environment.
52 * Return values: (char *)0 - no value for that variable
55 * Xgetenv() returns the environment value from the
56 * alternate environment.
58 * Return values: "" - no value for that variable
62 * Assumes the environment is < 5120 bytes, as in the UNIX
63 * System environment. Assumes < 512 lines in the file.
64 * These values may be adjusted below.
67 #include <sys/types.h>
80 static char **xenv
= 0;
81 static char *(xenvptrs
[MAXVARS
]);
82 static char xbuf
[MAXENV
];
84 static void reduce(char *);
87 * set up an environment buffer
88 * and the pointers into it
97 infd
= open(xfile
, O_RDONLY
);
102 /* Read in the entire file. */
103 nread
= read(infd
, xbuf
, sizeof (xbuf
));
110 * Set up pointers into the buffer.
111 * Replace \n with \0.
112 * Collapse white space around the = sign and at the
113 * beginning and end of the line.
117 for (i
= 0, envctr
= 0; i
< nread
; i
++) {
118 if (xbuf
[i
] == '\n') {
120 reduce(xenv
[envctr
]);
121 xenv
[++envctr
] = &xbuf
[i
+1];
122 if (envctr
== MAXVARS
) {
134 * Let getenv() do the dirty work
135 * of looking up the variable. We
136 * do this by temporarily resetting
137 * environ to point to the local area.
142 extern char **environ
;
143 char *ret
, **svenviron
= environ
;
152 * Let xgetenv() do the dirty work
153 * of looking up the variable.
158 char *ret
= xgetenv(env
);
159 return (ret
? ret
: "");
163 * Remove the spaces within the environment variable.
164 * The variable can look like this:
166 * <sp1> variable <sp2> = <sp3> value <sp4> \0
168 * All spaces can be removed, except within
169 * the variable name and the value.
179 while (*from
&&isspace((int)*from
))
183 while (*from
&& (*from
!= '=') && !isspace((int)*from
))
187 while (*from
&& isspace((int)*from
))
195 while (*from
&& isspace((int)*from
))
203 while ((to
> svfrom
) && isspace((int)to
[-1]))