progress.c revision 1.12
11.12Shubertf/*	$NetBSD: progress.c,v 1.12 2006/04/20 23:20:55 hubertf Exp $ */
21.1Sjhawk
31.1Sjhawk/*-
41.1Sjhawk * Copyright (c) 2003 The NetBSD Foundation, Inc.
51.1Sjhawk * All rights reserved.
61.1Sjhawk *
71.1Sjhawk * This code is derived from software contributed to The NetBSD Foundation
81.1Sjhawk * by John Hawkinson.
91.1Sjhawk *
101.1Sjhawk * Redistribution and use in source and binary forms, with or without
111.1Sjhawk * modification, are permitted provided that the following conditions
121.1Sjhawk * are met:
131.1Sjhawk * 1. Redistributions of source code must retain the above copyright
141.1Sjhawk *    notice, this list of conditions and the following disclaimer.
151.1Sjhawk * 2. Redistributions in binary form must reproduce the above copyright
161.1Sjhawk *    notice, this list of conditions and the following disclaimer in the
171.1Sjhawk *    documentation and/or other materials provided with the distribution.
181.1Sjhawk * 3. Neither the name of The NetBSD Foundation nor the names of its
191.1Sjhawk *    contributors may be used to endorse or promote products derived
201.1Sjhawk *    from this software without specific prior written permission.
211.1Sjhawk *
221.1Sjhawk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
231.1Sjhawk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
241.1Sjhawk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
251.1Sjhawk * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
261.1Sjhawk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
271.1Sjhawk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
281.1Sjhawk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
291.1Sjhawk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
301.1Sjhawk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
311.1Sjhawk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
321.1Sjhawk * POSSIBILITY OF SUCH DAMAGE.
331.1Sjhawk */
341.1Sjhawk
351.1Sjhawk#include <sys/cdefs.h>
361.1Sjhawk#ifndef lint
371.12Shubertf__RCSID("$NetBSD: progress.c,v 1.12 2006/04/20 23:20:55 hubertf Exp $");
381.1Sjhawk#endif				/* not lint */
391.1Sjhawk
401.1Sjhawk#include <sys/types.h>
411.1Sjhawk#include <sys/param.h>
421.1Sjhawk#include <sys/socket.h>
431.1Sjhawk#include <sys/ioctl.h>
441.1Sjhawk#include <sys/time.h>
451.1Sjhawk#include <sys/wait.h>
461.1Sjhawk#include <netinet/in.h>
471.1Sjhawk#include <arpa/ftp.h>
481.1Sjhawk
491.1Sjhawk#include <ctype.h>
501.1Sjhawk#include <err.h>
511.1Sjhawk#include <errno.h>
521.1Sjhawk#include <fcntl.h>
531.1Sjhawk#include <glob.h>
541.1Sjhawk#include <signal.h>
551.1Sjhawk#include <limits.h>
561.1Sjhawk#include <netdb.h>
571.1Sjhawk#include <stdio.h>
581.1Sjhawk#include <stdlib.h>
591.1Sjhawk#include <string.h>
601.1Sjhawk#include <termios.h>
611.1Sjhawk#include <time.h>
621.1Sjhawk#include <tzfile.h>
631.1Sjhawk#include <unistd.h>
641.1Sjhawk
651.1Sjhawk#define GLOBAL			/* force GLOBAL decls in progressbar.h to be
661.1Sjhawk				 * declared */
671.1Sjhawk#include "progressbar.h"
681.1Sjhawk
691.1Sjhawkstatic void usage(void);
701.1Sjhawkint main(int, char *[]);
711.1Sjhawk
721.1Sjhawkstatic void
731.1Sjhawkusage(void)
741.1Sjhawk{
751.1Sjhawk	fprintf(stderr,
761.11Sgarbled	    "usage: %s [-ez] [-f file] [-l length] [-p prefix] cmd [args...]\n",
771.1Sjhawk	    getprogname());
781.1Sjhawk	exit(1);
791.1Sjhawk}
801.1Sjhawk
811.1Sjhawk
821.1Sjhawkint
831.1Sjhawkmain(int argc, char *argv[])
841.1Sjhawk{
851.1Sjhawk	static char fb_buf[BUFSIZ];
861.1Sjhawk	char *infile = NULL;
871.10Sdsl	pid_t pid = 0, gzippid = 0, deadpid;
881.10Sdsl	int ch, fd, outpipe[2];
891.10Sdsl	int ws, gzipstat, cmdstat;
901.11Sgarbled	int eflag = 0, lflag = 0, zflag = 0;
911.1Sjhawk	ssize_t nr, nw, off;
921.1Sjhawk	struct stat statb;
931.3Schristos	struct ttysize ts;
941.1Sjhawk
951.1Sjhawk	setprogname(argv[0]);
961.1Sjhawk
971.1Sjhawk	/* defaults: Read from stdin, 0 filesize (no completion estimate) */
981.1Sjhawk	fd = STDIN_FILENO;
991.1Sjhawk	filesize = 0;
1001.8Shubertf	prefix=NULL;
1011.1Sjhawk
1021.11Sgarbled	while ((ch = getopt(argc, argv, "ef:l:p:z")) != -1)
1031.1Sjhawk		switch (ch) {
1041.11Sgarbled		case 'e':
1051.11Sgarbled			eflag++;
1061.11Sgarbled			break;
1071.1Sjhawk		case 'f':
1081.1Sjhawk			infile = optarg;
1091.1Sjhawk			break;
1101.1Sjhawk		case 'l':
1111.1Sjhawk			lflag++;
1121.9Slukem			filesize = strsuftoll("input size", optarg, 0,
1131.9Slukem			    LLONG_MAX);
1141.1Sjhawk			break;
1151.8Shubertf		case 'p':
1161.8Shubertf			prefix = optarg;
1171.8Shubertf			break;
1181.1Sjhawk		case 'z':
1191.1Sjhawk			zflag++;
1201.1Sjhawk			break;
1211.1Sjhawk		default:
1221.1Sjhawk		case '?':
1231.1Sjhawk			usage();
1241.1Sjhawk			/* NOTREACHED */
1251.1Sjhawk		}
1261.1Sjhawk	argc -= optind;
1271.1Sjhawk	argv += optind;
1281.1Sjhawk
1291.1Sjhawk	if (argc < 1)
1301.1Sjhawk		usage();
1311.1Sjhawk	if (infile && (fd = open(infile, O_RDONLY, 0)) < 0)
1321.1Sjhawk		err(1, "%s", infile);
1331.1Sjhawk
1341.1Sjhawk	/* stat() to get the filesize unless overridden, or -z */
1351.12Shubertf	if (!zflag && !lflag && (fstat(fd, &statb) == 0)) {
1361.12Shubertf		if (S_ISFIFO(statb.st_mode)) {
1371.12Shubertf			/* stat(2) on pipe may return only the
1381.12Shubertf			 * first few bytes with more coming.
1391.12Shubertf			 * Don't trust!
1401.12Shubertf			 */
1411.12Shubertf		} else {
1421.12Shubertf			filesize = statb.st_size;
1431.12Shubertf		}
1441.12Shubertf	}
1451.1Sjhawk
1461.1Sjhawk	/* gzip -l the file if we have the name and -z is given */
1471.1Sjhawk	if (zflag && !lflag && infile != NULL) {
1481.1Sjhawk		FILE *gzipsizepipe;
1491.10Sdsl		char buf[256], *cp, *cmd;
1501.1Sjhawk
1511.1Sjhawk		/*
1521.1Sjhawk		 * Read second word of last line of gzip -l output. Looks like:
1531.1Sjhawk		 * % gzip -l ../etc.tgz
1541.1Sjhawk		 *   compressed uncompressed  ratio uncompressed_name
1551.1Sjhawk		 * 	 119737       696320  82.8% ../etc.tar
1561.1Sjhawk		 */
1571.1Sjhawk
1581.1Sjhawk		asprintf(&cmd, "gzip -l %s", infile);
1591.1Sjhawk		if ((gzipsizepipe = popen(cmd, "r")) == NULL)
1601.1Sjhawk			err(1, "reading compressed file length");
1611.1Sjhawk		for (; fgets(buf, 256, gzipsizepipe) != NULL;)
1621.1Sjhawk		    continue;
1631.10Sdsl		strtoimax(buf, &cp, 10);
1641.10Sdsl		filesize = strtoimax(cp, NULL, 10);
1651.1Sjhawk		if (pclose(gzipsizepipe) < 0)
1661.1Sjhawk			err(1, "closing compressed file length pipe");
1671.1Sjhawk		free(cmd);
1681.1Sjhawk	}
1691.1Sjhawk	/* Pipe input through gzip -dc if -z is given */
1701.1Sjhawk	if (zflag) {
1711.1Sjhawk		int gzippipe[2];
1721.1Sjhawk
1731.1Sjhawk		if (pipe(gzippipe) < 0)
1741.1Sjhawk			err(1, "gzip pipe");
1751.1Sjhawk		gzippid = fork();
1761.1Sjhawk		if (gzippid < 0)
1771.1Sjhawk			err(1, "fork for gzip");
1781.1Sjhawk
1791.1Sjhawk		if (gzippid) {
1801.1Sjhawk			/* parent */
1811.1Sjhawk			dup2(gzippipe[0], fd);
1821.1Sjhawk			close(gzippipe[0]);
1831.1Sjhawk			close(gzippipe[1]);
1841.1Sjhawk		} else {
1851.1Sjhawk			dup2(gzippipe[1], STDOUT_FILENO);
1861.1Sjhawk			dup2(fd, STDIN_FILENO);
1871.1Sjhawk			close(gzippipe[0]);
1881.1Sjhawk			close(gzippipe[1]);
1891.1Sjhawk			if (execlp("gzip", "gzip", "-dc", NULL))
1901.1Sjhawk				err(1, "exec()ing gzip");
1911.1Sjhawk		}
1921.1Sjhawk	}
1931.1Sjhawk
1941.1Sjhawk	/* Initialize progressbar.c's global state */
1951.1Sjhawk	bytes = 0;
1961.1Sjhawk	progress = 1;
1971.11Sgarbled	ttyout = eflag ? stderr : stdout;
1981.3Schristos
1991.3Schristos	if (ioctl(fileno(ttyout), TIOCGSIZE, &ts) == -1) {
2001.3Schristos		ttywidth = 80;
2011.3Schristos	} else
2021.3Schristos		ttywidth = ts.ts_cols;
2031.1Sjhawk
2041.1Sjhawk	if (pipe(outpipe) < 0)
2051.1Sjhawk		err(1, "output pipe");
2061.1Sjhawk	pid = fork();
2071.1Sjhawk	if (pid < 0)
2081.1Sjhawk		err(1, "fork for output pipe");
2091.1Sjhawk
2101.1Sjhawk	if (pid == 0) {
2111.1Sjhawk		/* child */
2121.1Sjhawk		dup2(outpipe[0], STDIN_FILENO);
2131.1Sjhawk		close(outpipe[0]);
2141.1Sjhawk		close(outpipe[1]);
2151.1Sjhawk		execvp(argv[0], argv);
2161.1Sjhawk		err(1, "could not exec %s", argv[0]);
2171.1Sjhawk	}
2181.1Sjhawk	close(outpipe[0]);
2191.1Sjhawk
2201.1Sjhawk	progressmeter(-1);
2211.1Sjhawk	while ((nr = read(fd, fb_buf, BUFSIZ)) > 0)
2221.2Senami		for (off = 0; nr; nr -= nw, off += nw, bytes += nw)
2231.2Senami			if ((nw = write(outpipe[1], fb_buf + off,
2241.2Senami			    (size_t) nr)) < 0)
2251.5Sagc				err(1, "writing %u bytes to output pipe",
2261.5Sagc							(unsigned) nr);
2271.1Sjhawk	close(outpipe[1]);
2281.1Sjhawk
2291.10Sdsl	gzipstat = 0;
2301.10Sdsl	cmdstat = 0;
2311.7Sross	while (pid || gzippid) {
2321.10Sdsl		deadpid = wait(&ws);
2331.10Sdsl		/*
2341.10Sdsl		 * We need to exit with an error if the command (or gzip)
2351.10Sdsl		 * exited abnormally.
2361.10Sdsl		 * Unfortunately we can't generate a true 'exited by signal'
2371.10Sdsl		 * error without sending the signal to ourselves :-(
2381.10Sdsl		 */
2391.10Sdsl		ws = WIFSIGNALED(ws) ? WTERMSIG(ws) : WEXITSTATUS(ws);
2401.7Sross
2411.10Sdsl		if (deadpid != -1 && errno == EINTR)
2421.10Sdsl			continue;
2431.10Sdsl		if (deadpid == pid) {
2441.7Sross			pid = 0;
2451.10Sdsl			cmdstat = ws;
2461.10Sdsl			continue;
2471.10Sdsl		}
2481.10Sdsl		if (deadpid == gzippid) {
2491.7Sross			gzippid = 0;
2501.10Sdsl			gzipstat = ws;
2511.7Sross			continue;
2521.10Sdsl		}
2531.10Sdsl		break;
2541.7Sross	}
2551.1Sjhawk
2561.1Sjhawk	progressmeter(1);
2571.10Sdsl	exit(cmdstat ? cmdstat : gzipstat);
2581.1Sjhawk}
259