ungz.c 1018 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* vim: ts=4 sw=4 noexpandtab
  2. * Really dumb wrapper around gzopen/gzread
  3. *
  4. * build with:
  5. * i686-pc-toaru-gcc -o base/usr/bin/ungz ungz.c -lz
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <zlib.h>
  11. int main(int argc, char **argv) {
  12. int ret;
  13. if (argc < 2) {
  14. fprintf(stderr, "Usage: %s file.gz\n", argv[0]);
  15. return 1;
  16. }
  17. char * dest_name = NULL;
  18. if (argc < 3) {
  19. if (strstr(argv[1],".gz") != (argv[1] + strlen(argv[1]) - 3)) {
  20. fprintf(stderr, "%s: Not sure if this file is gzipped. Try renaming it to include `.gz' at the end.\n", argv[0]);
  21. return 1;
  22. }
  23. dest_name = strdup(argv[1]);
  24. char * t = strstr(dest_name,".gz");
  25. *t = '\0';
  26. } else {
  27. dest_name = argv[2];
  28. }
  29. gzFile src = gzopen(argv[1], "r");
  30. if (!src) return 1;
  31. FILE * dest = fopen(dest_name, "w");
  32. if (!dest) return 1;
  33. while (!gzeof(src)) {
  34. char buf[1024];
  35. int r = gzread(src, buf, 1024);
  36. if (r < 0) return 1;
  37. fwrite(buf, r, 1, dest);
  38. }
  39. fclose(dest);
  40. unlink(argv[1]);
  41. return 0;
  42. }