progress.c revision 1.13
1/* $NetBSD: progress.c,v 1.13 2007/02/07 14:06:57 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.13 2007/02/07 14:06:57 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 <err.h> 50#include <errno.h> 51#include <fcntl.h> 52#include <glob.h> 53#include <signal.h> 54#include <limits.h> 55#include <netdb.h> 56#include <stdio.h> 57#include <stdlib.h> 58#include <string.h> 59#include <termios.h> 60#include <time.h> 61#include <tzfile.h> 62#include <unistd.h> 63 64#define GLOBAL /* force GLOBAL decls in progressbar.h to be 65 * declared */ 66#include "progressbar.h" 67 68static void usage(void); 69int main(int, char *[]); 70 71static void 72usage(void) 73{ 74 fprintf(stderr, 75 "usage: %s [-ez] [-f file] [-l length] [-p prefix] cmd [args...]\n", 76 getprogname()); 77 exit(EXIT_FAILURE); 78} 79 80 81int 82main(int argc, char *argv[]) 83{ 84 static char fb_buf[BUFSIZ]; 85 char *infile = NULL; 86 pid_t pid = 0, gzippid = 0, deadpid; 87 int ch, fd, outpipe[2]; 88 int ws, gzipstat, cmdstat; 89 int eflag = 0, 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, "ef:l:p:z")) != -1) 102 switch (ch) { 103 case 'e': 104 eflag++; 105 break; 106 case 'f': 107 infile = optarg; 108 break; 109 case 'l': 110 lflag++; 111 filesize = strsuftoll("input size", optarg, 0, 112 LLONG_MAX); 113 break; 114 case 'p': 115 prefix = optarg; 116 break; 117 case 'z': 118 zflag++; 119 break; 120 case '?': 121 default: 122 usage(); 123 /* NOTREACHED */ 124 } 125 argc -= optind; 126 argv += optind; 127 128 if (argc < 1) 129 usage(); 130 131 if (infile && (fd = open(infile, O_RDONLY, 0)) < 0) 132 err(1, "%s", infile); 133 134 /* stat() to get the filesize unless overridden, or -z */ 135 if (!zflag && !lflag && (fstat(fd, &statb) == 0)) { 136 if (S_ISFIFO(statb.st_mode)) { 137 /* stat(2) on pipe may return only the 138 * first few bytes with more coming. 139 * Don't trust! 140 */ 141 } else { 142 filesize = statb.st_size; 143 } 144 } 145 146 /* gzip -l the file if we have the name and -z is given */ 147 if (zflag && !lflag && infile != NULL) { 148 FILE *gzipsizepipe; 149 char buf[256], *cp, *cmd; 150 151 /* 152 * Read second word of last line of gzip -l output. Looks like: 153 * % gzip -l ../etc.tgz 154 * compressed uncompressed ratio uncompressed_name 155 * 119737 696320 82.8% ../etc.tar 156 */ 157 158 asprintf(&cmd, "gzip -l %s", infile); 159 if ((gzipsizepipe = popen(cmd, "r")) == NULL) 160 err(1, "reading compressed file length"); 161 for (; fgets(buf, 256, gzipsizepipe) != NULL;) 162 continue; 163 strtoimax(buf, &cp, 10); 164 filesize = strtoimax(cp, NULL, 10); 165 if (pclose(gzipsizepipe) < 0) 166 err(1, "closing compressed file length pipe"); 167 free(cmd); 168 } 169 /* Pipe input through gzip -dc if -z is given */ 170 if (zflag) { 171 int gzippipe[2]; 172 173 if (pipe(gzippipe) < 0) 174 err(1, "gzip pipe"); 175 gzippid = fork(); 176 if (gzippid < 0) 177 err(1, "fork for gzip"); 178 179 if (gzippid) { 180 /* parent */ 181 dup2(gzippipe[0], fd); 182 close(gzippipe[0]); 183 close(gzippipe[1]); 184 } else { 185 dup2(gzippipe[1], STDOUT_FILENO); 186 dup2(fd, STDIN_FILENO); 187 close(gzippipe[0]); 188 close(gzippipe[1]); 189 if (execlp("gzip", "gzip", "-dc", NULL)) 190 err(1, "exec()ing gzip"); 191 } 192 } 193 194 /* Initialize progressbar.c's global state */ 195 bytes = 0; 196 progress = 1; 197 ttyout = eflag ? stderr : stdout; 198 199 if (ioctl(fileno(ttyout), TIOCGSIZE, &ts) == -1) 200 ttywidth = 80; 201 else 202 ttywidth = ts.ts_cols; 203 204 if (pipe(outpipe) < 0) 205 err(1, "output pipe"); 206 pid = fork(); 207 if (pid < 0) 208 err(1, "fork for output pipe"); 209 210 if (pid == 0) { 211 /* child */ 212 dup2(outpipe[0], STDIN_FILENO); 213 close(outpipe[0]); 214 close(outpipe[1]); 215 execvp(argv[0], argv); 216 err(1, "could not exec %s", argv[0]); 217 } 218 close(outpipe[0]); 219 220 progressmeter(-1); 221 while ((nr = read(fd, fb_buf, BUFSIZ)) > 0) 222 for (off = 0; nr; nr -= nw, off += nw, bytes += nw) 223 if ((nw = write(outpipe[1], fb_buf + off, 224 (size_t) nr)) < 0) 225 err(1, "writing %u bytes to output pipe", 226 (unsigned) nr); 227 close(outpipe[1]); 228 229 gzipstat = 0; 230 cmdstat = 0; 231 while (pid || gzippid) { 232 deadpid = wait(&ws); 233 /* 234 * We need to exit with an error if the command (or gzip) 235 * exited abnormally. 236 * Unfortunately we can't generate a true 'exited by signal' 237 * error without sending the signal to ourselves :-( 238 */ 239 ws = WIFSIGNALED(ws) ? WTERMSIG(ws) : WEXITSTATUS(ws); 240 241 if (deadpid != -1 && errno == EINTR) 242 continue; 243 if (deadpid == pid) { 244 pid = 0; 245 cmdstat = ws; 246 continue; 247 } 248 if (deadpid == gzippid) { 249 gzippid = 0; 250 gzipstat = ws; 251 continue; 252 } 253 break; 254 } 255 256 progressmeter(1); 257 exit(cmdstat ? cmdstat : gzipstat); 258} 259