bunzip2.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. * Licensed under GPL v2, see file LICENSE in this tarball for details.
  6. */
  7. #include <fcntl.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include "busybox.h"
  13. #include "unarchive.h"
  14. #define BUNZIP2_OPT_STDOUT 1
  15. #define BUNZIP2_OPT_FORCE 2
  16. int bunzip2_main(int argc, char **argv)
  17. {
  18. char *filename;
  19. unsigned long opt;
  20. int status, src_fd, dst_fd;
  21. opt = bb_getopt_ulflags(argc, argv, "cf");
  22. /* Set input filename and number */
  23. filename = argv[optind];
  24. if ((filename) && (filename[0] != '-') && (filename[1] != '\0')) {
  25. /* Open input file */
  26. src_fd = bb_xopen(filename, O_RDONLY);
  27. } else {
  28. src_fd = STDIN_FILENO;
  29. filename = 0;
  30. }
  31. /* if called as bzcat force the stdout flag */
  32. if ((opt & BUNZIP2_OPT_STDOUT) || bb_applet_name[2] == 'c')
  33. filename = 0;
  34. /* Check that the input is sane. */
  35. if (isatty(src_fd) && (opt & BUNZIP2_OPT_FORCE) == 0) {
  36. bb_error_msg_and_die("compressed data not read from terminal. Use -f to force it.");
  37. }
  38. if (filename) {
  39. char *extension=filename+strlen(filename)-4;
  40. if (strcmp(extension, ".bz2") != 0) {
  41. bb_error_msg_and_die("Invalid extension");
  42. }
  43. *extension=0;
  44. dst_fd = bb_xopen(filename, O_WRONLY | O_CREAT);
  45. } else dst_fd = STDOUT_FILENO;
  46. status = uncompressStream(src_fd, dst_fd);
  47. if(filename) {
  48. if (!status) filename[strlen(filename)]='.';
  49. if (unlink(filename) < 0) {
  50. bb_error_msg_and_die("Couldn't remove %s", filename);
  51. }
  52. }
  53. return status;
  54. }
  55. /* vi:set ts=4: */