--- /dev/null
+/* A program to test BFD.\r
+ Copyright 2012 Free Software Foundation, Inc.\r
+\r
+ This file is part of the GNU Binutils.\r
+\r
+ This program is free software; you can redistribute it and/or modify\r
+ it under the terms of the GNU General Public License as published by\r
+ the Free Software Foundation; either version 3 of the License, or\r
+ (at your option) any later version.\r
+\r
+ This program is distributed in the hope that it will be useful,\r
+ but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ GNU General Public License for more details.\r
+\r
+ You should have received a copy of the GNU General Public License\r
+ along with this program; if not, write to the Free Software\r
+ Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,\r
+ MA 02110-1301, USA. */\r
+\r
+#include "sysdep.h"\r
+#include "bfd.h"\r
+\r
+static void\r
+die (const char *s)\r
+{\r
+ printf ("oops: %s\n", s);\r
+ exit (1);\r
+}\r
+\r
+static void *\r
+iovec_open (struct bfd *nbfd ATTRIBUTE_UNUSED, void *open_closure)\r
+{\r
+ return open_closure;\r
+}\r
+\r
+static file_ptr iovec_read (struct bfd *nbfd ATTRIBUTE_UNUSED,\r
+ void *stream, void *buf, file_ptr nbytes,\r
+ file_ptr offset)\r
+{\r
+ FILE* file = (FILE*) stream;\r
+\r
+ if (fseek(file, offset, SEEK_SET) != 0)\r
+ die ("fseek error");\r
+\r
+ return fread (buf, 1, nbytes, file);\r
+}\r
+\r
+static int\r
+iovec_stat (struct bfd *abfd ATTRIBUTE_UNUSED, \r
+ void *stream, struct stat *sb)\r
+{\r
+ return fstat (fileno ((FILE*) stream), sb);\r
+}\r
+\r
+static bfd_boolean\r
+check_format_any (struct bfd *abfd, bfd_format format)\r
+{\r
+ char** targets = NULL;\r
+\r
+ if (bfd_check_format_matches (abfd, format, &targets))\r
+ return TRUE;\r
+\r
+ if (targets)\r
+ {\r
+ bfd_find_target (targets[0], abfd);\r
+\r
+ return bfd_check_format (abfd, format);\r
+ }\r
+\r
+ return FALSE;\r
+}\r
+\r
+int\r
+main (int argc, const char** argv)\r
+{\r
+ FILE* file;\r
+ bfd *abfd, *mbfd;\r
+\r
+ if (argc < 2)\r
+ die ("Usage: test archivefile");\r
+\r
+ file = fopen(argv[1], "rb");\r
+ if (!file)\r
+ die ("file not found");\r
+\r
+ abfd = bfd_openr_iovec (argv[1], 0, iovec_open, file,\r
+ iovec_read, NULL, iovec_stat);\r
+ if (!abfd)\r
+ die ("error opening file");\r
+\r
+ if (!check_format_any (abfd, bfd_archive))\r
+ die ("not an archive");\r
+\r
+ mbfd = bfd_openr_next_archived_file (abfd, 0);\r
+ if (!mbfd)\r
+ die ("error opening archive member");\r
+\r
+ if (!bfd_close (mbfd))\r
+ die ("error closing archive member");\r
+\r
+ if (!bfd_close (abfd))\r
+ die ("error closing archive");\r
+\r
+ return 0;\r
+}\r