2 * Copyright (c) 1985, 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * FTP User Program -- Command Interface.
39 RCSID("$Id: main.c,v 1.27.2.1 2000/10/10 13:01:50 assar Exp $");
42 main(int argc, char **argv)
45 struct passwd *pw = NULL;
46 char homedir[MaxPathLen];
49 set_progname(argv[0]);
51 sp = getservbyname("ftp", "tcp");
53 errx(1, "ftp/tcp: unknown service");
57 passivemode = 0; /* passive mode not active */
59 while ((ch = getopt(argc, argv, "dginptv")) != -1) {
91 "usage: ftp [-dginptv] [host [port]]\n");
98 fromatty = isatty(fileno(stdin));
101 cpend = 0; /* no pending replies */
102 proxy = 0; /* proxy not active */
103 crflag = 1; /* strip c.r. on ascii gets */
104 sendport = -1; /* not using ports */
106 * Set up the home directory in case we're globbing.
108 pw = k_getpwuid(getuid());
110 strlcpy(homedir, pw->pw_dir, sizeof(homedir));
116 if (setjmp(toplevel))
118 signal(SIGINT, intr);
119 signal(SIGPIPE, lostpeer);
120 xargv[0] = (char*)__progname;
125 setpeer(argc+1, xargv);
127 if(setjmp(toplevel) == 0)
132 signal(SIGINT, intr);
133 signal(SIGPIPE, lostpeer);
145 longjmp(toplevel, 1);
158 shutdown(fileno(cout), SHUT_RDWR);
163 shutdown(data, SHUT_RDWR);
172 shutdown(fileno(cout), SHUT_RDWR);
192 s = strrchr(filename, '/');
203 #ifndef HAVE_READLINE
206 readline(char *prompt)
209 printf ("%s", prompt);
211 if(fgets(buf, sizeof(buf), stdin) == NULL)
213 if (buf[strlen(buf) - 1] == '\n')
214 buf[strlen(buf) - 1] = '\0';
225 /* These should not really be here */
227 char *readline(char *);
228 void add_history(char *);
246 p = readline("ftp> ");
251 strlcpy(line, p, sizeof(line));
255 if (fgets(line, sizeof line, stdin) == NULL)
258 /* XXX will break on long lines */
262 if (line[--l] == '\n') {
266 } else if (l == sizeof(line) - 2) {
267 printf("sorry, input line too long\n");
268 while ((l = getchar()) != '\n' && l != EOF)
271 } /* else it was a line without a newline */
276 c = getcmd(margv[0]);
277 if (c == (struct cmd *)-1) {
278 printf("?Ambiguous command\n");
282 printf("?Invalid command\n");
285 if (c->c_conn && !connected) {
286 printf("Not connected.\n");
289 (*c->c_handler)(margc, margv);
290 if (bell && c->c_bell)
292 if (c->c_handler != help)
295 signal(SIGINT, intr);
296 signal(SIGPIPE, lostpeer);
303 struct cmd *c, *found;
304 int nmatches, longest;
309 for (c = cmdtab; (p = c->c_name); c++) {
310 for (q = name; *q == *p++; q++)
311 if (*q == 0) /* exact match? */
313 if (!*q) { /* the name was a prefix */
314 if (q - name > longest) {
318 } else if (q - name == longest)
323 return ((struct cmd *)-1);
328 * Slice a string up into argc/argv.
339 stringbase = line; /* scan from first of buffer */
340 argbase = argbuf; /* store from first of buffer */
342 for (margc = 0; ; margc++) {
343 /* Expand array if necessary */
344 if (margc == margvlen) {
347 margv = (margvlen == 0)
348 ? (char **)malloc(20 * sizeof(char *))
349 : (char **)realloc(margv,
350 (margvlen + 20)*sizeof(char *));
352 errx(1, "cannot realloc argv array");
353 for(i = margvlen; i < margvlen + 20; ++i)
356 argp = margv + margc;
359 if ((*argp++ = slurpstring()) == NULL)
366 * Parse string into argbuf;
367 * implemented with FSM to
368 * handle quoting and strings
374 char *sb = stringbase;
376 char *tmp = argbase; /* will return this if token found */
378 if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */
379 switch (slrflag) { /* and $ as token for macro invoke */
383 return ((*sb == '!') ? "!" : "$");
425 goto OUT; /* end of token */
428 sb++; goto S2; /* slurp next character */
431 sb++; goto S3; /* slurp quoted string */
434 *ap++ = *sb++; /* add character to token */
469 argbase = ap; /* update storage pointer */
470 stringbase = sb; /* update scan pointer */
488 #define HELPINDENT ((int) sizeof ("directory"))
492 * Call each command handler with argc == 0 and argv[0] == name.
495 help(int argc, char **argv)
501 int columns, width = 0, lines;
503 printf("Commands may be abbreviated. Commands are:\n\n");
504 for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
505 int len = strlen(c->c_name);
510 width = (width + 8) &~ 7;
511 columns = 80 / width;
514 lines = (NCMDS + columns - 1) / columns;
515 for (i = 0; i < lines; i++) {
516 for (j = 0; j < columns; j++) {
517 c = cmdtab + j * lines + i;
518 if (c->c_name && (!proxy || c->c_proxy)) {
519 printf("%s", c->c_name);
521 else if (c->c_name) {
522 for (k=0; k < strlen(c->c_name); k++) {
526 if (c + lines >= &cmdtab[NCMDS]) {
530 w = strlen(c->c_name);
543 if (c == (struct cmd *)-1)
544 printf("?Ambiguous help command %s\n", arg);
545 else if (c == (struct cmd *)0)
546 printf("?Invalid help command %s\n", arg);
548 printf("%-*s\t%s\n", HELPINDENT,
549 c->c_name, c->c_help);