Changes from Paul Kranenburg which bring us into sync with his sources:
[unix-history] / gnu / usr.bin / ld / ldd / ldd.c
CommitLineData
1136f72d
PR
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
6a61ea88 17 * derived from this software without specific prior written permission
1136f72d
PR
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 *
1c8a0fd5 30 * $Id: ldd.c,v 1.3 1994/02/13 20:42:43 jkh Exp $
1136f72d
PR
31 */
32
1136f72d
PR
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>
1136f72d
PR
38#include <sys/wait.h>
39#include <a.out.h>
1c8a0fd5
PR
40#include <err.h>
41#include <fcntl.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
1136f72d
PR
46
47void
48usage()
49{
1c8a0fd5
PR
50 extern char *__progname;
51
52 fprintf(stderr, "Usage: %s <filename> ...\n", __progname);
53 exit(1);
1136f72d
PR
54}
55
56int
57main(argc, argv)
58int argc;
59char *argv[];
60{
1c8a0fd5 61 int rval;
1136f72d 62 int c;
1136f72d
PR
63
64 while ((c = getopt(argc, argv, "")) != EOF) {
65 switch (c) {
66 default:
67 usage();
1c8a0fd5 68 /*NOTREACHED*/
1136f72d
PR
69 }
70 }
71 argc -= optind;
72 argv += optind;
73
74 if (argc <= 0) {
75 usage();
1c8a0fd5 76 /*NOTREACHED*/
1136f72d
PR
77 }
78
79 /* ld.so magic */
80 setenv("LD_TRACE_LOADED_OBJECTS", "", 1);
81
1c8a0fd5 82 rval = 0;
1136f72d
PR
83 while (argc--) {
84 int fd;
85 struct exec hdr;
86 int status;
87
88 if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
1c8a0fd5 89 warn("%s", *argv);
1136f72d
PR
90 rval |= 1;
91 argv++;
92 continue;
93 }
94 if (read(fd, &hdr, sizeof hdr) != sizeof hdr ||
1c8a0fd5
PR
95 !(N_GETFLAG(hdr) & EX_DYNAMIC) ||
96 hdr.a_entry < __LDPGSZ) {
97
98 warnx("%s: not a dynamic executable", *argv);
1136f72d
PR
99 (void)close(fd);
100 rval |= 1;
101 argv++;
102 continue;
103 }
104 (void)close(fd);
105
106 printf("%s:\n", *argv);
6a61ea88 107 fflush(stdout);
1136f72d
PR
108
109 switch (fork()) {
110 case -1:
1c8a0fd5 111 err(1, "fork");
1136f72d
PR
112 break;
113 default:
1c8a0fd5
PR
114 if (wait(&status) <= 0) {
115 warn("wait");
116 rval |= 1;
117 } else if (WIFSIGNALED(status)) {
1136f72d
PR
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:
1c8a0fd5 128 rval |= execl(*argv, *argv, NULL) != 0;
1136f72d
PR
129 perror(*argv);
130 _exit(1);
131 }
132 argv++;
133 }
134
135 return rval;
136}