1 /* $FreeBSD: src/usr.bin/ftp/main.c,v 1.25.2.4 2002/08/27 09:55:08 yar Exp $ */
2 /* $DragonFly: src/usr.bin/ftp/Attic/main.c,v 1.3 2003/10/04 20:36:45 hmp Exp $ */
3 /* $NetBSD: main.c,v 1.26 1997/10/14 16:31:22 christos Exp $ */
6 * Copyright (c) 1985, 1989, 1993, 1994
7 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#) Copyright (c) 1985, 1989, 1993, 1994 \tThe Regents of the University of California. All rights reserved.
38 * @(#)main.c 8.6 (Berkeley) 10/9/94
39 * $NetBSD: main.c,v 1.26 1997/10/14 16:31:22 christos Exp $
40 * $FreeBSD: src/usr.bin/ftp/main.c,v 1.25.2.4 2002/08/27 09:55:08 yar Exp $
43 #include <sys/cdefs.h>
46 * FTP User Program -- Command Interface.
48 #include <sys/types.h>
49 #include <sys/socket.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
64 #include "pathnames.h"
66 int family = AF_UNSPEC;
68 int main(int, char **);
71 main(int argc, char **argv)
74 struct passwd *pw = NULL;
75 char *cp, homedir[MAXPATHLEN], *s;
77 char *src_addr = NULL;
79 (void) setlocale(LC_ALL, "");
84 cp = getenv("FTPSERVERPORT");
86 asprintf(&gateport, "%s", cp);
88 asprintf(&gateport, "ftpgate");
94 try_epsv = epsv4; /* so status w/o connection isn't bogus */
95 restricted_data_ports = 1;
107 if ((tmpdir = getenv("TMPDIR")) == NULL)
110 cp = strrchr(argv[0], '/');
111 cp = (cp == NULL) ? argv[0] : cp + 1;
112 if ((s = getenv("FTP_PASSIVE_MODE")) != NULL
113 && strcasecmp(s, "no") != 0)
115 if (strcmp(cp, "pftp") == 0)
117 else if (strcmp(cp, "gate-ftp") == 0)
120 gateserver = getenv("FTPSERVER");
121 if (gateserver == NULL || *gateserver == '\0')
122 gateserver = GATE_SERVER;
124 if (*gateserver == '\0') {
126 "Neither $FTPSERVER nor GATE_SERVER is defined; disabling gate-ftp");
132 if (cp == NULL || strcmp(cp, "dumb") == 0)
136 fromatty = isatty(fileno(stdin));
138 verbose = 1; /* verbose if from a tty */
141 editing = 1; /* editing mode on if tty is usable */
144 if (isatty(fileno(stdout)) && !dumbterm)
145 progress = 1; /* progress bar on if tty is usable */
147 while ((ch = getopt(argc, argv, "46adeginpP:s:tUvV")) != -1) {
202 restricted_data_ports = 0;
220 cpend = 0; /* no pending replies */
221 proxy = 0; /* proxy not active */
222 crflag = 1; /* strip c.r. on ascii gets */
223 sendport = -1; /* not using ports */
226 struct addrinfo hints;
227 struct addrinfo *res;
230 memset(&hints, 0, sizeof(hints));
231 hints.ai_family = family;
232 hints.ai_socktype = SOCK_STREAM;
233 error = getaddrinfo(src_addr, NULL, &hints, &res);
235 warnx("%s: %s", src_addr, gai_strerror(error));
236 if (error == EAI_SYSTEM)
237 warnx("%s", strerror(errno));
244 * Set up the home directory in case we're globbing.
251 pw = getpwuid(getuid());
254 (void)strcpy(home, pw->pw_dir);
258 (void)signal(SIGWINCH, setttywidth);
260 #ifdef __GNUC__ /* XXX: to shut up gcc warnings */
266 if (strchr(argv[0], ':') != NULL && ! isipv6addr(argv[0])) {
267 anonftp = 1; /* Handle "automatic" transfers. */
268 rval = auto_fetch(argc, argv);
269 if (rval >= 0) /* -1 == connected and cd-ed */
272 char *xargv[4], **xargp = xargv;
274 #ifdef __GNUC__ /* XXX: to shut up gcc warnings */
277 if (setjmp(toplevel))
279 (void)signal(SIGINT, (sig_t)intr);
280 (void)signal(SIGPIPE, (sig_t)lostpeer);
281 *xargp++ = __progname;
282 *xargp++ = argv[0]; /* host */
284 *xargp++ = argv[1]; /* port */
286 setpeer(xargp-xargv, xargv);
292 top = setjmp(toplevel) == 0;
294 (void)signal(SIGINT, (sig_t)intr);
295 (void)signal(SIGPIPE, (sig_t)lostpeer);
308 longjmp(toplevel, 1);
318 (void)shutdown(fileno(cout), 1+1);
323 (void)shutdown(data, 1+1);
332 (void)shutdown(fileno(cout), 1+1);
371 fputs(prompt(), stdout);
372 (void)fflush(stdout);
374 if (fgets(line, sizeof(line), stdin) == NULL)
379 if (line[--num] == '\n') {
383 } else if (num == sizeof(line) - 2) {
384 puts("sorry, input line too long.");
385 while ((num = getchar()) != '\n' && num != EOF)
388 } /* else it was a line without a newline */
394 if ((buf = el_gets(el, &num)) == NULL || num == 0)
396 if (buf[--num] == '\n') {
399 } else if (num >= sizeof(line)) {
400 puts("sorry, input line too long.");
403 memcpy(line, buf, num);
405 history(hist, H_ENTER, buf);
412 #if 0 && !defined(SMALL) /* XXX: don't want el_parse */
414 * el_parse returns -1 to signal that it's not been handled
417 if (el_parse(el, margc, margv) != -1)
420 c = getcmd(margv[0]);
421 if (c == (struct cmd *)-1) {
422 puts("?Ambiguous command.");
426 puts("?Invalid command.");
429 if (c->c_conn && !connected) {
430 puts("Not connected.");
434 (*c->c_handler)(margc, margv);
435 if (bell && c->c_bell)
436 (void)putchar('\007');
437 if (c->c_handler != help)
440 (void)signal(SIGINT, (sig_t)intr);
441 (void)signal(SIGPIPE, (sig_t)lostpeer);
445 getcmd(const char *name)
448 struct cmd *c, *found;
449 int nmatches, longest;
457 for (c = cmdtab; (p = c->c_name) != NULL; c++) {
458 for (q = name; *q == *p++; q++)
459 if (*q == 0) /* exact match? */
461 if (!*q) { /* the name was a prefix */
462 if (q - name > longest) {
466 } else if (q - name == longest)
471 return ((struct cmd *)-1);
476 * Slice a string up into argc/argv.
486 stringbase = line; /* scan from first of buffer */
487 argbase = argbuf; /* store from first of buffer */
489 marg_sl->sl_cur = 0; /* reset to start of marg_sl */
490 for (margc = 0; ; margc++) {
491 argp = slurpstring();
492 sl_add(marg_sl, argp);
497 if (cursor_pos == line) {
500 } else if (cursor_pos != NULL) {
502 cursor_argo = strlen(margv[margc-1]);
508 #define INC_CHKCURSOR(x) (x)++
510 #define INC_CHKCURSOR(x) { (x)++ ; \
511 if (x == cursor_pos) { \
512 cursor_argc = margc; \
513 cursor_argo = ap-argbase; \
520 * Parse string into argbuf;
521 * implemented with FSM to
522 * handle quoting and strings
528 char *sb = stringbase;
530 char *tmp = argbase; /* will return this if token found */
532 if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */
533 switch (slrflag) { /* and $ as token for macro invoke */
536 INC_CHKCURSOR(stringbase);
537 return ((*sb == '!') ? "!" : "$");
580 goto OUT; /* end of token */
584 goto S2; /* slurp next character */
588 goto S3; /* slurp quoted string */
591 *ap = *sb; /* add character to token */
633 argbase = ap; /* update storage pointer */
634 stringbase = sb; /* update scan pointer */
654 * Call each command handler with argc == 0 and argv[0] == name.
657 help(int argc, char **argv)
665 printf("%sommands may be abbreviated. Commands are:\n\n",
666 proxy ? "Proxy c" : "C");
667 for (c = cmdtab; c < &cmdtab[NCMDS]; c++)
668 if (c->c_name && (!proxy || c->c_proxy))
669 sl_add(buf, c->c_name);
675 #define HELPINDENT ((int) sizeof("disconnect"))
682 if (c == (struct cmd *)-1)
683 printf("?Ambiguous help command %s\n", arg);
684 else if (c == (struct cmd *)0)
685 printf("?Invalid help command %s\n", arg);
687 printf("%-*s\t%s\n", HELPINDENT,
688 c->c_name, c->c_help);
695 (void)fprintf(stderr,
696 "usage: %s [-46adeginptUvV] [-P port] [-s src_addr] [host [port]]\n"
698 " %s ftp://host[:port]/path[/]\n"
699 " %s http://host[:port]/file\n",
700 __progname, __progname, __progname, __progname);