3
0

bunzip2.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Modified for busybox by Glenn McGrath <bug1@iinet.net.au>
  3. * Added support output to stdout by Thomas Lundquist <thomasez@zelow.no>
  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 <fcntl.h>
  20. #include <getopt.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include "busybox.h"
  26. #include "unarchive.h"
  27. #define BUNZIP2_OPT_STDOUT 1
  28. #define BUNZIP2_OPT_FORCE 2
  29. int bunzip2_main(int argc, char **argv)
  30. {
  31. char *compressed_name;
  32. /* Note: Ignore the warning about save_name being used uninitialized.
  33. * That is not the case, but gcc has trouble working that out... */
  34. char *save_name;
  35. unsigned long opt;
  36. int status;
  37. int src_fd;
  38. int dst_fd;
  39. opt = bb_getopt_ulflags(argc, argv, "cf");
  40. /* if called as bzcat force the stdout flag */
  41. if (bb_applet_name[2] == 'c') {
  42. opt |= BUNZIP2_OPT_STDOUT;
  43. }
  44. /* Set input filename and number */
  45. compressed_name = argv[optind];
  46. if ((compressed_name) && (compressed_name[0] != '-') && (compressed_name[1] != '\0')) {
  47. /* Open input file */
  48. src_fd = bb_xopen(compressed_name, O_RDONLY);
  49. } else {
  50. src_fd = STDIN_FILENO;
  51. opt |= BUNZIP2_OPT_STDOUT;
  52. }
  53. /* Check that the input is sane. */
  54. if (isatty(src_fd) && (opt & BUNZIP2_OPT_FORCE) == 0) {
  55. bb_error_msg_and_die("compressed data not read from terminal. Use -f to force it.");
  56. }
  57. if (opt & BUNZIP2_OPT_STDOUT) {
  58. dst_fd = STDOUT_FILENO;
  59. } else {
  60. int len = strlen(compressed_name) - 4;
  61. if (strcmp(compressed_name + len, ".bz2") != 0) {
  62. bb_error_msg_and_die("Invalid extension");
  63. }
  64. save_name = bb_xstrndup(compressed_name, len);
  65. dst_fd = bb_xopen(save_name, O_WRONLY | O_CREAT);
  66. }
  67. status = uncompressStream(src_fd, dst_fd);
  68. if(!(opt & BUNZIP2_OPT_STDOUT)) {
  69. char *delete_name;
  70. if (status) {
  71. delete_name = save_name;
  72. } else {
  73. delete_name = compressed_name;
  74. }
  75. if (unlink(delete_name) < 0) {
  76. bb_error_msg_and_die("Couldn't remove %s", delete_name);
  77. }
  78. }
  79. return status;
  80. }