]>
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" | |
46660a9e | 78 | RCSID("$OpenBSD: scp.c,v 1.81 2001/08/29 20:44:03 markus 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 | |
cddb9003 | 94 | /* alarm() interval for updating progress meter */ |
95 | #define PROGRESSTIME 1 | |
8efc0c15 | 96 | |
a4070484 | 97 | /* Progress meter bar */ |
98 | #define BAR \ | |
99 | "************************************************************"\ | |
100 | "************************************************************"\ | |
101 | "************************************************************"\ | |
102 | "************************************************************" | |
103 | #define MAX_BARLENGTH (sizeof(BAR) - 1) | |
104 | ||
8efc0c15 | 105 | /* Visual statistics about files as they are transferred. */ |
106 | void progressmeter(int); | |
107 | ||
108 | /* Returns width of the terminal (for progress meter calculations). */ | |
109 | int getttywidth(void); | |
2e73a022 | 110 | int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc); |
8efc0c15 | 111 | |
8a624ebf | 112 | /* Struct for addargs */ |
113 | arglist args; | |
94ec8c6b | 114 | |
8efc0c15 | 115 | /* Time a transfer started. */ |
116 | static struct timeval start; | |
117 | ||
118 | /* Number of bytes of current file transferred so far. */ | |
ba917921 | 119 | volatile off_t statbytes; |
8efc0c15 | 120 | |
121 | /* Total size of current file. */ | |
b4ad3727 | 122 | off_t totalbytes = 0; |
8efc0c15 | 123 | |
124 | /* Name of current file being transferred. */ | |
125 | char *curfile; | |
126 | ||
127 | /* This is set to non-zero to enable verbose mode. */ | |
5260325f | 128 | int verbose_mode = 0; |
8efc0c15 | 129 | |
8efc0c15 | 130 | /* This is set to zero if the progressmeter is not desired. */ |
131 | int showprogress = 1; | |
132 | ||
2e73a022 | 133 | /* This is the program to execute for the secured connection. ("ssh" or -S) */ |
42f11eb2 | 134 | char *ssh_program = _PATH_SSH_PROGRAM; |
2e73a022 | 135 | |
aa3378df | 136 | /* |
137 | * This function executes the given command as the specified user on the | |
138 | * given host. This returns < 0 if execution fails, and >= 0 otherwise. This | |
139 | * assigns the input and output file descriptors on success. | |
140 | */ | |
8efc0c15 | 141 | |
6ae2364d | 142 | int |
2e73a022 | 143 | do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc) |
8efc0c15 | 144 | { |
5260325f | 145 | int pin[2], pout[2], reserved[2]; |
146 | ||
147 | if (verbose_mode) | |
0b2988ca | 148 | fprintf(stderr, |
149 | "Executing: program %s host %s, user %s, command %s\n", | |
150 | ssh_program, host, | |
151 | remuser ? remuser : "(unspecified)", cmd); | |
5260325f | 152 | |
aa3378df | 153 | /* |
154 | * Reserve two descriptors so that the real pipes won't get | |
155 | * descriptors 0 and 1 because that will screw up dup2 below. | |
156 | */ | |
5260325f | 157 | pipe(reserved); |
158 | ||
159 | /* Create a socket pair for communicating with ssh. */ | |
160 | if (pipe(pin) < 0) | |
161 | fatal("pipe: %s", strerror(errno)); | |
162 | if (pipe(pout) < 0) | |
163 | fatal("pipe: %s", strerror(errno)); | |
164 | ||
165 | /* Free the reserved descriptors. */ | |
166 | close(reserved[0]); | |
167 | close(reserved[1]); | |
168 | ||
169 | /* For a child to execute the command on the remote host using ssh. */ | |
94ec8c6b | 170 | if (fork() == 0) { |
5260325f | 171 | /* Child. */ |
172 | close(pin[1]); | |
173 | close(pout[0]); | |
174 | dup2(pin[0], 0); | |
175 | dup2(pout[1], 1); | |
176 | close(pin[0]); | |
177 | close(pout[1]); | |
178 | ||
94ec8c6b | 179 | args.list[0] = ssh_program; |
180 | if (remuser != NULL) | |
8a624ebf | 181 | addargs(&args, "-l%s", remuser); |
182 | addargs(&args, "%s", host); | |
183 | addargs(&args, "%s", cmd); | |
5260325f | 184 | |
94ec8c6b | 185 | execvp(ssh_program, args.list); |
2e73a022 | 186 | perror(ssh_program); |
5260325f | 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 | ||
8efc0c15 | 197 | typedef struct { |
198 | int cnt; | |
199 | char *buf; | |
200 | } BUF; | |
201 | ||
5260325f | 202 | BUF *allocbuf(BUF *, int, int); |
5260325f | 203 | void lostconn(int); |
204 | void nospace(void); | |
205 | int okname(char *); | |
206 | void run_err(const char *,...); | |
207 | void verifydir(char *); | |
8efc0c15 | 208 | |
8efc0c15 | 209 | struct passwd *pwd; |
5260325f | 210 | uid_t userid; |
8efc0c15 | 211 | int errs, remin, remout; |
212 | int pflag, iamremote, iamrecursive, targetshouldbedirectory; | |
213 | ||
214 | #define CMDNEEDS 64 | |
215 | char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */ | |
216 | ||
5260325f | 217 | int response(void); |
218 | void rsource(char *, struct stat *); | |
219 | void sink(int, char *[]); | |
220 | void source(int, char *[]); | |
221 | void tolocal(int, char *[]); | |
222 | void toremote(char *, int, char *[]); | |
223 | void usage(void); | |
8efc0c15 | 224 | |
225 | int | |
226 | main(argc, argv) | |
227 | int argc; | |
228 | char *argv[]; | |
229 | { | |
230 | int ch, fflag, tflag; | |
231 | char *targ; | |
232 | extern char *optarg; | |
233 | extern int optind; | |
234 | ||
260d427b | 235 | __progname = get_progname(argv[0]); |
236 | ||
94ec8c6b | 237 | args.list = NULL; |
8a624ebf | 238 | addargs(&args, "ssh"); /* overwritten with ssh_program */ |
239 | addargs(&args, "-x"); | |
240 | addargs(&args, "-oFallBackToRsh no"); | |
94ec8c6b | 241 | |
8efc0c15 | 242 | fflag = tflag = 0; |
b5c334cc | 243 | while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q46S:o:")) != -1) |
5260325f | 244 | switch (ch) { |
245 | /* User-visible flags. */ | |
48e671d5 | 246 | case '4': |
48e671d5 | 247 | case '6': |
94ec8c6b | 248 | case 'C': |
8a624ebf | 249 | addargs(&args, "-%c", ch); |
48e671d5 | 250 | break; |
94ec8c6b | 251 | case 'o': |
252 | case 'c': | |
253 | case 'i': | |
8a624ebf | 254 | addargs(&args, "-%c%s", ch, optarg); |
8efc0c15 | 255 | break; |
256 | case 'P': | |
8a624ebf | 257 | addargs(&args, "-p%s", optarg); |
94ec8c6b | 258 | break; |
259 | case 'B': | |
8a624ebf | 260 | addargs(&args, "-oBatchmode yes"); |
94ec8c6b | 261 | break; |
262 | case 'p': | |
263 | pflag = 1; | |
5260325f | 264 | break; |
8efc0c15 | 265 | case 'r': |
266 | iamrecursive = 1; | |
267 | break; | |
2e73a022 | 268 | case 'S': |
94ec8c6b | 269 | ssh_program = xstrdup(optarg); |
270 | break; | |
271 | case 'v': | |
4fc334a2 | 272 | addargs(&args, "-v"); |
94ec8c6b | 273 | verbose_mode = 1; |
274 | break; | |
275 | case 'q': | |
276 | showprogress = 0; | |
2e73a022 | 277 | break; |
278 | ||
5260325f | 279 | /* Server options. */ |
8efc0c15 | 280 | case 'd': |
281 | targetshouldbedirectory = 1; | |
282 | break; | |
5260325f | 283 | case 'f': /* "from" */ |
8efc0c15 | 284 | iamremote = 1; |
285 | fflag = 1; | |
286 | break; | |
5260325f | 287 | case 't': /* "to" */ |
8efc0c15 | 288 | iamremote = 1; |
289 | tflag = 1; | |
fe56c12b | 290 | #ifdef HAVE_CYGWIN |
291 | setmode(0, O_BINARY); | |
292 | #endif | |
8efc0c15 | 293 | break; |
8efc0c15 | 294 | default: |
295 | usage(); | |
296 | } | |
297 | argc -= optind; | |
298 | argv += optind; | |
299 | ||
300 | if ((pwd = getpwuid(userid = getuid())) == NULL) | |
5260325f | 301 | fatal("unknown user %d", (int) userid); |
8efc0c15 | 302 | |
5260325f | 303 | if (!isatty(STDERR_FILENO)) |
8efc0c15 | 304 | showprogress = 0; |
305 | ||
306 | remin = STDIN_FILENO; | |
307 | remout = STDOUT_FILENO; | |
308 | ||
2b87da3b | 309 | if (fflag) { |
5260325f | 310 | /* Follow "protocol", send data. */ |
311 | (void) response(); | |
8efc0c15 | 312 | source(argc, argv); |
313 | exit(errs != 0); | |
314 | } | |
5260325f | 315 | if (tflag) { |
316 | /* Receive data. */ | |
8efc0c15 | 317 | sink(argc, argv); |
318 | exit(errs != 0); | |
319 | } | |
8efc0c15 | 320 | if (argc < 2) |
321 | usage(); | |
322 | if (argc > 2) | |
323 | targetshouldbedirectory = 1; | |
324 | ||
325 | remin = remout = -1; | |
326 | /* Command to be executed on remote system using "ssh". */ | |
0ed28836 | 327 | (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s", |
328 | verbose_mode ? " -v" : "", | |
2e73a022 | 329 | iamrecursive ? " -r" : "", pflag ? " -p" : "", |
330 | targetshouldbedirectory ? " -d" : ""); | |
8efc0c15 | 331 | |
5260325f | 332 | (void) signal(SIGPIPE, lostconn); |
8efc0c15 | 333 | |
334 | if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */ | |
335 | toremote(targ, argc, argv); | |
336 | else { | |
5260325f | 337 | tolocal(argc, argv); /* Dest is local host. */ |
8efc0c15 | 338 | if (targetshouldbedirectory) |
339 | verifydir(argv[argc - 1]); | |
340 | } | |
341 | exit(errs != 0); | |
342 | } | |
343 | ||
344 | void | |
345 | toremote(targ, argc, argv) | |
346 | char *targ, *argv[]; | |
347 | int argc; | |
348 | { | |
349 | int i, len; | |
350 | char *bp, *host, *src, *suser, *thost, *tuser; | |
351 | ||
352 | *targ++ = 0; | |
353 | if (*targ == 0) | |
354 | targ = "."; | |
355 | ||
356 | if ((thost = strchr(argv[argc - 1], '@'))) { | |
357 | /* user@host */ | |
358 | *thost++ = 0; | |
359 | tuser = argv[argc - 1]; | |
360 | if (*tuser == '\0') | |
361 | tuser = NULL; | |
362 | else if (!okname(tuser)) | |
363 | exit(1); | |
364 | } else { | |
365 | thost = argv[argc - 1]; | |
366 | tuser = NULL; | |
367 | } | |
368 | ||
369 | for (i = 0; i < argc - 1; i++) { | |
370 | src = colon(argv[i]); | |
5260325f | 371 | if (src) { /* remote to remote */ |
8efc0c15 | 372 | *src++ = 0; |
373 | if (*src == 0) | |
374 | src = "."; | |
375 | host = strchr(argv[i], '@'); | |
2e73a022 | 376 | len = strlen(ssh_program) + strlen(argv[i]) + |
377 | strlen(src) + (tuser ? strlen(tuser) : 0) + | |
378 | strlen(thost) + strlen(targ) + CMDNEEDS + 32; | |
5260325f | 379 | bp = xmalloc(len); |
8efc0c15 | 380 | if (host) { |
381 | *host++ = 0; | |
48e671d5 | 382 | host = cleanhostname(host); |
8efc0c15 | 383 | suser = argv[i]; |
384 | if (*suser == '\0') | |
385 | suser = pwd->pw_name; | |
386 | else if (!okname(suser)) | |
387 | continue; | |
0ed28836 | 388 | snprintf(bp, len, |
389 | "%s%s -x -o'FallBackToRsh no' -n " | |
390 | "-l %s %s %s %s '%s%s%s:%s'", | |
71c0d06a | 391 | ssh_program, verbose_mode ? " -v" : "", |
392 | suser, host, cmd, src, | |
393 | tuser ? tuser : "", tuser ? "@" : "", | |
394 | thost, targ); | |
48e671d5 | 395 | } else { |
396 | host = cleanhostname(argv[i]); | |
0ed28836 | 397 | snprintf(bp, len, |
398 | "exec %s%s -x -o'FallBackToRsh no' -n %s " | |
399 | "%s %s '%s%s%s:%s'", | |
71c0d06a | 400 | ssh_program, verbose_mode ? " -v" : "", |
401 | host, cmd, src, | |
402 | tuser ? tuser : "", tuser ? "@" : "", | |
403 | thost, targ); | |
48e671d5 | 404 | } |
5260325f | 405 | if (verbose_mode) |
406 | fprintf(stderr, "Executing: %s\n", bp); | |
407 | (void) system(bp); | |
408 | (void) xfree(bp); | |
409 | } else { /* local to remote */ | |
8efc0c15 | 410 | if (remin == -1) { |
411 | len = strlen(targ) + CMDNEEDS + 20; | |
5260325f | 412 | bp = xmalloc(len); |
0ed28836 | 413 | (void) snprintf(bp, len, "%s -t %s", cmd, targ); |
48e671d5 | 414 | host = cleanhostname(thost); |
2e73a022 | 415 | if (do_cmd(host, tuser, bp, &remin, |
416 | &remout, argc) < 0) | |
5260325f | 417 | exit(1); |
8efc0c15 | 418 | if (response() < 0) |
419 | exit(1); | |
5260325f | 420 | (void) xfree(bp); |
8efc0c15 | 421 | } |
5260325f | 422 | source(1, argv + i); |
8efc0c15 | 423 | } |
424 | } | |
425 | } | |
426 | ||
427 | void | |
428 | tolocal(argc, argv) | |
429 | int argc; | |
430 | char *argv[]; | |
431 | { | |
432 | int i, len; | |
433 | char *bp, *host, *src, *suser; | |
434 | ||
435 | for (i = 0; i < argc - 1; i++) { | |
5260325f | 436 | if (!(src = colon(argv[i]))) { /* Local to local. */ |
8efc0c15 | 437 | len = strlen(_PATH_CP) + strlen(argv[i]) + |
2e73a022 | 438 | strlen(argv[argc - 1]) + 20; |
8efc0c15 | 439 | bp = xmalloc(len); |
0ed28836 | 440 | (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP, |
2e73a022 | 441 | iamrecursive ? " -r" : "", pflag ? " -p" : "", |
442 | argv[i], argv[argc - 1]); | |
5260325f | 443 | if (verbose_mode) |
444 | fprintf(stderr, "Executing: %s\n", bp); | |
8efc0c15 | 445 | if (system(bp)) |
446 | ++errs; | |
5260325f | 447 | (void) xfree(bp); |
8efc0c15 | 448 | continue; |
449 | } | |
450 | *src++ = 0; | |
451 | if (*src == 0) | |
452 | src = "."; | |
453 | if ((host = strchr(argv[i], '@')) == NULL) { | |
454 | host = argv[i]; | |
455 | suser = NULL; | |
456 | } else { | |
457 | *host++ = 0; | |
458 | suser = argv[i]; | |
459 | if (*suser == '\0') | |
460 | suser = pwd->pw_name; | |
461 | else if (!okname(suser)) | |
462 | continue; | |
463 | } | |
48e671d5 | 464 | host = cleanhostname(host); |
8efc0c15 | 465 | len = strlen(src) + CMDNEEDS + 20; |
5260325f | 466 | bp = xmalloc(len); |
0ed28836 | 467 | (void) snprintf(bp, len, "%s -f %s", cmd, src); |
2e73a022 | 468 | if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) { |
5260325f | 469 | (void) xfree(bp); |
470 | ++errs; | |
471 | continue; | |
8efc0c15 | 472 | } |
5260325f | 473 | xfree(bp); |
8efc0c15 | 474 | sink(1, argv + argc - 1); |
5260325f | 475 | (void) close(remin); |
8efc0c15 | 476 | remin = remout = -1; |
477 | } | |
478 | } | |
479 | ||
480 | void | |
481 | source(argc, argv) | |
482 | int argc; | |
483 | char *argv[]; | |
484 | { | |
485 | struct stat stb; | |
486 | static BUF buffer; | |
487 | BUF *bp; | |
ba917921 | 488 | off_t i, amt, result; |
489 | int fd, haderr, indx; | |
8efc0c15 | 490 | char *last, *name, buf[2048]; |
0fc791ba | 491 | int len; |
8efc0c15 | 492 | |
493 | for (indx = 0; indx < argc; ++indx) { | |
5260325f | 494 | name = argv[indx]; |
8efc0c15 | 495 | statbytes = 0; |
0fc791ba | 496 | len = strlen(name); |
497 | while (len > 1 && name[len-1] == '/') | |
498 | name[--len] = '\0'; | |
8efc0c15 | 499 | if ((fd = open(name, O_RDONLY, 0)) < 0) |
500 | goto syserr; | |
501 | if (fstat(fd, &stb) < 0) { | |
502 | syserr: run_err("%s: %s", name, strerror(errno)); | |
503 | goto next; | |
504 | } | |
505 | switch (stb.st_mode & S_IFMT) { | |
506 | case S_IFREG: | |
507 | break; | |
508 | case S_IFDIR: | |
509 | if (iamrecursive) { | |
510 | rsource(name, &stb); | |
511 | goto next; | |
512 | } | |
513 | /* FALLTHROUGH */ | |
514 | default: | |
515 | run_err("%s: not a regular file", name); | |
516 | goto next; | |
517 | } | |
518 | if ((last = strrchr(name, '/')) == NULL) | |
519 | last = name; | |
520 | else | |
521 | ++last; | |
522 | curfile = last; | |
523 | if (pflag) { | |
524 | /* | |
525 | * Make it compatible with possible future | |
526 | * versions expecting microseconds. | |
527 | */ | |
0ed28836 | 528 | (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n", |
1e3b8b07 | 529 | (u_long) stb.st_mtime, |
530 | (u_long) stb.st_atime); | |
3189621b | 531 | (void) atomicio(write, remout, buf, strlen(buf)); |
8efc0c15 | 532 | if (response() < 0) |
533 | goto next; | |
534 | } | |
535 | #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO) | |
30eac028 | 536 | #ifdef HAVE_LONG_LONG_INT |
bf1d27bd | 537 | snprintf(buf, sizeof buf, "C%04o %lld %s\n", |
1e3b8b07 | 538 | (u_int) (stb.st_mode & FILEMODEMASK), |
ec0ad9c2 | 539 | (long long) stb.st_size, last); |
f8f230bf | 540 | #else |
541 | /* XXX: Handle integer overflow? */ | |
d1581d5f | 542 | snprintf(buf, sizeof buf, "C%04o %lu %s\n", |
f8f230bf | 543 | (u_int) (stb.st_mode & FILEMODEMASK), |
d1581d5f | 544 | (u_long) stb.st_size, last); |
f8f230bf | 545 | #endif |
546 | ||
5260325f | 547 | if (verbose_mode) { |
548 | fprintf(stderr, "Sending file modes: %s", buf); | |
549 | fflush(stderr); | |
550 | } | |
3189621b | 551 | (void) atomicio(write, remout, buf, strlen(buf)); |
8efc0c15 | 552 | if (response() < 0) |
553 | goto next; | |
554 | if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) { | |
5260325f | 555 | next: (void) close(fd); |
8efc0c15 | 556 | continue; |
557 | } | |
8efc0c15 | 558 | if (showprogress) { |
559 | totalbytes = stb.st_size; | |
560 | progressmeter(-1); | |
561 | } | |
8efc0c15 | 562 | /* Keep writing after an error so that we stay sync'd up. */ |
563 | for (haderr = i = 0; i < stb.st_size; i += bp->cnt) { | |
564 | amt = bp->cnt; | |
565 | if (i + amt > stb.st_size) | |
566 | amt = stb.st_size - i; | |
567 | if (!haderr) { | |
1d1ffb87 | 568 | result = atomicio(read, fd, bp->buf, amt); |
8efc0c15 | 569 | if (result != amt) |
570 | haderr = result >= 0 ? EIO : errno; | |
571 | } | |
572 | if (haderr) | |
3189621b | 573 | (void) atomicio(write, remout, bp->buf, amt); |
8efc0c15 | 574 | else { |
68227e6d | 575 | result = atomicio(write, remout, bp->buf, amt); |
8efc0c15 | 576 | if (result != amt) |
577 | haderr = result >= 0 ? EIO : errno; | |
578 | statbytes += result; | |
579 | } | |
580 | } | |
5260325f | 581 | if (showprogress) |
8efc0c15 | 582 | progressmeter(1); |
583 | ||
584 | if (close(fd) < 0 && !haderr) | |
585 | haderr = errno; | |
586 | if (!haderr) | |
3189621b | 587 | (void) atomicio(write, remout, "", 1); |
8efc0c15 | 588 | else |
589 | run_err("%s: %s", name, strerror(haderr)); | |
5260325f | 590 | (void) response(); |
8efc0c15 | 591 | } |
592 | } | |
593 | ||
594 | void | |
595 | rsource(name, statp) | |
596 | char *name; | |
597 | struct stat *statp; | |
598 | { | |
599 | DIR *dirp; | |
600 | struct dirent *dp; | |
601 | char *last, *vect[1], path[1100]; | |
602 | ||
603 | if (!(dirp = opendir(name))) { | |
604 | run_err("%s: %s", name, strerror(errno)); | |
605 | return; | |
606 | } | |
607 | last = strrchr(name, '/'); | |
608 | if (last == 0) | |
609 | last = name; | |
610 | else | |
611 | last++; | |
612 | if (pflag) { | |
0ed28836 | 613 | (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n", |
1e3b8b07 | 614 | (u_long) statp->st_mtime, |
615 | (u_long) statp->st_atime); | |
3189621b | 616 | (void) atomicio(write, remout, path, strlen(path)); |
8efc0c15 | 617 | if (response() < 0) { |
618 | closedir(dirp); | |
619 | return; | |
620 | } | |
621 | } | |
0ed28836 | 622 | (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n", |
1e3b8b07 | 623 | (u_int) (statp->st_mode & FILEMODEMASK), 0, last); |
5260325f | 624 | if (verbose_mode) |
625 | fprintf(stderr, "Entering directory: %s", path); | |
3189621b | 626 | (void) atomicio(write, remout, path, strlen(path)); |
8efc0c15 | 627 | if (response() < 0) { |
628 | closedir(dirp); | |
629 | return; | |
630 | } | |
e5ff6ecf | 631 | while ((dp = readdir(dirp)) != NULL) { |
8efc0c15 | 632 | if (dp->d_ino == 0) |
633 | continue; | |
634 | if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) | |
635 | continue; | |
636 | if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) { | |
637 | run_err("%s/%s: name too long", name, dp->d_name); | |
638 | continue; | |
639 | } | |
0ed28836 | 640 | (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name); |
8efc0c15 | 641 | vect[0] = path; |
642 | source(1, vect); | |
643 | } | |
5260325f | 644 | (void) closedir(dirp); |
3189621b | 645 | (void) atomicio(write, remout, "E\n", 2); |
5260325f | 646 | (void) response(); |
8efc0c15 | 647 | } |
648 | ||
649 | void | |
650 | sink(argc, argv) | |
651 | int argc; | |
652 | char *argv[]; | |
653 | { | |
654 | static BUF buffer; | |
655 | struct stat stb; | |
5260325f | 656 | enum { |
657 | YES, NO, DISPLAYED | |
658 | } wrerr; | |
8efc0c15 | 659 | BUF *bp; |
660 | off_t i, j; | |
661 | int amt, count, exists, first, mask, mode, ofd, omode; | |
14a9a859 | 662 | off_t size; |
663 | int setimes, targisdir, wrerrno = 0; | |
8efc0c15 | 664 | char ch, *cp, *np, *targ, *why, *vect[1], buf[2048]; |
188adeb2 | 665 | struct timeval tv[2]; |
8efc0c15 | 666 | |
36967a16 | 667 | #define atime tv[0] |
668 | #define mtime tv[1] | |
cb220a93 | 669 | #define SCREWUP(str) do { why = str; goto screwup; } while (0) |
8efc0c15 | 670 | |
671 | setimes = targisdir = 0; | |
672 | mask = umask(0); | |
673 | if (!pflag) | |
5260325f | 674 | (void) umask(mask); |
8efc0c15 | 675 | if (argc != 1) { |
676 | run_err("ambiguous target"); | |
677 | exit(1); | |
678 | } | |
679 | targ = *argv; | |
680 | if (targetshouldbedirectory) | |
681 | verifydir(targ); | |
5260325f | 682 | |
3189621b | 683 | (void) atomicio(write, remout, "", 1); |
8efc0c15 | 684 | if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode)) |
685 | targisdir = 1; | |
686 | for (first = 1;; first = 0) { | |
687 | cp = buf; | |
1d1ffb87 | 688 | if (atomicio(read, remin, cp, 1) <= 0) |
8efc0c15 | 689 | return; |
690 | if (*cp++ == '\n') | |
691 | SCREWUP("unexpected <newline>"); | |
692 | do { | |
1d1ffb87 | 693 | if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch)) |
8efc0c15 | 694 | SCREWUP("lost connection"); |
695 | *cp++ = ch; | |
696 | } while (cp < &buf[sizeof(buf) - 1] && ch != '\n'); | |
697 | *cp = 0; | |
698 | ||
699 | if (buf[0] == '\01' || buf[0] == '\02') { | |
700 | if (iamremote == 0) | |
3189621b | 701 | (void) atomicio(write, STDERR_FILENO, |
71c0d06a | 702 | buf + 1, strlen(buf + 1)); |
8efc0c15 | 703 | if (buf[0] == '\02') |
704 | exit(1); | |
705 | ++errs; | |
706 | continue; | |
707 | } | |
708 | if (buf[0] == 'E') { | |
3189621b | 709 | (void) atomicio(write, remout, "", 1); |
8efc0c15 | 710 | return; |
711 | } | |
8efc0c15 | 712 | if (ch == '\n') |
713 | *--cp = 0; | |
714 | ||
8efc0c15 | 715 | cp = buf; |
716 | if (*cp == 'T') { | |
717 | setimes++; | |
718 | cp++; | |
36967a16 | 719 | mtime.tv_sec = strtol(cp, &cp, 10); |
720 | if (!cp || *cp++ != ' ') | |
8efc0c15 | 721 | SCREWUP("mtime.sec not delimited"); |
36967a16 | 722 | mtime.tv_usec = strtol(cp, &cp, 10); |
723 | if (!cp || *cp++ != ' ') | |
8efc0c15 | 724 | SCREWUP("mtime.usec not delimited"); |
36967a16 | 725 | atime.tv_sec = strtol(cp, &cp, 10); |
726 | if (!cp || *cp++ != ' ') | |
8efc0c15 | 727 | SCREWUP("atime.sec not delimited"); |
36967a16 | 728 | atime.tv_usec = strtol(cp, &cp, 10); |
729 | if (!cp || *cp++ != '\0') | |
8efc0c15 | 730 | SCREWUP("atime.usec not delimited"); |
3189621b | 731 | (void) atomicio(write, remout, "", 1); |
8efc0c15 | 732 | continue; |
733 | } | |
734 | if (*cp != 'C' && *cp != 'D') { | |
735 | /* | |
736 | * Check for the case "rcp remote:foo\* local:bar". | |
737 | * In this case, the line "No match." can be returned | |
738 | * by the shell before the rcp command on the remote is | |
739 | * executed so the ^Aerror_message convention isn't | |
740 | * followed. | |
741 | */ | |
742 | if (first) { | |
743 | run_err("%s", cp); | |
744 | exit(1); | |
745 | } | |
746 | SCREWUP("expected control record"); | |
747 | } | |
748 | mode = 0; | |
749 | for (++cp; cp < buf + 5; cp++) { | |
750 | if (*cp < '0' || *cp > '7') | |
751 | SCREWUP("bad mode"); | |
752 | mode = (mode << 3) | (*cp - '0'); | |
753 | } | |
754 | if (*cp++ != ' ') | |
755 | SCREWUP("mode not delimited"); | |
756 | ||
e5ff6ecf | 757 | for (size = 0; isdigit(*cp);) |
8efc0c15 | 758 | size = size * 10 + (*cp++ - '0'); |
759 | if (*cp++ != ' ') | |
760 | SCREWUP("size not delimited"); | |
761 | if (targisdir) { | |
762 | static char *namebuf; | |
763 | static int cursize; | |
764 | size_t need; | |
765 | ||
766 | need = strlen(targ) + strlen(cp) + 250; | |
0ed28836 | 767 | if (need > cursize) { |
768 | if (namebuf) | |
769 | xfree(namebuf); | |
5260325f | 770 | namebuf = xmalloc(need); |
0ed28836 | 771 | cursize = need; |
772 | } | |
773 | (void) snprintf(namebuf, need, "%s%s%s", targ, | |
2e73a022 | 774 | *targ ? "/" : "", cp); |
8efc0c15 | 775 | np = namebuf; |
776 | } else | |
777 | np = targ; | |
778 | curfile = cp; | |
779 | exists = stat(np, &stb) == 0; | |
780 | if (buf[0] == 'D') { | |
781 | int mod_flag = pflag; | |
782 | if (exists) { | |
783 | if (!S_ISDIR(stb.st_mode)) { | |
784 | errno = ENOTDIR; | |
785 | goto bad; | |
786 | } | |
787 | if (pflag) | |
5260325f | 788 | (void) chmod(np, mode); |
8efc0c15 | 789 | } else { |
5260325f | 790 | /* Handle copying from a read-only |
791 | directory */ | |
8efc0c15 | 792 | mod_flag = 1; |
793 | if (mkdir(np, mode | S_IRWXU) < 0) | |
794 | goto bad; | |
795 | } | |
5c470997 | 796 | vect[0] = xstrdup(np); |
8efc0c15 | 797 | sink(1, vect); |
798 | if (setimes) { | |
799 | setimes = 0; | |
e68225b2 | 800 | if (utimes(vect[0], tv) < 0) |
5260325f | 801 | run_err("%s: set times: %s", |
e68225b2 | 802 | vect[0], strerror(errno)); |
8efc0c15 | 803 | } |
804 | if (mod_flag) | |
e68225b2 | 805 | (void) chmod(vect[0], mode); |
806 | if (vect[0]) | |
807 | xfree(vect[0]); | |
8efc0c15 | 808 | continue; |
809 | } | |
810 | omode = mode; | |
811 | mode |= S_IWRITE; | |
3e4fc5f9 | 812 | if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) { |
8efc0c15 | 813 | bad: run_err("%s: %s", np, strerror(errno)); |
814 | continue; | |
815 | } | |
3189621b | 816 | (void) atomicio(write, remout, "", 1); |
8efc0c15 | 817 | if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) { |
5260325f | 818 | (void) close(ofd); |
8efc0c15 | 819 | continue; |
820 | } | |
821 | cp = bp->buf; | |
822 | wrerr = NO; | |
823 | ||
824 | if (showprogress) { | |
825 | totalbytes = size; | |
826 | progressmeter(-1); | |
827 | } | |
828 | statbytes = 0; | |
829 | for (count = i = 0; i < size; i += 4096) { | |
830 | amt = 4096; | |
831 | if (i + amt > size) | |
832 | amt = size - i; | |
833 | count += amt; | |
834 | do { | |
a22aff1f | 835 | j = read(remin, cp, amt); |
0b2988ca | 836 | if (j == -1 && (errno == EINTR || |
837 | errno == EAGAIN)) { | |
a22aff1f | 838 | continue; |
839 | } else if (j <= 0) { | |
8efc0c15 | 840 | run_err("%s", j ? strerror(errno) : |
e5ff6ecf | 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) { | |
1d1ffb87 | 851 | j = atomicio(write, ofd, bp->buf, count); |
8efc0c15 | 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 && | |
1d1ffb87 | 864 | (j = atomicio(write, ofd, bp->buf, count)) != count) { |
8efc0c15 | 865 | wrerr = YES; |
5260325f | 866 | wrerrno = j >= 0 ? EIO : errno; |
8efc0c15 | 867 | } |
8efc0c15 | 868 | if (ftruncate(ofd, size)) { |
869 | run_err("%s: truncate: %s", np, strerror(errno)); | |
870 | wrerr = DISPLAYED; | |
871 | } | |
8efc0c15 | 872 | if (pflag) { |
873 | if (exists || omode != mode) | |
77bb0bca | 874 | #ifdef HAVE_FCHMOD |
8efc0c15 | 875 | if (fchmod(ofd, omode)) |
77bb0bca | 876 | #else /* HAVE_FCHMOD */ |
877 | if (chmod(np, omode)) | |
878 | #endif /* HAVE_FCHMOD */ | |
8efc0c15 | 879 | run_err("%s: set mode: %s", |
e5ff6ecf | 880 | np, strerror(errno)); |
8efc0c15 | 881 | } else { |
882 | if (!exists && omode != mode) | |
77bb0bca | 883 | #ifdef HAVE_FCHMOD |
8efc0c15 | 884 | if (fchmod(ofd, omode & ~mask)) |
77bb0bca | 885 | #else /* HAVE_FCHMOD */ |
886 | if (chmod(np, omode & ~mask)) | |
887 | #endif /* HAVE_FCHMOD */ | |
8efc0c15 | 888 | run_err("%s: set mode: %s", |
e5ff6ecf | 889 | np, strerror(errno)); |
8efc0c15 | 890 | } |
704b1659 | 891 | if (close(ofd) == -1) { |
892 | wrerr = YES; | |
893 | wrerrno = errno; | |
894 | } | |
5260325f | 895 | (void) response(); |
8efc0c15 | 896 | if (setimes && wrerr == NO) { |
897 | setimes = 0; | |
188adeb2 | 898 | if (utimes(np, tv) < 0) { |
8efc0c15 | 899 | run_err("%s: set times: %s", |
e5ff6ecf | 900 | np, strerror(errno)); |
8efc0c15 | 901 | wrerr = DISPLAYED; |
902 | } | |
903 | } | |
5260325f | 904 | switch (wrerr) { |
8efc0c15 | 905 | case YES: |
906 | run_err("%s: %s", np, strerror(wrerrno)); | |
907 | break; | |
908 | case NO: | |
3189621b | 909 | (void) atomicio(write, remout, "", 1); |
8efc0c15 | 910 | break; |
911 | case DISPLAYED: | |
912 | break; | |
913 | } | |
914 | } | |
915 | screwup: | |
916 | run_err("protocol error: %s", why); | |
917 | exit(1); | |
918 | } | |
919 | ||
920 | int | |
921 | response() | |
922 | { | |
923 | char ch, *cp, resp, rbuf[2048]; | |
924 | ||
1d1ffb87 | 925 | if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp)) |
8efc0c15 | 926 | lostconn(0); |
927 | ||
928 | cp = rbuf; | |
5260325f | 929 | switch (resp) { |
930 | case 0: /* ok */ | |
8efc0c15 | 931 | return (0); |
932 | default: | |
933 | *cp++ = resp; | |
934 | /* FALLTHROUGH */ | |
5260325f | 935 | case 1: /* error, followed by error msg */ |
936 | case 2: /* fatal error, "" */ | |
8efc0c15 | 937 | do { |
1d1ffb87 | 938 | if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch)) |
8efc0c15 | 939 | lostconn(0); |
940 | *cp++ = ch; | |
941 | } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n'); | |
942 | ||
943 | if (!iamremote) | |
3189621b | 944 | (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf); |
8efc0c15 | 945 | ++errs; |
946 | if (resp == 1) | |
947 | return (-1); | |
948 | exit(1); | |
949 | } | |
950 | /* NOTREACHED */ | |
951 | } | |
952 | ||
953 | void | |
954 | usage() | |
955 | { | |
2e73a022 | 956 | (void) fprintf(stderr, "usage: scp " |
c3941fa6 | 957 | "[-pqrvBC46] [-S ssh] [-P port] [-c cipher] [-i identity] " |
958 | "[-o option] f1 f2\n" | |
03cb2621 | 959 | " or: scp [options] f1 ... fn directory\n"); |
8efc0c15 | 960 | exit(1); |
961 | } | |
962 | ||
963 | void | |
5260325f | 964 | run_err(const char *fmt,...) |
8efc0c15 | 965 | { |
966 | static FILE *fp; | |
967 | va_list ap; | |
8efc0c15 | 968 | |
969 | ++errs; | |
970 | if (fp == NULL && !(fp = fdopen(remout, "w"))) | |
971 | return; | |
5260325f | 972 | (void) fprintf(fp, "%c", 0x01); |
973 | (void) fprintf(fp, "scp: "); | |
b92964b7 | 974 | va_start(ap, fmt); |
5260325f | 975 | (void) vfprintf(fp, fmt, ap); |
b92964b7 | 976 | va_end(ap); |
5260325f | 977 | (void) fprintf(fp, "\n"); |
978 | (void) fflush(fp); | |
979 | ||
980 | if (!iamremote) { | |
b92964b7 | 981 | va_start(ap, fmt); |
5260325f | 982 | vfprintf(stderr, fmt, ap); |
b92964b7 | 983 | va_end(ap); |
5260325f | 984 | fprintf(stderr, "\n"); |
985 | } | |
8efc0c15 | 986 | } |
987 | ||
8efc0c15 | 988 | void |
989 | verifydir(cp) | |
990 | char *cp; | |
991 | { | |
992 | struct stat stb; | |
993 | ||
994 | if (!stat(cp, &stb)) { | |
995 | if (S_ISDIR(stb.st_mode)) | |
996 | return; | |
997 | errno = ENOTDIR; | |
998 | } | |
999 | run_err("%s: %s", cp, strerror(errno)); | |
1000 | exit(1); | |
1001 | } | |
1002 | ||
1003 | int | |
1004 | okname(cp0) | |
1005 | char *cp0; | |
1006 | { | |
1007 | int c; | |
1008 | char *cp; | |
1009 | ||
1010 | cp = cp0; | |
1011 | do { | |
cb220a93 | 1012 | c = (int)*cp; |
8efc0c15 | 1013 | if (c & 0200) |
1014 | goto bad; | |
731c1541 | 1015 | if (!isalpha(c) && !isdigit(c) && |
1016 | c != '_' && c != '-' && c != '.' && c != '+') | |
8efc0c15 | 1017 | goto bad; |
1018 | } while (*++cp); | |
1019 | return (1); | |
1020 | ||
c8d54615 | 1021 | bad: fprintf(stderr, "%s: invalid user name\n", cp0); |
8efc0c15 | 1022 | return (0); |
1023 | } | |
1024 | ||
1025 | BUF * | |
1026 | allocbuf(bp, fd, blksize) | |
1027 | BUF *bp; | |
1028 | int fd, blksize; | |
1029 | { | |
1030 | size_t size; | |
77bb0bca | 1031 | #ifdef HAVE_ST_BLKSIZE |
8efc0c15 | 1032 | struct stat stb; |
1033 | ||
1034 | if (fstat(fd, &stb) < 0) { | |
1035 | run_err("fstat: %s", strerror(errno)); | |
1036 | return (0); | |
1037 | } | |
5260325f | 1038 | if (stb.st_blksize == 0) |
1039 | size = blksize; | |
1040 | else | |
1041 | size = blksize + (stb.st_blksize - blksize % stb.st_blksize) % | |
2e73a022 | 1042 | stb.st_blksize; |
77bb0bca | 1043 | #else /* HAVE_ST_BLKSIZE */ |
2b87da3b | 1044 | size = blksize; |
77bb0bca | 1045 | #endif /* HAVE_ST_BLKSIZE */ |
8efc0c15 | 1046 | if (bp->cnt >= size) |
1047 | return (bp); | |
5260325f | 1048 | if (bp->buf == NULL) |
1049 | bp->buf = xmalloc(size); | |
1050 | else | |
1051 | bp->buf = xrealloc(bp->buf, size); | |
46660a9e | 1052 | memset(bp->buf, 0, size); |
8efc0c15 | 1053 | bp->cnt = size; |
1054 | return (bp); | |
1055 | } | |
1056 | ||
1057 | void | |
1058 | lostconn(signo) | |
1059 | int signo; | |
1060 | { | |
1061 | if (!iamremote) | |
08c260ea | 1062 | write(STDERR_FILENO, "lost connection\n", 16); |
1063 | if (signo) | |
1064 | _exit(1); | |
1065 | else | |
1066 | exit(1); | |
8efc0c15 | 1067 | } |
1068 | ||
396c147e | 1069 | static void |
610cd5c6 | 1070 | updateprogressmeter(int ignore) |
8efc0c15 | 1071 | { |
1072 | int save_errno = errno; | |
1073 | ||
1074 | progressmeter(0); | |
cddb9003 | 1075 | signal(SIGALRM, updateprogressmeter); |
1076 | alarm(PROGRESSTIME); | |
8efc0c15 | 1077 | errno = save_errno; |
1078 | } | |
1079 | ||
396c147e | 1080 | static int |
5ca51e19 | 1081 | foregroundproc(void) |
2bd61362 | 1082 | { |
1083 | static pid_t pgrp = -1; | |
1084 | int ctty_pgrp; | |
1085 | ||
1086 | if (pgrp == -1) | |
1087 | pgrp = getpgrp(); | |
1088 | ||
63fe0529 | 1089 | #ifdef HAVE_TCGETPGRP |
3c62e7eb | 1090 | return ((ctty_pgrp = tcgetpgrp(STDOUT_FILENO)) != -1 && |
1091 | ctty_pgrp == pgrp); | |
1092 | #else | |
5260325f | 1093 | return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 && |
1094 | ctty_pgrp == pgrp)); | |
3c62e7eb | 1095 | #endif |
2bd61362 | 1096 | } |
1097 | ||
8efc0c15 | 1098 | void |
1099 | progressmeter(int flag) | |
1100 | { | |
1101 | static const char prefixes[] = " KMGTP"; | |
1102 | static struct timeval lastupdate; | |
1103 | static off_t lastsize; | |
1104 | struct timeval now, td, wait; | |
1105 | off_t cursize, abbrevsize; | |
1106 | double elapsed; | |
b6573a35 | 1107 | int ratio, barlength, i, remaining; |
8efc0c15 | 1108 | char buf[256]; |
1109 | ||
1110 | if (flag == -1) { | |
5260325f | 1111 | (void) gettimeofday(&start, (struct timezone *) 0); |
8efc0c15 | 1112 | lastupdate = start; |
1113 | lastsize = 0; | |
5260325f | 1114 | } |
2bd61362 | 1115 | if (foregroundproc() == 0) |
1116 | return; | |
1117 | ||
5260325f | 1118 | (void) gettimeofday(&now, (struct timezone *) 0); |
8efc0c15 | 1119 | cursize = statbytes; |
b4ad3727 | 1120 | if (totalbytes != 0) { |
aa3378df | 1121 | ratio = 100.0 * cursize / totalbytes; |
8efc0c15 | 1122 | ratio = MAX(ratio, 0); |
1123 | ratio = MIN(ratio, 100); | |
5260325f | 1124 | } else |
8efc0c15 | 1125 | ratio = 100; |
1126 | ||
5260325f | 1127 | snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio); |
8efc0c15 | 1128 | |
1129 | barlength = getttywidth() - 51; | |
a4070484 | 1130 | barlength = (barlength <= MAX_BARLENGTH)?barlength:MAX_BARLENGTH; |
8efc0c15 | 1131 | if (barlength > 0) { |
1132 | i = barlength * ratio / 100; | |
1133 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | |
a4070484 | 1134 | "|%.*s%*s|", i, BAR, barlength - i, ""); |
8efc0c15 | 1135 | } |
8efc0c15 | 1136 | i = 0; |
1137 | abbrevsize = cursize; | |
1138 | while (abbrevsize >= 100000 && i < sizeof(prefixes)) { | |
1139 | i++; | |
1140 | abbrevsize >>= 10; | |
1141 | } | |
6a416424 | 1142 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5lu %c%c ", |
2c92c680 | 1143 | (unsigned long) abbrevsize, prefixes[i], |
1144 | prefixes[i] == ' ' ? ' ' : 'B'); | |
8efc0c15 | 1145 | |
1146 | timersub(&now, &lastupdate, &wait); | |
1147 | if (cursize > lastsize) { | |
1148 | lastupdate = now; | |
1149 | lastsize = cursize; | |
1150 | if (wait.tv_sec >= STALLTIME) { | |
1151 | start.tv_sec += wait.tv_sec; | |
1152 | start.tv_usec += wait.tv_usec; | |
1153 | } | |
1154 | wait.tv_sec = 0; | |
1155 | } | |
8efc0c15 | 1156 | timersub(&now, &start, &td); |
1157 | elapsed = td.tv_sec + (td.tv_usec / 1000000.0); | |
1158 | ||
71c0d06a | 1159 | if (flag != 1 && |
1160 | (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes)) { | |
8efc0c15 | 1161 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
71c0d06a | 1162 | " --:-- ETA"); |
8efc0c15 | 1163 | } else if (wait.tv_sec >= STALLTIME) { |
1164 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | |
71c0d06a | 1165 | " - stalled -"); |
8efc0c15 | 1166 | } else { |
d6f24e45 | 1167 | if (flag != 1) |
71c0d06a | 1168 | remaining = (int)(totalbytes / (statbytes / elapsed) - |
1169 | elapsed); | |
d6f24e45 | 1170 | else |
1171 | remaining = elapsed; | |
1172 | ||
5068f573 | 1173 | i = remaining / 3600; |
8efc0c15 | 1174 | if (i) |
1175 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | |
2e73a022 | 1176 | "%2d:", i); |
8efc0c15 | 1177 | else |
1178 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | |
2e73a022 | 1179 | " "); |
8efc0c15 | 1180 | i = remaining % 3600; |
1181 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | |
2e73a022 | 1182 | "%02d:%02d%s", i / 60, i % 60, |
1183 | (flag != 1) ? " ETA" : " "); | |
8efc0c15 | 1184 | } |
1185 | atomicio(write, fileno(stdout), buf, strlen(buf)); | |
1186 | ||
1187 | if (flag == -1) { | |
36a358ca | 1188 | mysignal(SIGALRM, updateprogressmeter); |
cddb9003 | 1189 | alarm(PROGRESSTIME); |
8efc0c15 | 1190 | } else if (flag == 1) { |
cddb9003 | 1191 | alarm(0); |
3189621b | 1192 | atomicio(write, fileno(stdout), "\n", 1); |
8efc0c15 | 1193 | statbytes = 0; |
1194 | } | |
1195 | } | |
1196 | ||
1197 | int | |
1198 | getttywidth(void) | |
1199 | { | |
1200 | struct winsize winsize; | |
1201 | ||
1202 | if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1) | |
5260325f | 1203 | return (winsize.ws_col ? winsize.ws_col : 80); |
8efc0c15 | 1204 | else |
5260325f | 1205 | return (80); |
8efc0c15 | 1206 | } |