main.c revision 1.14 1 1.14 lukem /* $NetBSD: main.c,v 1.14 1996/12/06 02:06:52 lukem Exp $ */
2 1.12 lukem
3 1.1 cgd /*
4 1.3 cgd * Copyright (c) 1985, 1989, 1993, 1994
5 1.3 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.1 cgd #ifndef lint
37 1.3 cgd static char copyright[] =
38 1.3 cgd "@(#) Copyright (c) 1985, 1989, 1993, 1994\n\
39 1.3 cgd The Regents of the University of California. All rights reserved.\n";
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.8 tls #if 0
44 1.8 tls static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 10/9/94";
45 1.8 tls #else
46 1.14 lukem static char rcsid[] = "$NetBSD: main.c,v 1.14 1996/12/06 02:06:52 lukem Exp $";
47 1.8 tls #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.1 cgd /*
51 1.1 cgd * FTP User Program -- Command Interface.
52 1.1 cgd */
53 1.3 cgd /*#include <sys/ioctl.h>*/
54 1.3 cgd #include <sys/types.h>
55 1.1 cgd #include <sys/socket.h>
56 1.1 cgd
57 1.1 cgd #include <arpa/ftp.h>
58 1.1 cgd
59 1.1 cgd #include <ctype.h>
60 1.3 cgd #include <err.h>
61 1.1 cgd #include <netdb.h>
62 1.1 cgd #include <pwd.h>
63 1.3 cgd #include <signal.h>
64 1.3 cgd #include <stdio.h>
65 1.3 cgd #include <stdlib.h>
66 1.9 cgd #include <string.h>
67 1.3 cgd #include <unistd.h>
68 1.1 cgd
69 1.3 cgd #include "ftp_var.h"
70 1.1 cgd
71 1.12 lukem extern char *__progname; /* from crt0.o */
72 1.12 lukem
73 1.12 lukem char *portnum; /* alternate port number */
74 1.12 lukem
75 1.12 lukem int auto_fetch __P((int, char **));
76 1.12 lukem
77 1.3 cgd int
78 1.1 cgd main(argc, argv)
79 1.3 cgd int argc;
80 1.1 cgd char *argv[];
81 1.1 cgd {
82 1.3 cgd int ch, top;
83 1.1 cgd struct passwd *pw = NULL;
84 1.3 cgd char *cp, homedir[MAXPATHLEN];
85 1.1 cgd
86 1.1 cgd sp = getservbyname("ftp", "tcp");
87 1.3 cgd if (sp == 0)
88 1.3 cgd errx(1, "ftp/tcp: unknown service");
89 1.1 cgd doglob = 1;
90 1.1 cgd interactive = 1;
91 1.1 cgd autologin = 1;
92 1.12 lukem passivemode = 0;
93 1.13 lukem preserve = 1;
94 1.12 lukem mark = HASHBYTES;
95 1.3 cgd
96 1.12 lukem while ((ch = getopt(argc, argv, "adginpP:tv")) != EOF) {
97 1.6 mycroft switch (ch) {
98 1.12 lukem case 'a':
99 1.12 lukem anonftp = 1;
100 1.12 lukem break;
101 1.12 lukem
102 1.3 cgd case 'd':
103 1.3 cgd options |= SO_DEBUG;
104 1.3 cgd debug++;
105 1.3 cgd break;
106 1.12 lukem
107 1.3 cgd case 'g':
108 1.3 cgd doglob = 0;
109 1.3 cgd break;
110 1.1 cgd
111 1.3 cgd case 'i':
112 1.3 cgd interactive = 0;
113 1.3 cgd break;
114 1.1 cgd
115 1.3 cgd case 'n':
116 1.3 cgd autologin = 0;
117 1.3 cgd break;
118 1.1 cgd
119 1.12 lukem case 'p':
120 1.12 lukem passivemode = 1;
121 1.12 lukem break;
122 1.12 lukem
123 1.12 lukem case 'P':
124 1.12 lukem portnum = optarg;
125 1.12 lukem break;
126 1.12 lukem
127 1.3 cgd case 't':
128 1.3 cgd trace++;
129 1.3 cgd break;
130 1.1 cgd
131 1.3 cgd case 'v':
132 1.3 cgd verbose++;
133 1.3 cgd break;
134 1.1 cgd
135 1.3 cgd default:
136 1.14 lukem usage();
137 1.3 cgd }
138 1.1 cgd }
139 1.3 cgd argc -= optind;
140 1.3 cgd argv += optind;
141 1.3 cgd
142 1.1 cgd fromatty = isatty(fileno(stdin));
143 1.1 cgd if (fromatty)
144 1.1 cgd verbose++;
145 1.1 cgd cpend = 0; /* no pending replies */
146 1.1 cgd proxy = 0; /* proxy not active */
147 1.1 cgd crflag = 1; /* strip c.r. on ascii gets */
148 1.1 cgd sendport = -1; /* not using ports */
149 1.1 cgd /*
150 1.1 cgd * Set up the home directory in case we're globbing.
151 1.1 cgd */
152 1.1 cgd cp = getlogin();
153 1.1 cgd if (cp != NULL) {
154 1.1 cgd pw = getpwnam(cp);
155 1.1 cgd }
156 1.1 cgd if (pw == NULL)
157 1.1 cgd pw = getpwuid(getuid());
158 1.1 cgd if (pw != NULL) {
159 1.1 cgd home = homedir;
160 1.1 cgd (void) strcpy(home, pw->pw_dir);
161 1.1 cgd }
162 1.12 lukem
163 1.12 lukem /* Handle "automatic" transfers. */
164 1.12 lukem if (argc > 0 && strchr(argv[0], ':') != NULL) {
165 1.12 lukem anonftp = 1;
166 1.12 lukem exit(auto_fetch(argc, argv));
167 1.12 lukem }
168 1.12 lukem
169 1.1 cgd if (argc > 0) {
170 1.3 cgd char *xargv[5];
171 1.3 cgd
172 1.1 cgd if (setjmp(toplevel))
173 1.1 cgd exit(0);
174 1.1 cgd (void) signal(SIGINT, intr);
175 1.1 cgd (void) signal(SIGPIPE, lostpeer);
176 1.3 cgd xargv[0] = __progname;
177 1.3 cgd xargv[1] = argv[0];
178 1.3 cgd xargv[2] = argv[1];
179 1.3 cgd xargv[3] = argv[2];
180 1.3 cgd xargv[4] = NULL;
181 1.3 cgd setpeer(argc+1, xargv);
182 1.1 cgd }
183 1.1 cgd top = setjmp(toplevel) == 0;
184 1.1 cgd if (top) {
185 1.1 cgd (void) signal(SIGINT, intr);
186 1.1 cgd (void) signal(SIGPIPE, lostpeer);
187 1.1 cgd }
188 1.1 cgd for (;;) {
189 1.1 cgd cmdscanner(top);
190 1.1 cgd top = 1;
191 1.1 cgd }
192 1.1 cgd }
193 1.1 cgd
194 1.1 cgd void
195 1.1 cgd intr()
196 1.1 cgd {
197 1.1 cgd
198 1.1 cgd longjmp(toplevel, 1);
199 1.1 cgd }
200 1.1 cgd
201 1.1 cgd void
202 1.1 cgd lostpeer()
203 1.1 cgd {
204 1.1 cgd
205 1.1 cgd if (connected) {
206 1.1 cgd if (cout != NULL) {
207 1.1 cgd (void) shutdown(fileno(cout), 1+1);
208 1.1 cgd (void) fclose(cout);
209 1.1 cgd cout = NULL;
210 1.1 cgd }
211 1.1 cgd if (data >= 0) {
212 1.1 cgd (void) shutdown(data, 1+1);
213 1.1 cgd (void) close(data);
214 1.1 cgd data = -1;
215 1.1 cgd }
216 1.1 cgd connected = 0;
217 1.1 cgd }
218 1.1 cgd pswitch(1);
219 1.1 cgd if (connected) {
220 1.1 cgd if (cout != NULL) {
221 1.1 cgd (void) shutdown(fileno(cout), 1+1);
222 1.1 cgd (void) fclose(cout);
223 1.1 cgd cout = NULL;
224 1.1 cgd }
225 1.1 cgd connected = 0;
226 1.1 cgd }
227 1.1 cgd proxflag = 0;
228 1.1 cgd pswitch(0);
229 1.1 cgd }
230 1.1 cgd
231 1.3 cgd /*
232 1.3 cgd char *
233 1.1 cgd tail(filename)
234 1.1 cgd char *filename;
235 1.1 cgd {
236 1.3 cgd char *s;
237 1.12 lukem
238 1.1 cgd while (*filename) {
239 1.3 cgd s = strrchr(filename, '/');
240 1.1 cgd if (s == NULL)
241 1.1 cgd break;
242 1.1 cgd if (s[1])
243 1.1 cgd return (s + 1);
244 1.12 lukem if (s == filename)
245 1.12 lukem break; XXX
246 1.1 cgd *s = '\0';
247 1.1 cgd }
248 1.1 cgd return (filename);
249 1.1 cgd }
250 1.1 cgd */
251 1.3 cgd
252 1.1 cgd /*
253 1.1 cgd * Command parser.
254 1.1 cgd */
255 1.3 cgd void
256 1.1 cgd cmdscanner(top)
257 1.1 cgd int top;
258 1.1 cgd {
259 1.3 cgd struct cmd *c;
260 1.3 cgd int l;
261 1.1 cgd
262 1.1 cgd if (!top)
263 1.1 cgd (void) putchar('\n');
264 1.1 cgd for (;;) {
265 1.1 cgd if (fromatty) {
266 1.1 cgd printf("ftp> ");
267 1.1 cgd (void) fflush(stdout);
268 1.1 cgd }
269 1.1 cgd if (fgets(line, sizeof line, stdin) == NULL)
270 1.3 cgd quit(0, 0);
271 1.1 cgd l = strlen(line);
272 1.1 cgd if (l == 0)
273 1.1 cgd break;
274 1.1 cgd if (line[--l] == '\n') {
275 1.1 cgd if (l == 0)
276 1.1 cgd break;
277 1.1 cgd line[l] = '\0';
278 1.1 cgd } else if (l == sizeof(line) - 2) {
279 1.1 cgd printf("sorry, input line too long\n");
280 1.1 cgd while ((l = getchar()) != '\n' && l != EOF)
281 1.1 cgd /* void */;
282 1.1 cgd break;
283 1.1 cgd } /* else it was a line without a newline */
284 1.1 cgd makeargv();
285 1.1 cgd if (margc == 0) {
286 1.1 cgd continue;
287 1.1 cgd }
288 1.1 cgd c = getcmd(margv[0]);
289 1.1 cgd if (c == (struct cmd *)-1) {
290 1.1 cgd printf("?Ambiguous command\n");
291 1.1 cgd continue;
292 1.1 cgd }
293 1.1 cgd if (c == 0) {
294 1.1 cgd printf("?Invalid command\n");
295 1.1 cgd continue;
296 1.1 cgd }
297 1.1 cgd if (c->c_conn && !connected) {
298 1.1 cgd printf("Not connected.\n");
299 1.1 cgd continue;
300 1.1 cgd }
301 1.13 lukem confirmrest = 0;
302 1.1 cgd (*c->c_handler)(margc, margv);
303 1.1 cgd if (bell && c->c_bell)
304 1.1 cgd (void) putchar('\007');
305 1.1 cgd if (c->c_handler != help)
306 1.1 cgd break;
307 1.1 cgd }
308 1.1 cgd (void) signal(SIGINT, intr);
309 1.1 cgd (void) signal(SIGPIPE, lostpeer);
310 1.1 cgd }
311 1.1 cgd
312 1.1 cgd struct cmd *
313 1.1 cgd getcmd(name)
314 1.13 lukem const char *name;
315 1.1 cgd {
316 1.13 lukem const char *p, *q;
317 1.3 cgd struct cmd *c, *found;
318 1.3 cgd int nmatches, longest;
319 1.11 pk
320 1.11 pk if (name == NULL)
321 1.11 pk return (0);
322 1.1 cgd
323 1.1 cgd longest = 0;
324 1.1 cgd nmatches = 0;
325 1.1 cgd found = 0;
326 1.12 lukem for (c = cmdtab; (p = c->c_name) != NULL; c++) {
327 1.1 cgd for (q = name; *q == *p++; q++)
328 1.1 cgd if (*q == 0) /* exact match? */
329 1.1 cgd return (c);
330 1.1 cgd if (!*q) { /* the name was a prefix */
331 1.1 cgd if (q - name > longest) {
332 1.1 cgd longest = q - name;
333 1.1 cgd nmatches = 1;
334 1.1 cgd found = c;
335 1.1 cgd } else if (q - name == longest)
336 1.1 cgd nmatches++;
337 1.1 cgd }
338 1.1 cgd }
339 1.1 cgd if (nmatches > 1)
340 1.1 cgd return ((struct cmd *)-1);
341 1.1 cgd return (found);
342 1.1 cgd }
343 1.1 cgd
344 1.1 cgd /*
345 1.1 cgd * Slice a string up into argc/argv.
346 1.1 cgd */
347 1.1 cgd
348 1.1 cgd int slrflag;
349 1.1 cgd
350 1.3 cgd void
351 1.1 cgd makeargv()
352 1.1 cgd {
353 1.1 cgd char **argp;
354 1.1 cgd
355 1.1 cgd argp = margv;
356 1.1 cgd stringbase = line; /* scan from first of buffer */
357 1.1 cgd argbase = argbuf; /* store from first of buffer */
358 1.1 cgd slrflag = 0;
359 1.10 pk for (margc = 0; ; margc++) {
360 1.10 pk /* Expand array if necessary */
361 1.10 pk if (margc == margvlen) {
362 1.10 pk margv = (margvlen == 0)
363 1.10 pk ? (char **)malloc(20 * sizeof(char *))
364 1.10 pk : (char **)realloc(margv,
365 1.10 pk (margvlen + 20)*sizeof(char *));
366 1.10 pk if (margv == NULL)
367 1.10 pk errx(1, "cannot realloc argv array");
368 1.10 pk margvlen += 20;
369 1.10 pk argp = margv + margc;
370 1.10 pk }
371 1.10 pk
372 1.10 pk if ((*argp++ = slurpstring()) == NULL)
373 1.10 pk break;
374 1.10 pk }
375 1.10 pk
376 1.1 cgd }
377 1.1 cgd
378 1.1 cgd /*
379 1.1 cgd * Parse string into argbuf;
380 1.1 cgd * implemented with FSM to
381 1.1 cgd * handle quoting and strings
382 1.1 cgd */
383 1.1 cgd char *
384 1.1 cgd slurpstring()
385 1.1 cgd {
386 1.1 cgd int got_one = 0;
387 1.3 cgd char *sb = stringbase;
388 1.3 cgd char *ap = argbase;
389 1.1 cgd char *tmp = argbase; /* will return this if token found */
390 1.1 cgd
391 1.1 cgd if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */
392 1.1 cgd switch (slrflag) { /* and $ as token for macro invoke */
393 1.1 cgd case 0:
394 1.1 cgd slrflag++;
395 1.1 cgd stringbase++;
396 1.1 cgd return ((*sb == '!') ? "!" : "$");
397 1.1 cgd /* NOTREACHED */
398 1.1 cgd case 1:
399 1.1 cgd slrflag++;
400 1.1 cgd altarg = stringbase;
401 1.1 cgd break;
402 1.1 cgd default:
403 1.1 cgd break;
404 1.1 cgd }
405 1.1 cgd }
406 1.1 cgd
407 1.1 cgd S0:
408 1.1 cgd switch (*sb) {
409 1.1 cgd
410 1.1 cgd case '\0':
411 1.1 cgd goto OUT;
412 1.1 cgd
413 1.1 cgd case ' ':
414 1.1 cgd case '\t':
415 1.1 cgd sb++; goto S0;
416 1.1 cgd
417 1.1 cgd default:
418 1.1 cgd switch (slrflag) {
419 1.1 cgd case 0:
420 1.1 cgd slrflag++;
421 1.1 cgd break;
422 1.1 cgd case 1:
423 1.1 cgd slrflag++;
424 1.1 cgd altarg = sb;
425 1.1 cgd break;
426 1.1 cgd default:
427 1.1 cgd break;
428 1.1 cgd }
429 1.1 cgd goto S1;
430 1.1 cgd }
431 1.1 cgd
432 1.1 cgd S1:
433 1.1 cgd switch (*sb) {
434 1.1 cgd
435 1.1 cgd case ' ':
436 1.1 cgd case '\t':
437 1.1 cgd case '\0':
438 1.1 cgd goto OUT; /* end of token */
439 1.1 cgd
440 1.1 cgd case '\\':
441 1.1 cgd sb++; goto S2; /* slurp next character */
442 1.1 cgd
443 1.1 cgd case '"':
444 1.1 cgd sb++; goto S3; /* slurp quoted string */
445 1.1 cgd
446 1.1 cgd default:
447 1.1 cgd *ap++ = *sb++; /* add character to token */
448 1.1 cgd got_one = 1;
449 1.1 cgd goto S1;
450 1.1 cgd }
451 1.1 cgd
452 1.1 cgd S2:
453 1.1 cgd switch (*sb) {
454 1.1 cgd
455 1.1 cgd case '\0':
456 1.1 cgd goto OUT;
457 1.1 cgd
458 1.1 cgd default:
459 1.1 cgd *ap++ = *sb++;
460 1.1 cgd got_one = 1;
461 1.1 cgd goto S1;
462 1.1 cgd }
463 1.1 cgd
464 1.1 cgd S3:
465 1.1 cgd switch (*sb) {
466 1.1 cgd
467 1.1 cgd case '\0':
468 1.1 cgd goto OUT;
469 1.1 cgd
470 1.1 cgd case '"':
471 1.1 cgd sb++; goto S1;
472 1.1 cgd
473 1.1 cgd default:
474 1.1 cgd *ap++ = *sb++;
475 1.1 cgd got_one = 1;
476 1.1 cgd goto S3;
477 1.1 cgd }
478 1.1 cgd
479 1.1 cgd OUT:
480 1.1 cgd if (got_one)
481 1.1 cgd *ap++ = '\0';
482 1.1 cgd argbase = ap; /* update storage pointer */
483 1.1 cgd stringbase = sb; /* update scan pointer */
484 1.1 cgd if (got_one) {
485 1.3 cgd return (tmp);
486 1.1 cgd }
487 1.1 cgd switch (slrflag) {
488 1.1 cgd case 0:
489 1.1 cgd slrflag++;
490 1.1 cgd break;
491 1.1 cgd case 1:
492 1.1 cgd slrflag++;
493 1.1 cgd altarg = (char *) 0;
494 1.1 cgd break;
495 1.1 cgd default:
496 1.1 cgd break;
497 1.1 cgd }
498 1.3 cgd return ((char *)0);
499 1.1 cgd }
500 1.1 cgd
501 1.3 cgd #define HELPINDENT ((int) sizeof ("directory"))
502 1.1 cgd
503 1.1 cgd /*
504 1.1 cgd * Help command.
505 1.1 cgd * Call each command handler with argc == 0 and argv[0] == name.
506 1.1 cgd */
507 1.3 cgd void
508 1.1 cgd help(argc, argv)
509 1.1 cgd int argc;
510 1.1 cgd char *argv[];
511 1.1 cgd {
512 1.3 cgd struct cmd *c;
513 1.1 cgd
514 1.1 cgd if (argc == 1) {
515 1.3 cgd int i, j, w, k;
516 1.1 cgd int columns, width = 0, lines;
517 1.1 cgd
518 1.1 cgd printf("Commands may be abbreviated. Commands are:\n\n");
519 1.1 cgd for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
520 1.1 cgd int len = strlen(c->c_name);
521 1.1 cgd
522 1.1 cgd if (len > width)
523 1.1 cgd width = len;
524 1.1 cgd }
525 1.1 cgd width = (width + 8) &~ 7;
526 1.1 cgd columns = 80 / width;
527 1.1 cgd if (columns == 0)
528 1.1 cgd columns = 1;
529 1.1 cgd lines = (NCMDS + columns - 1) / columns;
530 1.1 cgd for (i = 0; i < lines; i++) {
531 1.1 cgd for (j = 0; j < columns; j++) {
532 1.1 cgd c = cmdtab + j * lines + i;
533 1.1 cgd if (c->c_name && (!proxy || c->c_proxy)) {
534 1.1 cgd printf("%s", c->c_name);
535 1.1 cgd }
536 1.1 cgd else if (c->c_name) {
537 1.1 cgd for (k=0; k < strlen(c->c_name); k++) {
538 1.1 cgd (void) putchar(' ');
539 1.1 cgd }
540 1.1 cgd }
541 1.1 cgd if (c + lines >= &cmdtab[NCMDS]) {
542 1.1 cgd printf("\n");
543 1.1 cgd break;
544 1.1 cgd }
545 1.1 cgd w = strlen(c->c_name);
546 1.1 cgd while (w < width) {
547 1.1 cgd w = (w + 8) &~ 7;
548 1.1 cgd (void) putchar('\t');
549 1.1 cgd }
550 1.1 cgd }
551 1.1 cgd }
552 1.1 cgd return;
553 1.1 cgd }
554 1.1 cgd while (--argc > 0) {
555 1.3 cgd char *arg;
556 1.1 cgd arg = *++argv;
557 1.1 cgd c = getcmd(arg);
558 1.1 cgd if (c == (struct cmd *)-1)
559 1.1 cgd printf("?Ambiguous help command %s\n", arg);
560 1.1 cgd else if (c == (struct cmd *)0)
561 1.1 cgd printf("?Invalid help command %s\n", arg);
562 1.1 cgd else
563 1.1 cgd printf("%-*s\t%s\n", HELPINDENT,
564 1.1 cgd c->c_name, c->c_help);
565 1.1 cgd }
566 1.12 lukem }
567 1.12 lukem
568 1.12 lukem int
569 1.12 lukem auto_fetch(argc, argv)
570 1.12 lukem int argc;
571 1.13 lukem char *argv[];
572 1.12 lukem {
573 1.12 lukem char *xargv[5];
574 1.12 lukem char *cp, *host, *dir, *file;
575 1.12 lukem int rval, xargc;
576 1.12 lukem size_t urllen;
577 1.12 lukem
578 1.12 lukem urllen = strlen(FTPURL);
579 1.12 lukem rval = 0;
580 1.12 lukem
581 1.12 lukem if (setjmp(toplevel))
582 1.12 lukem exit(0);
583 1.12 lukem (void) signal(SIGINT, intr);
584 1.12 lukem (void) signal(SIGPIPE, lostpeer);
585 1.12 lukem
586 1.12 lukem /*
587 1.12 lukem * Loop through as long as there's files to fetch.
588 1.12 lukem */
589 1.12 lukem while (argc > 0 && strchr(argv[0], ':') != NULL) {
590 1.12 lukem host = dir = file = portnum = NULL;
591 1.12 lukem
592 1.12 lukem /*
593 1.12 lukem * We muck with the string, so we make a copy.
594 1.12 lukem */
595 1.12 lukem host = strdup(argv[0]);
596 1.12 lukem if (host == NULL)
597 1.12 lukem errx(1, "Can't allocate memory for auto-fetch.");
598 1.12 lukem
599 1.12 lukem /*
600 1.12 lukem * Try URL-style arguments first, then host:file.
601 1.12 lukem */
602 1.12 lukem if (strncmp(host, FTPURL, urllen) == 0) {
603 1.12 lukem host += urllen;
604 1.12 lukem cp = strchr(host, '/');
605 1.12 lukem
606 1.12 lukem /* Look for a port number after the nost name. */
607 1.12 lukem portnum = strchr(host, ':');
608 1.12 lukem if (portnum != NULL)
609 1.12 lukem *portnum++ = '\0';
610 1.12 lukem } else
611 1.12 lukem cp = strchr(host, ':');
612 1.12 lukem
613 1.12 lukem /*
614 1.14 lukem * If cp is NULL, the file wasn't specified
615 1.14 lukem * (URL looked something like ftp://host)
616 1.14 lukem */
617 1.14 lukem if (cp == NULL)
618 1.14 lukem usage();
619 1.14 lukem
620 1.14 lukem /*
621 1.12 lukem * Extract the file and (if present) directory name.
622 1.12 lukem */
623 1.12 lukem *cp++ = '\0';
624 1.12 lukem dir = cp;
625 1.12 lukem cp = strrchr(cp, '/');
626 1.12 lukem if (cp != NULL) {
627 1.12 lukem *cp++ = '\0';
628 1.12 lukem file = cp;
629 1.12 lukem } else {
630 1.12 lukem file = dir;
631 1.12 lukem dir = NULL;
632 1.12 lukem }
633 1.14 lukem if (file == NULL || *file == '\0')
634 1.14 lukem usage();
635 1.12 lukem
636 1.12 lukem /*
637 1.12 lukem * Set up the connection.
638 1.12 lukem */
639 1.12 lukem xargv[0] = __progname;
640 1.12 lukem xargv[1] = host;
641 1.12 lukem xargv[2] = NULL;
642 1.12 lukem xargc = 2;
643 1.12 lukem if (portnum != NULL) {
644 1.12 lukem xargv[2] = portnum;
645 1.12 lukem xargv[3] = NULL;
646 1.12 lukem xargc = 3;
647 1.12 lukem }
648 1.12 lukem setpeer(xargc, xargv);
649 1.12 lukem if (connected == 0) {
650 1.12 lukem warnx("Can't connect to host `%s'.", host);
651 1.12 lukem rval = 1;
652 1.12 lukem free(host);
653 1.12 lukem continue;
654 1.12 lukem }
655 1.12 lukem
656 1.12 lukem /* Always use binary transfers. */
657 1.12 lukem setbinary(0, NULL);
658 1.12 lukem
659 1.12 lukem /* Change directories, if necessary. */
660 1.12 lukem if (dir != NULL) {
661 1.12 lukem xargv[0] = "cd";
662 1.12 lukem xargv[1] = dir;
663 1.12 lukem xargv[2] = NULL;
664 1.12 lukem cd(2, xargv);
665 1.12 lukem /* XXX SHOULD CHECK FOR ERROR CONDITION! */
666 1.12 lukem }
667 1.12 lukem
668 1.12 lukem /* Fetch the file. */
669 1.12 lukem xargv[0] = "get";
670 1.12 lukem xargv[1] = file;
671 1.12 lukem xargv[2] = NULL;
672 1.12 lukem get(2, xargv);
673 1.12 lukem
674 1.12 lukem disconnect(0, NULL);
675 1.12 lukem free(host);
676 1.12 lukem
677 1.12 lukem --argc, ++argv;
678 1.12 lukem }
679 1.12 lukem
680 1.12 lukem return (rval);
681 1.14 lukem }
682 1.14 lukem
683 1.14 lukem void
684 1.14 lukem usage()
685 1.14 lukem {
686 1.14 lukem (void)fprintf(stderr,
687 1.14 lukem "usage: %s [-adginptv] [-P port] [host [port]]\n"
688 1.14 lukem " %s ftp://host[:port]/file\n"
689 1.14 lukem " %s host:file\n",
690 1.14 lukem __progname, __progname, __progname);
691 1.14 lukem exit(1);
692 1.1 cgd }
693