3
0

ar.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini ar implementation for busybox
  4. *
  5. * Copyright (C) 2000 by Glenn McGrath
  6. *
  7. * Based in part on BusyBox tar, Debian dpkg-deb and GNU ar.
  8. *
  9. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  10. *
  11. * There is no single standard to adhere to so ar may not portable
  12. * between different systems
  13. * http://www.unix-systems.org/single_unix_specification_v2/xcu/ar.html
  14. */
  15. #include "libbb.h"
  16. #include "unarchive.h"
  17. static void FAST_FUNC header_verbose_list_ar(const file_header_t *file_header)
  18. {
  19. const char *mode = bb_mode_string(file_header->mode);
  20. char *mtime;
  21. mtime = ctime(&file_header->mtime);
  22. mtime[16] = ' ';
  23. memmove(&mtime[17], &mtime[20], 4);
  24. mtime[21] = '\0';
  25. printf("%s %d/%d%7d %s %s\n", &mode[1], file_header->uid, file_header->gid,
  26. (int) file_header->size, &mtime[4], file_header->name);
  27. }
  28. #define AR_CTX_PRINT 0x01
  29. #define AR_CTX_LIST 0x02
  30. #define AR_CTX_EXTRACT 0x04
  31. #define AR_OPT_PRESERVE_DATE 0x08
  32. #define AR_OPT_VERBOSE 0x10
  33. #define AR_OPT_CREATE 0x20
  34. #define AR_OPT_INSERT 0x40
  35. int ar_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  36. int ar_main(int argc UNUSED_PARAM, char **argv)
  37. {
  38. static const char msg_unsupported_err[] ALIGN1 =
  39. "archive %s is not supported";
  40. archive_handle_t *archive_handle;
  41. unsigned opt;
  42. archive_handle = init_handle();
  43. /* Prepend '-' to the first argument if required */
  44. opt_complementary = "--:p:t:x:-1:p--tx:t--px:x--pt";
  45. opt = getopt32(argv, "ptxovcr");
  46. argv += optind;
  47. if (opt & AR_CTX_PRINT) {
  48. archive_handle->action_data = data_extract_to_stdout;
  49. }
  50. if (opt & AR_CTX_LIST) {
  51. archive_handle->action_header = header_list;
  52. }
  53. if (opt & AR_CTX_EXTRACT) {
  54. archive_handle->action_data = data_extract_all;
  55. }
  56. if (opt & AR_OPT_PRESERVE_DATE) {
  57. archive_handle->ah_flags |= ARCHIVE_RESTORE_DATE;
  58. }
  59. if (opt & AR_OPT_VERBOSE) {
  60. archive_handle->action_header = header_verbose_list_ar;
  61. }
  62. if (opt & AR_OPT_CREATE) {
  63. bb_error_msg_and_die(msg_unsupported_err, "creation");
  64. }
  65. if (opt & AR_OPT_INSERT) {
  66. bb_error_msg_and_die(msg_unsupported_err, "insertion");
  67. }
  68. archive_handle->src_fd = xopen(*argv++, O_RDONLY);
  69. while (*argv) {
  70. archive_handle->filter = filter_accept_list;
  71. llist_add_to(&archive_handle->accept, *argv++);
  72. }
  73. unpack_ar_archive(archive_handle);
  74. return EXIT_SUCCESS;
  75. }