1/*	$NetBSD: shlib.c,v 1.23 2009/08/16 18:01:49 martin 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
33char	*strsep();
34int	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
63static	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 */
69char	 **search_dirs;
70int	n_search_dirs;
71
72const char	*standard_search_dirs[] = {
73	STANDARD_SEARCH_DIRS
74};
75
76static void
77add_search_dir(name)
78	const char	*name;
79{
80	n_search_dirs += 2;
81	search_dirs = (char **)
82		xrealloc(search_dirs, n_search_dirs * sizeof search_dirs[0]);
83	search_dirs[n_search_dirs - 2] = strdup(name);
84	search_dirs[n_search_dirs - 1] =
85	    xmalloc(sizeof(_PATH_EMUL_AOUT) + strlen(name));
86	strcpy(search_dirs[n_search_dirs - 1], _PATH_EMUL_AOUT);
87	strcat(search_dirs[n_search_dirs - 1], name);
88}
89
90void
91std_search_path()
92{
93	int	i, n;
94
95	/* Append standard search directories */
96	n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
97	for (i = 0; i < n; i++)
98		add_search_dir(standard_search_dirs[i]);
99}
100
101/*
102 * Return true if CP points to a valid dewey number.
103 * Decode and leave the result in the array DEWEY.
104 * Return the number of decoded entries in DEWEY.
105 */
106
107int
108getdewey(dewey, cp)
109int	dewey[];
110char	*cp;
111{
112	int	i, n;
113
114	for (n = 0, i = 0; i < MAXDEWEY; i++) {
115		if (*cp == '\0')
116			break;
117
118		if (*cp == '.') cp++;
119#ifdef SUNOS_LIB_COMPAT
120		if (!(isdigit)(*cp))
121#else
122		if (!isdigit((unsigned char)*cp))
123#endif
124			return 0;
125
126		dewey[n++] = strtol(cp, &cp, 10);
127	}
128
129	return n;
130}
131
132/*
133 * Compare two dewey arrays.
134 * Return -1 if `d1' represents a smaller value than `d2'.
135 * Return  1 if `d1' represents a greater value than `d2'.
136 * Return  0 if equal.
137 */
138int
139cmpndewey(d1, n1, d2, n2)
140int	d1[], d2[];
141int	n1, n2;
142{
143	register int	i;
144
145	for (i = 0; i < n1 && i < n2; i++) {
146		if (d1[i] < d2[i])
147			return -1;
148		if (d1[i] > d2[i])
149			return 1;
150	}
151
152	if (n1 == n2)
153		return 0;
154
155	if (i == n1)
156		return -1;
157
158	if (i == n2)
159		return 1;
160
161	errx(1, "cmpndewey: cant happen");
162	return 0;
163}
164
165
166/*
167 * Utility functions shared with others.
168 */
169
170
171/*
172 * Like malloc but get fatal error if memory is exhausted.
173 */
174void *
175xmalloc(size)
176	size_t size;
177{
178	void	*result = (void *)malloc(size);
179
180	if (!result)
181		errx(1, "virtual memory exhausted");
182
183	return (result);
184}
185
186/*
187 * Like realloc but get fatal error if memory is exhausted.
188 */
189void *
190xrealloc(ptr, size)
191	void *ptr;
192	size_t size;
193{
194	void	*result;
195
196	result = (ptr == NULL) ? malloc(size) : realloc(ptr, size);
197	if (result == NULL)
198		errx(1, "virtual memory exhausted");
199
200	return (result);
201}
202
203/*
204 * Return a newly-allocated string whose contents concatenate
205 * the strings S1, S2, S3.
206 */
207char *
208concat(s1, s2, s3)
209	const char *s1, *s2, *s3;
210{
211	int	len1 = strlen(s1),
212		len2 = strlen(s2),
213		len3 = strlen(s3);
214
215	char *result = (char *)xmalloc(len1 + len2 + len3 + 1);
216
217	strcpy(result, s1);
218	strcpy(result + len1, s2);
219	strcpy(result + len1 + len2, s3);
220	result[len1 + len2 + len3] = 0;
221
222	return (result);
223}
224
225