1 /* $OpenBSD: shlib.c,v 1.9 2006/05/13 16:33:40 deraadt Exp $ */ 2 /* $NetBSD: shlib.c,v 1.13 1998/04/04 01:00:29 fvdl Exp $ */ 3 4 /* 5 * Copyright (c) 1993 Paul Kranenburg 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Paul Kranenburg. 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 */ 34 35 #include <sys/param.h> 36 #include <sys/types.h> 37 #include <sys/stat.h> 38 #include <sys/file.h> 39 #include <sys/time.h> 40 #include <ranlib.h> 41 #include <a.out.h> 42 #include <ctype.h> 43 #include <dirent.h> 44 #include <err.h> 45 #include <fcntl.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 50 #include "ld.h" 51 52 /* 53 * Standard directories to search for files specified by -l. 54 */ 55 #ifndef STANDARD_SEARCH_DIRS 56 #define STANDARD_SEARCH_DIRS "/usr/lib" 57 #endif 58 59 /* 60 * Actual vector of library search directories, 61 * including `-L'ed and LD_LIBRARY_PATH spec'd ones. 62 */ 63 char **search_dirs; 64 int n_search_dirs; 65 66 char *standard_search_dirs[] = { 67 STANDARD_SEARCH_DIRS 68 }; 69 70 void 71 add_search_dir(char *name) 72 { 73 size_t len; 74 int i; 75 76 len = strlen(name); 77 78 while (len > 1 && name[len - 1] == '/') 79 --len; 80 81 for (i = 0; i < n_search_dirs; i++) 82 if (strlen(search_dirs[i]) == len && 83 !strncmp(search_dirs[i], name, len)) 84 return; 85 n_search_dirs++; 86 search_dirs = (char **)xrealloc(search_dirs, 87 n_search_dirs * sizeof search_dirs[0]); 88 search_dirs[n_search_dirs - 1] = xmalloc(++len); 89 (void)strlcpy(search_dirs[n_search_dirs - 1], name, len); 90 } 91 92 void 93 remove_search_dir(char *name) 94 { 95 size_t len; 96 int i; 97 98 len = strlen(name); 99 100 while (len > 1 && name[len - 1] == '/') 101 --len; 102 103 for (i = 0; i < n_search_dirs; i++) { 104 if (strlen(search_dirs[i]) != len || 105 strncmp(search_dirs[i], name, len)) 106 continue; 107 free(search_dirs[i]); 108 if (i < (n_search_dirs - 1)) 109 bcopy(&search_dirs[i+1], &search_dirs[i], 110 (n_search_dirs - i - 1) * sizeof search_dirs[0]); 111 n_search_dirs--; 112 search_dirs = (char **)xrealloc(search_dirs, 113 n_search_dirs * sizeof search_dirs[0]); 114 break; 115 } 116 } 117 118 void 119 add_search_path(char *path) 120 { 121 char *cp, *dup; 122 123 if (path == NULL) 124 return; 125 126 /* Add search directories from `path' */ 127 path = dup = strdup(path); 128 while ((cp = strsep(&path, ":")) != NULL) 129 add_search_dir(cp); 130 free(dup); 131 } 132 133 void 134 std_search_path(void) 135 { 136 int i, n; 137 138 /* Append standard search directories */ 139 n = sizeof standard_search_dirs / sizeof standard_search_dirs[0]; 140 for (i = 0; i < n; i++) 141 add_search_dir(standard_search_dirs[i]); 142 } 143 144 /* 145 * Return true if CP points to a valid dewey number. 146 * Decode and leave the result in the array DEWEY. 147 * Return the number of decoded entries in DEWEY. 148 */ 149 150 int 151 getdewey(int dewey[], char *cp) 152 { 153 int i, n; 154 155 for (n = 0, i = 0; i < MAXDEWEY; i++) { 156 if (*cp == '\0') 157 break; 158 159 if (*cp == '.') cp++; 160 #ifdef SUNOS_LIB_COMPAT 161 if (!(isdigit)(*cp)) 162 #else 163 if (!isdigit(*cp)) 164 #endif 165 return 0; 166 167 dewey[n++] = strtol(cp, &cp, 10); 168 } 169 return n; 170 } 171 172 /* 173 * Compare two dewey arrays. 174 * Return -1 if `d1' represents a smaller value than `d2'. 175 * Return 1 if `d1' represents a greater value than `d2'. 176 * Return 0 if equal. 177 */ 178 int 179 cmpndewey(int d1[], int n1, int d2[], int n2) 180 { 181 int i; 182 183 for (i = 0; i < n1 && i < n2; i++) { 184 if (d1[i] < d2[i]) 185 return -1; 186 if (d1[i] > d2[i]) 187 return 1; 188 } 189 if (n1 == n2) 190 return 0; 191 if (i == n1) 192 return -1; 193 if (i == n2) 194 return 1; 195 errx(1, "cmpndewey: cant happen"); 196 return 0; 197 } 198