uncompress.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Uncompress applet for busybox (c) 2002 Glenn McGrath
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #include "libbb.h"
  26. #include "unarchive.h"
  27. #define GUNZIP_TO_STDOUT 1
  28. #define GUNZIP_FORCE 2
  29. int uncompress_main(int argc, char **argv)
  30. {
  31. int status = EXIT_SUCCESS;
  32. unsigned long flags;
  33. flags = bb_getopt_ulflags(argc, argv, "cf");
  34. while (optind < argc) {
  35. const char *compressed_file = argv[optind++];
  36. const char *delete_path = NULL;
  37. char *uncompressed_file = NULL;
  38. int src_fd;
  39. int dst_fd;
  40. if (strcmp(compressed_file, "-") == 0) {
  41. src_fd = STDIN_FILENO;
  42. flags |= GUNZIP_TO_STDOUT;
  43. } else {
  44. src_fd = bb_xopen(compressed_file, O_RDONLY);
  45. }
  46. /* Check that the input is sane. */
  47. if (isatty(src_fd) && ((flags & GUNZIP_FORCE) == 0)) {
  48. bb_error_msg_and_die
  49. ("compressed data not read from terminal. Use -f to force it.");
  50. }
  51. /* Set output filename and number */
  52. if (flags & GUNZIP_TO_STDOUT) {
  53. dst_fd = STDOUT_FILENO;
  54. } else {
  55. struct stat stat_buf;
  56. char *extension;
  57. uncompressed_file = bb_xstrdup(compressed_file);
  58. extension = strrchr(uncompressed_file, '.');
  59. if (!extension || (strcmp(extension, ".Z") != 0)) {
  60. bb_error_msg_and_die("Invalid extension");
  61. }
  62. *extension = '\0';
  63. /* Open output file */
  64. dst_fd = bb_xopen(uncompressed_file, O_WRONLY | O_CREAT);
  65. /* Set permissions on the file */
  66. stat(compressed_file, &stat_buf);
  67. chmod(uncompressed_file, stat_buf.st_mode);
  68. /* If unzip succeeds remove the old file */
  69. delete_path = compressed_file;
  70. }
  71. /* do the decompression, and cleanup */
  72. if ((bb_xread_char(src_fd) != 0x1f) || (bb_xread_char(src_fd) != 0x9d)) {
  73. bb_error_msg_and_die("Invalid magic");
  74. }
  75. status = uncompress(src_fd, dst_fd);
  76. if ((status != EXIT_SUCCESS) && (uncompressed_file)) {
  77. /* Unzip failed, remove the uncomressed file instead of compressed file */
  78. delete_path = uncompressed_file;
  79. }
  80. if (dst_fd != STDOUT_FILENO) {
  81. close(dst_fd);
  82. }
  83. if (src_fd != STDIN_FILENO) {
  84. close(src_fd);
  85. }
  86. /* delete_path will be NULL if in test mode or from stdin */
  87. if (delete_path && (unlink(delete_path) == -1)) {
  88. bb_error_msg_and_die("Couldn't remove %s", delete_path);
  89. }
  90. free(uncompressed_file);
  91. }
  92. return status;
  93. }