xref: /titanic_41/usr/src/cmd/sgs/link_audit/common/env.c (revision 5fbb41393be5d63f75952b1d72d4df2642d22557)
1 /*
2  * CDDL HEADER START
3  *
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
7  * with the License.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <env.h>
34 
35 static const char	*token = ":";
36 
37 void
38 build_env_list(Elist **list, const char *env)
39 {
40 	char	*envstr;
41 	char	*tok;
42 	char	*lasts;
43 
44 	if ((envstr = getenv(env)) == NULL)
45 		return;
46 	envstr = strdup(envstr);
47 	tok = strtok_r(envstr, token, &lasts);
48 	while (tok) {
49 		Elist	*lp;
50 		if ((lp = (Elist *)malloc(sizeof (Elist))) == 0) {
51 			(void) printf("build_list: malloc failed\n");
52 			exit(1);
53 		}
54 		lp->l_libname = strdup(tok);
55 		lp->l_next = *list;
56 		*list = lp;
57 		tok = strtok_r(NULL, token, &lasts);
58 	}
59 	free(envstr);
60 }
61 
62 
63 Elist *
64 check_list(Elist *list, const char *str)
65 {
66 	const char	*basestr;
67 
68 	if (list == NULL)
69 		return (NULL);
70 
71 	/*
72 	 * Is this a basename or a relativepath name
73 	 */
74 	if ((basestr = strrchr(str, '/')) != 0)
75 		basestr++;
76 	else
77 		basestr = str;
78 
79 
80 	for (; list; list = list->l_next) {
81 		if (strchr(list->l_libname, '/') == 0) {
82 			if (strcmp(basestr, list->l_libname) == 0)
83 				return (list);
84 		} else {
85 			if (strcmp(str, list->l_libname) == 0)
86 				return (list);
87 		}
88 	}
89 	return (NULL);
90 }
91 
92 char *
93 checkenv(const char *env)
94 {
95 	char	*envstr;
96 	if ((envstr = getenv(env)) == NULL)
97 		return (NULL);
98 	while (*envstr == ' ')
99 		envstr++;
100 	if (*envstr == '\0')
101 		return (NULL);
102 	return (envstr);
103 }
104