shlib.c revision 1.2 1 1.2 he /* $NetBSD: shlib.c,v 1.2 2012/03/21 16:11:26 he Exp $ */
2 1.1 mrg
3 1.1 mrg /*-
4 1.1 mrg * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 mrg * All rights reserved.
6 1.1 mrg *
7 1.1 mrg * This code is derived from software contributed to The NetBSD Foundation
8 1.1 mrg * by Paul Kranenburg.
9 1.1 mrg *
10 1.1 mrg * Redistribution and use in source and binary forms, with or without
11 1.1 mrg * modification, are permitted provided that the following conditions
12 1.1 mrg * are met:
13 1.1 mrg * 1. Redistributions of source code must retain the above copyright
14 1.1 mrg * notice, this list of conditions and the following disclaimer.
15 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 mrg * notice, this list of conditions and the following disclaimer in the
17 1.1 mrg * documentation and/or other materials provided with the distribution.
18 1.1 mrg *
19 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 mrg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 mrg * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 mrg * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 mrg * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 mrg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 mrg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 mrg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 mrg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 mrg * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 mrg * POSSIBILITY OF SUCH DAMAGE.
30 1.1 mrg */
31 1.1 mrg
32 1.1 mrg #ifdef sun
33 1.1 mrg char *strsep();
34 1.1 mrg int isdigit();
35 1.1 mrg #endif
36 1.1 mrg
37 1.1 mrg #include <sys/param.h>
38 1.1 mrg #include <sys/types.h>
39 1.1 mrg #include <sys/stat.h>
40 1.1 mrg #include <sys/file.h>
41 1.1 mrg #include <sys/time.h>
42 1.1 mrg #include <sys/exec_aout.h>
43 1.1 mrg #include <ctype.h>
44 1.1 mrg #include <dirent.h>
45 1.1 mrg #include <err.h>
46 1.1 mrg #include <fcntl.h>
47 1.1 mrg #include <a.out.h>
48 1.1 mrg #include <stdio.h>
49 1.1 mrg #include <stdlib.h>
50 1.1 mrg #include <string.h>
51 1.1 mrg #include <paths.h>
52 1.1 mrg #include <link_aout.h>
53 1.1 mrg
54 1.1 mrg #include "shlib.h"
55 1.1 mrg
56 1.1 mrg /*
57 1.1 mrg * Standard directories to search for files specified by -l.
58 1.1 mrg */
59 1.1 mrg #ifndef STANDARD_SEARCH_DIRS
60 1.1 mrg #define STANDARD_SEARCH_DIRS "/usr/lib"
61 1.1 mrg #endif
62 1.1 mrg
63 1.1 mrg static void add_search_dir(const char *);
64 1.1 mrg
65 1.1 mrg /*
66 1.1 mrg * Actual vector of library search directories,
67 1.1 mrg * including `-L'ed and LD_LIBRARY_PATH spec'd ones.
68 1.1 mrg */
69 1.1 mrg char **search_dirs;
70 1.1 mrg int n_search_dirs;
71 1.1 mrg
72 1.1 mrg const char *standard_search_dirs[] = {
73 1.1 mrg STANDARD_SEARCH_DIRS
74 1.1 mrg };
75 1.1 mrg
76 1.1 mrg static void
77 1.2 he add_search_dir(const char *name)
78 1.1 mrg {
79 1.1 mrg n_search_dirs += 2;
80 1.1 mrg search_dirs = (char **)
81 1.1 mrg xrealloc(search_dirs, n_search_dirs * sizeof search_dirs[0]);
82 1.1 mrg search_dirs[n_search_dirs - 2] = strdup(name);
83 1.1 mrg search_dirs[n_search_dirs - 1] =
84 1.1 mrg xmalloc(sizeof(_PATH_EMUL_AOUT) + strlen(name));
85 1.1 mrg strcpy(search_dirs[n_search_dirs - 1], _PATH_EMUL_AOUT);
86 1.1 mrg strcat(search_dirs[n_search_dirs - 1], name);
87 1.1 mrg }
88 1.1 mrg
89 1.1 mrg void
90 1.2 he std_search_path(void)
91 1.1 mrg {
92 1.1 mrg int i, n;
93 1.1 mrg
94 1.1 mrg /* Append standard search directories */
95 1.1 mrg n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
96 1.1 mrg for (i = 0; i < n; i++)
97 1.1 mrg add_search_dir(standard_search_dirs[i]);
98 1.1 mrg }
99 1.1 mrg
100 1.1 mrg /*
101 1.1 mrg * Return true if CP points to a valid dewey number.
102 1.1 mrg * Decode and leave the result in the array DEWEY.
103 1.1 mrg * Return the number of decoded entries in DEWEY.
104 1.1 mrg */
105 1.1 mrg
106 1.1 mrg int
107 1.2 he getdewey(int dewey[], char *cp)
108 1.1 mrg {
109 1.1 mrg int i, n;
110 1.1 mrg
111 1.1 mrg for (n = 0, i = 0; i < MAXDEWEY; i++) {
112 1.1 mrg if (*cp == '\0')
113 1.1 mrg break;
114 1.1 mrg
115 1.1 mrg if (*cp == '.') cp++;
116 1.1 mrg #ifdef SUNOS_LIB_COMPAT
117 1.1 mrg if (!(isdigit)(*cp))
118 1.1 mrg #else
119 1.1 mrg if (!isdigit((unsigned char)*cp))
120 1.1 mrg #endif
121 1.1 mrg return 0;
122 1.1 mrg
123 1.1 mrg dewey[n++] = strtol(cp, &cp, 10);
124 1.1 mrg }
125 1.1 mrg
126 1.1 mrg return n;
127 1.1 mrg }
128 1.1 mrg
129 1.1 mrg /*
130 1.1 mrg * Compare two dewey arrays.
131 1.1 mrg * Return -1 if `d1' represents a smaller value than `d2'.
132 1.1 mrg * Return 1 if `d1' represents a greater value than `d2'.
133 1.1 mrg * Return 0 if equal.
134 1.1 mrg */
135 1.1 mrg int
136 1.2 he cmpndewey(int d1[], int n1, int d2[], int n2)
137 1.1 mrg {
138 1.1 mrg register int i;
139 1.1 mrg
140 1.1 mrg for (i = 0; i < n1 && i < n2; i++) {
141 1.1 mrg if (d1[i] < d2[i])
142 1.1 mrg return -1;
143 1.1 mrg if (d1[i] > d2[i])
144 1.1 mrg return 1;
145 1.1 mrg }
146 1.1 mrg
147 1.1 mrg if (n1 == n2)
148 1.1 mrg return 0;
149 1.1 mrg
150 1.1 mrg if (i == n1)
151 1.1 mrg return -1;
152 1.1 mrg
153 1.1 mrg if (i == n2)
154 1.1 mrg return 1;
155 1.1 mrg
156 1.1 mrg errx(1, "cmpndewey: cant happen");
157 1.1 mrg return 0;
158 1.1 mrg }
159 1.1 mrg
160 1.1 mrg
161 1.1 mrg /*
162 1.1 mrg * Utility functions shared with others.
163 1.1 mrg */
164 1.1 mrg
165 1.1 mrg
166 1.1 mrg /*
167 1.1 mrg * Like malloc but get fatal error if memory is exhausted.
168 1.1 mrg */
169 1.1 mrg void *
170 1.2 he xmalloc(size_t size)
171 1.1 mrg {
172 1.1 mrg void *result = (void *)malloc(size);
173 1.1 mrg
174 1.1 mrg if (!result)
175 1.1 mrg errx(1, "virtual memory exhausted");
176 1.1 mrg
177 1.1 mrg return (result);
178 1.1 mrg }
179 1.1 mrg
180 1.1 mrg /*
181 1.1 mrg * Like realloc but get fatal error if memory is exhausted.
182 1.1 mrg */
183 1.1 mrg void *
184 1.2 he xrealloc(void *ptr, size_t size)
185 1.1 mrg {
186 1.1 mrg void *result;
187 1.1 mrg
188 1.1 mrg result = (ptr == NULL) ? malloc(size) : realloc(ptr, size);
189 1.1 mrg if (result == NULL)
190 1.1 mrg errx(1, "virtual memory exhausted");
191 1.1 mrg
192 1.1 mrg return (result);
193 1.1 mrg }
194 1.1 mrg
195 1.1 mrg /*
196 1.1 mrg * Return a newly-allocated string whose contents concatenate
197 1.1 mrg * the strings S1, S2, S3.
198 1.1 mrg */
199 1.1 mrg char *
200 1.2 he concat(const char *s1, const char *s2, const char *s3)
201 1.1 mrg {
202 1.1 mrg int len1 = strlen(s1),
203 1.1 mrg len2 = strlen(s2),
204 1.1 mrg len3 = strlen(s3);
205 1.1 mrg
206 1.1 mrg char *result = (char *)xmalloc(len1 + len2 + len3 + 1);
207 1.1 mrg
208 1.1 mrg strcpy(result, s1);
209 1.1 mrg strcpy(result + len1, s2);
210 1.1 mrg strcpy(result + len1 + len2, s3);
211 1.1 mrg result[len1 + len2 + len3] = 0;
212 1.1 mrg
213 1.1 mrg return (result);
214 1.1 mrg }
215 1.1 mrg
216