uncompress.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Uncompress applet for busybox (c) 2002 Glenn McGrath
  4. *
  5. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  6. */
  7. #include "busybox.h"
  8. #include "unarchive.h"
  9. #define GUNZIP_TO_STDOUT 1
  10. #define GUNZIP_FORCE 2
  11. int uncompress_main(int argc, char **argv)
  12. {
  13. int status = EXIT_SUCCESS;
  14. unsigned long flags;
  15. flags = getopt32(argc, argv, "cf");
  16. while (optind < argc) {
  17. char *compressed_file = argv[optind++];
  18. char *delete_path = NULL;
  19. char *uncompressed_file = NULL;
  20. int src_fd;
  21. int dst_fd;
  22. if (strcmp(compressed_file, "-") == 0) {
  23. src_fd = STDIN_FILENO;
  24. flags |= GUNZIP_TO_STDOUT;
  25. } else {
  26. src_fd = xopen(compressed_file, O_RDONLY);
  27. }
  28. /* Check that the input is sane. */
  29. if (isatty(src_fd) && ((flags & GUNZIP_FORCE) == 0)) {
  30. bb_error_msg_and_die
  31. ("compressed data not read from terminal. Use -f to force it.");
  32. }
  33. /* Set output filename and number */
  34. if (flags & GUNZIP_TO_STDOUT) {
  35. dst_fd = STDOUT_FILENO;
  36. } else {
  37. struct stat stat_buf;
  38. char *extension;
  39. uncompressed_file = xstrdup(compressed_file);
  40. extension = strrchr(uncompressed_file, '.');
  41. if (!extension || (strcmp(extension, ".Z") != 0)) {
  42. bb_error_msg_and_die("invalid extension");
  43. }
  44. *extension = '\0';
  45. /* Open output file */
  46. xstat(compressed_file, &stat_buf);
  47. dst_fd = xopen3(uncompressed_file,
  48. O_WRONLY | O_CREAT | O_TRUNC,
  49. stat_buf.st_mode);
  50. /* If unzip succeeds remove the old file */
  51. delete_path = compressed_file;
  52. }
  53. /* do the decompression, and cleanup */
  54. if ((xread_char(src_fd) != 0x1f) || (xread_char(src_fd) != 0x9d)) {
  55. bb_error_msg_and_die("invalid magic");
  56. }
  57. status = uncompress(src_fd, dst_fd);
  58. if ((status != EXIT_SUCCESS) && (uncompressed_file)) {
  59. /* Unzip failed, remove the uncomressed file instead of compressed file */
  60. delete_path = uncompressed_file;
  61. }
  62. if (dst_fd != STDOUT_FILENO) {
  63. close(dst_fd);
  64. }
  65. if (src_fd != STDIN_FILENO) {
  66. close(src_fd);
  67. }
  68. /* delete_path will be NULL if in test mode or from stdin */
  69. if (delete_path && (unlink(delete_path) == -1)) {
  70. bb_error_msg_and_die("cannot remove %s", delete_path);
  71. }
  72. free(uncompressed_file);
  73. }
  74. return status;
  75. }