Release 2.33.1
[external/binutils.git] / binutils / bfdtest2.c
1 /* A program to test BFD.\r
2    Copyright (C) 2012-2019 Free Software Foundation, Inc.
3 \r
4    This file is part of the GNU Binutils.\r
5 \r
6    This program is free software; you can redistribute it and/or modify\r
7    it under the terms of the GNU General Public License as published by\r
8    the Free Software Foundation; either version 3 of the License, or\r
9    (at your option) any later version.\r
10 \r
11    This program is distributed in the hope that it will be useful,\r
12    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14    GNU General Public License for more details.\r
15 \r
16    You should have received a copy of the GNU General Public License\r
17    along with this program; if not, write to the Free Software\r
18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,\r
19    MA 02110-1301, USA.  */\r
20 \r
21 #include "sysdep.h"\r
22 #include "bfd.h"\r
23 \r
24 static void\r
25 die (const char *s)\r
26 {\r
27   printf ("oops: %s\n", s);\r
28   exit (1);\r
29 }\r
30 \r
31 static void *\r
32 iovec_open (struct bfd *nbfd ATTRIBUTE_UNUSED, void *open_closure)\r
33 {\r
34   return open_closure;\r
35 }\r
36 \r
37 static file_ptr iovec_read (struct bfd *nbfd ATTRIBUTE_UNUSED,\r
38                             void *stream, void *buf, file_ptr nbytes,\r
39                             file_ptr offset)\r
40 {\r
41   FILE* file = (FILE*) stream;\r
42 \r
43   if (fseek(file, offset, SEEK_SET) != 0)\r
44     die ("fseek error");\r
45 \r
46   return fread (buf, 1, nbytes, file);\r
47 }\r
48 \r
49 static int\r
50 iovec_stat (struct bfd *abfd ATTRIBUTE_UNUSED, \r
51             void *stream, struct stat *sb)\r
52 {\r
53   return fstat (fileno ((FILE*) stream), sb);\r
54 }\r
55 \r
56 static bfd_boolean\r
57 check_format_any (struct bfd *abfd, bfd_format format)\r
58 {\r
59   char** targets = NULL;\r
60 \r
61   if (bfd_check_format_matches (abfd, format, &targets))\r
62     return TRUE;\r
63 \r
64   if (targets)\r
65     {\r
66       bfd_find_target (targets[0], abfd);\r
67 \r
68       return bfd_check_format (abfd, format);\r
69     }\r
70 \r
71   return FALSE;\r
72 }\r
73 \r
74 int\r
75 main (int argc, const char** argv)\r
76 {\r
77   FILE* file;\r
78   bfd *abfd, *mbfd;\r
79 \r
80   if (argc < 2)\r
81     die ("Usage: test archivefile");\r
82 \r
83   file = fopen(argv[1], "rb");\r
84   if (!file)\r
85     die ("file not found");\r
86 \r
87   abfd = bfd_openr_iovec (argv[1], 0, iovec_open, file,\r
88                           iovec_read, NULL, iovec_stat);\r
89   if (!abfd)\r
90     die ("error opening file");\r
91 \r
92   if (!check_format_any (abfd, bfd_archive))\r
93     die ("not an archive");\r
94 \r
95   mbfd = bfd_openr_next_archived_file (abfd, 0);\r
96   if (!mbfd)\r
97     die ("error opening archive member");\r
98 \r
99   if (!bfd_close (mbfd))\r
100     die ("error closing archive member");\r
101 \r
102   if (!bfd_close (abfd))\r
103     die ("error closing archive");\r
104 \r
105   return 0;\r
106 }\r