Blame tests/arextract.c

Packit 032894
/* Copyright (C) 1999, 2000, 2002 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
   Written by Ulrich Drepper <drepper@redhat.com>, 1999.
Packit 032894
Packit 032894
   This file is free software; you can redistribute it and/or modify
Packit 032894
   it under the terms of the GNU General Public License as published by
Packit 032894
   the Free Software Foundation; either version 3 of the License, or
Packit 032894
   (at your option) any later version.
Packit 032894
Packit 032894
   elfutils is distributed in the hope that it will be useful, but
Packit 032894
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 032894
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 032894
   GNU General Public License for more details.
Packit 032894
Packit 032894
   You should have received a copy of the GNU General Public License
Packit 032894
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 032894
Packit 032894
#ifdef HAVE_CONFIG_H
Packit 032894
# include <config.h>
Packit 032894
#endif
Packit 032894
Packit 032894
#include <fcntl.h>
Packit 032894
#include <gelf.h>
Packit 032894
#include <stdio.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include <string.h>
Packit 032894
#include <unistd.h>
Packit 032894
#include <system.h>
Packit 032894
Packit 032894
Packit 032894
int
Packit 032894
main (int argc, char *argv[])
Packit 032894
{
Packit 032894
  int fd;
Packit 032894
  Elf *elf;
Packit 032894
  Elf *subelf;
Packit 032894
  Elf_Cmd cmd;
Packit 032894
  off_t offset;
Packit 032894
  size_t todo;
Packit 032894
Packit 032894
  if (argc < 4)
Packit 032894
    exit (1);
Packit 032894
Packit 032894
  /* Open the archive.  */
Packit 032894
  fd = open (argv[1], O_RDONLY);
Packit 032894
  if (fd == -1)
Packit 032894
    {
Packit 032894
      printf ("Cannot open input file: %m");
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  /* Set the ELF version.  */
Packit 032894
  elf_version (EV_CURRENT);
Packit 032894
Packit 032894
  /* Create an ELF descriptor.  */
Packit 032894
  cmd = ELF_C_READ;
Packit 032894
  elf = elf_begin (fd, cmd, NULL);
Packit 032894
  if (elf == NULL)
Packit 032894
    {
Packit 032894
      printf ("Cannot create ELF descriptor: %s\n", elf_errmsg (-1));
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  /* If it is no archive punt.  */
Packit 032894
  if (elf_kind (elf) != ELF_K_AR)
Packit 032894
    {
Packit 032894
      printf ("`%s' is no archive\n", argv[1]);
Packit 032894
      exit (1);
Packit 032894
    }
Packit 032894
Packit 032894
  /* Get the elements of the archive one after the other.  */
Packit 032894
  while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
Packit 032894
    {
Packit 032894
      /* The the header for this element.  */
Packit 032894
      Elf_Arhdr *arhdr = elf_getarhdr (subelf);
Packit 032894
Packit 032894
      if (arhdr == NULL)
Packit 032894
	{
Packit 032894
	  printf ("cannot get arhdr: %s\n", elf_errmsg (-1));
Packit 032894
	  exit (1);
Packit 032894
	}
Packit 032894
Packit 032894
      if (strcmp (arhdr->ar_name, argv[2]) == 0)
Packit 032894
	{
Packit 032894
	  int outfd;
Packit 032894
Packit 032894
	  /* Get the offset of the file in the archive.  */
Packit 032894
	  offset = elf_getbase (subelf);
Packit 032894
	  if (offset == -1)
Packit 032894
	    {
Packit 032894
	      printf ("\
Packit 032894
Failed to get base address for the archive element: %s\n",
Packit 032894
		      elf_errmsg (-1));
Packit 032894
	      exit (1);
Packit 032894
	    }
Packit 032894
Packit 032894
	  /* Open the output file.  */
Packit 032894
	  outfd = open (argv[3], O_CREAT | O_TRUNC | O_RDWR, 0666);
Packit 032894
	  if (outfd == -1)
Packit 032894
	    {
Packit 032894
	      printf ("cannot open output file: %m");
Packit 032894
	      exit (1);
Packit 032894
	    }
Packit 032894
Packit 032894
	  /* Now write out the data.  */
Packit 032894
	  todo = arhdr->ar_size;
Packit 032894
	  while (todo > 0)
Packit 032894
	    {
Packit 032894
	      char buf[1024];
Packit 032894
	      ssize_t n = pread (fd, buf, MIN (sizeof buf, todo), offset);
Packit 032894
	      if (n == 0)
Packit 032894
		break;
Packit 032894
Packit 032894
	      if (write (outfd, buf, n) != n)
Packit 032894
		{
Packit 032894
		  puts ("Writing output failed");
Packit 032894
		  exit (1);
Packit 032894
		}
Packit 032894
Packit 032894
	      offset += n;
Packit 032894
	      todo -= n;
Packit 032894
	    }
Packit 032894
Packit 032894
	  /* Check whether all the date was read and written out.  */
Packit 032894
	  if (todo != 0)
Packit 032894
	    {
Packit 032894
	      puts ("Reading archive member failed.");
Packit 032894
	      exit (1);
Packit 032894
	    }
Packit 032894
Packit 032894
	  /* Close the descriptors.  */
Packit 032894
	  if (elf_end (subelf) != 0 || elf_end (elf) != 0)
Packit 032894
	    {
Packit 032894
	      printf ("Freeing ELF descriptors failed: %s", elf_errmsg (-1));
Packit 032894
	      exit (1);
Packit 032894
	    }
Packit 032894
Packit 032894
	  close (outfd);
Packit 032894
	  close (fd);
Packit 032894
Packit 032894
	  /* All went well.  */
Packit 032894
	  exit (0);
Packit 032894
	}
Packit 032894
Packit 032894
      /* Get next archive element.  */
Packit 032894
      cmd = elf_next (subelf);
Packit 032894
      if (elf_end (subelf) != 0)
Packit 032894
	{
Packit 032894
	  printf ("error while freeing sub-ELF descriptor: %s\n",
Packit 032894
		  elf_errmsg (-1));
Packit 032894
	  exit (1);
Packit 032894
	}
Packit 032894
    }
Packit 032894
Packit 032894
  /* When we reach this point we haven't found the given file in the
Packit 032894
     archive.  */
Packit 032894
  printf ("File `%s' not found in archive\n", argv[2]);
Packit 032894
  exit (1);
Packit 032894
}