Skip to content

Instantly share code, notes, and snippets.

@NanXiao
Created October 17, 2017 03:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NanXiao/473d01ad9aab9c20a088973901c8c7a8 to your computer and use it in GitHub Desktop.
Save NanXiao/473d01ad9aab9c20a088973901c8c7a8 to your computer and use it in GitHub Desktop.
A simple bfd usage example
#define PACKAGE "bfd_test"
#include <iostream>
#include <bfd.h>
int main(int argc, char** argv)
{
if (argc == 1)
{
std::cout << "Lack input file!\n";
return 1;
}
bfd_init();
auto file = bfd_openr(argv[1], nullptr);
if (!file)
{
std::cout << "Open file failed!\n";
return 1;
}
if (!bfd_check_format (file, bfd_archive))
{
std::cout << argv[1] << " is not archive!\n";
return 1;
}
else
{
std::cout << argv[1] << " contains following files:\n";
}
bfd *arfile = nullptr;
bfd *last_arfile = nullptr;
while (1)
{
arfile = bfd_openr_next_archived_file(file, arfile);
if (arfile)
{
std::cout << bfd_get_filename(arfile) << '\n';
}
else
{
if (bfd_get_error () != bfd_error_no_more_archived_files)
{
std::cout << bfd_errmsg(bfd_get_error()) << '\n';
return 1;
}
break;
}
if (last_arfile)
{
bfd_close(last_arfile);
last_arfile = nullptr;
if (last_arfile == arfile)
{
break;
}
}
last_arfile = arfile;
}
if (last_arfile)
{
bfd_close(last_arfile);
}
bfd_close(file);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment