diff options
Diffstat (limited to 'scp.c')
-rw-r--r-- | scp.c | 637 |
1 files changed, 313 insertions, 324 deletions
@@ -1,13 +1,13 @@ | |||
1 | /* | 1 | /* |
2 | 2 | * | |
3 | scp - secure remote copy. This is basically patched BSD rcp which uses ssh | 3 | * scp - secure remote copy. This is basically patched BSD rcp which uses ssh |
4 | to do the data transfer (instead of using rcmd). | 4 | * to do the data transfer (instead of using rcmd). |
5 | 5 | * | |
6 | NOTE: This version should NOT be suid root. (This uses ssh to do the transfer | 6 | * NOTE: This version should NOT be suid root. (This uses ssh to do the transfer |
7 | and ssh has the necessary privileges.) | 7 | * and ssh has the necessary privileges.) |
8 | 8 | * | |
9 | 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi> | 9 | * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi> |
10 | 10 | * | |
11 | */ | 11 | */ |
12 | 12 | ||
13 | /* | 13 | /* |
@@ -42,11 +42,10 @@ and ssh has the necessary privileges.) | |||
42 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 42 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
43 | * SUCH DAMAGE. | 43 | * SUCH DAMAGE. |
44 | * | 44 | * |
45 | * $Id: scp.c,v 1.8 1999/11/19 01:34:14 damien Exp $ | ||
46 | */ | 45 | */ |
47 | 46 | ||
48 | #include "includes.h" | 47 | #include "includes.h" |
49 | RCSID("$Id: scp.c,v 1.8 1999/11/19 01:34:14 damien Exp $"); | 48 | RCSID("$Id: scp.c,v 1.9 1999/11/24 13:26:22 damien Exp $"); |
50 | 49 | ||
51 | #include "ssh.h" | 50 | #include "ssh.h" |
52 | #include "xmalloc.h" | 51 | #include "xmalloc.h" |
@@ -76,7 +75,7 @@ off_t totalbytes = 0; | |||
76 | char *curfile; | 75 | char *curfile; |
77 | 76 | ||
78 | /* This is set to non-zero to enable verbose mode. */ | 77 | /* This is set to non-zero to enable verbose mode. */ |
79 | int verbose = 0; | 78 | int verbose_mode = 0; |
80 | 79 | ||
81 | /* This is set to non-zero if compression is desired. */ | 80 | /* This is set to non-zero if compression is desired. */ |
82 | int compress = 0; | 81 | int compress = 0; |
@@ -91,7 +90,7 @@ int batchmode = 0; | |||
91 | /* This is set to the cipher type string if given on the command line. */ | 90 | /* This is set to the cipher type string if given on the command line. */ |
92 | char *cipher = NULL; | 91 | char *cipher = NULL; |
93 | 92 | ||
94 | /* This is set to the RSA authentication identity file name if given on | 93 | /* This is set to the RSA authentication identity file name if given on |
95 | the command line. */ | 94 | the command line. */ |
96 | char *identity = NULL; | 95 | char *identity = NULL; |
97 | 96 | ||
@@ -102,98 +101,95 @@ char *port = NULL; | |||
102 | host. This returns < 0 if execution fails, and >= 0 otherwise. | 101 | host. This returns < 0 if execution fails, and >= 0 otherwise. |
103 | This assigns the input and output file descriptors on success. */ | 102 | This assigns the input and output file descriptors on success. */ |
104 | 103 | ||
105 | int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout) | 104 | int |
105 | do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout) | ||
106 | { | 106 | { |
107 | int pin[2], pout[2], reserved[2]; | 107 | int pin[2], pout[2], reserved[2]; |
108 | 108 | ||
109 | if (verbose) | 109 | if (verbose_mode) |
110 | fprintf(stderr, "Executing: host %s, user %s, command %s\n", | 110 | fprintf(stderr, "Executing: host %s, user %s, command %s\n", |
111 | host, remuser ? remuser : "(unspecified)", cmd); | 111 | host, remuser ? remuser : "(unspecified)", cmd); |
112 | 112 | ||
113 | /* Reserve two descriptors so that the real pipes won't get descriptors | 113 | /* Reserve two descriptors so that the real pipes won't get |
114 | 0 and 1 because that will screw up dup2 below. */ | 114 | descriptors 0 and 1 because that will screw up dup2 below. */ |
115 | pipe(reserved); | 115 | pipe(reserved); |
116 | 116 | ||
117 | /* Create a socket pair for communicating with ssh. */ | 117 | /* Create a socket pair for communicating with ssh. */ |
118 | if (pipe(pin) < 0) | 118 | if (pipe(pin) < 0) |
119 | fatal("pipe: %s", strerror(errno)); | 119 | fatal("pipe: %s", strerror(errno)); |
120 | if (pipe(pout) < 0) | 120 | if (pipe(pout) < 0) |
121 | fatal("pipe: %s", strerror(errno)); | 121 | fatal("pipe: %s", strerror(errno)); |
122 | 122 | ||
123 | /* Free the reserved descriptors. */ | 123 | /* Free the reserved descriptors. */ |
124 | close(reserved[0]); | 124 | close(reserved[0]); |
125 | close(reserved[1]); | 125 | close(reserved[1]); |
126 | 126 | ||
127 | /* For a child to execute the command on the remote host using ssh. */ | 127 | /* For a child to execute the command on the remote host using ssh. */ |
128 | if (fork() == 0) | 128 | if (fork() == 0) { |
129 | { | 129 | char *args[100]; |
130 | char *args[100]; | 130 | unsigned int i; |
131 | unsigned int i; | 131 | |
132 | 132 | /* Child. */ | |
133 | /* Child. */ | 133 | close(pin[1]); |
134 | close(pin[1]); | 134 | close(pout[0]); |
135 | close(pout[0]); | 135 | dup2(pin[0], 0); |
136 | dup2(pin[0], 0); | 136 | dup2(pout[1], 1); |
137 | dup2(pout[1], 1); | 137 | close(pin[0]); |
138 | close(pin[0]); | 138 | close(pout[1]); |
139 | close(pout[1]); | 139 | |
140 | 140 | i = 0; | |
141 | i = 0; | 141 | args[i++] = SSH_PROGRAM; |
142 | args[i++] = SSH_PROGRAM; | 142 | args[i++] = "-x"; |
143 | args[i++] = "-x"; | 143 | args[i++] = "-oFallBackToRsh no"; |
144 | args[i++] = "-oFallBackToRsh no"; | 144 | if (verbose_mode) |
145 | if (verbose) | 145 | args[i++] = "-v"; |
146 | args[i++] = "-v"; | 146 | if (compress) |
147 | if (compress) | 147 | args[i++] = "-C"; |
148 | args[i++] = "-C"; | 148 | if (batchmode) |
149 | if (batchmode) | 149 | args[i++] = "-oBatchMode yes"; |
150 | args[i++] = "-oBatchMode yes"; | 150 | if (cipher != NULL) { |
151 | if (cipher != NULL) | 151 | args[i++] = "-c"; |
152 | { | 152 | args[i++] = cipher; |
153 | args[i++] = "-c"; | 153 | } |
154 | args[i++] = cipher; | 154 | if (identity != NULL) { |
155 | } | 155 | args[i++] = "-i"; |
156 | if (identity != NULL) | 156 | args[i++] = identity; |
157 | { | 157 | } |
158 | args[i++] = "-i"; | 158 | if (port != NULL) { |
159 | args[i++] = identity; | 159 | args[i++] = "-p"; |
160 | } | 160 | args[i++] = port; |
161 | if (port != NULL) | 161 | } |
162 | { | 162 | if (remuser != NULL) { |
163 | args[i++] = "-p"; | 163 | args[i++] = "-l"; |
164 | args[i++] = port; | 164 | args[i++] = remuser; |
165 | } | 165 | } |
166 | if (remuser != NULL) | 166 | args[i++] = host; |
167 | { | 167 | args[i++] = cmd; |
168 | args[i++] = "-l"; | 168 | args[i++] = NULL; |
169 | args[i++] = remuser; | 169 | |
170 | execvp(SSH_PROGRAM, args); | ||
171 | perror(SSH_PROGRAM); | ||
172 | exit(1); | ||
170 | } | 173 | } |
171 | args[i++] = host; | 174 | /* Parent. Close the other side, and return the local side. */ |
172 | args[i++] = cmd; | 175 | close(pin[0]); |
173 | args[i++] = NULL; | 176 | *fdout = pin[1]; |
174 | 177 | close(pout[1]); | |
175 | execvp(SSH_PROGRAM, args); | 178 | *fdin = pout[0]; |
176 | perror(SSH_PROGRAM); | 179 | return 0; |
177 | exit(1); | ||
178 | } | ||
179 | /* Parent. Close the other side, and return the local side. */ | ||
180 | close(pin[0]); | ||
181 | *fdout = pin[1]; | ||
182 | close(pout[1]); | ||
183 | *fdin = pout[0]; | ||
184 | return 0; | ||
185 | } | 180 | } |
186 | 181 | ||
187 | void fatal(const char *fmt, ...) | 182 | void |
183 | fatal(const char *fmt,...) | ||
188 | { | 184 | { |
189 | va_list ap; | 185 | va_list ap; |
190 | char buf[1024]; | 186 | char buf[1024]; |
191 | 187 | ||
192 | va_start(ap, fmt); | 188 | va_start(ap, fmt); |
193 | vsnprintf(buf, sizeof(buf), fmt, ap); | 189 | vsnprintf(buf, sizeof(buf), fmt, ap); |
194 | va_end(ap); | 190 | va_end(ap); |
195 | fprintf(stderr, "%s\n", buf); | 191 | fprintf(stderr, "%s\n", buf); |
196 | exit(255); | 192 | exit(255); |
197 | } | 193 | } |
198 | 194 | ||
199 | /* This stuff used to be in BSD rcp extern.h. */ | 195 | /* This stuff used to be in BSD rcp extern.h. */ |
@@ -205,31 +201,31 @@ typedef struct { | |||
205 | 201 | ||
206 | extern int iamremote; | 202 | extern int iamremote; |
207 | 203 | ||
208 | BUF *allocbuf(BUF *, int, int); | 204 | BUF *allocbuf(BUF *, int, int); |
209 | char *colon(char *); | 205 | char *colon(char *); |
210 | void lostconn(int); | 206 | void lostconn(int); |
211 | void nospace(void); | 207 | void nospace(void); |
212 | int okname(char *); | 208 | int okname(char *); |
213 | void run_err(const char *, ...); | 209 | void run_err(const char *,...); |
214 | void verifydir(char *); | 210 | void verifydir(char *); |
215 | 211 | ||
216 | /* Stuff from BSD rcp.c continues. */ | 212 | /* Stuff from BSD rcp.c continues. */ |
217 | 213 | ||
218 | struct passwd *pwd; | 214 | struct passwd *pwd; |
219 | uid_t userid; | 215 | uid_t userid; |
220 | int errs, remin, remout; | 216 | int errs, remin, remout; |
221 | int pflag, iamremote, iamrecursive, targetshouldbedirectory; | 217 | int pflag, iamremote, iamrecursive, targetshouldbedirectory; |
222 | 218 | ||
223 | #define CMDNEEDS 64 | 219 | #define CMDNEEDS 64 |
224 | char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */ | 220 | char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */ |
225 | 221 | ||
226 | int response(void); | 222 | int response(void); |
227 | void rsource(char *, struct stat *); | 223 | void rsource(char *, struct stat *); |
228 | void sink(int, char *[]); | 224 | void sink(int, char *[]); |
229 | void source(int, char *[]); | 225 | void source(int, char *[]); |
230 | void tolocal(int, char *[]); | 226 | void tolocal(int, char *[]); |
231 | void toremote(char *, int, char *[]); | 227 | void toremote(char *, int, char *[]); |
232 | void usage(void); | 228 | void usage(void); |
233 | 229 | ||
234 | int | 230 | int |
235 | main(argc, argv) | 231 | main(argc, argv) |
@@ -242,47 +238,48 @@ main(argc, argv) | |||
242 | extern int optind; | 238 | extern int optind; |
243 | 239 | ||
244 | fflag = tflag = 0; | 240 | fflag = tflag = 0; |
245 | while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q")) != EOF) | 241 | while ((ch = getopt(argc, argv, "dfprtvBCc:i:P:q")) != EOF) |
246 | switch(ch) { /* User-visible flags. */ | 242 | switch (ch) { |
243 | /* User-visible flags. */ | ||
247 | case 'p': | 244 | case 'p': |
248 | pflag = 1; | 245 | pflag = 1; |
249 | break; | 246 | break; |
250 | case 'P': | 247 | case 'P': |
251 | port = optarg; | 248 | port = optarg; |
252 | break; | 249 | break; |
253 | case 'r': | 250 | case 'r': |
254 | iamrecursive = 1; | 251 | iamrecursive = 1; |
255 | break; | 252 | break; |
256 | /* Server options. */ | 253 | /* Server options. */ |
257 | case 'd': | 254 | case 'd': |
258 | targetshouldbedirectory = 1; | 255 | targetshouldbedirectory = 1; |
259 | break; | 256 | break; |
260 | case 'f': /* "from" */ | 257 | case 'f': /* "from" */ |
261 | iamremote = 1; | 258 | iamremote = 1; |
262 | fflag = 1; | 259 | fflag = 1; |
263 | break; | 260 | break; |
264 | case 't': /* "to" */ | 261 | case 't': /* "to" */ |
265 | iamremote = 1; | 262 | iamremote = 1; |
266 | tflag = 1; | 263 | tflag = 1; |
267 | break; | 264 | break; |
268 | case 'c': | 265 | case 'c': |
269 | cipher = optarg; | 266 | cipher = optarg; |
270 | break; | 267 | break; |
271 | case 'i': | 268 | case 'i': |
272 | identity = optarg; | 269 | identity = optarg; |
273 | break; | 270 | break; |
274 | case 'v': | 271 | case 'v': |
275 | verbose = 1; | 272 | verbose_mode = 1; |
276 | break; | 273 | break; |
277 | case 'B': | 274 | case 'B': |
278 | batchmode = 1; | 275 | batchmode = 1; |
279 | break; | 276 | break; |
280 | case 'C': | 277 | case 'C': |
281 | compress = 1; | 278 | compress = 1; |
282 | break; | 279 | break; |
283 | case 'q': | 280 | case 'q': |
284 | showprogress = 0; | 281 | showprogress = 0; |
285 | break; | 282 | break; |
286 | case '?': | 283 | case '?': |
287 | default: | 284 | default: |
288 | usage(); | 285 | usage(); |
@@ -291,25 +288,25 @@ main(argc, argv) | |||
291 | argv += optind; | 288 | argv += optind; |
292 | 289 | ||
293 | if ((pwd = getpwuid(userid = getuid())) == NULL) | 290 | if ((pwd = getpwuid(userid = getuid())) == NULL) |
294 | fatal("unknown user %d", (int)userid); | 291 | fatal("unknown user %d", (int) userid); |
295 | 292 | ||
296 | if (! isatty(STDERR_FILENO)) | 293 | if (!isatty(STDERR_FILENO)) |
297 | showprogress = 0; | 294 | showprogress = 0; |
298 | 295 | ||
299 | remin = STDIN_FILENO; | 296 | remin = STDIN_FILENO; |
300 | remout = STDOUT_FILENO; | 297 | remout = STDOUT_FILENO; |
301 | 298 | ||
302 | if (fflag) { /* Follow "protocol", send data. */ | 299 | if (fflag) { |
303 | (void)response(); | 300 | /* Follow "protocol", send data. */ |
301 | (void) response(); | ||
304 | source(argc, argv); | 302 | source(argc, argv); |
305 | exit(errs != 0); | 303 | exit(errs != 0); |
306 | } | 304 | } |
307 | 305 | if (tflag) { | |
308 | if (tflag) { /* Receive data. */ | 306 | /* Receive data. */ |
309 | sink(argc, argv); | 307 | sink(argc, argv); |
310 | exit(errs != 0); | 308 | exit(errs != 0); |
311 | } | 309 | } |
312 | |||
313 | if (argc < 2) | 310 | if (argc < 2) |
314 | usage(); | 311 | usage(); |
315 | if (argc > 2) | 312 | if (argc > 2) |
@@ -317,16 +314,16 @@ main(argc, argv) | |||
317 | 314 | ||
318 | remin = remout = -1; | 315 | remin = remout = -1; |
319 | /* Command to be executed on remote system using "ssh". */ | 316 | /* Command to be executed on remote system using "ssh". */ |
320 | (void)sprintf(cmd, "scp%s%s%s%s", verbose ? " -v" : "", | 317 | (void) sprintf(cmd, "scp%s%s%s%s", verbose_mode ? " -v" : "", |
321 | iamrecursive ? " -r" : "", pflag ? " -p" : "", | 318 | iamrecursive ? " -r" : "", pflag ? " -p" : "", |
322 | targetshouldbedirectory ? " -d" : ""); | 319 | targetshouldbedirectory ? " -d" : ""); |
323 | 320 | ||
324 | (void)signal(SIGPIPE, lostconn); | 321 | (void) signal(SIGPIPE, lostconn); |
325 | 322 | ||
326 | if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */ | 323 | if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */ |
327 | toremote(targ, argc, argv); | 324 | toremote(targ, argc, argv); |
328 | else { | 325 | else { |
329 | tolocal(argc, argv); /* Dest is local host. */ | 326 | tolocal(argc, argv); /* Dest is local host. */ |
330 | if (targetshouldbedirectory) | 327 | if (targetshouldbedirectory) |
331 | verifydir(argv[argc - 1]); | 328 | verifydir(argv[argc - 1]); |
332 | } | 329 | } |
@@ -360,15 +357,15 @@ toremote(targ, argc, argv) | |||
360 | 357 | ||
361 | for (i = 0; i < argc - 1; i++) { | 358 | for (i = 0; i < argc - 1; i++) { |
362 | src = colon(argv[i]); | 359 | src = colon(argv[i]); |
363 | if (src) { /* remote to remote */ | 360 | if (src) { /* remote to remote */ |
364 | *src++ = 0; | 361 | *src++ = 0; |
365 | if (*src == 0) | 362 | if (*src == 0) |
366 | src = "."; | 363 | src = "."; |
367 | host = strchr(argv[i], '@'); | 364 | host = strchr(argv[i], '@'); |
368 | len = strlen(SSH_PROGRAM) + strlen(argv[i]) + | 365 | len = strlen(SSH_PROGRAM) + strlen(argv[i]) + |
369 | strlen(src) + (tuser ? strlen(tuser) : 0) + | 366 | strlen(src) + (tuser ? strlen(tuser) : 0) + |
370 | strlen(thost) + strlen(targ) + CMDNEEDS + 32; | 367 | strlen(thost) + strlen(targ) + CMDNEEDS + 32; |
371 | bp = xmalloc(len); | 368 | bp = xmalloc(len); |
372 | if (host) { | 369 | if (host) { |
373 | *host++ = 0; | 370 | *host++ = 0; |
374 | suser = argv[i]; | 371 | suser = argv[i]; |
@@ -376,37 +373,37 @@ toremote(targ, argc, argv) | |||
376 | suser = pwd->pw_name; | 373 | suser = pwd->pw_name; |
377 | else if (!okname(suser)) | 374 | else if (!okname(suser)) |
378 | continue; | 375 | continue; |
379 | (void)sprintf(bp, | 376 | (void) sprintf(bp, |
380 | "%s%s -x -o'FallBackToRsh no' -n -l %s %s %s %s '%s%s%s:%s'", | 377 | "%s%s -x -o'FallBackToRsh no' -n -l %s %s %s %s '%s%s%s:%s'", |
381 | SSH_PROGRAM, verbose ? " -v" : "", | 378 | SSH_PROGRAM, verbose_mode ? " -v" : "", |
382 | suser, host, cmd, src, | 379 | suser, host, cmd, src, |
383 | tuser ? tuser : "", tuser ? "@" : "", | 380 | tuser ? tuser : "", tuser ? "@" : "", |
384 | thost, targ); | 381 | thost, targ); |
385 | } else | 382 | } else |
386 | (void)sprintf(bp, | 383 | (void) sprintf(bp, |
387 | "exec %s%s -x -o'FallBackToRsh no' -n %s %s %s '%s%s%s:%s'", | 384 | "exec %s%s -x -o'FallBackToRsh no' -n %s %s %s '%s%s%s:%s'", |
388 | SSH_PROGRAM, verbose ? " -v" : "", | 385 | SSH_PROGRAM, verbose_mode ? " -v" : "", |
389 | argv[i], cmd, src, | 386 | argv[i], cmd, src, |
390 | tuser ? tuser : "", tuser ? "@" : "", | 387 | tuser ? tuser : "", tuser ? "@" : "", |
391 | thost, targ); | 388 | thost, targ); |
392 | if (verbose) | 389 | if (verbose_mode) |
393 | fprintf(stderr, "Executing: %s\n", bp); | 390 | fprintf(stderr, "Executing: %s\n", bp); |
394 | (void)system(bp); | 391 | (void) system(bp); |
395 | (void)xfree(bp); | 392 | (void) xfree(bp); |
396 | } else { /* local to remote */ | 393 | } else { /* local to remote */ |
397 | if (remin == -1) { | 394 | if (remin == -1) { |
398 | len = strlen(targ) + CMDNEEDS + 20; | 395 | len = strlen(targ) + CMDNEEDS + 20; |
399 | bp = xmalloc(len); | 396 | bp = xmalloc(len); |
400 | (void)sprintf(bp, "%s -t %s", cmd, targ); | 397 | (void) sprintf(bp, "%s -t %s", cmd, targ); |
401 | host = thost; | 398 | host = thost; |
402 | if (do_cmd(host, tuser, | 399 | if (do_cmd(host, tuser, |
403 | bp, &remin, &remout) < 0) | 400 | bp, &remin, &remout) < 0) |
404 | exit(1); | 401 | exit(1); |
405 | if (response() < 0) | 402 | if (response() < 0) |
406 | exit(1); | 403 | exit(1); |
407 | (void)xfree(bp); | 404 | (void) xfree(bp); |
408 | } | 405 | } |
409 | source(1, argv+i); | 406 | source(1, argv + i); |
410 | } | 407 | } |
411 | } | 408 | } |
412 | } | 409 | } |
@@ -420,18 +417,18 @@ tolocal(argc, argv) | |||
420 | char *bp, *host, *src, *suser; | 417 | char *bp, *host, *src, *suser; |
421 | 418 | ||
422 | for (i = 0; i < argc - 1; i++) { | 419 | for (i = 0; i < argc - 1; i++) { |
423 | if (!(src = colon(argv[i]))) { /* Local to local. */ | 420 | if (!(src = colon(argv[i]))) { /* Local to local. */ |
424 | len = strlen(_PATH_CP) + strlen(argv[i]) + | 421 | len = strlen(_PATH_CP) + strlen(argv[i]) + |
425 | strlen(argv[argc - 1]) + 20; | 422 | strlen(argv[argc - 1]) + 20; |
426 | bp = xmalloc(len); | 423 | bp = xmalloc(len); |
427 | (void)sprintf(bp, "exec %s%s%s %s %s", _PATH_CP, | 424 | (void) sprintf(bp, "exec %s%s%s %s %s", _PATH_CP, |
428 | iamrecursive ? " -r" : "", pflag ? " -p" : "", | 425 | iamrecursive ? " -r" : "", pflag ? " -p" : "", |
429 | argv[i], argv[argc - 1]); | 426 | argv[i], argv[argc - 1]); |
430 | if (verbose) | 427 | if (verbose_mode) |
431 | fprintf(stderr, "Executing: %s\n", bp); | 428 | fprintf(stderr, "Executing: %s\n", bp); |
432 | if (system(bp)) | 429 | if (system(bp)) |
433 | ++errs; | 430 | ++errs; |
434 | (void)xfree(bp); | 431 | (void) xfree(bp); |
435 | continue; | 432 | continue; |
436 | } | 433 | } |
437 | *src++ = 0; | 434 | *src++ = 0; |
@@ -449,16 +446,16 @@ tolocal(argc, argv) | |||
449 | continue; | 446 | continue; |
450 | } | 447 | } |
451 | len = strlen(src) + CMDNEEDS + 20; | 448 | len = strlen(src) + CMDNEEDS + 20; |
452 | bp = xmalloc(len); | 449 | bp = xmalloc(len); |
453 | (void)sprintf(bp, "%s -f %s", cmd, src); | 450 | (void) sprintf(bp, "%s -f %s", cmd, src); |
454 | if (do_cmd(host, suser, bp, &remin, &remout) < 0) { | 451 | if (do_cmd(host, suser, bp, &remin, &remout) < 0) { |
455 | (void)xfree(bp); | 452 | (void) xfree(bp); |
456 | ++errs; | 453 | ++errs; |
457 | continue; | 454 | continue; |
458 | } | 455 | } |
459 | xfree(bp); | 456 | xfree(bp); |
460 | sink(1, argv + argc - 1); | 457 | sink(1, argv + argc - 1); |
461 | (void)close(remin); | 458 | (void) close(remin); |
462 | remin = remout = -1; | 459 | remin = remout = -1; |
463 | } | 460 | } |
464 | } | 461 | } |
@@ -476,7 +473,7 @@ source(argc, argv) | |||
476 | char *last, *name, buf[2048]; | 473 | char *last, *name, buf[2048]; |
477 | 474 | ||
478 | for (indx = 0; indx < argc; ++indx) { | 475 | for (indx = 0; indx < argc; ++indx) { |
479 | name = argv[indx]; | 476 | name = argv[indx]; |
480 | statbytes = 0; | 477 | statbytes = 0; |
481 | if ((fd = open(name, O_RDONLY, 0)) < 0) | 478 | if ((fd = open(name, O_RDONLY, 0)) < 0) |
482 | goto syserr; | 479 | goto syserr; |
@@ -507,36 +504,33 @@ syserr: run_err("%s: %s", name, strerror(errno)); | |||
507 | * Make it compatible with possible future | 504 | * Make it compatible with possible future |
508 | * versions expecting microseconds. | 505 | * versions expecting microseconds. |
509 | */ | 506 | */ |
510 | (void)sprintf(buf, "T%lu 0 %lu 0\n", | 507 | (void) sprintf(buf, "T%lu 0 %lu 0\n", |
511 | (unsigned long)stb.st_mtime, | 508 | (unsigned long) stb.st_mtime, |
512 | (unsigned long)stb.st_atime); | 509 | (unsigned long) stb.st_atime); |
513 | (void)write(remout, buf, strlen(buf)); | 510 | (void) write(remout, buf, strlen(buf)); |
514 | if (response() < 0) | 511 | if (response() < 0) |
515 | goto next; | 512 | goto next; |
516 | } | 513 | } |
517 | #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO) | 514 | #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO) |
518 | (void)sprintf(buf, "C%04o %lu %s\n", | 515 | (void) sprintf(buf, "C%04o %lu %s\n", |
519 | (unsigned int)(stb.st_mode & FILEMODEMASK), | 516 | (unsigned int) (stb.st_mode & FILEMODEMASK), |
520 | (unsigned long)stb.st_size, | 517 | (unsigned long) stb.st_size, |
521 | last); | 518 | last); |
522 | if (verbose) | 519 | if (verbose_mode) { |
523 | { | 520 | fprintf(stderr, "Sending file modes: %s", buf); |
524 | fprintf(stderr, "Sending file modes: %s", buf); | 521 | fflush(stderr); |
525 | fflush(stderr); | 522 | } |
526 | } | 523 | (void) write(remout, buf, strlen(buf)); |
527 | (void)write(remout, buf, strlen(buf)); | ||
528 | if (response() < 0) | 524 | if (response() < 0) |
529 | goto next; | 525 | goto next; |
530 | if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) { | 526 | if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) { |
531 | next: (void)close(fd); | 527 | next: (void) close(fd); |
532 | continue; | 528 | continue; |
533 | } | 529 | } |
534 | |||
535 | if (showprogress) { | 530 | if (showprogress) { |
536 | totalbytes = stb.st_size; | 531 | totalbytes = stb.st_size; |
537 | progressmeter(-1); | 532 | progressmeter(-1); |
538 | } | 533 | } |
539 | |||
540 | /* Keep writing after an error so that we stay sync'd up. */ | 534 | /* Keep writing after an error so that we stay sync'd up. */ |
541 | for (haderr = i = 0; i < stb.st_size; i += bp->cnt) { | 535 | for (haderr = i = 0; i < stb.st_size; i += bp->cnt) { |
542 | amt = bp->cnt; | 536 | amt = bp->cnt; |
@@ -548,7 +542,7 @@ next: (void)close(fd); | |||
548 | haderr = result >= 0 ? EIO : errno; | 542 | haderr = result >= 0 ? EIO : errno; |
549 | } | 543 | } |
550 | if (haderr) | 544 | if (haderr) |
551 | (void)write(remout, bp->buf, amt); | 545 | (void) write(remout, bp->buf, amt); |
552 | else { | 546 | else { |
553 | result = write(remout, bp->buf, amt); | 547 | result = write(remout, bp->buf, amt); |
554 | if (result != amt) | 548 | if (result != amt) |
@@ -556,16 +550,16 @@ next: (void)close(fd); | |||
556 | statbytes += result; | 550 | statbytes += result; |
557 | } | 551 | } |
558 | } | 552 | } |
559 | if(showprogress) | 553 | if (showprogress) |
560 | progressmeter(1); | 554 | progressmeter(1); |
561 | 555 | ||
562 | if (close(fd) < 0 && !haderr) | 556 | if (close(fd) < 0 && !haderr) |
563 | haderr = errno; | 557 | haderr = errno; |
564 | if (!haderr) | 558 | if (!haderr) |
565 | (void)write(remout, "", 1); | 559 | (void) write(remout, "", 1); |
566 | else | 560 | else |
567 | run_err("%s: %s", name, strerror(haderr)); | 561 | run_err("%s: %s", name, strerror(haderr)); |
568 | (void)response(); | 562 | (void) response(); |
569 | } | 563 | } |
570 | } | 564 | } |
571 | 565 | ||
@@ -588,21 +582,21 @@ rsource(name, statp) | |||
588 | else | 582 | else |
589 | last++; | 583 | last++; |
590 | if (pflag) { | 584 | if (pflag) { |
591 | (void)sprintf(path, "T%lu 0 %lu 0\n", | 585 | (void) sprintf(path, "T%lu 0 %lu 0\n", |
592 | (unsigned long)statp->st_mtime, | 586 | (unsigned long) statp->st_mtime, |
593 | (unsigned long)statp->st_atime); | 587 | (unsigned long) statp->st_atime); |
594 | (void)write(remout, path, strlen(path)); | 588 | (void) write(remout, path, strlen(path)); |
595 | if (response() < 0) { | 589 | if (response() < 0) { |
596 | closedir(dirp); | 590 | closedir(dirp); |
597 | return; | 591 | return; |
598 | } | 592 | } |
599 | } | 593 | } |
600 | (void)sprintf(path, | 594 | (void) sprintf(path, "D%04o %d %.1024s\n", |
601 | "D%04o %d %.1024s\n", (unsigned int)(statp->st_mode & FILEMODEMASK), | 595 | (unsigned int) (statp->st_mode & FILEMODEMASK), |
602 | 0, last); | 596 | 0, last); |
603 | if (verbose) | 597 | if (verbose_mode) |
604 | fprintf(stderr, "Entering directory: %s", path); | 598 | fprintf(stderr, "Entering directory: %s", path); |
605 | (void)write(remout, path, strlen(path)); | 599 | (void) write(remout, path, strlen(path)); |
606 | if (response() < 0) { | 600 | if (response() < 0) { |
607 | closedir(dirp); | 601 | closedir(dirp); |
608 | return; | 602 | return; |
@@ -616,13 +610,13 @@ rsource(name, statp) | |||
616 | run_err("%s/%s: name too long", name, dp->d_name); | 610 | run_err("%s/%s: name too long", name, dp->d_name); |
617 | continue; | 611 | continue; |
618 | } | 612 | } |
619 | (void)sprintf(path, "%s/%s", name, dp->d_name); | 613 | (void) sprintf(path, "%s/%s", name, dp->d_name); |
620 | vect[0] = path; | 614 | vect[0] = path; |
621 | source(1, vect); | 615 | source(1, vect); |
622 | } | 616 | } |
623 | (void)closedir(dirp); | 617 | (void) closedir(dirp); |
624 | (void)write(remout, "E\n", 2); | 618 | (void) write(remout, "E\n", 2); |
625 | (void)response(); | 619 | (void) response(); |
626 | } | 620 | } |
627 | 621 | ||
628 | void | 622 | void |
@@ -632,21 +626,23 @@ sink(argc, argv) | |||
632 | { | 626 | { |
633 | static BUF buffer; | 627 | static BUF buffer; |
634 | struct stat stb; | 628 | struct stat stb; |
635 | enum { YES, NO, DISPLAYED } wrerr; | 629 | enum { |
630 | YES, NO, DISPLAYED | ||
631 | } wrerr; | ||
636 | BUF *bp; | 632 | BUF *bp; |
637 | off_t i, j; | 633 | off_t i, j; |
638 | int amt, count, exists, first, mask, mode, ofd, omode; | 634 | int amt, count, exists, first, mask, mode, ofd, omode; |
639 | int setimes, size, targisdir, wrerrno = 0; | 635 | int setimes, size, targisdir, wrerrno = 0; |
640 | char ch, *cp, *np, *targ, *why, *vect[1], buf[2048]; | 636 | char ch, *cp, *np, *targ, *why, *vect[1], buf[2048]; |
641 | struct utimbuf ut; | 637 | struct utimbuf ut; |
642 | int dummy_usec; | 638 | int dummy_usec; |
643 | 639 | ||
644 | #define SCREWUP(str) { why = str; goto screwup; } | 640 | #define SCREWUP(str) { why = str; goto screwup; } |
645 | 641 | ||
646 | setimes = targisdir = 0; | 642 | setimes = targisdir = 0; |
647 | mask = umask(0); | 643 | mask = umask(0); |
648 | if (!pflag) | 644 | if (!pflag) |
649 | (void)umask(mask); | 645 | (void) umask(mask); |
650 | if (argc != 1) { | 646 | if (argc != 1) { |
651 | run_err("ambiguous target"); | 647 | run_err("ambiguous target"); |
652 | exit(1); | 648 | exit(1); |
@@ -654,8 +650,8 @@ sink(argc, argv) | |||
654 | targ = *argv; | 650 | targ = *argv; |
655 | if (targetshouldbedirectory) | 651 | if (targetshouldbedirectory) |
656 | verifydir(targ); | 652 | verifydir(targ); |
657 | 653 | ||
658 | (void)write(remout, "", 1); | 654 | (void) write(remout, "", 1); |
659 | if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode)) | 655 | if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode)) |
660 | targisdir = 1; | 656 | targisdir = 1; |
661 | for (first = 1;; first = 0) { | 657 | for (first = 1;; first = 0) { |
@@ -673,18 +669,17 @@ sink(argc, argv) | |||
673 | 669 | ||
674 | if (buf[0] == '\01' || buf[0] == '\02') { | 670 | if (buf[0] == '\01' || buf[0] == '\02') { |
675 | if (iamremote == 0) | 671 | if (iamremote == 0) |
676 | (void)write(STDERR_FILENO, | 672 | (void) write(STDERR_FILENO, |
677 | buf + 1, strlen(buf + 1)); | 673 | buf + 1, strlen(buf + 1)); |
678 | if (buf[0] == '\02') | 674 | if (buf[0] == '\02') |
679 | exit(1); | 675 | exit(1); |
680 | ++errs; | 676 | ++errs; |
681 | continue; | 677 | continue; |
682 | } | 678 | } |
683 | if (buf[0] == 'E') { | 679 | if (buf[0] == 'E') { |
684 | (void)write(remout, "", 1); | 680 | (void) write(remout, "", 1); |
685 | return; | 681 | return; |
686 | } | 682 | } |
687 | |||
688 | if (ch == '\n') | 683 | if (ch == '\n') |
689 | *--cp = 0; | 684 | *--cp = 0; |
690 | 685 | ||
@@ -706,7 +701,7 @@ sink(argc, argv) | |||
706 | getnum(dummy_usec); | 701 | getnum(dummy_usec); |
707 | if (*cp++ != '\0') | 702 | if (*cp++ != '\0') |
708 | SCREWUP("atime.usec not delimited"); | 703 | SCREWUP("atime.usec not delimited"); |
709 | (void)write(remout, "", 1); | 704 | (void) write(remout, "", 1); |
710 | continue; | 705 | continue; |
711 | } | 706 | } |
712 | if (*cp != 'C' && *cp != 'D') { | 707 | if (*cp != 'C' && *cp != 'D') { |
@@ -732,7 +727,7 @@ sink(argc, argv) | |||
732 | if (*cp++ != ' ') | 727 | if (*cp++ != ' ') |
733 | SCREWUP("mode not delimited"); | 728 | SCREWUP("mode not delimited"); |
734 | 729 | ||
735 | for (size = 0; *cp >= '0' && *cp <= '9';) | 730 | for (size = 0; *cp >= '0' && *cp <= '9';) |
736 | size = size * 10 + (*cp++ - '0'); | 731 | size = size * 10 + (*cp++ - '0'); |
737 | if (*cp++ != ' ') | 732 | if (*cp++ != ' ') |
738 | SCREWUP("size not delimited"); | 733 | SCREWUP("size not delimited"); |
@@ -743,9 +738,9 @@ sink(argc, argv) | |||
743 | 738 | ||
744 | need = strlen(targ) + strlen(cp) + 250; | 739 | need = strlen(targ) + strlen(cp) + 250; |
745 | if (need > cursize) | 740 | if (need > cursize) |
746 | namebuf = xmalloc(need); | 741 | namebuf = xmalloc(need); |
747 | (void)sprintf(namebuf, "%s%s%s", targ, | 742 | (void) sprintf(namebuf, "%s%s%s", targ, |
748 | *targ ? "/" : "", cp); | 743 | *targ ? "/" : "", cp); |
749 | np = namebuf; | 744 | np = namebuf; |
750 | } else | 745 | } else |
751 | np = targ; | 746 | np = targ; |
@@ -759,9 +754,10 @@ sink(argc, argv) | |||
759 | goto bad; | 754 | goto bad; |
760 | } | 755 | } |
761 | if (pflag) | 756 | if (pflag) |
762 | (void)chmod(np, mode); | 757 | (void) chmod(np, mode); |
763 | } else { | 758 | } else { |
764 | /* Handle copying from a read-only directory */ | 759 | /* Handle copying from a read-only |
760 | directory */ | ||
765 | mod_flag = 1; | 761 | mod_flag = 1; |
766 | if (mkdir(np, mode | S_IRWXU) < 0) | 762 | if (mkdir(np, mode | S_IRWXU) < 0) |
767 | goto bad; | 763 | goto bad; |
@@ -771,22 +767,22 @@ sink(argc, argv) | |||
771 | if (setimes) { | 767 | if (setimes) { |
772 | setimes = 0; | 768 | setimes = 0; |
773 | if (utime(np, &ut) < 0) | 769 | if (utime(np, &ut) < 0) |
774 | run_err("%s: set times: %s", | 770 | run_err("%s: set times: %s", |
775 | np, strerror(errno)); | 771 | np, strerror(errno)); |
776 | } | 772 | } |
777 | if (mod_flag) | 773 | if (mod_flag) |
778 | (void)chmod(np, mode); | 774 | (void) chmod(np, mode); |
779 | continue; | 775 | continue; |
780 | } | 776 | } |
781 | omode = mode; | 777 | omode = mode; |
782 | mode |= S_IWRITE; | 778 | mode |= S_IWRITE; |
783 | if ((ofd = open(np, O_WRONLY|O_CREAT|O_TRUNC, mode)) < 0) { | 779 | if ((ofd = open(np, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) { |
784 | bad: run_err("%s: %s", np, strerror(errno)); | 780 | bad: run_err("%s: %s", np, strerror(errno)); |
785 | continue; | 781 | continue; |
786 | } | 782 | } |
787 | (void)write(remout, "", 1); | 783 | (void) write(remout, "", 1); |
788 | if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) { | 784 | if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) { |
789 | (void)close(ofd); | 785 | (void) close(ofd); |
790 | continue; | 786 | continue; |
791 | } | 787 | } |
792 | cp = bp->buf; | 788 | cp = bp->buf; |
@@ -806,12 +802,12 @@ bad: run_err("%s: %s", np, strerror(errno)); | |||
806 | j = read(remin, cp, amt); | 802 | j = read(remin, cp, amt); |
807 | if (j <= 0) { | 803 | if (j <= 0) { |
808 | run_err("%s", j ? strerror(errno) : | 804 | run_err("%s", j ? strerror(errno) : |
809 | "dropped connection"); | 805 | "dropped connection"); |
810 | exit(1); | 806 | exit(1); |
811 | } | 807 | } |
812 | amt -= j; | 808 | amt -= j; |
813 | cp += j; | 809 | cp += j; |
814 | statbytes += j; | 810 | statbytes += j; |
815 | } while (amt > 0); | 811 | } while (amt > 0); |
816 | if (count == bp->cnt) { | 812 | if (count == bp->cnt) { |
817 | /* Keep reading so we stay sync'd up. */ | 813 | /* Keep reading so we stay sync'd up. */ |
@@ -819,7 +815,7 @@ bad: run_err("%s: %s", np, strerror(errno)); | |||
819 | j = write(ofd, bp->buf, count); | 815 | j = write(ofd, bp->buf, count); |
820 | if (j != count) { | 816 | if (j != count) { |
821 | wrerr = YES; | 817 | wrerr = YES; |
822 | wrerrno = j >= 0 ? EIO : errno; | 818 | wrerrno = j >= 0 ? EIO : errno; |
823 | } | 819 | } |
824 | } | 820 | } |
825 | count = 0; | 821 | count = 0; |
@@ -831,7 +827,7 @@ bad: run_err("%s: %s", np, strerror(errno)); | |||
831 | if (count != 0 && wrerr == NO && | 827 | if (count != 0 && wrerr == NO && |
832 | (j = write(ofd, bp->buf, count)) != count) { | 828 | (j = write(ofd, bp->buf, count)) != count) { |
833 | wrerr = YES; | 829 | wrerr = YES; |
834 | wrerrno = j >= 0 ? EIO : errno; | 830 | wrerrno = j >= 0 ? EIO : errno; |
835 | } | 831 | } |
836 | #if 0 | 832 | #if 0 |
837 | if (ftruncate(ofd, size)) { | 833 | if (ftruncate(ofd, size)) { |
@@ -843,29 +839,29 @@ bad: run_err("%s: %s", np, strerror(errno)); | |||
843 | if (exists || omode != mode) | 839 | if (exists || omode != mode) |
844 | if (fchmod(ofd, omode)) | 840 | if (fchmod(ofd, omode)) |
845 | run_err("%s: set mode: %s", | 841 | run_err("%s: set mode: %s", |
846 | np, strerror(errno)); | 842 | np, strerror(errno)); |
847 | } else { | 843 | } else { |
848 | if (!exists && omode != mode) | 844 | if (!exists && omode != mode) |
849 | if (fchmod(ofd, omode & ~mask)) | 845 | if (fchmod(ofd, omode & ~mask)) |
850 | run_err("%s: set mode: %s", | 846 | run_err("%s: set mode: %s", |
851 | np, strerror(errno)); | 847 | np, strerror(errno)); |
852 | } | 848 | } |
853 | (void)close(ofd); | 849 | (void) close(ofd); |
854 | (void)response(); | 850 | (void) response(); |
855 | if (setimes && wrerr == NO) { | 851 | if (setimes && wrerr == NO) { |
856 | setimes = 0; | 852 | setimes = 0; |
857 | if (utime(np, &ut) < 0) { | 853 | if (utime(np, &ut) < 0) { |
858 | run_err("%s: set times: %s", | 854 | run_err("%s: set times: %s", |
859 | np, strerror(errno)); | 855 | np, strerror(errno)); |
860 | wrerr = DISPLAYED; | 856 | wrerr = DISPLAYED; |
861 | } | 857 | } |
862 | } | 858 | } |
863 | switch(wrerr) { | 859 | switch (wrerr) { |
864 | case YES: | 860 | case YES: |
865 | run_err("%s: %s", np, strerror(wrerrno)); | 861 | run_err("%s: %s", np, strerror(wrerrno)); |
866 | break; | 862 | break; |
867 | case NO: | 863 | case NO: |
868 | (void)write(remout, "", 1); | 864 | (void) write(remout, "", 1); |
869 | break; | 865 | break; |
870 | case DISPLAYED: | 866 | case DISPLAYED: |
871 | break; | 867 | break; |
@@ -885,14 +881,14 @@ response() | |||
885 | lostconn(0); | 881 | lostconn(0); |
886 | 882 | ||
887 | cp = rbuf; | 883 | cp = rbuf; |
888 | switch(resp) { | 884 | switch (resp) { |
889 | case 0: /* ok */ | 885 | case 0: /* ok */ |
890 | return (0); | 886 | return (0); |
891 | default: | 887 | default: |
892 | *cp++ = resp; | 888 | *cp++ = resp; |
893 | /* FALLTHROUGH */ | 889 | /* FALLTHROUGH */ |
894 | case 1: /* error, followed by error msg */ | 890 | case 1: /* error, followed by error msg */ |
895 | case 2: /* fatal error, "" */ | 891 | case 2: /* fatal error, "" */ |
896 | do { | 892 | do { |
897 | if (read(remin, &ch, sizeof(ch)) != sizeof(ch)) | 893 | if (read(remin, &ch, sizeof(ch)) != sizeof(ch)) |
898 | lostconn(0); | 894 | lostconn(0); |
@@ -900,7 +896,7 @@ response() | |||
900 | } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n'); | 896 | } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n'); |
901 | 897 | ||
902 | if (!iamremote) | 898 | if (!iamremote) |
903 | (void)write(STDERR_FILENO, rbuf, cp - rbuf); | 899 | (void) write(STDERR_FILENO, rbuf, cp - rbuf); |
904 | ++errs; | 900 | ++errs; |
905 | if (resp == 1) | 901 | if (resp == 1) |
906 | return (-1); | 902 | return (-1); |
@@ -912,13 +908,13 @@ response() | |||
912 | void | 908 | void |
913 | usage() | 909 | usage() |
914 | { | 910 | { |
915 | (void)fprintf(stderr, | 911 | (void) fprintf(stderr, |
916 | "usage: scp [-pqrvC] [-P port] [-c cipher] [-i identity] f1 f2; or:\n scp [options] f1 ... fn directory\n"); | 912 | "usage: scp [-pqrvC] [-P port] [-c cipher] [-i identity] f1 f2; or:\n scp [options] f1 ... fn directory\n"); |
917 | exit(1); | 913 | exit(1); |
918 | } | 914 | } |
919 | 915 | ||
920 | void | 916 | void |
921 | run_err(const char *fmt, ...) | 917 | run_err(const char *fmt,...) |
922 | { | 918 | { |
923 | static FILE *fp; | 919 | static FILE *fp; |
924 | va_list ap; | 920 | va_list ap; |
@@ -927,18 +923,16 @@ run_err(const char *fmt, ...) | |||
927 | ++errs; | 923 | ++errs; |
928 | if (fp == NULL && !(fp = fdopen(remout, "w"))) | 924 | if (fp == NULL && !(fp = fdopen(remout, "w"))) |
929 | return; | 925 | return; |
930 | (void)fprintf(fp, "%c", 0x01); | 926 | (void) fprintf(fp, "%c", 0x01); |
931 | (void)fprintf(fp, "scp: "); | 927 | (void) fprintf(fp, "scp: "); |
932 | (void)vfprintf(fp, fmt, ap); | 928 | (void) vfprintf(fp, fmt, ap); |
933 | (void)fprintf(fp, "\n"); | 929 | (void) fprintf(fp, "\n"); |
934 | (void)fflush(fp); | 930 | (void) fflush(fp); |
935 | 931 | ||
936 | if (!iamremote) | 932 | if (!iamremote) { |
937 | { | 933 | vfprintf(stderr, fmt, ap); |
938 | vfprintf(stderr, fmt, ap); | 934 | fprintf(stderr, "\n"); |
939 | fprintf(stderr, "\n"); | 935 | } |
940 | } | ||
941 | |||
942 | va_end(ap); | 936 | va_end(ap); |
943 | } | 937 | } |
944 | 938 | ||
@@ -976,7 +970,7 @@ run_err(const char *fmt, ...) | |||
976 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 970 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
977 | * SUCH DAMAGE. | 971 | * SUCH DAMAGE. |
978 | * | 972 | * |
979 | * $Id: scp.c,v 1.8 1999/11/19 01:34:14 damien Exp $ | 973 | * $Id: scp.c,v 1.9 1999/11/24 13:26:22 damien Exp $ |
980 | */ | 974 | */ |
981 | 975 | ||
982 | char * | 976 | char * |
@@ -1043,17 +1037,17 @@ allocbuf(bp, fd, blksize) | |||
1043 | run_err("fstat: %s", strerror(errno)); | 1037 | run_err("fstat: %s", strerror(errno)); |
1044 | return (0); | 1038 | return (0); |
1045 | } | 1039 | } |
1046 | if (stb.st_blksize == 0) | 1040 | if (stb.st_blksize == 0) |
1047 | size = blksize; | 1041 | size = blksize; |
1048 | else | 1042 | else |
1049 | size = blksize + (stb.st_blksize - blksize % stb.st_blksize) % | 1043 | size = blksize + (stb.st_blksize - blksize % stb.st_blksize) % |
1050 | stb.st_blksize; | 1044 | stb.st_blksize; |
1051 | if (bp->cnt >= size) | 1045 | if (bp->cnt >= size) |
1052 | return (bp); | 1046 | return (bp); |
1053 | if (bp->buf == NULL) | 1047 | if (bp->buf == NULL) |
1054 | bp->buf = xmalloc(size); | 1048 | bp->buf = xmalloc(size); |
1055 | else | 1049 | else |
1056 | bp->buf = xrealloc(bp->buf, size); | 1050 | bp->buf = xrealloc(bp->buf, size); |
1057 | bp->cnt = size; | 1051 | bp->cnt = size; |
1058 | return (bp); | 1052 | return (bp); |
1059 | } | 1053 | } |
@@ -1072,16 +1066,16 @@ lostconn(signo) | |||
1072 | */ | 1066 | */ |
1073 | int | 1067 | int |
1074 | atomicio(f, fd, s, n) | 1068 | atomicio(f, fd, s, n) |
1075 | int (*f)(); | 1069 | int (*f) (); |
1076 | char *s; | 1070 | char *s; |
1077 | { | 1071 | { |
1078 | int res, pos = 0; | 1072 | int res, pos = 0; |
1079 | 1073 | ||
1080 | while (n>pos) { | 1074 | while (n > pos) { |
1081 | res = (f)(fd, s+pos, n-pos); | 1075 | res = (f) (fd, s + pos, n - pos); |
1082 | switch (res) { | 1076 | switch (res) { |
1083 | case -1: | 1077 | case -1: |
1084 | if (errno==EINTR || errno==EAGAIN) | 1078 | if (errno == EINTR || errno == EAGAIN) |
1085 | continue; | 1079 | continue; |
1086 | case 0: | 1080 | case 0: |
1087 | return (res); | 1081 | return (res); |
@@ -1095,12 +1089,12 @@ char *s; | |||
1095 | void | 1089 | void |
1096 | alarmtimer(int wait) | 1090 | alarmtimer(int wait) |
1097 | { | 1091 | { |
1098 | struct itimerval itv; | 1092 | struct itimerval itv; |
1099 | 1093 | ||
1100 | itv.it_value.tv_sec = wait; | 1094 | itv.it_value.tv_sec = wait; |
1101 | itv.it_value.tv_usec = 0; | 1095 | itv.it_value.tv_usec = 0; |
1102 | itv.it_interval = itv.it_value; | 1096 | itv.it_interval = itv.it_value; |
1103 | setitimer(ITIMER_REAL, &itv, NULL); | 1097 | setitimer(ITIMER_REAL, &itv, NULL); |
1104 | } | 1098 | } |
1105 | 1099 | ||
1106 | void | 1100 | void |
@@ -1121,8 +1115,8 @@ foregroundproc() | |||
1121 | if (pgrp == -1) | 1115 | if (pgrp == -1) |
1122 | pgrp = getpgrp(); | 1116 | pgrp = getpgrp(); |
1123 | 1117 | ||
1124 | return((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 && | 1118 | return ((ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 && |
1125 | ctty_pgrp == pgrp)); | 1119 | ctty_pgrp == pgrp)); |
1126 | } | 1120 | } |
1127 | 1121 | ||
1128 | void | 1122 | void |
@@ -1138,35 +1132,33 @@ progressmeter(int flag) | |||
1138 | char buf[256]; | 1132 | char buf[256]; |
1139 | 1133 | ||
1140 | if (flag == -1) { | 1134 | if (flag == -1) { |
1141 | (void)gettimeofday(&start, (struct timezone *)0); | 1135 | (void) gettimeofday(&start, (struct timezone *) 0); |
1142 | lastupdate = start; | 1136 | lastupdate = start; |
1143 | lastsize = 0; | 1137 | lastsize = 0; |
1144 | } | 1138 | } |
1145 | if (foregroundproc() == 0) | 1139 | if (foregroundproc() == 0) |
1146 | return; | 1140 | return; |
1147 | 1141 | ||
1148 | (void)gettimeofday(&now, (struct timezone *)0); | 1142 | (void) gettimeofday(&now, (struct timezone *) 0); |
1149 | cursize = statbytes; | 1143 | cursize = statbytes; |
1150 | if (totalbytes != 0) { | 1144 | if (totalbytes != 0) { |
1151 | ratio = cursize * 100.0 / totalbytes; | 1145 | ratio = cursize * 100.0 / totalbytes; |
1152 | ratio = MAX(ratio, 0); | 1146 | ratio = MAX(ratio, 0); |
1153 | ratio = MIN(ratio, 100); | 1147 | ratio = MIN(ratio, 100); |
1154 | } | 1148 | } else |
1155 | else | ||
1156 | ratio = 100; | 1149 | ratio = 100; |
1157 | 1150 | ||
1158 | snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio); | 1151 | snprintf(buf, sizeof(buf), "\r%-20.20s %3d%% ", curfile, ratio); |
1159 | 1152 | ||
1160 | barlength = getttywidth() - 51; | 1153 | barlength = getttywidth() - 51; |
1161 | if (barlength > 0) { | 1154 | if (barlength > 0) { |
1162 | i = barlength * ratio / 100; | 1155 | i = barlength * ratio / 100; |
1163 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | 1156 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
1164 | "|%.*s%*s|", i, | 1157 | "|%.*s%*s|", i, |
1165 | "*****************************************************************************" | 1158 | "*****************************************************************************" |
1166 | "*****************************************************************************", | 1159 | "*****************************************************************************", |
1167 | barlength - i, ""); | 1160 | barlength - i, ""); |
1168 | } | 1161 | } |
1169 | |||
1170 | i = 0; | 1162 | i = 0; |
1171 | abbrevsize = cursize; | 1163 | abbrevsize = cursize; |
1172 | while (abbrevsize >= 100000 && i < sizeof(prefixes)) { | 1164 | while (abbrevsize >= 100000 && i < sizeof(prefixes)) { |
@@ -1174,8 +1166,8 @@ progressmeter(int flag) | |||
1174 | abbrevsize >>= 10; | 1166 | abbrevsize >>= 10; |
1175 | } | 1167 | } |
1176 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5qd %c%c ", | 1168 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %5qd %c%c ", |
1177 | (quad_t)abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' : | 1169 | (quad_t) abbrevsize, prefixes[i], prefixes[i] == ' ' ? ' ' : |
1178 | 'B'); | 1170 | 'B'); |
1179 | 1171 | ||
1180 | timersub(&now, &lastupdate, &wait); | 1172 | timersub(&now, &lastupdate, &wait); |
1181 | if (cursize > lastsize) { | 1173 | if (cursize > lastsize) { |
@@ -1187,33 +1179,32 @@ progressmeter(int flag) | |||
1187 | } | 1179 | } |
1188 | wait.tv_sec = 0; | 1180 | wait.tv_sec = 0; |
1189 | } | 1181 | } |
1190 | |||
1191 | timersub(&now, &start, &td); | 1182 | timersub(&now, &start, &td); |
1192 | elapsed = td.tv_sec + (td.tv_usec / 1000000.0); | 1183 | elapsed = td.tv_sec + (td.tv_usec / 1000000.0); |
1193 | 1184 | ||
1194 | if (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes) { | 1185 | if (statbytes <= 0 || elapsed <= 0.0 || cursize > totalbytes) { |
1195 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | 1186 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
1196 | " --:-- ETA"); | 1187 | " --:-- ETA"); |
1197 | } else if (wait.tv_sec >= STALLTIME) { | 1188 | } else if (wait.tv_sec >= STALLTIME) { |
1198 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | 1189 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
1199 | " - stalled -"); | 1190 | " - stalled -"); |
1200 | } else { | 1191 | } else { |
1201 | remaining = (int)(totalbytes / (statbytes / elapsed) - elapsed); | 1192 | remaining = (int) (totalbytes / (statbytes / elapsed) - elapsed); |
1202 | i = remaining / 3600; | 1193 | i = remaining / 3600; |
1203 | if (i) | 1194 | if (i) |
1204 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | 1195 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
1205 | "%2d:", i); | 1196 | "%2d:", i); |
1206 | else | 1197 | else |
1207 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | 1198 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
1208 | " "); | 1199 | " "); |
1209 | i = remaining % 3600; | 1200 | i = remaining % 3600; |
1210 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), | 1201 | snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), |
1211 | "%02d:%02d ETA", i / 60, i % 60); | 1202 | "%02d:%02d ETA", i / 60, i % 60); |
1212 | } | 1203 | } |
1213 | atomicio(write, fileno(stdout), buf, strlen(buf)); | 1204 | atomicio(write, fileno(stdout), buf, strlen(buf)); |
1214 | 1205 | ||
1215 | if (flag == -1) { | 1206 | if (flag == -1) { |
1216 | signal(SIGALRM, (void *)updateprogressmeter); | 1207 | signal(SIGALRM, (void *) updateprogressmeter); |
1217 | alarmtimer(1); | 1208 | alarmtimer(1); |
1218 | } else if (flag == 1) { | 1209 | } else if (flag == 1) { |
1219 | alarmtimer(0); | 1210 | alarmtimer(0); |
@@ -1228,9 +1219,7 @@ getttywidth(void) | |||
1228 | struct winsize winsize; | 1219 | struct winsize winsize; |
1229 | 1220 | ||
1230 | if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1) | 1221 | if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1) |
1231 | return(winsize.ws_col ? winsize.ws_col : 80); | 1222 | return (winsize.ws_col ? winsize.ws_col : 80); |
1232 | else | 1223 | else |
1233 | return(80); | 1224 | return (80); |
1234 | } | 1225 | } |
1235 | |||
1236 | |||