]>
git.cameronkatri.com Git - bsd-progress.git/blob - progress.c
1 /* $NetBSD: progress.c,v 1.23 2021/01/07 12:02:52 lukem Exp $ */
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
34 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: progress.c,v 1.23 2021/01/07 12:02:52 lukem Exp $");
39 #include <sys/types.h>
40 #include <sys/ioctl.h>
56 long long strsuftoll(const char *, const char *, long long, long long);
58 long long strsuftollx(const char *, const char *, long long, long long,
61 #define GLOBAL /* force GLOBAL decls in progressbar.h to be
63 #include "progressbar.h"
65 static void broken_pipe(int unused
);
66 static void usage(void);
69 broken_pipe(int unused
)
71 signal(SIGPIPE
, SIG_DFL
);
73 kill(getpid(), SIGPIPE
);
80 "usage: %s [-ez] [-b buffersize] [-f file] [-l length]\n"
81 " %*.s [-p prefix] cmd [args...]\n",
82 getprogname(), (int) strlen(getprogname()), "");
87 main(int argc
, char *argv
[])
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;
100 setprogname(argv
[0]);
102 /* defaults: Read from stdin, 0 filesize (no completion estimate) */
105 buffersize
= 64 * 1024;
108 while ((ch
= getopt(argc
, argv
, "b:ef:l:p:z")) != -1)
111 buffersize
= (size_t) strsuftoll("buffer size", optarg
,
122 filesize
= strsuftoll("input size", optarg
, 0,
142 if (infile
&& (fd
= open(infile
, O_RDONLY
, 0)) < 0)
143 err(1, "%s", infile
);
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.
153 filesize
= statb
.st_size
;
157 /* gzip -l the file if we have the name and -z is given */
158 if (zflag
&& !lflag
&& infile
!= NULL
) {
160 char buf
[256], *cp
, *cmd
;
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
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
;)
174 strtoimax(buf
, &cp
, 10);
175 filesize
= strtoimax(cp
, NULL
, 10);
176 if (pclose(gzipsizepipe
) < 0)
177 err(1, "closing compressed file length pipe");
180 /* Pipe input through gzip -dc if -z is given */
184 if (pipe(gzippipe
) < 0)
188 err(1, "fork for gzip");
192 dup2(gzippipe
[0], fd
);
196 dup2(gzippipe
[1], STDOUT_FILENO
);
197 dup2(fd
, STDIN_FILENO
);
200 if (execlp("gzip", "gzip", "-dc", NULL
))
201 err(1, "exec()ing gzip");
205 /* Initialize progressbar.c's global state */
208 ttyout
= eflag
? stderr
: stdout
;
210 if (ioctl(fileno(ttyout
), TIOCGWINSZ
, &wins
) == -1)
213 ttywidth
= wins
.ws_col
;
215 fb_buf
= malloc(buffersize
);
217 err(1, "malloc for buffersize");
219 if (pipe(outpipe
) < 0)
220 err(1, "output pipe");
223 err(1, "fork for output pipe");
227 dup2(outpipe
[0], STDIN_FILENO
);
230 execvp(argv
[0], argv
);
231 err(1, "could not exec %s", argv
[0]);
235 signal(SIGPIPE
, broken_pipe
);
240 nr
= read(fd
, fb_buf
, buffersize
);
241 } while (nr
< 0 && errno
== EINTR
);
244 for (off
= 0; nr
; nr
-= nw
, off
+= nw
, bytes
+= nw
)
245 if ((nw
= write(outpipe
[1], fb_buf
+ off
,
247 if (errno
== EINTR
) {
252 err(1, "writing %u bytes to output pipe",
260 while (pid
|| gzippid
) {
263 * We need to exit with an error if the command (or gzip)
265 * Unfortunately we can't generate a true 'exited by signal'
266 * error without sending the signal to ourselves :-(
268 ws
= WIFSIGNALED(ws
) ? WTERMSIG(ws
) : WEXITSTATUS(ws
);
270 if (deadpid
!= -1 && errno
== EINTR
)
272 if (deadpid
== pid
) {
277 if (deadpid
== gzippid
) {
286 signal(SIGPIPE
, SIG_DFL
);
290 exit(cmdstat
? cmdstat
: gzipstat
);