xref: /openbsd-src/libexec/ld.so/ldconfig/shlib.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: shlib.c,v 1.12 2015/12/22 08:15:05 mmcc 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 <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/file.h>
38 #include <sys/time.h>
39 #include <ranlib.h>
40 #include <ctype.h>
41 #include <dirent.h>
42 #include <err.h>
43 #include <fcntl.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 
48 #include "ld.h"
49 
50 /*
51  * Standard directories to search for files specified by -l.
52  */
53 #ifndef STANDARD_SEARCH_DIRS
54 #define	STANDARD_SEARCH_DIRS	"/usr/lib"
55 #endif
56 
57 /*
58  * Actual vector of library search directories,
59  * including `-L'ed and LD_LIBRARY_PATH spec'd ones.
60  */
61 char	 **search_dirs;
62 int	n_search_dirs;
63 
64 char	*standard_search_dirs[] = {
65 	STANDARD_SEARCH_DIRS
66 };
67 
68 void
69 add_search_dir(char *name)
70 {
71 	size_t len;
72 	int i;
73 
74 	len = strlen(name);
75 
76 	while (len > 1 && name[len - 1] == '/')
77 		--len;
78 
79 	for (i = 0; i < n_search_dirs; i++)
80 		if (strlen(search_dirs[i]) == len &&
81 		    !strncmp(search_dirs[i], name, len))
82 				return;
83 	n_search_dirs++;
84 	search_dirs = xrealloc(search_dirs,
85 	    n_search_dirs * sizeof search_dirs[0]);
86 	search_dirs[n_search_dirs - 1] = xmalloc(++len);
87 	(void)strlcpy(search_dirs[n_search_dirs - 1], name, len);
88 }
89 
90 void
91 remove_search_dir(char *name)
92 {
93 	size_t	len;
94 	int	i;
95 
96 	len = strlen(name);
97 
98 	while (len > 1 && name[len - 1] == '/')
99 		--len;
100 
101 	for (i = 0; i < n_search_dirs; i++) {
102 		if (strlen(search_dirs[i]) != len ||
103 		    strncmp(search_dirs[i], name, len))
104 			continue;
105 		free(search_dirs[i]);
106 		if (i < (n_search_dirs - 1))
107 			bcopy(&search_dirs[i+1], &search_dirs[i],
108 			    (n_search_dirs - i - 1) * sizeof search_dirs[0]);
109 		n_search_dirs--;
110 		search_dirs = xrealloc(search_dirs,
111 		    n_search_dirs * sizeof search_dirs[0]);
112 		break;
113 	}
114 }
115 
116 void
117 add_search_path(char *path)
118 {
119 	char	*cp, *dup;
120 
121 	if (path == NULL)
122 		return;
123 
124 	/* Add search directories from `path' */
125 	path = dup = strdup(path);
126 	while ((cp = strsep(&path, ":")) != NULL)
127 		add_search_dir(cp);
128 	free(dup);
129 }
130 
131 void
132 std_search_path(void)
133 {
134 	int	i, n;
135 
136 	/* Append standard search directories */
137 	n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
138 	for (i = 0; i < n; i++)
139 		add_search_dir(standard_search_dirs[i]);
140 }
141 
142 /*
143  * Return true if CP points to a valid dewey number.
144  * Decode and leave the result in the array DEWEY.
145  * Return the number of decoded entries in DEWEY.
146  */
147 
148 int
149 getdewey(int dewey[], char *cp)
150 {
151 	int	i, n;
152 
153 	for (n = 0, i = 0; i < MAXDEWEY; i++) {
154 		if (*cp == '\0')
155 			break;
156 
157 		if (*cp == '.') cp++;
158 #ifdef SUNOS_LIB_COMPAT
159 		if (!(isdigit)(*cp))
160 #else
161 		if (!isdigit(*cp))
162 #endif
163 			return 0;
164 
165 		dewey[n++] = strtol(cp, &cp, 10);
166 	}
167 	return n;
168 }
169 
170 /*
171  * Compare two dewey arrays.
172  * Return -1 if `d1' represents a smaller value than `d2'.
173  * Return  1 if `d1' represents a greater value than `d2'.
174  * Return  0 if equal.
175  */
176 int
177 cmpndewey(int d1[], int n1, int d2[], int n2)
178 {
179 	int	i;
180 
181 	for (i = 0; i < n1 && i < n2; i++) {
182 		if (d1[i] < d2[i])
183 			return -1;
184 		if (d1[i] > d2[i])
185 			return 1;
186 	}
187 	if (n1 == n2)
188 		return 0;
189 	if (i == n1)
190 		return -1;
191 	if (i == n2)
192 		return 1;
193 	errx(1, "cmpndewey: cant happen");
194 	return 0;
195 }
196