Back to index

glibc  2.9
lddlibc4.c
Go to the documentation of this file.
00001 /* Stub for ldd script to print Linux libc4 dependencies.
00002    Copyright (C) 1998, 2005 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 /* This code is based on the `ldd' program code from the Linux ld.so
00022    package.  */
00023 
00024 #include <a.out.h>
00025 #include <errno.h>
00026 #include <error.h>
00027 #include <libintl.h>
00028 #include <locale.h>
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032 #include <unistd.h>
00033 
00034 /* Get libc version number.  */
00035 #include "../version.h"
00036 
00037 #define PACKAGE _libc_intl_domainname
00038 
00039 
00040 int
00041 main (int argc, char *argv[])
00042 {
00043   const char *filename;
00044   size_t filename_len;
00045   struct exec exec;
00046   char *buf;
00047   FILE *fp;
00048 
00049   /* Set locale via LC_ALL.  */
00050   setlocale (LC_ALL, "");
00051 
00052   /* Set the text message domain.  */
00053   textdomain (PACKAGE);
00054 
00055   /* We expect exactly one argument.  */
00056   if (argc != 2)
00057     return 1;
00058 
00059   filename = argv[1];
00060 
00061   /* First see whether this is really an a.out binary.  */
00062   fp = fopen (filename, "rb");
00063   if (fp == NULL)
00064     error (2, errno, gettext ("cannot open `%s'"), filename);
00065 
00066   /* Read the program header.  */
00067   if (fread (&exec, sizeof exec, 1, fp) < 1)
00068     error (2, errno, gettext ("cannot read header from `%s'"), filename);
00069 
00070   /* Test for the magic numbers.  */
00071   if (N_MAGIC (exec) != ZMAGIC && N_MAGIC (exec) != QMAGIC
00072       && N_MAGIC (exec) != OMAGIC)
00073     exit (3);
00074 
00075   /* We don't need the file open anymore.  */
00076   fclose (fp);
00077 
00078   /* We must put `__LDD_ARGV0=<program-name>' in the environment.  */
00079   filename_len = strlen (filename);
00080   buf = (char *) alloca (sizeof "__LDD_ARGV0=" + filename_len);
00081   mempcpy (mempcpy (buf, "__LDD_ARGV0=", sizeof "__LDD_ARGV0=" - 1),
00082           filename, filename_len + 1);
00083   /* ...and put the value in the environment.  */
00084   putenv (buf);
00085 
00086   /* Now we can execute the binary.  */
00087   return execv (filename, &argv[argc]) ? 4 : 0;
00088 }