xref: /openbsd/libexec/ld.so/ldconfig/shlib.c (revision f4147939)
1 /*	$OpenBSD: shlib.c,v 1.13 2018/04/26 12:42:50 guenther Exp $	*/
2 /*	$NetBSD: shlib.c,v 1.13 1998/04/04 01:00:29 fvdl Exp $	*/
3 
4 /*
5  * Copyright (c) 1993 Paul Kranenburg
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by Paul Kranenburg.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
35 #include <ctype.h>
36 #include <err.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #include "ld.h"
41 
42 /*
43  * Standard directories to search for files specified by -l.
44  */
45 #ifndef STANDARD_SEARCH_DIRS
46 #define	STANDARD_SEARCH_DIRS	"/usr/lib"
47 #endif
48 
49 /*
50  * Actual vector of library search directories,
51  * including `-L'ed and LD_LIBRARY_PATH spec'd ones.
52  */
53 char	 **search_dirs;
54 int	n_search_dirs;
55 
56 char	*standard_search_dirs[] = {
57 	STANDARD_SEARCH_DIRS
58 };
59 
60 void
add_search_dir(char * name)61 add_search_dir(char *name)
62 {
63 	size_t len;
64 	int i;
65 
66 	len = strlen(name);
67 
68 	while (len > 1 && name[len - 1] == '/')
69 		--len;
70 
71 	for (i = 0; i < n_search_dirs; i++)
72 		if (strlen(search_dirs[i]) == len &&
73 		    !strncmp(search_dirs[i], name, len))
74 				return;
75 	n_search_dirs++;
76 	search_dirs = xrealloc(search_dirs,
77 	    n_search_dirs * sizeof search_dirs[0]);
78 	search_dirs[n_search_dirs - 1] = xmalloc(++len);
79 	(void)strlcpy(search_dirs[n_search_dirs - 1], name, len);
80 }
81 
82 void
remove_search_dir(char * name)83 remove_search_dir(char *name)
84 {
85 	size_t	len;
86 	int	i;
87 
88 	len = strlen(name);
89 
90 	while (len > 1 && name[len - 1] == '/')
91 		--len;
92 
93 	for (i = 0; i < n_search_dirs; i++) {
94 		if (strlen(search_dirs[i]) != len ||
95 		    strncmp(search_dirs[i], name, len))
96 			continue;
97 		free(search_dirs[i]);
98 		if (i < (n_search_dirs - 1))
99 			bcopy(&search_dirs[i+1], &search_dirs[i],
100 			    (n_search_dirs - i - 1) * sizeof search_dirs[0]);
101 		n_search_dirs--;
102 		search_dirs = xrealloc(search_dirs,
103 		    n_search_dirs * sizeof search_dirs[0]);
104 		break;
105 	}
106 }
107 
108 void
add_search_path(char * path)109 add_search_path(char *path)
110 {
111 	char	*cp, *dup;
112 
113 	if (path == NULL)
114 		return;
115 
116 	/* Add search directories from `path' */
117 	path = dup = strdup(path);
118 	while ((cp = strsep(&path, ":")) != NULL)
119 		add_search_dir(cp);
120 	free(dup);
121 }
122 
123 void
std_search_path(void)124 std_search_path(void)
125 {
126 	int	i, n;
127 
128 	/* Append standard search directories */
129 	n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
130 	for (i = 0; i < n; i++)
131 		add_search_dir(standard_search_dirs[i]);
132 }
133 
134 /*
135  * Return true if CP points to a valid dewey number.
136  * Decode and leave the result in the array DEWEY.
137  * Return the number of decoded entries in DEWEY.
138  */
139 
140 int
getdewey(int dewey[],char * cp)141 getdewey(int dewey[], char *cp)
142 {
143 	int	i, n;
144 
145 	for (n = 0, i = 0; i < MAXDEWEY; i++) {
146 		if (*cp == '\0')
147 			break;
148 
149 		if (*cp == '.') cp++;
150 #ifdef SUNOS_LIB_COMPAT
151 		if (!(isdigit)(*cp))
152 #else
153 		if (!isdigit(*cp))
154 #endif
155 			return 0;
156 
157 		dewey[n++] = strtol(cp, &cp, 10);
158 	}
159 	return n;
160 }
161 
162 /*
163  * Compare two dewey arrays.
164  * Return -1 if `d1' represents a smaller value than `d2'.
165  * Return  1 if `d1' represents a greater value than `d2'.
166  * Return  0 if equal.
167  */
168 int
cmpndewey(int d1[],int n1,int d2[],int n2)169 cmpndewey(int d1[], int n1, int d2[], int n2)
170 {
171 	int	i;
172 
173 	for (i = 0; i < n1 && i < n2; i++) {
174 		if (d1[i] < d2[i])
175 			return -1;
176 		if (d1[i] > d2[i])
177 			return 1;
178 	}
179 	if (n1 == n2)
180 		return 0;
181 	if (i == n1)
182 		return -1;
183 	if (i == n2)
184 		return 1;
185 	errx(1, "cmpndewey: cant happen");
186 	return 0;
187 }
188