1 /* $NetBSD: progress.c,v 1.11 2006/01/12 20:33:20 garbled 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.11 2006/01/12 20:33:20 garbled 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 69 static void usage(void); 70 int main(int, char *[]); 71 72 static void 73 usage(void) 74 { 75 fprintf(stderr, 76 "usage: %s [-ez] [-f file] [-l length] [-p prefix] cmd [args...]\n", 77 getprogname()); 78 exit(1); 79 } 80 81 82 int 83 main(int argc, char *argv[]) 84 { 85 static char fb_buf[BUFSIZ]; 86 char *infile = NULL; 87 pid_t pid = 0, gzippid = 0, deadpid; 88 int ch, fd, outpipe[2]; 89 int ws, gzipstat, cmdstat; 90 int eflag = 0, lflag = 0, zflag = 0; 91 ssize_t nr, nw, off; 92 struct stat statb; 93 struct ttysize ts; 94 95 setprogname(argv[0]); 96 97 /* defaults: Read from stdin, 0 filesize (no completion estimate) */ 98 fd = STDIN_FILENO; 99 filesize = 0; 100 prefix=NULL; 101 102 while ((ch = getopt(argc, argv, "ef:l:p:z")) != -1) 103 switch (ch) { 104 case 'e': 105 eflag++; 106 break; 107 case 'f': 108 infile = optarg; 109 break; 110 case 'l': 111 lflag++; 112 filesize = strsuftoll("input size", optarg, 0, 113 LLONG_MAX); 114 break; 115 case 'p': 116 prefix = optarg; 117 break; 118 case 'z': 119 zflag++; 120 break; 121 default: 122 case '?': 123 usage(); 124 /* NOTREACHED */ 125 } 126 argc -= optind; 127 argv += optind; 128 129 if (argc < 1) 130 usage(); 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 filesize = statb.st_size; 137 138 /* gzip -l the file if we have the name and -z is given */ 139 if (zflag && !lflag && infile != NULL) { 140 FILE *gzipsizepipe; 141 char buf[256], *cp, *cmd; 142 143 /* 144 * Read second word of last line of gzip -l output. Looks like: 145 * % gzip -l ../etc.tgz 146 * compressed uncompressed ratio uncompressed_name 147 * 119737 696320 82.8% ../etc.tar 148 */ 149 150 asprintf(&cmd, "gzip -l %s", infile); 151 if ((gzipsizepipe = popen(cmd, "r")) == NULL) 152 err(1, "reading compressed file length"); 153 for (; fgets(buf, 256, gzipsizepipe) != NULL;) 154 continue; 155 strtoimax(buf, &cp, 10); 156 filesize = strtoimax(cp, NULL, 10); 157 if (pclose(gzipsizepipe) < 0) 158 err(1, "closing compressed file length pipe"); 159 free(cmd); 160 } 161 /* Pipe input through gzip -dc if -z is given */ 162 if (zflag) { 163 int gzippipe[2]; 164 165 if (pipe(gzippipe) < 0) 166 err(1, "gzip pipe"); 167 gzippid = fork(); 168 if (gzippid < 0) 169 err(1, "fork for gzip"); 170 171 if (gzippid) { 172 /* parent */ 173 dup2(gzippipe[0], fd); 174 close(gzippipe[0]); 175 close(gzippipe[1]); 176 } else { 177 dup2(gzippipe[1], STDOUT_FILENO); 178 dup2(fd, STDIN_FILENO); 179 close(gzippipe[0]); 180 close(gzippipe[1]); 181 if (execlp("gzip", "gzip", "-dc", NULL)) 182 err(1, "exec()ing gzip"); 183 } 184 } 185 186 /* Initialize progressbar.c's global state */ 187 bytes = 0; 188 progress = 1; 189 ttyout = eflag ? stderr : stdout; 190 191 if (ioctl(fileno(ttyout), TIOCGSIZE, &ts) == -1) { 192 ttywidth = 80; 193 } else 194 ttywidth = ts.ts_cols; 195 196 if (pipe(outpipe) < 0) 197 err(1, "output pipe"); 198 pid = fork(); 199 if (pid < 0) 200 err(1, "fork for output pipe"); 201 202 if (pid == 0) { 203 /* child */ 204 dup2(outpipe[0], STDIN_FILENO); 205 close(outpipe[0]); 206 close(outpipe[1]); 207 execvp(argv[0], argv); 208 err(1, "could not exec %s", argv[0]); 209 } 210 close(outpipe[0]); 211 212 progressmeter(-1); 213 while ((nr = read(fd, fb_buf, BUFSIZ)) > 0) 214 for (off = 0; nr; nr -= nw, off += nw, bytes += nw) 215 if ((nw = write(outpipe[1], fb_buf + off, 216 (size_t) nr)) < 0) 217 err(1, "writing %u bytes to output pipe", 218 (unsigned) nr); 219 close(outpipe[1]); 220 221 gzipstat = 0; 222 cmdstat = 0; 223 while (pid || gzippid) { 224 deadpid = wait(&ws); 225 /* 226 * We need to exit with an error if the command (or gzip) 227 * exited abnormally. 228 * Unfortunately we can't generate a true 'exited by signal' 229 * error without sending the signal to ourselves :-( 230 */ 231 ws = WIFSIGNALED(ws) ? WTERMSIG(ws) : WEXITSTATUS(ws); 232 233 if (deadpid != -1 && errno == EINTR) 234 continue; 235 if (deadpid == pid) { 236 pid = 0; 237 cmdstat = ws; 238 continue; 239 } 240 if (deadpid == gzippid) { 241 gzippid = 0; 242 gzipstat = ws; 243 continue; 244 } 245 break; 246 } 247 248 progressmeter(1); 249 exit(cmdstat ? cmdstat : gzipstat); 250 } 251