progress.c revision 1.8
1/*	$NetBSD: progress.c,v 1.8 2004/03/09 17:04:24 hubertf Exp $ */
2
3/*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by John Hawkinson.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of The NetBSD Foundation nor the names of its
19 *    contributors may be used to endorse or promote products derived
20 *    from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#include <sys/cdefs.h>
36#ifndef lint
37__RCSID("$NetBSD: progress.c,v 1.8 2004/03/09 17:04:24 hubertf Exp $");
38#endif				/* not lint */
39
40#include <sys/types.h>
41#include <sys/param.h>
42#include <sys/socket.h>
43#include <sys/ioctl.h>
44#include <sys/time.h>
45#include <sys/wait.h>
46#include <netinet/in.h>
47#include <arpa/ftp.h>
48
49#include <ctype.h>
50#include <err.h>
51#include <errno.h>
52#include <fcntl.h>
53#include <glob.h>
54#include <signal.h>
55#include <limits.h>
56#include <netdb.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <termios.h>
61#include <time.h>
62#include <tzfile.h>
63#include <unistd.h>
64
65#define GLOBAL			/* force GLOBAL decls in progressbar.h to be
66				 * declared */
67#include "progressbar.h"
68
69static void usage(void);
70int main(int, char *[]);
71
72static void
73usage(void)
74{
75	fprintf(stderr,
76	    "usage: %s [-z] [-f file] [-l length] [-p prefix] cmd [args...]\n",
77	    getprogname());
78	exit(1);
79}
80
81
82int
83main(int argc, char *argv[])
84{
85	static char fb_buf[BUFSIZ];
86	char *infile = NULL;
87	pid_t pid = 0, gzippid = 0;
88	int ch, fd, outpipe[2], waitstat;
89	int lflag = 0, zflag = 0;
90	ssize_t nr, nw, off;
91	struct stat statb;
92	struct ttysize ts;
93
94	setprogname(argv[0]);
95
96	/* defaults: Read from stdin, 0 filesize (no completion estimate) */
97	fd = STDIN_FILENO;
98	filesize = 0;
99	prefix=NULL;
100
101	while ((ch = getopt(argc, argv, "f:l:p:z")) != -1)
102		switch (ch) {
103		case 'f':
104			infile = optarg;
105			break;
106		case 'l':
107			lflag++;
108			filesize = strtoull(optarg, NULL, 0);
109			break;
110		case 'p':
111			prefix = optarg;
112			break;
113		case 'z':
114			zflag++;
115			break;
116		default:
117		case '?':
118			usage();
119			/* NOTREACHED */
120		}
121	argc -= optind;
122	argv += optind;
123
124	if (argc < 1)
125		usage();
126	if (infile && (fd = open(infile, O_RDONLY, 0)) < 0)
127		err(1, "%s", infile);
128
129	/* stat() to get the filesize unless overridden, or -z */
130	if (!zflag && !lflag && (fstat(fd, &statb) == 0))
131		filesize = statb.st_size;
132
133	/* gzip -l the file if we have the name and -z is given */
134	if (zflag && !lflag && infile != NULL) {
135		FILE *gzipsizepipe;
136		char buf[256], *cmd;
137		long size;
138
139		/*
140		 * Read second word of last line of gzip -l output. Looks like:
141		 * % gzip -l ../etc.tgz
142		 *   compressed uncompressed  ratio uncompressed_name
143		 * 	 119737       696320  82.8% ../etc.tar
144		 */
145
146		asprintf(&cmd, "gzip -l %s", infile);
147		if ((gzipsizepipe = popen(cmd, "r")) == NULL)
148			err(1, "reading compressed file length");
149		for (; fgets(buf, 256, gzipsizepipe) != NULL;)
150		    continue;
151		sscanf(buf, "%*d %ld", &size);
152		filesize = size;
153		if (pclose(gzipsizepipe) < 0)
154			err(1, "closing compressed file length pipe");
155		free(cmd);
156	}
157	/* Pipe input through gzip -dc if -z is given */
158	if (zflag) {
159		int gzippipe[2];
160
161		if (pipe(gzippipe) < 0)
162			err(1, "gzip pipe");
163		gzippid = fork();
164		if (gzippid < 0)
165			err(1, "fork for gzip");
166
167		if (gzippid) {
168			/* parent */
169			dup2(gzippipe[0], fd);
170			close(gzippipe[0]);
171			close(gzippipe[1]);
172		} else {
173			dup2(gzippipe[1], STDOUT_FILENO);
174			dup2(fd, STDIN_FILENO);
175			close(gzippipe[0]);
176			close(gzippipe[1]);
177			if (execlp("gzip", "gzip", "-dc", NULL))
178				err(1, "exec()ing gzip");
179		}
180	}
181
182	/* Initialize progressbar.c's global state */
183	bytes = 0;
184	progress = 1;
185	ttyout = stdout;
186
187	if (ioctl(fileno(ttyout), TIOCGSIZE, &ts) == -1) {
188		ttywidth = 80;
189	} else
190		ttywidth = ts.ts_cols;
191
192	if (pipe(outpipe) < 0)
193		err(1, "output pipe");
194	pid = fork();
195	if (pid < 0)
196		err(1, "fork for output pipe");
197
198	if (pid == 0) {
199		/* child */
200		dup2(outpipe[0], STDIN_FILENO);
201		close(outpipe[0]);
202		close(outpipe[1]);
203		execvp(argv[0], argv);
204		err(1, "could not exec %s", argv[0]);
205	}
206	close(outpipe[0]);
207
208	progressmeter(-1);
209	while ((nr = read(fd, fb_buf, BUFSIZ)) > 0)
210		for (off = 0; nr; nr -= nw, off += nw, bytes += nw)
211			if ((nw = write(outpipe[1], fb_buf + off,
212			    (size_t) nr)) < 0)
213				err(1, "writing %u bytes to output pipe",
214							(unsigned) nr);
215	close(outpipe[1]);
216
217	while (pid || gzippid) {
218		int deadpid;
219
220		deadpid = wait(&waitstat);
221
222		if (deadpid == pid)
223			pid = 0;
224		else if (deadpid == gzippid)
225			gzippid = 0;
226		else if (deadpid != -1)
227			continue;
228		else if (errno == EINTR)
229			continue;
230		else break;
231	}
232
233	progressmeter(1);
234	return 0;
235}
236