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