1*1afead99She /* $NetBSD: shlib.c,v 1.2 2012/03/21 16:11:26 he Exp $ */
24c92852aSmrg
34c92852aSmrg /*-
44c92852aSmrg * Copyright (c) 1998 The NetBSD Foundation, Inc.
54c92852aSmrg * All rights reserved.
64c92852aSmrg *
74c92852aSmrg * This code is derived from software contributed to The NetBSD Foundation
84c92852aSmrg * by Paul Kranenburg.
94c92852aSmrg *
104c92852aSmrg * Redistribution and use in source and binary forms, with or without
114c92852aSmrg * modification, are permitted provided that the following conditions
124c92852aSmrg * are met:
134c92852aSmrg * 1. Redistributions of source code must retain the above copyright
144c92852aSmrg * notice, this list of conditions and the following disclaimer.
154c92852aSmrg * 2. Redistributions in binary form must reproduce the above copyright
164c92852aSmrg * notice, this list of conditions and the following disclaimer in the
174c92852aSmrg * documentation and/or other materials provided with the distribution.
184c92852aSmrg *
194c92852aSmrg * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
204c92852aSmrg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
214c92852aSmrg * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
224c92852aSmrg * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
234c92852aSmrg * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
244c92852aSmrg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
254c92852aSmrg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
264c92852aSmrg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
274c92852aSmrg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
284c92852aSmrg * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
294c92852aSmrg * POSSIBILITY OF SUCH DAMAGE.
304c92852aSmrg */
314c92852aSmrg
324c92852aSmrg #ifdef sun
334c92852aSmrg char *strsep();
344c92852aSmrg int isdigit();
354c92852aSmrg #endif
364c92852aSmrg
374c92852aSmrg #include <sys/param.h>
384c92852aSmrg #include <sys/types.h>
394c92852aSmrg #include <sys/stat.h>
404c92852aSmrg #include <sys/file.h>
414c92852aSmrg #include <sys/time.h>
424c92852aSmrg #include <sys/exec_aout.h>
434c92852aSmrg #include <ctype.h>
444c92852aSmrg #include <dirent.h>
454c92852aSmrg #include <err.h>
464c92852aSmrg #include <fcntl.h>
474c92852aSmrg #include <a.out.h>
484c92852aSmrg #include <stdio.h>
494c92852aSmrg #include <stdlib.h>
504c92852aSmrg #include <string.h>
514c92852aSmrg #include <paths.h>
524c92852aSmrg #include <link_aout.h>
534c92852aSmrg
544c92852aSmrg #include "shlib.h"
554c92852aSmrg
564c92852aSmrg /*
574c92852aSmrg * Standard directories to search for files specified by -l.
584c92852aSmrg */
594c92852aSmrg #ifndef STANDARD_SEARCH_DIRS
604c92852aSmrg #define STANDARD_SEARCH_DIRS "/usr/lib"
614c92852aSmrg #endif
624c92852aSmrg
634c92852aSmrg static void add_search_dir(const char *);
644c92852aSmrg
654c92852aSmrg /*
664c92852aSmrg * Actual vector of library search directories,
674c92852aSmrg * including `-L'ed and LD_LIBRARY_PATH spec'd ones.
684c92852aSmrg */
694c92852aSmrg char **search_dirs;
704c92852aSmrg int n_search_dirs;
714c92852aSmrg
724c92852aSmrg const char *standard_search_dirs[] = {
734c92852aSmrg STANDARD_SEARCH_DIRS
744c92852aSmrg };
754c92852aSmrg
764c92852aSmrg static void
add_search_dir(const char * name)77*1afead99She add_search_dir(const char *name)
784c92852aSmrg {
794c92852aSmrg n_search_dirs += 2;
804c92852aSmrg search_dirs = (char **)
814c92852aSmrg xrealloc(search_dirs, n_search_dirs * sizeof search_dirs[0]);
824c92852aSmrg search_dirs[n_search_dirs - 2] = strdup(name);
834c92852aSmrg search_dirs[n_search_dirs - 1] =
844c92852aSmrg xmalloc(sizeof(_PATH_EMUL_AOUT) + strlen(name));
854c92852aSmrg strcpy(search_dirs[n_search_dirs - 1], _PATH_EMUL_AOUT);
864c92852aSmrg strcat(search_dirs[n_search_dirs - 1], name);
874c92852aSmrg }
884c92852aSmrg
894c92852aSmrg void
std_search_path(void)90*1afead99She std_search_path(void)
914c92852aSmrg {
924c92852aSmrg int i, n;
934c92852aSmrg
944c92852aSmrg /* Append standard search directories */
954c92852aSmrg n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
964c92852aSmrg for (i = 0; i < n; i++)
974c92852aSmrg add_search_dir(standard_search_dirs[i]);
984c92852aSmrg }
994c92852aSmrg
1004c92852aSmrg /*
1014c92852aSmrg * Return true if CP points to a valid dewey number.
1024c92852aSmrg * Decode and leave the result in the array DEWEY.
1034c92852aSmrg * Return the number of decoded entries in DEWEY.
1044c92852aSmrg */
1054c92852aSmrg
1064c92852aSmrg int
getdewey(int dewey[],char * cp)107*1afead99She getdewey(int dewey[], char *cp)
1084c92852aSmrg {
1094c92852aSmrg int i, n;
1104c92852aSmrg
1114c92852aSmrg for (n = 0, i = 0; i < MAXDEWEY; i++) {
1124c92852aSmrg if (*cp == '\0')
1134c92852aSmrg break;
1144c92852aSmrg
1154c92852aSmrg if (*cp == '.') cp++;
1164c92852aSmrg #ifdef SUNOS_LIB_COMPAT
1174c92852aSmrg if (!(isdigit)(*cp))
1184c92852aSmrg #else
1194c92852aSmrg if (!isdigit((unsigned char)*cp))
1204c92852aSmrg #endif
1214c92852aSmrg return 0;
1224c92852aSmrg
1234c92852aSmrg dewey[n++] = strtol(cp, &cp, 10);
1244c92852aSmrg }
1254c92852aSmrg
1264c92852aSmrg return n;
1274c92852aSmrg }
1284c92852aSmrg
1294c92852aSmrg /*
1304c92852aSmrg * Compare two dewey arrays.
1314c92852aSmrg * Return -1 if `d1' represents a smaller value than `d2'.
1324c92852aSmrg * Return 1 if `d1' represents a greater value than `d2'.
1334c92852aSmrg * Return 0 if equal.
1344c92852aSmrg */
1354c92852aSmrg int
cmpndewey(int d1[],int n1,int d2[],int n2)136*1afead99She cmpndewey(int d1[], int n1, int d2[], int n2)
1374c92852aSmrg {
1384c92852aSmrg register int i;
1394c92852aSmrg
1404c92852aSmrg for (i = 0; i < n1 && i < n2; i++) {
1414c92852aSmrg if (d1[i] < d2[i])
1424c92852aSmrg return -1;
1434c92852aSmrg if (d1[i] > d2[i])
1444c92852aSmrg return 1;
1454c92852aSmrg }
1464c92852aSmrg
1474c92852aSmrg if (n1 == n2)
1484c92852aSmrg return 0;
1494c92852aSmrg
1504c92852aSmrg if (i == n1)
1514c92852aSmrg return -1;
1524c92852aSmrg
1534c92852aSmrg if (i == n2)
1544c92852aSmrg return 1;
1554c92852aSmrg
1564c92852aSmrg errx(1, "cmpndewey: cant happen");
1574c92852aSmrg return 0;
1584c92852aSmrg }
1594c92852aSmrg
1604c92852aSmrg
1614c92852aSmrg /*
1624c92852aSmrg * Utility functions shared with others.
1634c92852aSmrg */
1644c92852aSmrg
1654c92852aSmrg
1664c92852aSmrg /*
1674c92852aSmrg * Like malloc but get fatal error if memory is exhausted.
1684c92852aSmrg */
1694c92852aSmrg void *
xmalloc(size_t size)170*1afead99She xmalloc(size_t size)
1714c92852aSmrg {
1724c92852aSmrg void *result = (void *)malloc(size);
1734c92852aSmrg
1744c92852aSmrg if (!result)
1754c92852aSmrg errx(1, "virtual memory exhausted");
1764c92852aSmrg
1774c92852aSmrg return (result);
1784c92852aSmrg }
1794c92852aSmrg
1804c92852aSmrg /*
1814c92852aSmrg * Like realloc but get fatal error if memory is exhausted.
1824c92852aSmrg */
1834c92852aSmrg void *
xrealloc(void * ptr,size_t size)184*1afead99She xrealloc(void *ptr, size_t size)
1854c92852aSmrg {
1864c92852aSmrg void *result;
1874c92852aSmrg
1884c92852aSmrg result = (ptr == NULL) ? malloc(size) : realloc(ptr, size);
1894c92852aSmrg if (result == NULL)
1904c92852aSmrg errx(1, "virtual memory exhausted");
1914c92852aSmrg
1924c92852aSmrg return (result);
1934c92852aSmrg }
1944c92852aSmrg
1954c92852aSmrg /*
1964c92852aSmrg * Return a newly-allocated string whose contents concatenate
1974c92852aSmrg * the strings S1, S2, S3.
1984c92852aSmrg */
1994c92852aSmrg char *
concat(const char * s1,const char * s2,const char * s3)200*1afead99She concat(const char *s1, const char *s2, const char *s3)
2014c92852aSmrg {
2024c92852aSmrg int len1 = strlen(s1),
2034c92852aSmrg len2 = strlen(s2),
2044c92852aSmrg len3 = strlen(s3);
2054c92852aSmrg
2064c92852aSmrg char *result = (char *)xmalloc(len1 + len2 + len3 + 1);
2074c92852aSmrg
2084c92852aSmrg strcpy(result, s1);
2094c92852aSmrg strcpy(result + len1, s2);
2104c92852aSmrg strcpy(result + len1 + len2, s3);
2114c92852aSmrg result[len1 + len2 + len3] = 0;
2124c92852aSmrg
2134c92852aSmrg return (result);
2144c92852aSmrg }
2154c92852aSmrg
216