xref: /onnv-gate/usr/src/cmd/sgs/link_audit/common/env.c (revision 12927:a27c46eb192b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*12927SRod.Evans@Sun.COM  * Common Development and Distribution License (the "License").
6*12927SRod.Evans@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*12927SRod.Evans@Sun.COM 
220Sstevel@tonic-gate /*
23*12927SRod.Evans@Sun.COM  * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate #include <stdlib.h>
260Sstevel@tonic-gate #include <string.h>
270Sstevel@tonic-gate #include <stdio.h>
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <env.h>
300Sstevel@tonic-gate 
310Sstevel@tonic-gate static const char	*token = ":";
320Sstevel@tonic-gate 
330Sstevel@tonic-gate void
build_env_list(Elist ** list,const char * env)340Sstevel@tonic-gate build_env_list(Elist **list, const char *env)
350Sstevel@tonic-gate {
360Sstevel@tonic-gate 	char	*envstr;
370Sstevel@tonic-gate 	char	*tok;
380Sstevel@tonic-gate 	char	*lasts;
390Sstevel@tonic-gate 
400Sstevel@tonic-gate 	if ((envstr = getenv(env)) == NULL)
410Sstevel@tonic-gate 		return;
420Sstevel@tonic-gate 	envstr = strdup(envstr);
430Sstevel@tonic-gate 	tok = strtok_r(envstr, token, &lasts);
440Sstevel@tonic-gate 	while (tok) {
450Sstevel@tonic-gate 		Elist	*lp;
46*12927SRod.Evans@Sun.COM 		if ((lp = (Elist *)malloc(sizeof (Elist))) == NULL) {
470Sstevel@tonic-gate 			(void) printf("build_list: malloc failed\n");
480Sstevel@tonic-gate 			exit(1);
490Sstevel@tonic-gate 		}
500Sstevel@tonic-gate 		lp->l_libname = strdup(tok);
510Sstevel@tonic-gate 		lp->l_next = *list;
520Sstevel@tonic-gate 		*list = lp;
530Sstevel@tonic-gate 		tok = strtok_r(NULL, token, &lasts);
540Sstevel@tonic-gate 	}
550Sstevel@tonic-gate 	free(envstr);
560Sstevel@tonic-gate }
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 
590Sstevel@tonic-gate Elist *
check_list(Elist * list,const char * str)600Sstevel@tonic-gate check_list(Elist *list, const char *str)
610Sstevel@tonic-gate {
620Sstevel@tonic-gate 	const char	*basestr;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	if (list == NULL)
650Sstevel@tonic-gate 		return (NULL);
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	/*
68*12927SRod.Evans@Sun.COM 	 * Is this a basename or a relative path name
690Sstevel@tonic-gate 	 */
70*12927SRod.Evans@Sun.COM 	if ((basestr = strrchr(str, '/')) != NULL)
710Sstevel@tonic-gate 		basestr++;
720Sstevel@tonic-gate 	else
730Sstevel@tonic-gate 		basestr = str;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	for (; list; list = list->l_next) {
77*12927SRod.Evans@Sun.COM 		if (strchr(list->l_libname, '/') == NULL) {
780Sstevel@tonic-gate 			if (strcmp(basestr, list->l_libname) == 0)
790Sstevel@tonic-gate 				return (list);
800Sstevel@tonic-gate 		} else {
810Sstevel@tonic-gate 			if (strcmp(str, list->l_libname) == 0)
820Sstevel@tonic-gate 				return (list);
830Sstevel@tonic-gate 		}
840Sstevel@tonic-gate 	}
850Sstevel@tonic-gate 	return (NULL);
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
880Sstevel@tonic-gate char *
checkenv(const char * env)890Sstevel@tonic-gate checkenv(const char *env)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate 	char	*envstr;
920Sstevel@tonic-gate 	if ((envstr = getenv(env)) == NULL)
930Sstevel@tonic-gate 		return (NULL);
940Sstevel@tonic-gate 	while (*envstr == ' ')
950Sstevel@tonic-gate 		envstr++;
960Sstevel@tonic-gate 	if (*envstr == '\0')
970Sstevel@tonic-gate 		return (NULL);
980Sstevel@tonic-gate 	return (envstr);
990Sstevel@tonic-gate }
100