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