]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/lddlibc4.c
Add stub for ldd to examine libc binaries.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / lddlibc4.c
1 /* Stub for ldd script to print Linux libc4 dependencies.
2 Copyright (C) 1998 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 /* This code is based on the `ldd' program code from the Linux ld.so
22 package. */
23
24 #include <a.out.h>
25 #include <errno.h>
26 #include <libintl.h>
27 #include <locale.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32
33 /* Get libc version number. */
34 #include "../version.h"
35
36 #define PACKAGE _libc_intl_domainname
37
38
39 int
40 main (int argc, char *argv[])
41 {
42 const char *filename;
43 size_t filename_len;
44 struct exec exec;
45 char *buf;
46 FILE *fp;
47
48 /* Set locale via LC_ALL. */
49 setlocale (LC_ALL, "");
50
51 /* Set the text message domain. */
52 textdomain (PACKAGE);
53
54 /* We expect exactly one argument. */
55 if (argc != 2)
56 return 1;
57
58 filename = argv[1];
59
60 /* First see whether this is really an a.out binary. */
61 fp = fopen (filename, "rb");
62 if (fp == NULL)
63 error (2, errno, gettext ("cannot open `%s'"), filename);
64
65 /* Read the program header. */
66 if (fread (&exec, sizeof exec, 1, fp) < 1)
67 error (2, errno, gettext ("cannot read header from `%s'"), filename);
68
69 /* Test for the magic numbers. */
70 if (N_MAGIC (exec) != ZMAGIC && N_MAGIC (exec) != QMAGIC
71 && N_MAGIC (exec) != OMAGIC)
72 exit (3);
73
74 /* We don't need the file open anymore. */
75 fclose (fp);
76
77 /* We must put `__LDD=ARGV0=<program-name>' in the environment. */
78 filename_len = strlen (filename);
79 buf = (char *) alloca (sizeof "__LDD_ARGV0=" + filename_len);
80 mempcpy (mempcpy (buf, "__LDD_ARGV0=", sizeof "__LDD_ARGV0=" - 1),
81 filename, filename_len);
82 /* ...and put the value in the environment. */
83 putenv (buf);
84
85 /* Now we can execute the binary. */
86 return execl (filename, NULL) ? 4 : 0;
87 }