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 2009 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 */
38 /* 0 = both upper and lower case */
39 /* 1 = initial lower case only (build variables) */
40 /* 2 = initial upper case only (install variables) */
41 #define mode(flag, pt) (!flag || ((flag == 1) && islower(pt[1])) || \
42 ((flag == 2) && isupper(pt[1])))
45 * For next and last functions below, values indicate whether resolution
48 * 0 = all OK - the variable resolved within the established parameters
49 * or it wasn't time for the variable to bind.
50 * 1 = parameter did not resolve because there was no value in the
51 * environment or because it was a build variable at install
56 * This gets a raw path which may contain shell variables and returns in path
57 * a pathname with all appropriate parameters resolved. If it comes in
58 * relative, it goes out relative.
61 mappath(int flag
, char *path
)
63 char buffer
[PATH_MAX
];
65 char *npt
, *pt
, *pt2
, *copy
;
72 * For each "/" separated token. If the token contains an environment
73 * variable, then evaluate the variable and insert it into path.
75 for (pt
= path
; *pt
; /* void */) {
77 * If this is a token and it's an environment variable
78 * properly situated in the path...
80 if ((*pt
== '$') && isalpha(pt
[1]) &&
81 ((pt
== path
) || (pt
[-1] == '/'))) {
82 /* ... and it's the right time to evaluate it... */
84 /* replace the parameter with its value. */
86 for (npt
= pt
+1; *npt
&& (*npt
!= '/');
91 * At this point EVERY token should evaluate
92 * to a value. If it doesn't, there's an
95 if ((token
= getenv(varname
)) != NULL
&&
97 /* copy in parameter value */
106 * If evaluate time is wrong, determine of this is an
110 if (flag
== 2) { /* install-time. */
112 * ALL variables MUST evaluate at
117 } else if (flag
== 1 && /* build-time */
120 * All build-time variables must
121 * evaluate at build time.
125 } else /* no problem. */
129 * If it's a separator, copy it over to the target buffer and
130 * move to the start of the next token.
132 } else if (*pt
== '/') {
135 if ((pt
[1] == '\0') && (pt
> path
))
139 * If we're in the middle of a non-parametric token, copy
140 * that character over and try the next character.
146 (void) strcpy(path
, buffer
);
151 * This function resolves the path into an absolute path referred to
152 * an install root of ir.
155 basepath(char *path
, char *basedir
, char *ir
)
157 char buffer
[PATH_MAX
];
159 /* For a relative path, prepend the basedir */
161 (void) strcpy(buffer
, path
);
168 if (basedir
&& *basedir
) {
169 if (ir
&& *ir
&& *basedir
!= '/')
172 *path
++ = *basedir
++;
177 (void) strcpy(path
, buffer
);
179 /* For an absolute path, just prepend the install root */
182 (void) strcpy(buffer
, path
);
187 (void) strcpy(path
, buffer
);
193 * Evaluate varname and return with environment variables resolved.
194 * NOTE: This assumes that varname is a buffer long enough to hold the
198 mapvar(int flag
, char *varname
)
203 /* If its a parametric entry beginning with an alpha character. */
204 if (*varname
== '$' && isalpha(varname
[1])) {
205 /* ...and it's the right time to evaluate it... */
206 if (mode(flag
, varname
)) {
208 * then it MUST be possible to evaluate it. If not,
211 if (((token
= getenv(&varname
[1])) != NULL
) &&
213 /* copy token into varname */
215 *varname
++ = *token
++;
220 if (flag
== 2) /* install-time. */
222 * ALL variables MUST evaluate at install
226 else if (flag
== 1 && /* build-time */
229 * all build-time variables must evaluate at