ldd.c (1153) | ldd.c (1741) |
---|---|
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 * | 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.2 1993/11/09 04:19:27 paul Exp $ | 30 * $Id: ldd.c,v 1.3 1994/02/13 20:42:43 jkh Exp $ |
31 */ 32 | 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> | 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> |
43#include <fcntl.h> | |
44#include <sys/wait.h> 45#include <a.out.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 | 46 |
47static char *progname; 48 | |
49void 50usage() 51{ | 47void 48usage() 49{ |
52 fprintf(stderr, "Usage: %s <filename> ...\n", progname); | 50 extern char *__progname; 51 52 fprintf(stderr, "Usage: %s <filename> ...\n", __progname); 53 exit(1); |
53} 54 55int 56main(argc, argv) 57int argc; 58char *argv[]; 59{ | 54} 55 56int 57main(argc, argv) 58int argc; 59char *argv[]; 60{ |
60 int rval = 0; | 61 int rval; |
61 int c; | 62 int c; |
62 extern int optind; | |
63 | 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(); | 64 while ((c = getopt(argc, argv, "")) != EOF) { 65 switch (c) { 66 default: 67 usage(); |
73 exit(1); | 68 /*NOTREACHED*/ |
74 } 75 } 76 argc -= optind; 77 argv += optind; 78 79 if (argc <= 0) { 80 usage(); | 69 } 70 } 71 argc -= optind; 72 argv += optind; 73 74 if (argc <= 0) { 75 usage(); |
81 exit(1); | 76 /*NOTREACHED*/ |
82 } 83 84 /* ld.so magic */ 85 setenv("LD_TRACE_LOADED_OBJECTS", "", 1); 86 | 77 } 78 79 /* ld.so magic */ 80 setenv("LD_TRACE_LOADED_OBJECTS", "", 1); 81 |
82 rval = 0; |
|
87 while (argc--) { 88 int fd; 89 struct exec hdr; 90 int status; 91 92 if ((fd = open(*argv, O_RDONLY, 0)) < 0) { | 83 while (argc--) { 84 int fd; 85 struct exec hdr; 86 int status; 87 88 if ((fd = open(*argv, O_RDONLY, 0)) < 0) { |
93 perror(*argv); | 89 warn("%s", *argv); |
94 rval |= 1; 95 argv++; 96 continue; 97 } 98 if (read(fd, &hdr, sizeof hdr) != sizeof hdr || | 90 rval |= 1; 91 argv++; 92 continue; 93 } 94 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); | 95 !(N_GETFLAG(hdr) & EX_DYNAMIC) || 96 hdr.a_entry < __LDPGSZ) { 97 98 warnx("%s: not a dynamic executable", *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: | 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: |
114 perror("fork"); 115 exit(1); | 111 err(1, "fork"); |
116 break; 117 default: | 112 break; 113 default: |
118 if (wait(&status) <= 0) 119 perror("wait"); 120 121 if (WIFSIGNALED(status)) { | 114 if (wait(&status) <= 0) { 115 warn("wait"); 116 rval |= 1; 117 } else 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: | 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: |
132 rval != execl(*argv, *argv, NULL) != 0; | 128 rval |= execl(*argv, *argv, NULL) != 0; |
133 perror(*argv); 134 _exit(1); 135 } 136 argv++; 137 } 138 139 return rval; 140} | 129 perror(*argv); 130 _exit(1); 131 } 132 argv++; 133 } 134 135 return rval; 136} |