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