/*-
* Copyright (c) 2003 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by John Hawkinson.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/* stat() to get the filesize unless overridden, or -z */
if (!zflag && !lflag && (fstat(fd, &statb) == 0)) {
if (S_ISFIFO(statb.st_mode)) {
/* stat(2) on pipe may return only the
* first few bytes with more coming.
* Don't trust!
*/
} else {
filesize = statb.st_size;
}
}
/* gzip -l the file if we have the name and -z is given */
if (zflag && !lflag && infile != NULL) {
FILE *gzipsizepipe;
char buf[256], *cp, *cmd;
/*
* Read second word of last line of gzip -l output. Looks like:
* % gzip -l ../etc.tgz
* compressed uncompressed ratio uncompressed_name
* 119737 696320 82.8% ../etc.tar
*/
asprintf(&cmd, "gzip -l %s", infile);
if ((gzipsizepipe = popen(cmd, "r")) == NULL)
err(1, "reading compressed file length");
for (; fgets(buf, 256, gzipsizepipe) != NULL;)
continue;
strtoimax(buf, &cp, 10);
filesize = strtoimax(cp, NULL, 10);
if (pclose(gzipsizepipe) < 0)
err(1, "closing compressed file length pipe");
free(cmd);
}
/* Pipe input through gzip -dc if -z is given */
if (zflag) {
int gzippipe[2];
if (pipe(gzippipe) < 0)
err(1, "gzip pipe");
gzippid = fork();
if (gzippid < 0)
err(1, "fork for gzip");
while (1) {
do {
nr = read(fd, fb_buf, buffersize);
} while (nr < 0 && errno == EINTR);
if (nr < 0) {
progressmeter(1);
err(1, "reading input");
}
if (nr == 0)
break;
for (off = 0; nr; nr -= nw, off += nw, bytes += nw)
if ((nw = write(outpipe[1], fb_buf + off,
(size_t) nr)) < 0) {
if (errno == EINTR) {
nw = 0;
continue;
}
progressmeter(1);
err(1, "writing %u bytes to output pipe",
(unsigned) nr);
}
}
close(outpipe[1]);
gzipstat = 0;
cmdstat = 0;
while (pid || gzippid) {
deadpid = wait(&ws);
/*
* We need to exit with an error if the command (or gzip)
* exited abnormally.
* Unfortunately we can't generate a true 'exited by signal'
* error without sending the signal to ourselves :-(
*/
ws = WIFSIGNALED(ws) ? WTERMSIG(ws) : WEXITSTATUS(ws);