Posted to tcl by dbohdan at Mon Sep 21 08:21:05 GMT 2020view raw

  1. #! /usr/bin/env jimsh
  2.  
  3. package require jimlib
  4. namespace import lib::$ lib::id
  5.  
  6. proc decompressor path {
  7. set ext [file ext [$ path]]
  8. switch -- [$ ext] {
  9. .bz2 { list bzcat -- }
  10. .gz { list zcat -- }
  11. .lz4 { list lz4cat -- }
  12. .xz { list xzcat -- }
  13. .zst { list zstdcat -- }
  14. default { list cat -- }
  15. }
  16. }
  17.  
  18. proc main files {
  19. set verbose 0
  20. if {[lindex [$ files] 0] eq {--verbose}} {
  21. set files [lrange [$ files] 1 end]
  22. set verbose 1
  23. }
  24.  
  25. set ch stdout
  26.  
  27. foreach file [$ files] {
  28. if {[$ verbose]} {
  29. puts stderr [list [$ file]]
  30. }
  31.  
  32. set decomp [decompressor [$ file]]
  33. set dest $(
  34. [$ decomp] eq [list cat --]
  35. ? [$ file]
  36. : [file rootname [$ file]]
  37. )
  38. set contents [exec {*}[$ decomp] [$ file]]
  39. set stat [file stat [$ file]]
  40.  
  41. lib::file::tar::write \
  42. [$ ch] \
  43. [$ contents] \
  44. -filename [$ dest] \
  45. -mtime [$ stat mtime] \
  46. -uid [$ stat uid] \
  47. -gid [$ stat gid] \
  48. -mode [$ stat mode] \
  49.  
  50. }
  51.  
  52. lib::file::tar::finish [$ ch]
  53. }
  54.  
  55. main [$ argv]
  56.  
  57. # vim: set syntax=tcl shiftwidth=4 smarttab expandtab: