1/* A program to test BFD. 2 Copyright (C) 2012-2017 Free Software Foundation, Inc. 3 4 This file is part of the GNU Binutils. 5 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. 10 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. 15 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. */ 20 21#include "sysdep.h" 22#include "bfd.h" 23 24static void 25die (const char *s) 26{ 27 printf ("oops: %s\n", s); 28 exit (1); 29} 30 31static void * 32iovec_open (struct bfd *nbfd ATTRIBUTE_UNUSED, void *open_closure) 33{ 34 return open_closure; 35} 36 37static file_ptr iovec_read (struct bfd *nbfd ATTRIBUTE_UNUSED, 38 void *stream, void *buf, file_ptr nbytes, 39 file_ptr offset) 40{ 41 FILE* file = (FILE*) stream; 42 43 if (fseek(file, offset, SEEK_SET) != 0) 44 die ("fseek error"); 45 46 return fread (buf, 1, nbytes, file); 47} 48 49static int 50iovec_stat (struct bfd *abfd ATTRIBUTE_UNUSED, 51 void *stream, struct stat *sb) 52{ 53 return fstat (fileno ((FILE*) stream), sb); 54} 55 56static bfd_boolean 57check_format_any (struct bfd *abfd, bfd_format format) 58{ 59 char** targets = NULL; 60 61 if (bfd_check_format_matches (abfd, format, &targets)) 62 return TRUE; 63 64 if (targets) 65 { 66 bfd_find_target (targets[0], abfd); 67 68 return bfd_check_format (abfd, format); 69 } 70 71 return FALSE; 72} 73 74int 75main (int argc, const char** argv) 76{ 77 FILE* file; 78 bfd *abfd, *mbfd; 79 80 if (argc < 2) 81 die ("Usage: test archivefile"); 82 83 file = fopen(argv[1], "rb"); 84 if (!file) 85 die ("file not found"); 86 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"); 91 92 if (!check_format_any (abfd, bfd_archive)) 93 die ("not an archive"); 94 95 mbfd = bfd_openr_next_archived_file (abfd, 0); 96 if (!mbfd) 97 die ("error opening archive member"); 98 99 if (!bfd_close (mbfd)) 100 die ("error closing archive member"); 101 102 if (!bfd_close (abfd)) 103 die ("error closing archive"); 104 105 return 0; 106} 107