summaryrefslogtreecommitdiffstats
path: root/zcat.in
blob: 010b7bbc09b798e8039cfa009fe76b334c2b49a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#! /bin/sh
# Zcat - Cat wrapper for compressed files.
# Copyright (C) 2009 Antonio Diaz Diaz.
#
# This script is free software: you have unlimited permission
# to copy, distribute and modify it.

LC_ALL=C
export LC_ALL
args=
gz_args=
xz_args=
recursive=0
two_hyphens=0

# Loop over args until a filename is found
while [ x"$1" != x ] ; do

	case "$1" in
	--help | --he* | -h)
		echo "Zcat - Cat wrapper for compressed files."
		echo
		echo "Zcat is a wrapper script around the cat command that allows transparent"
		echo "concatenation of any combination of compressed and non-compressed files."
		echo "If any given file is compressed, its uncompressed content is used. If a"
		echo "given file does not exist, zcat tries the compressed file names"
		echo "corresponding to the supported compressors. If no files are specified,"
		echo "data is read from standard input, decompressed if needed, and sent to"
		echo "stdout. Data read from standard input must be of the same type; all"
		echo "uncompressed or all compressed with the same compressor."
		echo "The supported compressors are bzip2, gzip, lzip and xz."
		echo
		echo "Usage: $0 [OPTIONS] [CAT_OPTIONS] [FILES]"
		echo
		echo "CAT_OPTIONS are passed directly to cat."
		echo "The exit status from cat is preserved."
		echo
		echo "Options:"
		echo "  -h, --help          display this help and exit"
		echo "  -V, --version       output version information and exit"
		echo "  -r, --recursive     operate recursively on directories"
		echo
		echo "Report bugs to zutils-bug@nongnu.org"
		echo "Zutils home page: http://www.nongnu.org/zutils/zutils.html"
		exit 0 ;;
	--version | --ve* | -V)
		echo "Zcat VERSION"
		echo "Copyright (C) 2009 Antonio Diaz Diaz."
		echo "This script is free software: you have unlimited permission"
		echo "to copy, distribute and modify it."
		exit 0 ;;
	- | -c | --st* | -d | --de* | -f | --fo* | -q | --qu* | -L | --lic* )
		;;
	-l | --lis*)
		gz_args="${gz_args} $1"; xz_args="${xz_args} $1" ;;
	-r | --re*)
		recursive=1 ;;
	--)
		shift ; two_hyphens=1 ; break ;;
	-?*)
		args="${args} $1" ;;
	 *)
		break ;;
	esac
	shift
done

if [ $# = 0 ] ; then
	bindir=`echo "$0" | sed -e 's,[^/]*$,,'`
	prog_name=`"${bindir}"zutils -t`
	case "${prog_name}" in
	bzip2)	prog="bzip2 -cdfq" ;;
	gzip)	prog="gzip -cdfq ${gz_args}" ;;
	lzip)	prog="lzip -cdfq" ;;
	xz)	prog="xz -cdfq ${xz_args}" ;;
	*)	prog=cat ;;
	esac
	{ "${bindir}"zutils -m ${prog_name} ; cat ; } | ${prog} | cat ${args}
	exit $?
fi

retval=0
for i in "$@" ; do
	if [ "$i" = "--" ] && [ ${two_hyphens} = 0 ] ; then two_hyphens=1
	else
		if [ ! -f "$i" ] ; then
			if [ -f "$i.gz" ] ;    then i="$i.gz"
			elif [ -f "$i.bz2" ] ; then i="$i.bz2"
			elif [ -f "$i.lz" ] ;  then i="$i.lz"
			elif [ -f "$i.xz" ] ;  then i="$i.xz"
			elif [ ${recursive} = 1 ] && [ -d "$i" ] ; then
				find "$i" -type f -exec "$0" '{}' ';'
				continue
			else
				echo "$0: File \"$i\" not found or not a regular file" 1>&2
				if [ ${retval} = 0 ] ; then retval=1 ; fi
				continue
			fi
		fi
		bindir=`echo "$0" | sed -e 's,[^/]*$,,'`
		prog_name=`"${bindir}"zutils -t -- "$i"`
		case "${prog_name}" in
		bzip2)	prog="bzip2 -cdfq" ;;
		gzip)	prog="gzip -cdfq ${gz_args}" ;;
		lzip)	prog="lzip -cdfq" ;;
		xz)	prog="xz -cdfq ${xz_args}" ;;
		*)	prog=cat ;;
		esac
		${prog} -- "$i" | cat ${args}
		r=$?
		if [ $r != 0 ] ; then retval=$r ; fi
	fi
done

exit ${retval}