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