1
/* Copyright (C) 1999, 2000, 2002 Red Hat, Inc.
2
This file is part of Red Hat elfutils.
3
Written by Ulrich Drepper <drepper@redhat.com>, 1999.
5
Red Hat elfutils is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by the
7
Free Software Foundation; version 2 of the License.
9
Red Hat elfutils is distributed in the hope that it will be useful, but
10
WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
General Public License for more details.
14
You should have received a copy of the GNU General Public License along
15
with Red Hat elfutils; if not, write to the Free Software Foundation,
16
Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
18
Red Hat elfutils is an included package of the Open Invention Network.
19
An included package of the Open Invention Network is a package for which
20
Open Invention Network licensees cross-license their patents. No patent
21
license is granted, either expressly or impliedly, by designation as an
22
included package. Should you wish to participate in the Open Invention
23
Network licensing program, please visit www.openinventionnetwork.com
24
<http://www.openinventionnetwork.com>. */
36
#include <sys/param.h>
40
main (int argc, char *argv[])
52
/* Open the archive. */
53
fd = open (argv[1], O_RDONLY);
56
printf ("Cannot open input file: %m");
60
/* Set the ELF version. */
61
elf_version (EV_CURRENT);
63
/* Create an ELF descriptor. */
65
elf = elf_begin (fd, cmd, NULL);
68
printf ("Cannot create ELF descriptor: %s\n", elf_errmsg (-1));
72
/* If it is no archive punt. */
73
if (elf_kind (elf) != ELF_K_AR)
75
printf ("`%s' is no archive\n", argv[1]);
79
/* Get the elements of the archive one after the other. */
80
while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
82
/* The the header for this element. */
83
Elf_Arhdr *arhdr = elf_getarhdr (subelf);
87
printf ("cannot get arhdr: %s\n", elf_errmsg (-1));
91
if (strcmp (arhdr->ar_name, argv[2]) == 0)
95
/* Get the offset of the file in the archive. */
96
offset = elf_getbase (subelf);
100
Failed to get base address for the archive element: %s\n",
105
/* Open the output file. */
106
outfd = open (argv[3], O_CREAT | O_TRUNC | O_RDWR, 0666);
109
printf ("cannot open output file: %m");
113
/* Now write out the data. */
114
todo = arhdr->ar_size;
118
ssize_t n = pread (fd, buf, MIN (sizeof buf, todo), offset);
122
if (write (outfd, buf, n) != n)
124
puts ("Writing output failed");
132
/* Check whether all the date was read and written out. */
135
puts ("Reading archive member failed.");
139
/* Close the descriptors. */
140
if (elf_end (subelf) != 0 || elf_end (elf) != 0)
142
printf ("Freeing ELF descriptors failed: %s", elf_errmsg (-1));
153
/* Get next archive element. */
154
cmd = elf_next (subelf);
155
if (elf_end (subelf) != 0)
157
printf ("error while freeing sub-ELF descriptor: %s\n",
163
/* When we reach this point we haven't found the given file in the
165
printf ("File `%s' not found in archive\n", argv[2]);