X-Git-Url: https://git.subgeniuskitty.com/unix-history/.git/blobdiff_plain/c3b6c3a8c3df9b37b62f07b07e06a583bd71f116..522da3e451a5c5d93d6523b197c81de222ba5402:/usr/src/usr.bin/ftp/main.c diff --git a/usr/src/usr.bin/ftp/main.c b/usr/src/usr.bin/ftp/main.c index 5a52238cf7..3b5b846fb0 100644 --- a/usr/src/usr.bin/ftp/main.c +++ b/usr/src/usr.bin/ftp/main.c @@ -1,108 +1,99 @@ /* - * Copyright (c) 1985 Regents of the University of California. - * All rights reserved. The Berkeley software License Agreement - * specifies the terms and conditions for redistribution. + * Copyright (c) 1985, 1989, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * %sccs.include.redist.c% */ #ifndef lint -char copyright[] = -"@(#) Copyright (c) 1985 Regents of the University of California.\n\ - All rights reserved.\n"; -#endif not lint +static char copyright[] = +"@(#) Copyright (c) 1985, 1989, 1993, 1994\n\ + The Regents of the University of California. All rights reserved.\n"; +#endif /* not lint */ #ifndef lint -static char sccsid[] = "@(#)main.c 5.5 (Berkeley) %G%"; -#endif not lint +static char sccsid[] = "@(#)main.c 8.3 (Berkeley) %G%"; +#endif /* not lint */ /* * FTP User Program -- Command Interface. */ -#include "ftp_var.h" +/*#include */ +#include #include -#include #include -#include -#include -#include #include +#include #include #include +#include +#include +#include +#include +#include "ftp_var.h" -int intr(); -int lostpeer(); -extern char *home; -char *getlogin(); - +int main(argc, argv) + int argc; char *argv[]; { - register char *cp; - int top; + int ch, top; struct passwd *pw = NULL; - char homedir[MAXPATHLEN]; + char *cp, homedir[MAXPATHLEN]; sp = getservbyname("ftp", "tcp"); - if (sp == 0) { - fprintf(stderr, "ftp: ftp/tcp: unknown service\n"); - exit(1); - } + if (sp == 0) + errx(1, "ftp/tcp: unknown service"); doglob = 1; interactive = 1; autologin = 1; - argc--, argv++; - while (argc > 0 && **argv == '-') { - for (cp = *argv + 1; *cp; cp++) - switch (*cp) { - - case 'd': - options |= SO_DEBUG; - debug++; - break; + + while ((ch = getopt(argc, argv, "dgintv")) != EOF) { + switch (*cp) { + case 'd': + options |= SO_DEBUG; + debug++; + break; - case 'v': - verbose++; - break; + case 'g': + doglob = 0; + break; - case 't': - trace++; - break; + case 'i': + interactive = 0; + break; - case 'i': - interactive = 0; - break; + case 'n': + autologin = 0; + break; - case 'n': - autologin = 0; - break; + case 't': + trace++; + break; - case 'g': - doglob = 0; - break; + case 'v': + verbose++; + break; - default: - fprintf(stderr, - "ftp: %c: unknown option\n", *cp); - exit(1); - } - argc--, argv++; + default: + (void)fprintf(stderr, + "usage: ftp [-dgintv] [host [port]]\n"); + exit(1); + } } + argc -= optind; + argv += optind; + fromatty = isatty(fileno(stdin)); - /* - * Set up defaults for FTP. - */ - strcpy(typename, "ascii"), type = TYPE_A; - strcpy(formname, "non-print"), form = FORM_N; - strcpy(modename, "stream"), mode = MODE_S; - strcpy(structname, "file"), stru = STRU_F; - strcpy(bytename, "8"), bytesize = 8; if (fromatty) verbose++; - cpend = 0; /* no pending replies */ + cpend = 0; /* no pending replies */ proxy = 0; /* proxy not active */ - crflag = 1; /* strip c.r. on ascii gets */ + crflag = 1; /* strip c.r. on ascii gets */ + sendport = -1; /* not using ports */ /* * Set up the home directory in case we're globbing. */ @@ -114,19 +105,26 @@ main(argc, argv) pw = getpwuid(getuid()); if (pw != NULL) { home = homedir; - strcpy(home, pw->pw_dir); + (void) strcpy(home, pw->pw_dir); } if (argc > 0) { + char *xargv[3]; + extern char *__progname; + if (setjmp(toplevel)) exit(0); - signal(SIGINT, intr); - signal(SIGPIPE, lostpeer); - setpeer(argc + 1, argv - 1); + (void) signal(SIGINT, intr); + (void) signal(SIGPIPE, lostpeer); + xargv[0] = __progname; + xargv[1] = argv[0]; + xargv[2] = argv[1]; + xargv[3] = argv[2]; + setpeer(argc+1, xargv); } top = setjmp(toplevel) == 0; if (top) { - signal(SIGINT, intr); - signal(SIGPIPE, lostpeer); + (void) signal(SIGINT, intr); + (void) signal(SIGPIPE, lostpeer); } for (;;) { cmdscanner(top); @@ -134,25 +132,25 @@ main(argc, argv) } } +void intr() { longjmp(toplevel, 1); } +void lostpeer() { - extern FILE *cout; - extern int data; if (connected) { if (cout != NULL) { - shutdown(fileno(cout), 1+1); - fclose(cout); + (void) shutdown(fileno(cout), 1+1); + (void) fclose(cout); cout = NULL; } if (data >= 0) { - shutdown(data, 1+1); + (void) shutdown(data, 1+1); (void) close(data); data = -1; } @@ -161,8 +159,8 @@ lostpeer() pswitch(1); if (connected) { if (cout != NULL) { - shutdown(fileno(cout), 1+1); - fclose(cout); + (void) shutdown(fileno(cout), 1+1); + (void) fclose(cout); cout = NULL; } connected = 0; @@ -171,14 +169,15 @@ lostpeer() pswitch(0); } +/* char * tail(filename) char *filename; { - register char *s; + char *s; while (*filename) { - s = rindex(filename, '/'); + s = strrchr(filename, '/'); if (s == NULL) break; if (s[1]) @@ -187,32 +186,40 @@ tail(filename) } return (filename); } +*/ /* * Command parser. */ +void cmdscanner(top) int top; { - register struct cmd *c; - struct cmd *getcmd(); - extern struct cmd cmdtab[]; - extern int help(); + struct cmd *c; + int l; if (!top) - putchar('\n'); + (void) putchar('\n'); for (;;) { if (fromatty) { printf("ftp> "); - fflush(stdout); + (void) fflush(stdout); } - if (gets(line) == 0) { - if (feof(stdin)) - quit(); + if (fgets(line, sizeof line, stdin) == NULL) + quit(0, 0); + l = strlen(line); + if (l == 0) break; - } - if (line[0] == 0) + if (line[--l] == '\n') { + if (l == 0) + break; + line[l] = '\0'; + } else if (l == sizeof(line) - 2) { + printf("sorry, input line too long\n"); + while ((l = getchar()) != '\n' && l != EOF) + /* void */; break; + } /* else it was a line without a newline */ makeargv(); if (margc == 0) { continue; @@ -227,26 +234,26 @@ cmdscanner(top) continue; } if (c->c_conn && !connected) { - printf ("Not connected.\n"); + printf("Not connected.\n"); continue; } (*c->c_handler)(margc, margv); if (bell && c->c_bell) - putchar(CTRL(g)); + (void) putchar('\007'); if (c->c_handler != help) break; } - signal(SIGINT, intr); - signal(SIGPIPE, lostpeer); + (void) signal(SIGINT, intr); + (void) signal(SIGPIPE, lostpeer); } struct cmd * getcmd(name) - register char *name; + char *name; { - register char *p, *q; - register struct cmd *c, *found; - register int nmatches, longest; + char *p, *q; + struct cmd *c, *found; + int nmatches, longest; longest = 0; nmatches = 0; @@ -275,10 +282,10 @@ getcmd(name) int slrflag; +void makeargv() { char **argp; - char *slurpstring(); margc = 0; argp = margv; @@ -298,8 +305,8 @@ char * slurpstring() { int got_one = 0; - register char *sb = stringbase; - register char *ap = argbase; + char *sb = stringbase; + char *ap = argbase; char *tmp = argbase; /* will return this if token found */ if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */ @@ -308,7 +315,7 @@ slurpstring() slrflag++; stringbase++; return ((*sb == '!') ? "!" : "$"); - break; + /* NOTREACHED */ case 1: slrflag++; altarg = stringbase; @@ -396,7 +403,7 @@ OUT: argbase = ap; /* update storage pointer */ stringbase = sb; /* update scan pointer */ if (got_one) { - return(tmp); + return (tmp); } switch (slrflag) { case 0: @@ -409,25 +416,25 @@ OUT: default: break; } - return((char *)0); + return ((char *)0); } -#define HELPINDENT (sizeof ("directory")) +#define HELPINDENT ((int) sizeof ("directory")) /* * Help command. * Call each command handler with argc == 0 and argv[0] == name. */ +void help(argc, argv) int argc; char *argv[]; { - register struct cmd *c; + struct cmd *c; if (argc == 1) { - register int i, j, w, k; + int i, j, w, k; int columns, width = 0, lines; - extern int NCMDS; printf("Commands may be abbreviated. Commands are:\n\n"); for (c = cmdtab; c < &cmdtab[NCMDS]; c++) { @@ -449,7 +456,7 @@ help(argc, argv) } else if (c->c_name) { for (k=0; k < strlen(c->c_name); k++) { - putchar(' '); + (void) putchar(' '); } } if (c + lines >= &cmdtab[NCMDS]) { @@ -459,14 +466,14 @@ help(argc, argv) w = strlen(c->c_name); while (w < width) { w = (w + 8) &~ 7; - putchar('\t'); + (void) putchar('\t'); } } } return; } while (--argc > 0) { - register char *arg; + char *arg; arg = *++argv; c = getcmd(arg); if (c == (struct cmd *)-1) @@ -478,19 +485,3 @@ help(argc, argv) c->c_name, c->c_help); } } - -/* - * Call routine with argc, argv set from args (terminated by 0). - */ -/* VARARGS2 */ -call(routine, args) - int (*routine)(); - int args; -{ - register int *argp; - register int argc; - - for (argc = 0, argp = &args; *argp++ != 0; argc++) - ; - (*routine)(argc, &args); -}