progress.c revision 1.19
11.19Sjoerg/* $NetBSD: progress.c,v 1.19 2011/09/16 15:39:28 joerg 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 * 191.1Sjhawk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 201.1Sjhawk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 211.1Sjhawk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 221.1Sjhawk * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 231.1Sjhawk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 241.1Sjhawk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 251.1Sjhawk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 261.1Sjhawk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 271.1Sjhawk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 281.1Sjhawk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 291.1Sjhawk * POSSIBILITY OF SUCH DAMAGE. 301.1Sjhawk */ 311.1Sjhawk 321.1Sjhawk#include <sys/cdefs.h> 331.1Sjhawk#ifndef lint 341.19Sjoerg__RCSID("$NetBSD: progress.c,v 1.19 2011/09/16 15:39:28 joerg Exp $"); 351.1Sjhawk#endif /* not lint */ 361.1Sjhawk 371.1Sjhawk#include <sys/types.h> 381.1Sjhawk#include <sys/param.h> 391.1Sjhawk#include <sys/socket.h> 401.1Sjhawk#include <sys/ioctl.h> 411.1Sjhawk#include <sys/time.h> 421.1Sjhawk#include <sys/wait.h> 431.1Sjhawk#include <netinet/in.h> 441.1Sjhawk#include <arpa/ftp.h> 451.1Sjhawk 461.1Sjhawk#include <err.h> 471.1Sjhawk#include <errno.h> 481.1Sjhawk#include <fcntl.h> 491.1Sjhawk#include <glob.h> 501.1Sjhawk#include <signal.h> 511.14Shubertf#include <inttypes.h> 521.1Sjhawk#include <limits.h> 531.1Sjhawk#include <netdb.h> 541.1Sjhawk#include <stdio.h> 551.1Sjhawk#include <stdlib.h> 561.1Sjhawk#include <string.h> 571.1Sjhawk#include <termios.h> 581.1Sjhawk#include <time.h> 591.1Sjhawk#include <tzfile.h> 601.1Sjhawk#include <unistd.h> 611.1Sjhawk 621.1Sjhawk#define GLOBAL /* force GLOBAL decls in progressbar.h to be 631.1Sjhawk * declared */ 641.1Sjhawk#include "progressbar.h" 651.1Sjhawk 661.17Sdhollandstatic void broken_pipe(int unused); 671.19Sjoerg__dead static void usage(void); 681.1Sjhawk 691.1Sjhawkstatic void 701.17Sdhollandbroken_pipe(int unused) 711.17Sdholland{ 721.17Sdholland signal(SIGPIPE, SIG_DFL); 731.17Sdholland progressmeter(1); 741.17Sdholland kill(getpid(), SIGPIPE); 751.17Sdholland} 761.17Sdholland 771.17Sdhollandstatic void 781.1Sjhawkusage(void) 791.1Sjhawk{ 801.1Sjhawk fprintf(stderr, 811.15Sbriggs "usage: %s [-ez] [-b buffersize] [-f file] [-l length]\n" 821.15Sbriggs " %*.s [-p prefix] cmd [args...]\n", 831.15Sbriggs getprogname(), (int) strlen(getprogname()), ""); 841.13Shubertf exit(EXIT_FAILURE); 851.1Sjhawk} 861.1Sjhawk 871.1Sjhawk 881.1Sjhawkint 891.1Sjhawkmain(int argc, char *argv[]) 901.1Sjhawk{ 911.15Sbriggs char *fb_buf; 921.1Sjhawk char *infile = NULL; 931.10Sdsl pid_t pid = 0, gzippid = 0, deadpid; 941.10Sdsl int ch, fd, outpipe[2]; 951.10Sdsl int ws, gzipstat, cmdstat; 961.11Sgarbled int eflag = 0, lflag = 0, zflag = 0; 971.1Sjhawk ssize_t nr, nw, off; 981.15Sbriggs size_t buffersize; 991.1Sjhawk struct stat statb; 1001.3Schristos struct ttysize ts; 1011.1Sjhawk 1021.1Sjhawk setprogname(argv[0]); 1031.1Sjhawk 1041.1Sjhawk /* defaults: Read from stdin, 0 filesize (no completion estimate) */ 1051.1Sjhawk fd = STDIN_FILENO; 1061.1Sjhawk filesize = 0; 1071.15Sbriggs buffersize = 64 * 1024; 1081.13Shubertf prefix = NULL; 1091.1Sjhawk 1101.15Sbriggs while ((ch = getopt(argc, argv, "b:ef:l:p:z")) != -1) 1111.1Sjhawk switch (ch) { 1121.15Sbriggs case 'b': 1131.15Sbriggs buffersize = (size_t) strsuftoll("buffer size", optarg, 1141.18Stron 0, SSIZE_MAX); 1151.15Sbriggs break; 1161.11Sgarbled case 'e': 1171.11Sgarbled eflag++; 1181.11Sgarbled break; 1191.1Sjhawk case 'f': 1201.1Sjhawk infile = optarg; 1211.1Sjhawk break; 1221.1Sjhawk case 'l': 1231.1Sjhawk lflag++; 1241.9Slukem filesize = strsuftoll("input size", optarg, 0, 1251.9Slukem LLONG_MAX); 1261.1Sjhawk break; 1271.8Shubertf case 'p': 1281.8Shubertf prefix = optarg; 1291.8Shubertf break; 1301.1Sjhawk case 'z': 1311.1Sjhawk zflag++; 1321.1Sjhawk break; 1331.13Shubertf case '?': 1341.1Sjhawk default: 1351.1Sjhawk usage(); 1361.1Sjhawk /* NOTREACHED */ 1371.1Sjhawk } 1381.1Sjhawk argc -= optind; 1391.1Sjhawk argv += optind; 1401.1Sjhawk 1411.1Sjhawk if (argc < 1) 1421.1Sjhawk usage(); 1431.13Shubertf 1441.1Sjhawk if (infile && (fd = open(infile, O_RDONLY, 0)) < 0) 1451.1Sjhawk err(1, "%s", infile); 1461.1Sjhawk 1471.1Sjhawk /* stat() to get the filesize unless overridden, or -z */ 1481.12Shubertf if (!zflag && !lflag && (fstat(fd, &statb) == 0)) { 1491.12Shubertf if (S_ISFIFO(statb.st_mode)) { 1501.12Shubertf /* stat(2) on pipe may return only the 1511.12Shubertf * first few bytes with more coming. 1521.12Shubertf * Don't trust! 1531.12Shubertf */ 1541.12Shubertf } else { 1551.12Shubertf filesize = statb.st_size; 1561.12Shubertf } 1571.12Shubertf } 1581.1Sjhawk 1591.1Sjhawk /* gzip -l the file if we have the name and -z is given */ 1601.1Sjhawk if (zflag && !lflag && infile != NULL) { 1611.1Sjhawk FILE *gzipsizepipe; 1621.10Sdsl char buf[256], *cp, *cmd; 1631.1Sjhawk 1641.1Sjhawk /* 1651.1Sjhawk * Read second word of last line of gzip -l output. Looks like: 1661.1Sjhawk * % gzip -l ../etc.tgz 1671.1Sjhawk * compressed uncompressed ratio uncompressed_name 1681.1Sjhawk * 119737 696320 82.8% ../etc.tar 1691.1Sjhawk */ 1701.1Sjhawk 1711.1Sjhawk asprintf(&cmd, "gzip -l %s", infile); 1721.1Sjhawk if ((gzipsizepipe = popen(cmd, "r")) == NULL) 1731.1Sjhawk err(1, "reading compressed file length"); 1741.1Sjhawk for (; fgets(buf, 256, gzipsizepipe) != NULL;) 1751.1Sjhawk continue; 1761.10Sdsl strtoimax(buf, &cp, 10); 1771.10Sdsl filesize = strtoimax(cp, NULL, 10); 1781.1Sjhawk if (pclose(gzipsizepipe) < 0) 1791.1Sjhawk err(1, "closing compressed file length pipe"); 1801.1Sjhawk free(cmd); 1811.1Sjhawk } 1821.1Sjhawk /* Pipe input through gzip -dc if -z is given */ 1831.1Sjhawk if (zflag) { 1841.1Sjhawk int gzippipe[2]; 1851.1Sjhawk 1861.1Sjhawk if (pipe(gzippipe) < 0) 1871.1Sjhawk err(1, "gzip pipe"); 1881.1Sjhawk gzippid = fork(); 1891.1Sjhawk if (gzippid < 0) 1901.1Sjhawk err(1, "fork for gzip"); 1911.1Sjhawk 1921.1Sjhawk if (gzippid) { 1931.1Sjhawk /* parent */ 1941.1Sjhawk dup2(gzippipe[0], fd); 1951.1Sjhawk close(gzippipe[0]); 1961.1Sjhawk close(gzippipe[1]); 1971.1Sjhawk } else { 1981.1Sjhawk dup2(gzippipe[1], STDOUT_FILENO); 1991.1Sjhawk dup2(fd, STDIN_FILENO); 2001.1Sjhawk close(gzippipe[0]); 2011.1Sjhawk close(gzippipe[1]); 2021.1Sjhawk if (execlp("gzip", "gzip", "-dc", NULL)) 2031.1Sjhawk err(1, "exec()ing gzip"); 2041.1Sjhawk } 2051.1Sjhawk } 2061.1Sjhawk 2071.1Sjhawk /* Initialize progressbar.c's global state */ 2081.1Sjhawk bytes = 0; 2091.1Sjhawk progress = 1; 2101.11Sgarbled ttyout = eflag ? stderr : stdout; 2111.3Schristos 2121.13Shubertf if (ioctl(fileno(ttyout), TIOCGSIZE, &ts) == -1) 2131.3Schristos ttywidth = 80; 2141.13Shubertf else 2151.3Schristos ttywidth = ts.ts_cols; 2161.1Sjhawk 2171.15Sbriggs fb_buf = malloc(buffersize); 2181.15Sbriggs if (fb_buf == NULL) 2191.15Sbriggs err(1, "malloc for buffersize"); 2201.15Sbriggs 2211.1Sjhawk if (pipe(outpipe) < 0) 2221.1Sjhawk err(1, "output pipe"); 2231.1Sjhawk pid = fork(); 2241.1Sjhawk if (pid < 0) 2251.1Sjhawk err(1, "fork for output pipe"); 2261.1Sjhawk 2271.1Sjhawk if (pid == 0) { 2281.1Sjhawk /* child */ 2291.1Sjhawk dup2(outpipe[0], STDIN_FILENO); 2301.1Sjhawk close(outpipe[0]); 2311.1Sjhawk close(outpipe[1]); 2321.1Sjhawk execvp(argv[0], argv); 2331.1Sjhawk err(1, "could not exec %s", argv[0]); 2341.1Sjhawk } 2351.1Sjhawk close(outpipe[0]); 2361.1Sjhawk 2371.17Sdholland signal(SIGPIPE, broken_pipe); 2381.1Sjhawk progressmeter(-1); 2391.17Sdholland 2401.15Sbriggs while ((nr = read(fd, fb_buf, buffersize)) > 0) 2411.2Senami for (off = 0; nr; nr -= nw, off += nw, bytes += nw) 2421.2Senami if ((nw = write(outpipe[1], fb_buf + off, 2431.17Sdholland (size_t) nr)) < 0) { 2441.17Sdholland progressmeter(1); 2451.5Sagc err(1, "writing %u bytes to output pipe", 2461.5Sagc (unsigned) nr); 2471.17Sdholland } 2481.1Sjhawk close(outpipe[1]); 2491.1Sjhawk 2501.10Sdsl gzipstat = 0; 2511.10Sdsl cmdstat = 0; 2521.7Sross while (pid || gzippid) { 2531.10Sdsl deadpid = wait(&ws); 2541.10Sdsl /* 2551.10Sdsl * We need to exit with an error if the command (or gzip) 2561.10Sdsl * exited abnormally. 2571.10Sdsl * Unfortunately we can't generate a true 'exited by signal' 2581.10Sdsl * error without sending the signal to ourselves :-( 2591.10Sdsl */ 2601.10Sdsl ws = WIFSIGNALED(ws) ? WTERMSIG(ws) : WEXITSTATUS(ws); 2611.7Sross 2621.10Sdsl if (deadpid != -1 && errno == EINTR) 2631.10Sdsl continue; 2641.10Sdsl if (deadpid == pid) { 2651.7Sross pid = 0; 2661.10Sdsl cmdstat = ws; 2671.10Sdsl continue; 2681.10Sdsl } 2691.10Sdsl if (deadpid == gzippid) { 2701.7Sross gzippid = 0; 2711.10Sdsl gzipstat = ws; 2721.7Sross continue; 2731.10Sdsl } 2741.10Sdsl break; 2751.7Sross } 2761.1Sjhawk 2771.1Sjhawk progressmeter(1); 2781.17Sdholland signal(SIGPIPE, SIG_DFL); 2791.15Sbriggs 2801.15Sbriggs free(fb_buf); 2811.15Sbriggs 2821.10Sdsl exit(cmdstat ? cmdstat : gzipstat); 2831.1Sjhawk} 284