Blame sysdeps/unix/sysv/linux/lddlibc4.c

Packit 6c4009
/* Stub for ldd script to print Linux libc4 dependencies.
Packit 6c4009
   Copyright (C) 1998-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* This code is based on the `ldd' program code from the Linux ld.so
Packit 6c4009
   package.  */
Packit 6c4009
Packit 6c4009
#include <a.out.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <error.h>
Packit 6c4009
#include <libintl.h>
Packit 6c4009
#include <locale.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
/* Get libc version number.  */
Packit 6c4009
#include "../version.h"
Packit 6c4009
Packit 6c4009
#define PACKAGE _libc_intl_domainname
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
  const char *filename;
Packit 6c4009
  size_t filename_len;
Packit 6c4009
  struct exec exec;
Packit 6c4009
  char *buf;
Packit 6c4009
  FILE *fp;
Packit 6c4009
Packit 6c4009
  /* Set locale via LC_ALL.  */
Packit 6c4009
  setlocale (LC_ALL, "");
Packit 6c4009
Packit 6c4009
  /* Set the text message domain.  */
Packit 6c4009
  textdomain (PACKAGE);
Packit 6c4009
Packit 6c4009
  /* We expect exactly one argument.  */
Packit 6c4009
  if (argc != 2)
Packit 6c4009
    return 1;
Packit 6c4009
Packit 6c4009
  if (strcmp (argv[1], "--help") == 0)
Packit 6c4009
    {
Packit 6c4009
      printf (gettext ("Usage: lddlibc4 FILE\n\n"));
Packit 6c4009
      printf (gettext ("For bug reporting instructions, please see:\n\
Packit 6c4009
%s.\n"), REPORT_BUGS_TO);
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
  else if (strcmp (argv[1], "--version") == 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("lddlibc4 %s%s\n", PKGVERSION, VERSION);
Packit 6c4009
      printf (gettext ("\
Packit 6c4009
Copyright (C) %s Free Software Foundation, Inc.\n\
Packit 6c4009
This is free software; see the source for copying conditions.  There is NO\n\
Packit 6c4009
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
Packit 6c4009
"), "2018");
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  filename = argv[1];
Packit 6c4009
Packit 6c4009
  /* First see whether this is really an a.out binary.  */
Packit 6c4009
  fp = fopen (filename, "rb");
Packit 6c4009
  if (fp == NULL)
Packit 6c4009
    error (2, errno, gettext ("cannot open `%s'"), filename);
Packit 6c4009
Packit 6c4009
  /* Read the program header.  */
Packit 6c4009
  if (fread (&exec, sizeof exec, 1, fp) < 1)
Packit 6c4009
    error (2, errno, gettext ("cannot read header from `%s'"), filename);
Packit 6c4009
Packit 6c4009
  /* Test for the magic numbers.  */
Packit 6c4009
  if (N_MAGIC (exec) != ZMAGIC && N_MAGIC (exec) != QMAGIC
Packit 6c4009
      && N_MAGIC (exec) != OMAGIC)
Packit 6c4009
    exit (3);
Packit 6c4009
Packit 6c4009
  /* We don't need the file open anymore.  */
Packit 6c4009
  fclose (fp);
Packit 6c4009
Packit 6c4009
  /* We must put `__LDD_ARGV0=<program-name>' in the environment.  */
Packit 6c4009
  filename_len = strlen (filename);
Packit 6c4009
  buf = (char *) alloca (sizeof "__LDD_ARGV0=" + filename_len);
Packit 6c4009
  mempcpy (mempcpy (buf, "__LDD_ARGV0=", sizeof "__LDD_ARGV0=" - 1),
Packit 6c4009
	   filename, filename_len + 1);
Packit 6c4009
  /* ...and put the value in the environment.  */
Packit 6c4009
  putenv (buf);
Packit 6c4009
Packit 6c4009
  /* Now we can execute the binary.  */
Packit 6c4009
  return execv (filename, &argv[argc]) ? 4 : 0;
Packit 6c4009
}