]> git.cameronkatri.com Git - bsd-progress.git/blob - progress.c
progress: Port for Linux and Darwin
[bsd-progress.git] / progress.c
1 /* $NetBSD: progress.c,v 1.23 2021/01/07 12:02:52 lukem 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #define _GNU_SOURCE
33
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: progress.c,v 1.23 2021/01/07 12:02:52 lukem Exp $");
37 #endif /* not lint */
38
39 #include <sys/types.h>
40 #include <sys/ioctl.h>
41 #include <sys/stat.h>
42 #include <sys/wait.h>
43
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <inttypes.h>
48 #include <limits.h>
49 #include <signal.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 /* LONGLONG */
56 long long strsuftoll(const char *, const char *, long long, long long);
57 /* LONGLONG */
58 long long strsuftollx(const char *, const char *, long long, long long,
59 char *, size_t);
60
61 #define GLOBAL /* force GLOBAL decls in progressbar.h to be
62 * declared */
63 #include "progressbar.h"
64
65 static void broken_pipe(int unused);
66 static void usage(void);
67
68 static void
69 broken_pipe(int unused)
70 {
71 signal(SIGPIPE, SIG_DFL);
72 progressmeter(1);
73 kill(getpid(), SIGPIPE);
74 }
75
76 static void
77 usage(void)
78 {
79 fprintf(stderr,
80 "usage: %s [-ez] [-b buffersize] [-f file] [-l length]\n"
81 " %*.s [-p prefix] cmd [args...]\n",
82 getprogname(), (int) strlen(getprogname()), "");
83 exit(EXIT_FAILURE);
84 }
85
86 int
87 main(int argc, char *argv[])
88 {
89 char *fb_buf;
90 char *infile = NULL;
91 pid_t pid = 0, gzippid = 0, deadpid;
92 int ch, fd, outpipe[2];
93 int ws, gzipstat, cmdstat;
94 int eflag = 0, lflag = 0, zflag = 0;
95 ssize_t nr, nw, off;
96 size_t buffersize;
97 struct stat statb;
98 struct winsize wins;
99
100 setprogname(argv[0]);
101
102 /* defaults: Read from stdin, 0 filesize (no completion estimate) */
103 fd = STDIN_FILENO;
104 filesize = 0;
105 buffersize = 64 * 1024;
106 prefix = NULL;
107
108 while ((ch = getopt(argc, argv, "b:ef:l:p:z")) != -1)
109 switch (ch) {
110 case 'b':
111 buffersize = (size_t) strsuftoll("buffer size", optarg,
112 0, SSIZE_MAX);
113 break;
114 case 'e':
115 eflag++;
116 break;
117 case 'f':
118 infile = optarg;
119 break;
120 case 'l':
121 lflag++;
122 filesize = strsuftoll("input size", optarg, 0,
123 LLONG_MAX);
124 break;
125 case 'p':
126 prefix = optarg;
127 break;
128 case 'z':
129 zflag++;
130 break;
131 case '?':
132 default:
133 usage();
134 /* NOTREACHED */
135 }
136 argc -= optind;
137 argv += optind;
138
139 if (argc < 1)
140 usage();
141
142 if (infile && (fd = open(infile, O_RDONLY, 0)) < 0)
143 err(1, "%s", infile);
144
145 /* stat() to get the filesize unless overridden, or -z */
146 if (!zflag && !lflag && (fstat(fd, &statb) == 0)) {
147 if (S_ISFIFO(statb.st_mode)) {
148 /* stat(2) on pipe may return only the
149 * first few bytes with more coming.
150 * Don't trust!
151 */
152 } else {
153 filesize = statb.st_size;
154 }
155 }
156
157 /* gzip -l the file if we have the name and -z is given */
158 if (zflag && !lflag && infile != NULL) {
159 FILE *gzipsizepipe;
160 char buf[256], *cp, *cmd;
161
162 /*
163 * Read second word of last line of gzip -l output. Looks like:
164 * % gzip -l ../etc.tgz
165 * compressed uncompressed ratio uncompressed_name
166 * 119737 696320 82.8% ../etc.tar
167 */
168
169 asprintf(&cmd, "gzip -l %s", infile);
170 if ((gzipsizepipe = popen(cmd, "r")) == NULL)
171 err(1, "reading compressed file length");
172 for (; fgets(buf, 256, gzipsizepipe) != NULL;)
173 continue;
174 strtoimax(buf, &cp, 10);
175 filesize = strtoimax(cp, NULL, 10);
176 if (pclose(gzipsizepipe) < 0)
177 err(1, "closing compressed file length pipe");
178 free(cmd);
179 }
180 /* Pipe input through gzip -dc if -z is given */
181 if (zflag) {
182 int gzippipe[2];
183
184 if (pipe(gzippipe) < 0)
185 err(1, "gzip pipe");
186 gzippid = fork();
187 if (gzippid < 0)
188 err(1, "fork for gzip");
189
190 if (gzippid) {
191 /* parent */
192 dup2(gzippipe[0], fd);
193 close(gzippipe[0]);
194 close(gzippipe[1]);
195 } else {
196 dup2(gzippipe[1], STDOUT_FILENO);
197 dup2(fd, STDIN_FILENO);
198 close(gzippipe[0]);
199 close(gzippipe[1]);
200 if (execlp("gzip", "gzip", "-dc", NULL))
201 err(1, "exec()ing gzip");
202 }
203 }
204
205 /* Initialize progressbar.c's global state */
206 bytes = 0;
207 progress = 1;
208 ttyout = eflag ? stderr : stdout;
209
210 if (ioctl(fileno(ttyout), TIOCGWINSZ, &wins) == -1)
211 ttywidth = 80;
212 else
213 ttywidth = wins.ws_col;
214
215 fb_buf = malloc(buffersize);
216 if (fb_buf == NULL)
217 err(1, "malloc for buffersize");
218
219 if (pipe(outpipe) < 0)
220 err(1, "output pipe");
221 pid = fork();
222 if (pid < 0)
223 err(1, "fork for output pipe");
224
225 if (pid == 0) {
226 /* child */
227 dup2(outpipe[0], STDIN_FILENO);
228 close(outpipe[0]);
229 close(outpipe[1]);
230 execvp(argv[0], argv);
231 err(1, "could not exec %s", argv[0]);
232 }
233 close(outpipe[0]);
234
235 signal(SIGPIPE, broken_pipe);
236 progressmeter(-1);
237
238 while (1) {
239 do {
240 nr = read(fd, fb_buf, buffersize);
241 } while (nr < 0 && errno == EINTR);
242 if (nr <= 0)
243 break;
244 for (off = 0; nr; nr -= nw, off += nw, bytes += nw)
245 if ((nw = write(outpipe[1], fb_buf + off,
246 (size_t) nr)) < 0) {
247 if (errno == EINTR) {
248 nw = 0;
249 continue;
250 }
251 progressmeter(1);
252 err(1, "writing %u bytes to output pipe",
253 (unsigned) nr);
254 }
255 }
256 close(outpipe[1]);
257
258 gzipstat = 0;
259 cmdstat = 0;
260 while (pid || gzippid) {
261 deadpid = wait(&ws);
262 /*
263 * We need to exit with an error if the command (or gzip)
264 * exited abnormally.
265 * Unfortunately we can't generate a true 'exited by signal'
266 * error without sending the signal to ourselves :-(
267 */
268 ws = WIFSIGNALED(ws) ? WTERMSIG(ws) : WEXITSTATUS(ws);
269
270 if (deadpid != -1 && errno == EINTR)
271 continue;
272 if (deadpid == pid) {
273 pid = 0;
274 cmdstat = ws;
275 continue;
276 }
277 if (deadpid == gzippid) {
278 gzippid = 0;
279 gzipstat = ws;
280 continue;
281 }
282 break;
283 }
284
285 progressmeter(1);
286 signal(SIGPIPE, SIG_DFL);
287
288 free(fb_buf);
289
290 exit(cmdstat ? cmdstat : gzipstat);
291 }