]>
git.cameronkatri.com Git - bsd-progress.git/blob - progress.c
1 /* $NetBSD: progress.c,v 1.14 2007/02/07 15:21:21 hubertf 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.14 2007/02/07 15:21:21 hubertf 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] [-f file] [-l length] [-p prefix] cmd [args...]\n",
83 main(int argc
, char *argv
[])
85 static char fb_buf
[BUFSIZ
];
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;
97 /* defaults: Read from stdin, 0 filesize (no completion estimate) */
102 while ((ch
= getopt(argc
, argv
, "ef:l:p:z")) != -1)
112 filesize
= strsuftoll("input size", optarg
, 0,
132 if (infile
&& (fd
= open(infile
, O_RDONLY
, 0)) < 0)
133 err(1, "%s", infile
);
135 /* stat() to get the filesize unless overridden, or -z */
136 if (!zflag
&& !lflag
&& (fstat(fd
, &statb
) == 0)) {
137 if (S_ISFIFO(statb
.st_mode
)) {
138 /* stat(2) on pipe may return only the
139 * first few bytes with more coming.
143 filesize
= statb
.st_size
;
147 /* gzip -l the file if we have the name and -z is given */
148 if (zflag
&& !lflag
&& infile
!= NULL
) {
150 char buf
[256], *cp
, *cmd
;
153 * Read second word of last line of gzip -l output. Looks like:
154 * % gzip -l ../etc.tgz
155 * compressed uncompressed ratio uncompressed_name
156 * 119737 696320 82.8% ../etc.tar
159 asprintf(&cmd
, "gzip -l %s", infile
);
160 if ((gzipsizepipe
= popen(cmd
, "r")) == NULL
)
161 err(1, "reading compressed file length");
162 for (; fgets(buf
, 256, gzipsizepipe
) != NULL
;)
164 strtoimax(buf
, &cp
, 10);
165 filesize
= strtoimax(cp
, NULL
, 10);
166 if (pclose(gzipsizepipe
) < 0)
167 err(1, "closing compressed file length pipe");
170 /* Pipe input through gzip -dc if -z is given */
174 if (pipe(gzippipe
) < 0)
178 err(1, "fork for gzip");
182 dup2(gzippipe
[0], fd
);
186 dup2(gzippipe
[1], STDOUT_FILENO
);
187 dup2(fd
, STDIN_FILENO
);
190 if (execlp("gzip", "gzip", "-dc", NULL
))
191 err(1, "exec()ing gzip");
195 /* Initialize progressbar.c's global state */
198 ttyout
= eflag
? stderr
: stdout
;
200 if (ioctl(fileno(ttyout
), TIOCGSIZE
, &ts
) == -1)
203 ttywidth
= ts
.ts_cols
;
205 if (pipe(outpipe
) < 0)
206 err(1, "output pipe");
209 err(1, "fork for output pipe");
213 dup2(outpipe
[0], STDIN_FILENO
);
216 execvp(argv
[0], argv
);
217 err(1, "could not exec %s", argv
[0]);
222 while ((nr
= read(fd
, fb_buf
, BUFSIZ
)) > 0)
223 for (off
= 0; nr
; nr
-= nw
, off
+= nw
, bytes
+= nw
)
224 if ((nw
= write(outpipe
[1], fb_buf
+ off
,
226 err(1, "writing %u bytes to output pipe",
232 while (pid
|| gzippid
) {
235 * We need to exit with an error if the command (or gzip)
237 * Unfortunately we can't generate a true 'exited by signal'
238 * error without sending the signal to ourselves :-(
240 ws
= WIFSIGNALED(ws
) ? WTERMSIG(ws
) : WEXITSTATUS(ws
);
242 if (deadpid
!= -1 && errno
== EINTR
)
244 if (deadpid
== pid
) {
249 if (deadpid
== gzippid
) {
258 exit(cmdstat
? cmdstat
: gzipstat
);