]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
6ae2364d | 2 | * |
5260325f | 3 | * scp - secure remote copy. This is basically patched BSD rcp which uses ssh |
4 | * to do the data transfer (instead of using rcmd). | |
6ae2364d | 5 | * |
5260325f | 6 | * NOTE: This version should NOT be suid root. (This uses ssh to do the transfer |
7 | * and ssh has the necessary privileges.) | |
6ae2364d | 8 | * |
5260325f | 9 | * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi> |
6ae2364d | 10 | * |
8efc0c15 | 11 | */ |
12 | ||
13 | /* | |
14 | * Copyright (c) 1983, 1990, 1992, 1993, 1995 | |
15 | * The Regents of the University of California. All rights reserved. | |
16 | * | |
17 | * Redistribution and use in source and binary forms, with or without | |
18 | * modification, are permitted provided that the following conditions | |
19 | * are met: | |
20 | * 1. Redistributions of source code must retain the above copyright | |
21 | * notice, this list of conditions and the following disclaimer. | |
22 | * 2. Redistributions in binary form must reproduce the above copyright | |
23 | * notice, this list of conditions and the following disclaimer in the | |
24 | * documentation and/or other materials provided with the distribution. | |
25 | * 3. All advertising materials mentioning features or use of this software | |
26 | * must display the following acknowledgement: | |
27 | * This product includes software developed by the University of | |
28 | * California, Berkeley and its contributors. | |
29 | * 4. Neither the name of the University nor the names of its contributors | |
30 | * may be used to endorse or promote products derived from this software | |
31 | * without specific prior written permission. | |
32 | * | |
33 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
34 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
35 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
36 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
37 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
38 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
39 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
40 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
41 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
42 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
43 | * SUCH DAMAGE. | |
44 | * | |
8efc0c15 | 45 | */ |
46 | ||
47 | #include "includes.h" | |
48 | RCSID("$Id$"); | |
49 | ||
50 | #include "ssh.h" | |
51 | #include "xmalloc.h" | |
52 | #include <utime.h> | |
53 | ||
54 | #define _PATH_CP "cp" | |
55 | ||
56 | /* For progressmeter() -- number of seconds before xfer considered "stalled" */ | |
57 | #define STALLTIME 5 | |
58 | ||
59 | /* Visual statistics about files as they are transferred. */ | |
60 | void progressmeter(int); | |
61 | ||
62 | /* Returns width of the terminal (for progress meter calculations). */ | |
63 | int getttywidth(void); | |
64 | ||
65 | /* Time a transfer started. */ | |
66 | static struct timeval start; | |
67 | ||
68 | /* Number of bytes of current file transferred so far. */ | |
69 | volatile unsigned long statbytes; | |
70 | ||
71 | /* Total size of current file. */ | |
b4ad3727 | 72 | off_t totalbytes = 0; |
8efc0c15 | 73 | |
74 | /* Name of current file being transferred. */ | |
75 | char *curfile; | |
76 | ||
48e671d5 | 77 | /* This is set to non-zero if IPv4 is desired. */ |
78 | int IPv4 = 0; | |
79 | ||
80 | /* This is set to non-zero if IPv6 is desired. */ | |
81 | int IPv6 = 0; | |
82 | ||
8efc0c15 | 83 | /* This is set to non-zero to enable verbose mode. */ |
5260325f | 84 | int verbose_mode = 0; |
8efc0c15 | 85 | |
86 | /* This is set to non-zero if compression is desired. */ | |
cf8dd513 | 87 | int compress_flag = 0; |
8efc0c15 | 88 | |
89 | /* This is set to zero if the progressmeter is not desired. */ | |
90 | int showprogress = 1; | |
91 | ||
92 | /* This is set to non-zero if running in batch mode (that is, password | |
93 | and passphrase queries are not allowed). */ | |
94 | int batchmode = 0; | |
95 | ||
96 | /* This is set to the cipher type string if given on the command line. */ | |
97 | char *cipher = NULL; | |
98 | ||
5260325f | 99 | /* This is set to the RSA authentication identity file name if given on |
8efc0c15 | 100 | the command line. */ |
101 | char *identity = NULL; | |
102 | ||
103 | /* This is the port to use in contacting the remote site (is non-NULL). */ | |
104 | char *port = NULL; | |
105 | ||
aa3378df | 106 | /* |
107 | * This function executes the given command as the specified user on the | |
108 | * given host. This returns < 0 if execution fails, and >= 0 otherwise. This | |
109 | * assigns the input and output file descriptors on success. | |
110 | */ | |
8efc0c15 | 111 | |
6ae2364d | 112 | int |
5260325f | 113 | do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout) |
8efc0c15 | 114 | { |
5260325f | 115 | int pin[2], pout[2], reserved[2]; |
116 | ||
117 | if (verbose_mode) | |
118 | fprintf(stderr, "Executing: host %s, user %s, command %s\n", | |
119 | host, remuser ? remuser : "(unspecified)", cmd); | |
120 | ||
aa3378df | 121 | /* |
122 | * Reserve two descriptors so that the real pipes won't get | |
123 | * descriptors 0 and 1 because that will screw up dup2 below. | |
124 | */ | |
5260325f | 125 | pipe(reserved); |
126 | ||
127 | /* Create a socket pair for communicating with ssh. */ | |
128 | if (pipe(pin) < 0) | |
129 | fatal("pipe: %s", strerror(errno)); | |
130 | if (pipe(pout) < 0) | |
131 | fatal("pipe: %s", strerror(errno)); | |
132 | ||
133 | /* Free the reserved descriptors. */ | |
134 | close(reserved[0]); | |
135 | close(reserved[1]); | |
136 | ||
137 | /* For a child to execute the command on the remote host using ssh. */ | |
138 | if (fork() == 0) { | |
139 | char *args[100]; | |
140 | unsigned int i; | |
141 | ||
142 | /* Child. */ | |
143 | close(pin[1]); | |
144 | close(pout[0]); | |
145 | dup2(pin[0], 0); | |
146 | dup2(pout[1], 1); | |
147 | close(pin[0]); | |
148 | close(pout[1]); | |
149 | ||
150 | i = 0; | |
151 | args[i++] = SSH_PROGRAM; | |
152 | args[i++] = "-x"; | |
153 | args[i++] = "-oFallBackToRsh no"; | |
48e671d5 | 154 | if (IPv4) |
155 | args[i++] = "-4"; | |
156 | if (IPv6) | |
157 | args[i++] = "-6"; | |
158 | args[i++] = "-oFallBackToRsh no"; | |
5260325f | 159 | if (verbose_mode) |
160 | args[i++] = "-v"; | |
cf8dd513 | 161 | if (compress_flag) |
5260325f | 162 | args[i++] = "-C"; |
163 | if (batchmode) | |
164 | args[i++] = "-oBatchMode yes"; | |
165 | if (cipher != NULL) { | |
166 | args[i++] = "-c"; | |
167 | args[i++] = cipher; | |
168 | } | |
169 | if (identity != NULL) { | |
170 | args[i++] = "-i"; | |
171 | args[i++] = identity; | |
172 | } | |
173 | if (port != NULL) { | |
174 | args[i++] = "-p"; | |
175 | args[i++] = port; | |
176 | } | |
177 | if (remuser != NULL) { | |
178 | args[i++] = "-l"; | |
179 | args[i++] = remuser; | |
180 | } | |
181 | args[i++] = host; | |
182 | args[i++] = cmd; | |
183 | args[i++] = NULL; | |
184 | ||
185 | execvp(SSH_PROGRAM, args); | |
186 | perror(SSH_PROGRAM); | |
187 | exit(1); | |
8efc0c15 | 188 | } |
5260325f | 189 | /* Parent. Close the other side, and return the local side. */ |
190 | close(pin[0]); | |
191 | *fdout = pin[1]; | |
192 | close(pout[1]); | |
193 | *fdin = pout[0]; | |
194 | return 0; | |
8efc0c15 | 195 | } |
196 | ||
6ae2364d | 197 | void |
5260325f | 198 | fatal(const char *fmt,...) |
8efc0c15 | 199 | { |
5260325f | 200 | va_list ap; |
201 | char buf[1024]; | |
202 | ||
203 | va_start(ap, fmt); | |
204 | vsnprintf(buf, sizeof(buf), fmt, ap); | |
205 | va_end(ap); | |
206 | fprintf(stderr, "%s\n", buf); | |
207 | exit(255); | |
8efc0c15 | 208 | } |
209 | ||
210 | /* This stuff used to be in BSD rcp extern.h. */ | |
211 | ||
212 | typedef struct { | |
213 | int cnt; | |
214 | char *buf; | |
215 | } BUF; | |
216 | ||
217 | extern int iamremote; | |
218 | ||
5260325f | 219 | BUF *allocbuf(BUF *, int, int); |
220 | char *colon(char *); | |
221 | void lostconn(int); | |
222 | void nospace(void); | |
223 | int okname(char *); | |
224 | void run_err(const char *,...); | |
225 | void verifydir(char *); | |
8efc0c15 | 226 | |
227 | /* Stuff from BSD rcp.c continues. */ | |
228 | ||
229 | struct passwd *pwd; | |
5260325f | 230 | uid_t userid; |
8efc0c15 | 231 | int errs, remin, remout; |
232 | int pflag, iamremote, iamrecursive, targetshouldbedirectory; | |
233 | ||
234 | #define CMDNEEDS 64 | |
235 | char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */ | |
236 | ||
5260325f | 237 | int response(void); |
238 | void rsource(char *, struct stat *); | |
239 | void sink(int, char *[]); | |
240 | void source(int, char *[]); | |
241 | void tolocal(int, char *[]); | |
242 | void toremote(char *, int, char *[]); | |
243 | void usage(void); | |
8efc0c15 | 244 | |
245 | int | |
246 | main(argc, argv) | |
247 | int argc; | |
248 | char *argv[]; | |
249 | { | |
250 | int ch, fflag, tflag; | |
251 | char *targ; | |
252 | extern char *optarg; | |
253 | extern int optind; | |
254 | ||
255 | fflag = tflag = 0; | |
48e671d5 | 256 | while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46")) != EOF) |
5260325f | 257 | switch (ch) { |
258 | /* User-visible flags. */ | |
48e671d5 | 259 | case '4': |
6ae2364d | 260 | IPv4 = 1; |
48e671d5 | 261 | break; |
262 | case '6': | |
6ae2364d | 263 | IPv6 = 1; |
48e671d5 | 264 | break; |
8efc0c15 | 265 | case 'p': |
266 | pflag = 1; | |
267 | break; | |
268 | case 'P': | |
5260325f | 269 | port = optarg; |
270 | break; | |
8efc0c15 | 271 | case 'r': |
272 | iamrecursive = 1; | |
273 | break; | |
5260325f | 274 | /* Server options. */ |
8efc0c15 | 275 | case 'd': |
276 | targetshouldbedirectory = 1; | |
277 | break; | |
5260325f | 278 | case 'f': /* "from" */ |
8efc0c15 | 279 | iamremote = 1; |
280 | fflag = 1; | |
281 | break; | |
5260325f | 282 | case 't': /* "to" */ |
8efc0c15 | 283 | iamremote = 1; |
284 | tflag = 1; | |
285 | break; | |
286 | case 'c': | |
287 | cipher = optarg; | |
5260325f | 288 | break; |
8efc0c15 | 289 | case 'i': |
5260325f | 290 | identity = optarg; |
8efc0c15 | 291 | break; |
292 | case 'v': | |
5260325f | 293 | verbose_mode = 1; |
294 | break; | |
8efc0c15 | 295 | case 'B': |
5260325f | 296 | batchmode = 1; |
297 | break; | |
8efc0c15 | 298 | case 'C': |
cf8dd513 | 299 | compress_flag = 1; |
5260325f | 300 | break; |
8efc0c15 | 301 | case 'q': |
5260325f | 302 | showprogress = 0; |
303 | break; | |
8efc0c15 | 304 | case '?': |
305 | default: | |
306 | usage(); | |
307 | } | |
308 | argc -= optind; | |
309 | argv += optind; | |
310 | ||
311 | if ((pwd = getpwuid(userid = getuid())) == NULL) | |
5260325f | 312 | fatal("unknown user %d", (int) userid); |
8efc0c15 | 313 | |
5260325f | 314 | if (!isatty(STDERR_FILENO)) |
8efc0c15 | 315 | showprogress = 0; |
316 | ||
317 | remin = STDIN_FILENO; | |
318 | remout = STDOUT_FILENO; | |
319 | ||
5260325f | 320 | if (fflag) { |
321 | /* Follow "protocol", send data. */ | |
322 | (void) response(); | |
8efc0c15 | 323 | source(argc, argv); |
324 | exit(errs != 0); | |
325 | } | |
5260325f | 326 | if (tflag) { |
327 | /* Receive data. */ | |
8efc0c15 | 328 | sink(argc, argv); |
329 | exit(errs != 0); | |
330 | } | |
8efc0c15 | 331 | if (argc < 2) |
332 | usage(); | |
333 | if (argc > 2) | |
334 | targetshouldbedirectory = 1; | |
335 | ||
336 | remin = remout = -1; | |
337 | /* Command to be executed on remote system using "ssh". */ | |
5260325f | 338 | (void) sprintf(cmd, "scp%s%s%s%s", verbose_mode ? " -v" : "", |
339 | iamrecursive ? " -r" : "", pflag ? " -p" : "", | |
340 | targetshouldbedirectory ? " -d" : ""); | |
8efc0c15 | 341 | |
5260325f | 342 | (void) signal(SIGPIPE, lostconn); |
8efc0c15 | 343 | |
344 | if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */ | |
345 | toremote(targ, argc, argv); | |
346 | else { | |
5260325f | 347 | tolocal(argc, argv); /* Dest is local host. */ |
8efc0c15 | 348 | if (targetshouldbedirectory) |
349 | verifydir(argv[argc - 1]); | |
350 | } | |
351 | exit(errs != 0); | |
352 | } | |
353 | ||
48e671d5 | 354 | char * |
355 | cleanhostname(host) | |
356 | char *host; | |
357 | { | |
358 | if (*host == '[' && host[strlen(host) - 1] == ']') { | |
359 | host[strlen(host) - 1] = '\0'; | |
360 | return (host + 1); | |
361 | } else | |
362 | return host; | |
363 | } | |
364 | ||
8efc0c15 | 365 | void |
366 | toremote(targ, argc, argv) | |
367 | char *targ, *argv[]; | |
368 | int argc; | |
369 | { | |
370 | int i, len; | |
371 | char *bp, *host, *src, *suser, *thost, *tuser; | |
372 | ||
373 | *targ++ = 0; | |
374 | if (*targ == 0) | |
375 | targ = "."; | |
376 | ||
377 | if ((thost = strchr(argv[argc - 1], '@'))) { | |
378 | /* user@host */ | |
379 | *thost++ = 0; | |
380 | tuser = argv[argc - 1]; | |
381 | if (*tuser == '\0') | |
382 | tuser = NULL; | |
383 | else if (!okname(tuser)) | |
384 | exit(1); | |
385 | } else { | |
386 | thost = argv[argc - 1]; | |
387 | tuser = NULL; | |
388 | } | |
389 | ||
390 | for (i = 0; i < argc - 1; i++) { | |
391 | src = colon(argv[i]); | |
5260325f | 392 | if (src) { /* remote to remote */ |
8efc0c15 | 393 | *src++ = 0; |
394 | if (*src == 0) | |
395 | src = "."; | |
396 | host = strchr(argv[i], '@'); | |
397 | len = strlen(SSH_PROGRAM) + strlen(argv[i]) + | |
5260325f | 398 | strlen(src) + (tuser ? strlen(tuser) : 0) + |
399 | strlen(thost) + strlen(targ) + CMDNEEDS + 32; | |
400 | bp = xmalloc(len); | |
8efc0c15 | 401 | if (host) { |
402 | *host++ = 0; | |
48e671d5 | 403 | host = cleanhostname(host); |
8efc0c15 | 404 | suser = argv[i]; |
405 | if (*suser == '\0') | |
406 | suser = pwd->pw_name; | |
407 | else if (!okname(suser)) | |
408 | continue; | |
5260325f | 409 | (void) sprintf(bp, |
410 | "%s%s -x -o'FallBackToRsh no' -n -l %s %s %s %s '%s%s%s:%s'", | |
411 | SSH_PROGRAM, verbose_mode ? " -v" : "", | |
412 | suser, host, cmd, src, | |
413 | tuser ? tuser : "", tuser ? "@" : "", | |
414 | thost, targ); | |
48e671d5 | 415 | } else { |
416 | host = cleanhostname(argv[i]); | |
5260325f | 417 | (void) sprintf(bp, |
418 | "exec %s%s -x -o'FallBackToRsh no' -n %s %s %s '%s%s%s:%s'", | |
419 | SSH_PROGRAM, verbose_mode ? " -v" : "", | |
48e671d5 | 420 | host, cmd, src, |
5260325f | 421 | tuser ? tuser : "", tuser ? "@" : "", |
422 | thost, targ); | |
48e671d5 | 423 | } |
5260325f | 424 | if (verbose_mode) |
425 | fprintf(stderr, "Executing: %s\n", bp); | |
426 | (void) system(bp); | |
427 | (void) xfree(bp); | |
428 | } else { /* local to remote */ | |
8efc0c15 | 429 | if (remin == -1) { |
430 | len = strlen(targ) + CMDNEEDS + 20; | |
5260325f | 431 | bp = xmalloc(len); |
432 | (void) sprintf(bp, "%s -t %s", cmd, targ); | |
48e671d5 | 433 | host = cleanhostname(thost); |
5260325f | 434 | if (do_cmd(host, tuser, |
8efc0c15 | 435 | bp, &remin, &remout) < 0) |
5260325f | 436 | exit(1); |
8efc0c15 | 437 | if (response() < 0) |
438 | exit(1); | |
5260325f | 439 | (void) xfree(bp); |
8efc0c15 | 440 | } |
5260325f | 441 | source(1, argv + i); |
8efc0c15 | 442 | } |
443 | } | |
444 | } | |
445 | ||
446 | void | |
447 | tolocal(argc, argv) | |
448 | int argc; | |
449 | char *argv[]; | |
450 | { | |
451 | int i, len; | |
452 | char *bp, *host, *src, *suser; | |
453 | ||
454 | for (i = 0; i < argc - 1; i++) { | |
5260325f | 455 | if (!(src = colon(argv[i]))) { /* Local to local. */ |
8efc0c15 | 456 | len = strlen(_PATH_CP) + strlen(argv[i]) + |
5260325f | 457 | strlen(argv[argc - 1]) + 20; |
8efc0c15 | 458 | bp = xmalloc(len); |
5260325f | 459 | (void) sprintf(bp, "exec %s%s%s %s %s", _PATH_CP, |
460 | iamrecursive ? " -r" : "", pflag ? " -p" : "", | |
461 | argv[i], argv[argc - 1]); | |
462 | if (verbose_mode) | |
463 | fprintf(stderr, "Executing: %s\n", bp); | |
8efc0c15 | 464 | if (system(bp)) |
465 | ++errs; | |
5260325f | 466 | (void) xfree(bp); |
8efc0c15 | 467 | continue; |
468 | } | |
469 | *src++ = 0; | |
470 | if (*src == 0) | |
471 | src = "."; | |
472 | if ((host = strchr(argv[i], '@')) == NULL) { | |
473 | host = argv[i]; | |
474 | suser = NULL; | |
475 | } else { | |
476 | *host++ = 0; | |
477 | suser = argv[i]; | |
478 | if (*suser == '\0') | |
479 | suser = pwd->pw_name; | |
480 | else if (!okname(suser)) | |
481 | continue; | |
482 | } | |
48e671d5 | 483 | host = cleanhostname(host); |
8efc0c15 | 484 | len = strlen(src) + CMDNEEDS + 20; |
5260325f | 485 | bp = xmalloc(len); |
486 | (void) sprintf(bp, "%s -f %s", cmd, src); | |
487 | if (do_cmd(host, suser, bp, &remin, &remout) < 0) { | |
488 | (void) xfree(bp); | |
489 | ++errs; | |
490 | continue; | |
8efc0c15 | 491 | } |
5260325f | 492 | xfree(bp); |
8efc0c15 | 493 | sink(1, argv + argc - 1); |
5260325f | 494 | (void) close(remin); |
8efc0c15 | 495 | remin = remout = -1; |
496 | } | |
497 | } | |
498 | ||
499 | void | |
500 | source(argc, argv) | |
501 | int argc; | |
502 | char *argv[]; | |
503 | { | |
504 | struct stat stb; | |
505 | static BUF buffer; | |
506 | BUF *bp; | |
507 | off_t i; | |
508 | int amt, fd, haderr, indx, result; | |
509 | char *last, *name, buf[2048]; | |
510 | ||
511 | for (indx = 0; indx < argc; ++indx) { | |
5260325f | 512 | name = argv[indx]; |
8efc0c15 | 513 | statbytes = 0; |
514 | if ((fd = open(name, O_RDONLY, 0)) < 0) | |
515 | goto syserr; | |
516 | if (fstat(fd, &stb) < 0) { | |
517 | syserr: run_err("%s: %s", name, strerror(errno)); | |
518 | goto next; | |
519 | } | |
520 | switch (stb.st_mode & S_IFMT) { | |
521 | case S_IFREG: | |
522 | break; | |
523 | case S_IFDIR: | |
524 | if (iamrecursive) { | |
525 | rsource(name, &stb); | |
526 | goto next; | |
527 | } | |
528 | /* FALLTHROUGH */ | |
529 | default: | |
530 | run_err("%s: not a regular file", name); | |
531 | goto next; | |
532 | } | |
533 | if ((last = strrchr(name, '/')) == NULL) | |
534 | last = name; | |
535 | else | |
536 | ++last; | |
537 | curfile = last; | |
538 | if (pflag) { | |
539 | /* | |
540 | * Make it compatible with possible future | |
541 | * versions expecting microseconds. | |
542 | */ | |
5260325f | 543 | (void) sprintf(buf, "T%lu 0 %lu 0\n", |
544 | (unsigned long) stb.st_mtime, | |
545 | (unsigned long) stb.st_atime); | |
546 | (void) write(remout, buf, strlen(buf)); | |
8efc0c15 | 547 | if (response() < 0) |
548 | goto next; | |
549 | } | |
550 | #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO) | |
5260325f | 551 | (void) sprintf(buf, "C%04o %lu %s\n", |
552 | (unsigned int) (stb.st_mode & FILEMODEMASK), | |
553 | (unsigned long) stb.st_size, | |
554 | last); | |
555 | if (verbose_mode) { | |
556 | fprintf(stderr, "Sending file modes: %s", buf); | |
557 | fflush(stderr); | |
558 | } | |
559 | (void) write(remout, buf, strlen(buf)); | |
8efc0c15 | 560 | if (response() < 0) |
561 | goto next; | |
562 | if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) { | |
5260325f | 563 | next: (void) close(fd); |
8efc0c15 | 564 | continue; |
565 | } | |
8efc0c15 | 566 | if (showprogress) { |
567 | totalbytes = stb.st_size; | |
568 | progressmeter(-1); | |
569 | } | |
8efc0c15 | 570 | /* Keep writing after an error so that we stay sync'd up. */ |
571 | for (haderr = i = 0; i < stb.st_size; i += bp->cnt) { | |
572 | amt = bp->cnt; | |
573 | if (i + amt > stb.st_size) | |
574 | amt = stb.st_size - i; | |
575 | if (!haderr) { | |
576 | result = read(fd, bp->buf, amt); | |
577 | if (result != amt) | |
578 | haderr = result >= 0 ? EIO : errno; | |
579 | } | |
580 | if (haderr) | |
5260325f | 581 | (void) write(remout, bp->buf, amt); |
8efc0c15 | 582 | else { |
68227e6d | 583 | result = atomicio(write, remout, bp->buf, amt); |
8efc0c15 | 584 | if (result != amt) |
585 | haderr = result >= 0 ? EIO : errno; | |
586 | statbytes += result; | |
587 | } | |
588 | } | |
5260325f | 589 | if (showprogress) |
8efc0c15 | 590 | progressmeter(1); |
591 | ||
592 | if (close(fd) < 0 && !haderr) | |
593 | haderr = errno; | |
594 | if (!haderr) | |
5260325f | 595 | (void) write(remout, "", 1); |
8efc0c15 | 596 | else |
597 | run_err("%s: %s", name, strerror(haderr)); | |
5260325f | 598 | (void) response(); |
8efc0c15 | 599 | } |
600 | } | |
601 | ||
602 | void | |
603 | rsource(name, statp) | |
604 | char *name; | |
605 | struct stat *statp; | |
606 | { | |
607 | DIR *dirp; | |
608 | struct dirent *dp; | |
609 | char *last, *vect[1], path[1100]; | |
610 | ||
611 | if (!(dirp = opendir(name))) { | |
612 | run_err("%s: %s", name, strerror(errno)); | |
613 | return; | |
614 | } | |
615 | last = strrchr(name, '/'); | |
616 | if (last == 0) | |
617 | last = name; | |
618 | else | |
619 | last++; | |
620 | if (pflag) { | |
5260325f | 621 | (void) sprintf(path, "T%lu 0 %lu 0\n", |
622 | (unsigned long) statp->st_mtime, | |
623 | (unsigned long) statp->st_atime); | |
624 | (void) write(remout, path, strlen(path)); | |
8efc0c15 | 625 | if (response() < 0) { |
626 | closedir(dirp); | |
627 | return; | |
628 | } | |
629 | } | |
5260325f | 630 | (void) sprintf(path, "D%04o %d %.1024s\n", |
631 | (unsigned int) (statp->st_mode & FILEMODEMASK), | |
632 | 0, last); | |
633 | if (verbose_mode) | |
634 | fprintf(stderr, "Entering directory: %s", path); | |
635 | (void) write(remout, path, strlen(path)); | |
8efc0c15 | 636 | if (response() < 0) { |
637 | closedir(dirp); | |
638 | return; | |
639 | } | |
640 | while ((dp = readdir(dirp))) { | |
641 | if (dp->d_ino == 0) | |
642 | continue; | |
643 | if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) | |
644 | continue; | |
645 | if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) { | |
646 | run_err("%s/%s: name too long", name, dp->d_name); | |
647 | continue; | |
648 | } | |
5260325f | 649 | (void) sprintf(path, "%s/%s", name, dp->d_name); |
8efc0c15 | 650 | vect[0] = path; |
651 | source(1, vect); | |
652 | } | |
5260325f | 653 | (void) closedir(dirp); |
654 | (void) write(remout, "E\n", 2); | |
655 | (void) response(); | |
8efc0c15 | 656 | } |
657 | ||
658 | void | |
659 | sink(argc, argv) | |
660 | int argc; | |
661 | char *argv[]; | |
662 | { | |
663 | static BUF buffer; | |
664 | struct stat stb; | |
5260325f | 665 | enum { |
666 | YES, NO, DISPLAYED | |
667 | } wrerr; | |
8efc0c15 | 668 | BUF *bp; |
669 | off_t i, j; | |
670 | int amt, count, exists, first, mask, mode, ofd, omode; | |
671 | int setimes, size, targisdir, wrerrno = 0; | |
672 | char ch, *cp, *np, *targ, *why, *vect[1], buf[2048]; | |
5260325f | 673 | struct utimbuf ut; |
674 | int dummy_usec; | |
8efc0c15 | 675 | |
676 | #define SCREWUP(str) { why = str; goto screwup; } | |
677 | ||
678 | setimes = targisdir = 0; | |
679 | mask = umask(0); | |
680 | if (!pflag) | |
5260325f | 681 | (void) umask(mask); |
8efc0c15 | 682 | if (argc != 1) { |
683 | run_err("ambiguous target"); | |
684 | exit(1); | |
685 | } | |
686 | targ = *argv; | |
687 | if (targetshouldbedirectory) | |
688 | verifydir(targ); | |
5260325f | 689 | |
690 | (void) write(remout, "", 1); | |
8efc0c15 | 691 | if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode)) |
692 | targisdir = 1; | |
693 | for (first = 1;; first = 0) { | |
694 | cp = buf; | |
695 | if (read(remin, cp, 1) <= 0) | |
696 | return; | |
697 | if (*cp++ == '\n') | |
698 | SCREWUP("unexpected <newline>"); | |
699 | do { | |
700 | if (read(remin, &ch, sizeof(ch)) != sizeof(ch)) | |
701 | SCREWUP("lost connection"); | |
702 | *cp++ = ch; | |
703 | } while (cp < &buf[sizeof(buf) - 1] && ch != '\n'); | |
704 | *cp = 0; | |
705 | ||
706 | if (buf[0] == '\01' || buf[0] == '\02') { | |
707 | if (iamremote == 0) | |
5260325f | 708 | (void) write(STDERR_FILENO, |
709 | buf + 1, strlen(buf + 1)); | |
8efc0c15 | 710 | if (buf[0] == '\02') |
711 | exit(1); | |
712 | ++errs; | |
713 | continue; | |
714 | } | |
715 | if (buf[0] == 'E') { | |
5260325f | 716 | (void) write(remout, "", 1); |
8efc0c15 | 717 | return; |
718 | } | |
8efc0c15 | 719 | if (ch == '\n') |
720 | *--cp = 0; | |
721 | ||
722 | #define getnum(t) (t) = 0; \ | |
723 | while (*cp >= '0' && *cp <= '9') (t) = (t) * 10 + (*cp++ - '0'); | |
724 | cp = buf; | |
725 | if (*cp == 'T') { | |
726 | setimes++; | |
727 | cp++; | |
728 | getnum(ut.modtime); | |
729 | if (*cp++ != ' ') | |
730 | SCREWUP("mtime.sec not delimited"); | |
731 | getnum(dummy_usec); | |
732 | if (*cp++ != ' ') | |
733 | SCREWUP("mtime.usec not delimited"); | |
734 | getnum(ut.actime); | |
735 | if (*cp++ != ' ') | |
736 | SCREWUP("atime.sec not delimited"); | |
737 | getnum(dummy_usec); | |
738 | if (*cp++ != '\0') | |
739 | SCREWUP("atime.usec not delimited"); | |
5260325f | 740 | (void) write(remout, "", 1); |
8efc0c15 | 741 | continue; |
742 | } | |
743 | if (*cp != 'C' && *cp != 'D') { | |
744 | /* | |
745 | * Check for the case "rcp remote:foo\* local:bar". | |
746 | * In this case, the line "No match." can be returned | |
747 | * by the shell before the rcp command on the remote is | |
748 | * executed so the ^Aerror_message convention isn't | |
749 | * followed. | |
750 | */ | |
751 | if (first) { | |
752 | run_err("%s", cp); | |
753 | exit(1); | |
754 | } | |
755 | SCREWUP("expected control record"); | |
756 | } | |
757 | mode = 0; | |
758 | for (++cp; cp < buf + 5; cp++) { | |
759 | if (*cp < '0' || *cp > '7') | |
760 | SCREWUP("bad mode"); | |
761 | mode = (mode << 3) | (*cp - '0'); | |
762 | } | |
763 | if (*cp++ != ' ') | |
764 | SCREWUP("mode not delimited"); | |
765 | ||
5260325f | 766 | for (size = 0; *cp >= '0' && *cp <= '9';) |
8efc0c15 | 767 | size = size * 10 + (*cp++ - '0'); |
768 | if (*cp++ != ' ') | |
769 | SCREWUP("size not delimited"); | |
770 | if (targisdir) { | |
771 | static char *namebuf; | |
772 | static int cursize; | |
773 | size_t need; | |
774 | ||
775 | need = strlen(targ) + strlen(cp) + 250; | |
776 | if (need > cursize) | |
5260325f | 777 | namebuf = xmalloc(need); |
778 | (void) sprintf(namebuf, "%s%s%s", targ, | |
779 | *targ ? "/" : "", cp); | |
8efc0c15 | 780 | np = namebuf; |
781 | } else | |
782 | np = targ; | |
783 | curfile = cp; | |
784 | exists = stat(np, &stb) == 0; | |
785 | if (buf[0] == 'D') { | |
786 | int mod_flag = pflag; | |
787 | if (exists) { | |
788 | if (!S_ISDIR(stb.st_mode)) { | |
789 | errno = ENOTDIR; | |
790 | goto bad; | |
791 | } | |
792 | if (pflag) | |
5260325f | 793 | (void) chmod(np, mode); |
8efc0c15 | 794 | } else { |
5260325f | 795 | /* Handle copying from a read-only |
796 | directory */ | |
8efc0c15 | 797 | mod_flag = 1; |
798 | if (mkdir(np, mode | S_IRWXU) < 0) | |
799 | goto bad; | |
800 | } | |
801 | vect[0] = np; | |
802 | sink(1, vect); | |
803 | if (setimes) { | |
804 | setimes = 0; | |
805 | if (utime(np, &ut) < 0) | |
5260325f | 806 | run_err("%s: set times: %s", |
807 | np, strerror(errno)); | |
8efc0c15 | 808 | } |
809 | if (mod_flag) | |
5260325f | 810 | (void) chmod(np, mode); |
8efc0c15 | 811 | continue; |
812 | } | |
813 | omode = mode; | |
814 | mode |= S_IWRITE; | |
5260325f | 815 | if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) { |
8efc0c15 | 816 | bad: run_err("%s: %s", np, strerror(errno)); |
817 | continue; | |
818 | } | |
5260325f | 819 | (void) write(remout, "", 1); |
8efc0c15 | 820 | if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) { |
5260325f | 821 | (void) close(ofd); |
8efc0c15 | 822 | continue; |
823 | } | |
824 | cp = bp->buf; | |
825 | wrerr = NO; | |
826 | ||
827 | if (showprogress) { | |
828 | totalbytes = size; | |
829 | progressmeter(-1); | |
830 | } | |
831 | statbytes = 0; | |
832 | for (count = i = 0; i < size; i += 4096) { | |
833 | amt = 4096; | |
834 | if (i + amt > size) | |
835 | amt = size - i; | |
836 | count += amt; | |
837 | do { | |
838 | j = read(remin, cp, amt); | |
839 | if (j <= 0) { | |
840 | run_err("%s", j ? strerror(errno) : | |
5260325f | 841 | "dropped connection"); |
8efc0c15 | 842 | exit(1); |
843 | } | |
844 | amt -= j; | |
845 | cp += j; | |
5260325f | 846 | statbytes += j; |
8efc0c15 | 847 | } while (amt > 0); |
848 | if (count == bp->cnt) { | |
849 | /* Keep reading so we stay sync'd up. */ | |
850 | if (wrerr == NO) { | |
851 | j = write(ofd, bp->buf, count); | |
852 | if (j != count) { | |
853 | wrerr = YES; | |
5260325f | 854 | wrerrno = j >= 0 ? EIO : errno; |
8efc0c15 | 855 | } |
856 | } | |
857 | count = 0; | |
858 | cp = bp->buf; | |
859 | } | |
860 | } | |
861 | if (showprogress) | |
862 | progressmeter(1); | |
863 | if (count != 0 && wrerr == NO && | |
864 | (j = write(ofd, bp->buf, count)) != count) { | |
865 | wrerr = YES; | |
5260325f | 866 | wrerrno = j >= 0 ? EIO : errno; |
8efc0c15 | 867 | } |
868 | #if 0 | |
869 | if (ftruncate(ofd, size)) { | |
870 | run_err("%s: truncate: %s", np, strerror(errno)); | |
871 | wrerr = DISPLAYED; | |
872 | } | |
873 | #endif | |
874 | if (pflag) { | |
875 | if (exists || omode != mode) | |
876 | if (fchmod(ofd, omode)) | |
877 | run_err("%s: set mode: %s", | |
5260325f | 878 | np, strerror(errno)); |
8efc0c15 | 879 | } else { |
880 | if (!exists && omode != mode) | |
881 | if (fchmod(ofd, omode & ~mask)) | |
882 | run_err("%s: set mode: %s", | |
5260325f | 883 | np, strerror(errno)); |
8efc0c15 | 884 | } |
5260325f | 885 | (void) close(ofd); |
886 | (void) response(); | |
8efc0c15 | 887 | if (setimes && wrerr == NO) { |
888 | setimes = 0; | |
889 | if (utime(np, &ut) < 0) { | |
890 | run_err("%s: set times: %s", | |
5260325f | 891 | np, strerror(errno)); |
8efc0c15 | 892 | wrerr = DISPLAYED; |
893 | } | |
894 | } | |
5260325f | 895 | switch (wrerr) { |
8efc0c15 | 896 | case YES: |
897 | run_err("%s: %s", np, strerror(wrerrno)); | |
898 | break; | |
899 | case NO: | |
5260325f | 900 | (void) write(remout, "", 1); |
8efc0c15 | 901 | break; |
902 | case DISPLAYED: | |
903 | break; | |
904 | } | |
905 | } | |
906 | screwup: | |
907 | run_err("protocol error: %s", why); | |
908 | exit(1); | |
909 | } | |
910 | ||
911 | int | |
912 | response() | |
913 | { | |
914 | char ch, *cp, resp, rbuf[2048]; | |
915 | ||
916 | if (read(remin, &resp, sizeof(resp)) != sizeof(resp)) | |
917 | lostconn(0); | |
918 | ||
919 | cp = rbuf; | |
5260325f | 920 | switch (resp) { |
921 | case 0: /* ok */ | |
8efc0c15 | 922 | return (0); |
923 | default: | |
924 | *cp++ = resp; | |
925 | /* FALLTHROUGH */ | |
5260325f | 926 | case 1: /* error, followed by error msg */ |
927 | case 2: /* fatal error, "" */ | |
8efc0c15 | 928 | do { |
929 | if (read(remin, &ch, sizeof(ch)) != sizeof(ch)) | |
930 | lostconn(0); | |
931 | *cp++ = ch; | |
932 | } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n'); | |
933 | ||
934 | if (!iamremote) | |
5260325f | 935 | (void) write(STDERR_FILENO, rbuf, cp - rbuf); |
8efc0c15 | 936 | ++errs; |
937 | if (resp == 1) | |
938 | return (-1); | |
939 | exit(1); | |
940 | } | |
941 | /* NOTREACHED */ | |
942 | } | |
943 | ||
944 | void | |
945 | usage() | |
946 | { | |
5260325f | 947 | (void) fprintf(stderr, |
48e671d5 | 948 | "usage: scp [-pqrvC46] [-P port] [-c cipher] [-i identity] f1 f2; or:\n scp [options] f1 ... fn directory\n"); |
8efc0c15 | 949 | exit(1); |
950 | } | |
951 | ||
952 | void | |
5260325f | 953 | run_err(const char *fmt,...) |
8efc0c15 | 954 | { |
955 | static FILE *fp; | |
956 | va_list ap; | |
8efc0c15 | 957 | |
958 | ++errs; | |
959 | if (fp == NULL && !(fp = fdopen(remout, "w"))) | |
960 | return; | |
5260325f | 961 | (void) fprintf(fp, "%c", 0x01); |
962 | (void) fprintf(fp, "scp: "); | |
b92964b7 | 963 | va_start(ap, fmt); |
5260325f | 964 | (void) vfprintf(fp, fmt, ap); |
b92964b7 | 965 | va_end(ap); |
5260325f | 966 | (void) fprintf(fp, "\n"); |
967 | (void) fflush(fp); | |
968 | ||
969 | if (!iamremote) { | |
b92964b7 | 970 | va_start(ap, fmt); |
5260325f | 971 | vfprintf(stderr, fmt, ap); |
b92964b7 | 972 | va_end(ap); |
5260325f | 973 | fprintf(stderr, "\n"); |
974 | } | |
8efc0c15 | 975 | } |
976 | ||
977 | /* Stuff below is from BSD rcp util.c. */ | |
978 | ||
979 | /*- | |
980 | * Copyright (c) 1992, 1993 | |
981 | * The Regents of the University of California. All rights reserved. | |
982 | * | |
983 | * Redistribution and use in source and binary forms, with or without | |
984 | * modification, are permitted provided that the following conditions | |
985 | * are met: | |
986 | * 1. Redistributions of source code must retain the above copyright | |
987 | * notice, this list of conditions and the following disclaimer. | |
988 | * 2. Redistributions in binary form must reproduce the above copyright | |
989 | * notice, this list of conditions and the following disclaimer in the | |
990 | * documentation and/or other materials provided with the distribution. | |
991 | * 3. All advertising materials mentioning features or use of this software | |
992 | * must display the following acknowledgement: | |
993 | * This product includes software developed by the University of | |
994 | * California, Berkeley and its contributors. | |
995 | * 4. Neither the name of the University nor the names of its contributors | |
996 | * may be used to endorse or promote products derived from this software | |
997 | * without specific prior written permission. | |
998 | * | |
999 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
1000 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
1001 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
1002 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
1003 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
1004 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
1005 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
1006 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
1007 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
1008 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
1009 | * SUCH DAMAGE. | |
1010 | * | |
1011 | * $Id$ | |
1012 | */ | |
1013 | ||
1014 | char * | |
1015 | colon(cp) | |
1016 | char *cp; | |
1017 | { | |
48e671d5 | 1018 | int flag = 0; |
1019 | ||
8efc0c15 | 1020 | if (*cp == ':') /* Leading colon is part of file name. */ |
1021 | return (0); | |
48e671d5 | 1022 | if (*cp == '[') |
1023 | flag = 1; | |
8efc0c15 | 1024 | |
1025 | for (; *cp; ++cp) { | |
48e671d5 | 1026 | if (*cp == '@' && *(cp+1) == '[') |
1027 | flag = 1; | |
1028 | if (*cp == ']' && *(cp+1) == ':' && flag) | |
1029 | return (cp+1); | |
1030 | if (*cp == ':' && !flag) | |
8efc0c15 | 1031 | return (cp); |
1032 | if (*cp == '/') | |
1033 | return (0); | |
1034 | } | |
1035 | return (0); | |
1036 | } | |
1037 | ||
1038 | void | |
1039 | verifydir(cp) | |
1040 | char *cp; | |
1041 | { | |
1042 | struct stat stb; | |
1043 | ||
1044 | if (!stat(cp, &stb)) { | |
1045 | if (S_ISDIR(stb.st_mode)) | |
1046 | return; | |
1047 | errno = ENOTDIR; | |
1048 | } | |
1049 | run_err("%s: %s", cp, strerror(errno)); | |
1050 | exit(1); | |
1051 | } | |
1052 | ||
1053 | int | |
1054 | okname(cp0) | |
1055 | char *cp0; | |
1056 | { | |
1057 | int c; | |
1058 | char *cp; | |
1059 | ||
1060 | cp = cp0; | |
1061 | do { | |
1062 | c = *cp; | |
1063 | if (c & 0200) | |
1064 | goto bad; | |
1065 | if (!isalpha(c) && !isdigit(c) && c != '_' && c != '-') | |
1066 | goto bad; | |
1067 | } while (*++cp); | |
1068 | return (1); | |
1069 | ||
c8d54615 | 1070 | bad: fprintf(stderr, "%s: invalid user name\n", cp0); |
8efc0c15 | 1071 | return (0); |
1072 | } | |
1073 | ||
1074 | BUF * | |
1075 | allocbuf(bp, fd, blksize) | |
1076 | BUF *bp; | |
1077 | int fd, blksize; | |
1078 | { | |
1079 | size_t size; | |
1080 | struct stat stb; | |
1081 | ||
1082 | if (fstat(fd, &stb) < 0) { | |
1083 | run_err("fstat: %s", strerror(errno)); | |
1084 | return (0); | |
1085 | } | |
5260325f | 1086 | if (stb.st_blksize == 0) |
1087 | size = blksize; | |
1088 | else | |
1089 | size = blksize + (stb.st_blksize - blksize % stb.st_blksize) % | |
1090 | stb.st_blksize; | |
8efc0c15 | 1091 | if (bp->cnt >= size) |
1092 | return (bp); | |
5260325f | 1093 | if (bp->buf == NULL) |
1094 | bp->buf = xmalloc(size); | |
1095 | else | |
1096 | bp->buf = xrealloc(bp->buf, size); | |
8efc0c15 | 1097 | bp->cnt = size; |
1098 | return (bp); | |
1099 | } | |
1100 | ||
1101 | void | |
1102 | lostconn(signo) | |
1103 | int signo; | |
1104 | { | |
1105 | if (!iamremote) | |
1106 | fprintf(stderr, "lost connection\n"); | |
1107 | exit(1); | |
1108 | } | |
1109 | ||
8efc0c15 | 1110 | |
1111 | void | |
1112 | alarmtimer(int wait) | |
1113 | { | |
5260325f | 1114 | struct itimerval itv; |
8efc0c15 | 1115 | |
5260325f | 1116 | itv.it_value.tv_sec = wait; |
1117 | itv.it_value.tv_usec = 0; | |
1118 | itv.it_interval = itv.it_value; | |
1119 | setitimer(ITIMER_REAL, &itv, NULL); | |
8efc0c15 | 1120 | } |
1121 | ||
1122 | void | |
610cd5c6 | 1123 | updateprogressmeter(int ignore) |
8efc0c15 | 1124 | { |
1125 | int save_errno = errno; | |
1126 | ||
1127 | progressmeter(0); | |
1128 | errno = save_errno; | |
1129 | } | |
1130 | ||
2bd61362 | 1131 | int |
1132 | foregroundproc() | |
1133 | { | |
1134 | static pid_t pgrp = -1; | |
1135 | int ctty_pgrp; | |
1136 | ||
1137 | if (pgrp == -1) | |
1138 | pgrp = getpgrp(); | |
1139 | ||
5260325f | 1140 | return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 && |
1141 | ctty_pgrp == pgrp)); | |
2bd61362 | 1142 | } |
1143 | ||
8efc0c15 | 1144 | void |
1145 | progressmeter(int flag) | |
1146 | { | |
1147 | static const char prefixes[] = " KMGTP"; | |
1148 | static struct timeval lastupdate; | |
1149 | static off_t lastsize; | |
1150 | struct timeval now, td, wait; | |
1151 | off_t cursize, abbrevsize; | |
1152 | double elapsed; | |
b6573a35 | 1153 | int ratio, barlength, i, remaining; |
8efc0c15 | 1154 | char buf[256]; |
1155 | ||
1156 | if (flag == -1) { | |
5260325f | 1157 | (void) gettimeofday(&start, (struct timezone *) 0); |
8efc0c15 | 1158 | lastupdate = start; |
1159 | lastsize = 0; | |
5260325f | 1160 | } |
2bd61362 | 1161 | if (foregroundproc() == 0) |
1162 | return; | |
1163 | ||
5260325f | 1164 | (void) gettimeofday(&now, (struct timezone *) 0); |
8efc0c15 | 1165 | cursize = statbytes; |
b4ad3727 | 1166 | if (totalbytes != 0) { |
aa3378df | 1167 | ratio = 100.0 * cursize / totalbytes; |
8efc0c15 | 1168 | ratio = MAX(ratio, 0); |
1169 | ratio = MIN(ratio, 100); | |
5260325f | 1170 | } else |
8efc0c15 | 1171 | ratio = 100; |
1172 | ||
5260325f | 1173 | snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio); |
8efc0c15 | 1174 | |
1175 | barlength = getttywidth() - 51; | |
1176 | if (barlength > 0) { | |
1177 | i = barlength * ratio / 100; | |
1178 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | |
5260325f | 1179 | "|%.*s%*s|", i, |
1180 | "*****************************************************************************" | |
1181 | "*****************************************************************************", | |
1182 | barlength - i, ""); | |
8efc0c15 | 1183 | } |
8efc0c15 | 1184 | i = 0; |
1185 | abbrevsize = cursize; | |
1186 | while (abbrevsize >= 100000 && i < sizeof(prefixes)) { | |
1187 | i++; | |
1188 | abbrevsize >>= 10; | |
1189 | } | |
68227e6d | 1190 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5d %c%c ", |
1191 | (int) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' : | |
5260325f | 1192 | 'B'); |
8efc0c15 | 1193 | |
1194 | timersub(&now, &lastupdate, &wait); | |
1195 | if (cursize > lastsize) { | |
1196 | lastupdate = now; | |
1197 | lastsize = cursize; | |
1198 | if (wait.tv_sec >= STALLTIME) { | |
1199 | start.tv_sec += wait.tv_sec; | |
1200 | start.tv_usec += wait.tv_usec; | |
1201 | } | |
1202 | wait.tv_sec = 0; | |
1203 | } | |
8efc0c15 | 1204 | timersub(&now, &start, &td); |
1205 | elapsed = td.tv_sec + (td.tv_usec / 1000000.0); | |
1206 | ||
1207 | if (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes) { | |
1208 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | |
5260325f | 1209 | " --:-- ETA"); |
8efc0c15 | 1210 | } else if (wait.tv_sec >= STALLTIME) { |
1211 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | |
5260325f | 1212 | " - stalled -"); |
8efc0c15 | 1213 | } else { |
d6f24e45 | 1214 | if (flag != 1) |
1215 | remaining = | |
1216 | (int)(totalbytes / (statbytes / elapsed) - elapsed); | |
1217 | else | |
1218 | remaining = elapsed; | |
1219 | ||
5068f573 | 1220 | i = remaining / 3600; |
8efc0c15 | 1221 | if (i) |
1222 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | |
5260325f | 1223 | "%2d:", i); |
8efc0c15 | 1224 | else |
1225 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | |
5260325f | 1226 | " "); |
8efc0c15 | 1227 | i = remaining % 3600; |
1228 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | |
d6f24e45 | 1229 | "%02d:%02d%s", i / 60, i % 60, |
1230 | (flag != 1) ? " ETA" : " "); | |
8efc0c15 | 1231 | } |
1232 | atomicio(write, fileno(stdout), buf, strlen(buf)); | |
1233 | ||
1234 | if (flag == -1) { | |
68227e6d | 1235 | struct sigaction sa; |
1236 | sa.sa_handler = updateprogressmeter; | |
1237 | sigemptyset(&sa.sa_mask); | |
1238 | sa.sa_flags = SA_RESTART; | |
1239 | sigaction(SIGALRM, &sa, NULL); | |
8efc0c15 | 1240 | alarmtimer(1); |
1241 | } else if (flag == 1) { | |
1242 | alarmtimer(0); | |
1243 | write(fileno(stdout), "\n", 1); | |
1244 | statbytes = 0; | |
1245 | } | |
1246 | } | |
1247 | ||
1248 | int | |
1249 | getttywidth(void) | |
1250 | { | |
1251 | struct winsize winsize; | |
1252 | ||
1253 | if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1) | |
5260325f | 1254 | return (winsize.ws_col ? winsize.ws_col : 80); |
8efc0c15 | 1255 | else |
5260325f | 1256 | return (80); |
8efc0c15 | 1257 | } |