bunzip2.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include "lib/bzlib.h"
  13. static Biobuf bin;
  14. static int debug;
  15. static int verbose;
  16. static char *delfile;
  17. static char *infile;
  18. static int bunzipf(char *file, int stdout);
  19. static int bunzip(int ofd, char *ofile, Biobuf *bin);
  20. void
  21. usage(void)
  22. {
  23. fprint(2, "usage: bunzip2 [-cvD] [file ...]\n");
  24. exits("usage");
  25. }
  26. void
  27. main(int argc, char **argv)
  28. {
  29. int i, ok, stdout;
  30. stdout = 0;
  31. ARGBEGIN{
  32. case 'D':
  33. debug++;
  34. break;
  35. case 'c':
  36. stdout++;
  37. break;
  38. case 'v':
  39. verbose++;
  40. break;
  41. default:
  42. usage();
  43. }ARGEND
  44. if(argc == 0){
  45. Binit(&bin, 0, OREAD);
  46. infile = "<stdin>";
  47. ok = bunzip(1, "<stdout>", &bin);
  48. }else{
  49. ok = 1;
  50. for(i = 0; i < argc; i++)
  51. ok &= bunzipf(argv[i], stdout);
  52. }
  53. exits(ok ? nil: "errors");
  54. }
  55. static int
  56. bunzipf(char *file, int stdout)
  57. {
  58. char ofile[64], *s;
  59. int ofd, ifd, ok;
  60. infile = file;
  61. ifd = open(file, OREAD);
  62. if(ifd < 0){
  63. fprint(2, "bunzip2: can't open %s: %r\n", file);
  64. return 0;
  65. }
  66. Binit(&bin, ifd, OREAD);
  67. if(Bgetc(&bin) != 'B' || Bgetc(&bin) != 'Z' || Bgetc(&bin) != 'h'){
  68. fprint(2, "bunzip2: %s is not a bzip2 file\n", file);
  69. Bterm(&bin);
  70. close(ifd);
  71. return 0;
  72. }
  73. Bungetc(&bin);
  74. Bungetc(&bin);
  75. Bungetc(&bin);
  76. if(stdout){
  77. ofd = 1;
  78. strcpy(ofile, "<stdout>");
  79. }else{
  80. s = strrchr(file, '/');
  81. if(s != nil)
  82. s++;
  83. else
  84. s = file;
  85. strecpy(ofile, ofile+sizeof ofile, s);
  86. s = strrchr(ofile, '.');
  87. if(s != nil && s != ofile && strcmp(s, ".bz2") == 0)
  88. *s = '\0';
  89. else if(s != nil && (strcmp(s, ".tbz") == 0 || strcmp(s, ".tbz2") == 0))
  90. strcpy(s, ".tar");
  91. else if(strcmp(file, ofile) == 0){
  92. fprint(2, "bunzip2: can't overwrite %s\n", file);
  93. Bterm(&bin);
  94. close(ifd);
  95. return 0;
  96. }
  97. ofd = create(ofile, OWRITE, 0666);
  98. if(ofd < 0){
  99. fprint(2, "bunzip2: can't create %s: %r\n", ofile);
  100. Bterm(&bin);
  101. close(ifd);
  102. return 0;
  103. }
  104. delfile = ofile;
  105. }
  106. ok = bunzip(ofd, ofile, &bin);
  107. Bterm(&bin);
  108. close(ifd);
  109. if(!ok){
  110. fprint(2, "bunzip2: can't write %s: %r\n", ofile);
  111. if(delfile)
  112. remove(delfile);
  113. }
  114. delfile = nil;
  115. if(!stdout && ofd >= 0)
  116. close(ofd);
  117. return ok;
  118. }
  119. static int
  120. bunzip(int ofd, char *ofile, Biobuf *bin)
  121. {
  122. int e, n, done, onemore;
  123. char buf[8192];
  124. char obuf[8192];
  125. Biobuf bout;
  126. bz_stream strm;
  127. USED(ofile);
  128. memset(&strm, 0, sizeof strm);
  129. BZ2_bzDecompressInit(&strm, verbose, 0);
  130. strm.next_in = buf;
  131. strm.avail_in = 0;
  132. strm.next_out = obuf;
  133. strm.avail_out = sizeof obuf;
  134. done = 0;
  135. Binit(&bout, ofd, OWRITE);
  136. /*
  137. * onemore is a crummy hack to go 'round the loop
  138. * once after we finish, to flush the output buffer.
  139. */
  140. onemore = 1;
  141. SET(e);
  142. do {
  143. if(!done && strm.avail_in < sizeof buf) {
  144. if(strm.avail_in)
  145. memmove(buf, strm.next_in, strm.avail_in);
  146. n = Bread(bin, buf+strm.avail_in, sizeof(buf)-strm.avail_in);
  147. if(n <= 0)
  148. done = 1;
  149. else
  150. strm.avail_in += n;
  151. strm.next_in = buf;
  152. }
  153. if(strm.avail_out < sizeof obuf) {
  154. Bwrite(&bout, obuf, sizeof(obuf)-strm.avail_out);
  155. strm.next_out = obuf;
  156. strm.avail_out = sizeof obuf;
  157. }
  158. if(onemore == 0)
  159. break;
  160. if(strm.avail_in == 0 && strm.avail_out == sizeof obuf)
  161. break;
  162. } while((e=BZ2_bzDecompress(&strm)) == BZ_OK || onemore--);
  163. if(e != BZ_STREAM_END) {
  164. fprint(2, "bunzip2: decompress failed\n");
  165. return 0;
  166. }
  167. if(BZ2_bzDecompressEnd(&strm) != BZ_OK) {
  168. fprint(2, "bunzip2: decompress end failed (can't happen)\n");
  169. return 0;
  170. }
  171. Bterm(&bout);
  172. return 1;
  173. }