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 --- 13 unchanged lines hidden (view full) --- 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.3 1994/02/13 20:42:43 jkh Exp $ |
31 */ 32 |
33#include <sys/types.h> 34#include <sys/stat.h> 35#include <sys/file.h> 36#include <sys/time.h> 37#include <sys/resource.h> |
38#include <sys/wait.h> 39#include <a.out.h> |
40#include <err.h> 41#include <fcntl.h> 42#include <stdio.h> 43#include <stdlib.h> 44#include <string.h> 45#include <unistd.h> |
46 |
47void 48usage() 49{ |
50 extern char *__progname; 51 52 fprintf(stderr, "Usage: %s <filename> ...\n", __progname); 53 exit(1); |
54} 55 56int 57main(argc, argv) 58int argc; 59char *argv[]; 60{ |
61 int rval; |
62 int c; |
63 |
64 while ((c = getopt(argc, argv, "")) != EOF) { 65 switch (c) { 66 default: 67 usage(); |
68 /*NOTREACHED*/ |
69 } 70 } 71 argc -= optind; 72 argv += optind; 73 74 if (argc <= 0) { 75 usage(); |
76 /*NOTREACHED*/ |
77 } 78 79 /* ld.so magic */ 80 setenv("LD_TRACE_LOADED_OBJECTS", "", 1); 81 |
82 rval = 0; |
83 while (argc--) { 84 int fd; 85 struct exec hdr; 86 int status; 87 88 if ((fd = open(*argv, O_RDONLY, 0)) < 0) { |
89 warn("%s", *argv); |
90 rval |= 1; 91 argv++; 92 continue; 93 } 94 if (read(fd, &hdr, sizeof hdr) != sizeof hdr || |
95 !(N_GETFLAG(hdr) & EX_DYNAMIC) || 96 hdr.a_entry < __LDPGSZ) { 97 98 warnx("%s: not a dynamic executable", *argv); |
99 (void)close(fd); 100 rval |= 1; 101 argv++; 102 continue; 103 } 104 (void)close(fd); 105 106 printf("%s:\n", *argv); 107 fflush(stdout); 108 109 switch (fork()) { 110 case -1: |
111 err(1, "fork"); |
112 break; 113 default: |
114 if (wait(&status) <= 0) { 115 warn("wait"); 116 rval |= 1; 117 } else if (WIFSIGNALED(status)) { |
118 fprintf(stderr, "%s: signal %d\n", 119 *argv, WTERMSIG(status)); 120 rval |= 1; 121 } else if (WIFEXITED(status) && WEXITSTATUS(status)) { 122 fprintf(stderr, "%s: exit status %d\n", 123 *argv, WEXITSTATUS(status)); 124 rval |= 1; 125 } 126 break; 127 case 0: |
128 rval |= execl(*argv, *argv, NULL) != 0; |
129 perror(*argv); 130 _exit(1); 131 } 132 argv++; 133 } 134 135 return rval; 136} |