ldd.c revision 1153
1/* 2 * Copyright (c) 1993 Paul Kranenburg 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Paul Kranenburg. 16 * 4. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * 30 * $Id: ldd.c,v 1.2 1993/11/09 04:19:27 paul Exp $ 31 */ 32 33#include <sys/param.h> 34#include <stdio.h> 35#include <stdlib.h> 36#include <string.h> 37#include <errno.h> 38#include <sys/types.h> 39#include <sys/stat.h> 40#include <sys/file.h> 41#include <sys/time.h> 42#include <sys/resource.h> 43#include <fcntl.h> 44#include <sys/wait.h> 45#include <a.out.h> 46 47static char *progname; 48 49void 50usage() 51{ 52 fprintf(stderr, "Usage: %s <filename> ...\n", progname); 53} 54 55int 56main(argc, argv) 57int argc; 58char *argv[]; 59{ 60 int rval = 0; 61 int c; 62 extern int optind; 63 64 if ((progname = strrchr(argv[0], '/')) == NULL) 65 progname = argv[0]; 66 else 67 progname++; 68 69 while ((c = getopt(argc, argv, "")) != EOF) { 70 switch (c) { 71 default: 72 usage(); 73 exit(1); 74 } 75 } 76 argc -= optind; 77 argv += optind; 78 79 if (argc <= 0) { 80 usage(); 81 exit(1); 82 } 83 84 /* ld.so magic */ 85 setenv("LD_TRACE_LOADED_OBJECTS", "", 1); 86 87 while (argc--) { 88 int fd; 89 struct exec hdr; 90 int status; 91 92 if ((fd = open(*argv, O_RDONLY, 0)) < 0) { 93 perror(*argv); 94 rval |= 1; 95 argv++; 96 continue; 97 } 98 if (read(fd, &hdr, sizeof hdr) != sizeof hdr || 99 !(N_GETFLAG(hdr) & EX_DYNAMIC)) { 100 fprintf(stderr, "%s: not a dynamic executable\n", 101 *argv); 102 (void)close(fd); 103 rval |= 1; 104 argv++; 105 continue; 106 } 107 (void)close(fd); 108 109 printf("%s:\n", *argv); 110 fflush(stdout); 111 112 switch (fork()) { 113 case -1: 114 perror("fork"); 115 exit(1); 116 break; 117 default: 118 if (wait(&status) <= 0) 119 perror("wait"); 120 121 if (WIFSIGNALED(status)) { 122 fprintf(stderr, "%s: signal %d\n", 123 *argv, WTERMSIG(status)); 124 rval |= 1; 125 } else if (WIFEXITED(status) && WEXITSTATUS(status)) { 126 fprintf(stderr, "%s: exit status %d\n", 127 *argv, WEXITSTATUS(status)); 128 rval |= 1; 129 } 130 break; 131 case 0: 132 rval != execl(*argv, *argv, NULL) != 0; 133 perror(*argv); 134 _exit(1); 135 } 136 argv++; 137 } 138 139 return rval; 140} 141