1 /* Stub for ldd script to print Linux libc4 dependencies.
2 Copyright (C) 1998-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 /* This code is based on the `ldd' program code from the Linux ld.so
20 package. */
21
22 #include <a.out.h>
23 #include <errno.h>
24 #include <error.h>
25 #include <libintl.h>
26 #include <locale.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 /* Get libc version number. */
33 #include "../version.h"
34
35 #define PACKAGE _libc_intl_domainname
36
37
38 int
main(int argc,char * argv[])39 main (int argc, char *argv[])
40 {
41 const char *filename;
42 size_t filename_len;
43 struct exec exec;
44 char *buf;
45 FILE *fp;
46
47 /* Set locale via LC_ALL. */
48 setlocale (LC_ALL, "");
49
50 /* Set the text message domain. */
51 textdomain (PACKAGE);
52
53 /* We expect exactly one argument. */
54 if (argc != 2)
55 return 1;
56
57 if (strcmp (argv[1], "--help") == 0)
58 {
59 printf (gettext ("Usage: lddlibc4 FILE\n\n"));
60 printf (gettext ("For bug reporting instructions, please see:\n\
61 %s.\n"), REPORT_BUGS_TO);
62 return 0;
63 }
64 else if (strcmp (argv[1], "--version") == 0)
65 {
66 printf ("lddlibc4 %s%s\n", PKGVERSION, VERSION);
67 printf (gettext ("\
68 Copyright (C) %s Free Software Foundation, Inc.\n\
69 This is free software; see the source for copying conditions. There is NO\n\
70 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
71 "), "2022");
72 return 0;
73 }
74
75 filename = argv[1];
76
77 /* First see whether this is really an a.out binary. */
78 fp = fopen (filename, "rb");
79 if (fp == NULL)
80 error (2, errno, gettext ("cannot open `%s'"), filename);
81
82 /* Read the program header. */
83 if (fread (&exec, sizeof exec, 1, fp) < 1)
84 error (2, errno, gettext ("cannot read header from `%s'"), filename);
85
86 /* Test for the magic numbers. */
87 if (N_MAGIC (exec) != ZMAGIC && N_MAGIC (exec) != QMAGIC
88 && N_MAGIC (exec) != OMAGIC)
89 exit (3);
90
91 /* We don't need the file open anymore. */
92 fclose (fp);
93
94 /* We must put `__LDD_ARGV0=<program-name>' in the environment. */
95 filename_len = strlen (filename);
96 buf = (char *) alloca (sizeof "__LDD_ARGV0=" + filename_len);
97 mempcpy (mempcpy (buf, "__LDD_ARGV0=", sizeof "__LDD_ARGV0=" - 1),
98 filename, filename_len + 1);
99 /* ...and put the value in the environment. */
100 putenv (buf);
101
102 /* Now we can execute the binary. */
103 return execv (filename, &argv[argc]) ? 4 : 0;
104 }
105