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