]>
git.cameronkatri.com Git - bsd-progress.git/blob - progress.c
1 /* $NetBSD: progress.c,v 1.15 2007/06/06 17:49:14 briggs 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.
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.
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.
35 #include <sys/cdefs.h>
37 __RCSID("$NetBSD: progress.c,v 1.15 2007/06/06 17:49:14 briggs Exp $");
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/socket.h>
43 #include <sys/ioctl.h>
46 #include <netinet/in.h>
65 #define GLOBAL /* force GLOBAL decls in progressbar.h to be
67 #include "progressbar.h"
69 static void usage(void);
70 int main(int, char *[]);
76 "usage: %s [-ez] [-b buffersize] [-f file] [-l length]\n"
77 " %*.s [-p prefix] cmd [args...]\n",
78 getprogname(), (int) strlen(getprogname()), "");
84 main(int argc
, char *argv
[])
88 pid_t pid
= 0, gzippid
= 0, deadpid
;
89 int ch
, fd
, outpipe
[2];
90 int ws
, gzipstat
, cmdstat
;
91 int eflag
= 0, lflag
= 0, zflag
= 0;
99 /* defaults: Read from stdin, 0 filesize (no completion estimate) */
102 buffersize
= 64 * 1024;
105 while ((ch
= getopt(argc
, argv
, "b:ef:l:p:z")) != -1)
108 buffersize
= (size_t) strsuftoll("buffer size", optarg
,
119 filesize
= strsuftoll("input size", optarg
, 0,
139 if (infile
&& (fd
= open(infile
, O_RDONLY
, 0)) < 0)
140 err(1, "%s", infile
);
142 /* stat() to get the filesize unless overridden, or -z */
143 if (!zflag
&& !lflag
&& (fstat(fd
, &statb
) == 0)) {
144 if (S_ISFIFO(statb
.st_mode
)) {
145 /* stat(2) on pipe may return only the
146 * first few bytes with more coming.
150 filesize
= statb
.st_size
;
154 /* gzip -l the file if we have the name and -z is given */
155 if (zflag
&& !lflag
&& infile
!= NULL
) {
157 char buf
[256], *cp
, *cmd
;
160 * Read second word of last line of gzip -l output. Looks like:
161 * % gzip -l ../etc.tgz
162 * compressed uncompressed ratio uncompressed_name
163 * 119737 696320 82.8% ../etc.tar
166 asprintf(&cmd
, "gzip -l %s", infile
);
167 if ((gzipsizepipe
= popen(cmd
, "r")) == NULL
)
168 err(1, "reading compressed file length");
169 for (; fgets(buf
, 256, gzipsizepipe
) != NULL
;)
171 strtoimax(buf
, &cp
, 10);
172 filesize
= strtoimax(cp
, NULL
, 10);
173 if (pclose(gzipsizepipe
) < 0)
174 err(1, "closing compressed file length pipe");
177 /* Pipe input through gzip -dc if -z is given */
181 if (pipe(gzippipe
) < 0)
185 err(1, "fork for gzip");
189 dup2(gzippipe
[0], fd
);
193 dup2(gzippipe
[1], STDOUT_FILENO
);
194 dup2(fd
, STDIN_FILENO
);
197 if (execlp("gzip", "gzip", "-dc", NULL
))
198 err(1, "exec()ing gzip");
202 /* Initialize progressbar.c's global state */
205 ttyout
= eflag
? stderr
: stdout
;
207 if (ioctl(fileno(ttyout
), TIOCGSIZE
, &ts
) == -1)
210 ttywidth
= ts
.ts_cols
;
212 fb_buf
= malloc(buffersize
);
214 err(1, "malloc for buffersize");
216 if (pipe(outpipe
) < 0)
217 err(1, "output pipe");
220 err(1, "fork for output pipe");
224 dup2(outpipe
[0], STDIN_FILENO
);
227 execvp(argv
[0], argv
);
228 err(1, "could not exec %s", argv
[0]);
233 while ((nr
= read(fd
, fb_buf
, buffersize
)) > 0)
234 for (off
= 0; nr
; nr
-= nw
, off
+= nw
, bytes
+= nw
)
235 if ((nw
= write(outpipe
[1], fb_buf
+ off
,
237 err(1, "writing %u bytes to output pipe",
243 while (pid
|| gzippid
) {
246 * We need to exit with an error if the command (or gzip)
248 * Unfortunately we can't generate a true 'exited by signal'
249 * error without sending the signal to ourselves :-(
251 ws
= WIFSIGNALED(ws
) ? WTERMSIG(ws
) : WEXITSTATUS(ws
);
253 if (deadpid
!= -1 && errno
== EINTR
)
255 if (deadpid
== pid
) {
260 if (deadpid
== gzippid
) {
272 exit(cmdstat
? cmdstat
: gzipstat
);