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