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