ar.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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, 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. if (opt & AR_CTX_PRINT) {
  47. archive_handle->action_data = data_extract_to_stdout;
  48. }
  49. if (opt & AR_CTX_LIST) {
  50. archive_handle->action_header = header_list;
  51. }
  52. if (opt & AR_CTX_EXTRACT) {
  53. archive_handle->action_data = data_extract_all;
  54. }
  55. if (opt & AR_OPT_PRESERVE_DATE) {
  56. archive_handle->ah_flags |= ARCHIVE_PRESERVE_DATE;
  57. }
  58. if (opt & AR_OPT_VERBOSE) {
  59. archive_handle->action_header = header_verbose_list_ar;
  60. }
  61. if (opt & AR_OPT_CREATE) {
  62. bb_error_msg_and_die(msg_unsupported_err, "creation");
  63. }
  64. if (opt & AR_OPT_INSERT) {
  65. bb_error_msg_and_die(msg_unsupported_err, "insertion");
  66. }
  67. archive_handle->src_fd = xopen(argv[optind++], O_RDONLY);
  68. while (optind < argc) {
  69. archive_handle->filter = filter_accept_list;
  70. llist_add_to(&(archive_handle->accept), argv[optind++]);
  71. }
  72. unpack_ar_archive(archive_handle);
  73. return EXIT_SUCCESS;
  74. }