From 06acd2303950d356c512f37c0a748101d38d80ec Mon Sep 17 00:00:00 2001 From: millert Date: Mon, 3 Feb 1997 01:02:31 +0000 Subject: [PATCH] Sync with NetBSD --- usr.bin/ftp/Makefile | 17 +- usr.bin/ftp/cmds.c | 764 ++++++++++++++-------------------------- usr.bin/ftp/cmdtab.c | 191 +++++----- usr.bin/ftp/complete.c | 364 +++++++++++++++++++ usr.bin/ftp/domacro.c | 9 +- usr.bin/ftp/extern.h | 66 ++-- usr.bin/ftp/fetch.c | 508 ++++++++++++++++++++++++++ usr.bin/ftp/ftp.1 | 431 ++++++++++++++--------- usr.bin/ftp/ftp.c | 320 ++++++++++------- usr.bin/ftp/ftp_var.h | 58 ++- usr.bin/ftp/main.c | 481 ++++++++++++------------- usr.bin/ftp/pathnames.h | 5 +- usr.bin/ftp/ruserpass.c | 38 +- usr.bin/ftp/util.c | 614 ++++++++++++++++++++++++++++++++ 14 files changed, 2692 insertions(+), 1174 deletions(-) create mode 100644 usr.bin/ftp/complete.c create mode 100644 usr.bin/ftp/fetch.c create mode 100644 usr.bin/ftp/util.c diff --git a/usr.bin/ftp/Makefile b/usr.bin/ftp/Makefile index ca0d202f7ae..a73908d2179 100644 --- a/usr.bin/ftp/Makefile +++ b/usr.bin/ftp/Makefile @@ -1,8 +1,17 @@ -# $OpenBSD: Makefile,v 1.4 1996/09/03 18:00:05 deraadt Exp $ -# $NetBSD: Makefile,v 1.6 1995/11/22 21:52:48 cgd Exp $ +# $NetBSD: Makefile,v 1.8 1997/01/19 14:19:02 lukem Exp $ +# from: @(#)Makefile 8.2 (Berkeley) 4/3/94 + +# define SMALLFTP if editing is to be disabled +#SMALLFTP=yes PROG= ftp -SRCS= cmds.c cmdtab.c ftp.c main.c ruserpass.c domacro.c http.c -CFLAGS+= -Dunix +SRCS= cmds.c cmdtab.c domacro.c fetch.c ftp.c main.c ruserpass.c util.c + +.if defined(SMALLFTP) +CFLAGS+=-DSMALLFTP +.else +SRCS+= complete.c +LDADD+= -ledit -ltermcap +.endif .include diff --git a/usr.bin/ftp/cmds.c b/usr.bin/ftp/cmds.c index 9b7d4aa812f..76529c943f6 100644 --- a/usr.bin/ftp/cmds.c +++ b/usr.bin/ftp/cmds.c @@ -1,5 +1,4 @@ -/* $OpenBSD: cmds.c,v 1.12 1997/01/29 22:11:36 millert Exp $ */ -/* $NetBSD: cmds.c,v 1.8 1995/09/08 01:06:05 tls Exp $ */ +/* $NetBSD: cmds.c,v 1.18 1997/02/01 10:44:54 lukem Exp $ */ /* * Copyright (c) 1985, 1989, 1993, 1994 @@ -38,31 +37,27 @@ #if 0 static char sccsid[] = "@(#)cmds.c 8.6 (Berkeley) 10/9/94"; #else -static char rcsid[] = "$OpenBSD: cmds.c,v 1.12 1997/01/29 22:11:36 millert Exp $"; +static char rcsid[] = "$NetBSD: cmds.c,v 1.18 1997/02/01 10:44:54 lukem Exp $"; #endif #endif /* not lint */ /* * FTP User Program -- Command Routines. */ -#include -#include -#include +#include #include -#include +#include +#include #include #include #include #include #include -#include #include #include #include -#include #include -#include #include "ftp_var.h" #include "pathnames.h" @@ -71,147 +66,6 @@ jmp_buf jabort; char *mname; char *home = "/"; -/* - * `Another' gets another argument, and stores the new argc and argv. - * It reverts to the top level (via main.c's intr()) on EOF/error. - * - * Returns false if no new arguments have been added. - */ -int -another(pargc, pargv, prompt) - int *pargc; - char ***pargv; - char *prompt; -{ - int len = strlen(line), ret; - - if (len >= sizeof(line) - 3) { - printf("sorry, arguments too long\n"); - intr(); - } - printf("(%s) ", prompt); - line[len++] = ' '; - if (fgets(&line[len], sizeof(line) - len, stdin) == NULL) - intr(); - len += strlen(&line[len]); - if (len > 0 && line[len - 1] == '\n') - line[len - 1] = '\0'; - makeargv(); - ret = margc > *pargc; - *pargc = margc; - *pargv = margv; - return (ret); -} - -/* - * Connect to peer server and - * auto-login, if possible. - */ -void -setpeer(argc, argv) - int argc; - char *argv[]; -{ - char *host; - short port; - - if (connected) { - printf("Already connected to %s, use close first.\n", - hostname); - code = -1; - return; - } - if (argc < 2) - (void) another(&argc, &argv, "to"); - if (argc < 2 || argc > 3) { - printf("usage: %s host-name [port]\n", argv[0]); - code = -1; - return; - } - port = sp->s_port; - if (argc > 2) { - port = atoi(argv[2]); - if (port <= 0) { - printf("%s: bad port number-- %s\n", argv[1], argv[2]); - printf ("usage: %s host-name [port]\n", argv[0]); - code = -1; - return; - } - port = htons(port); - } - host = hookup(argv[1], port); - if (host) { - int overbose; - - connected = 1; - /* - * Set up defaults for FTP. - */ - (void) strcpy(typename, "ascii"), type = TYPE_A; - curtype = TYPE_A; - (void) strcpy(formname, "non-print"), form = FORM_N; - (void) strcpy(modename, "stream"), mode = MODE_S; - (void) strcpy(structname, "file"), stru = STRU_F; - (void) strcpy(bytename, "8"), bytesize = 8; - if (autologin) - (void) login(argv[1]); - -#if defined(unix) && NBBY == 8 -/* - * this ifdef is to keep someone form "porting" this to an incompatible - * system and not checking this out. This way they have to think about it. - */ - overbose = verbose; - if (debug == 0) - verbose = -1; - if (command("SYST") == COMPLETE && overbose) { - char *cp, c; - cp = strchr(reply_string+4, ' '); - if (cp == NULL) - cp = strchr(reply_string+4, '\r'); - if (cp) { - if (cp[-1] == '.') - cp--; - c = *cp; - *cp = '\0'; - } - - printf("Remote system type is %s.\n", - reply_string+4); - if (cp) - *cp = c; - } - if (!strncmp(reply_string, "215 UNIX Type: L8", 17)) { - if (proxy) - unix_proxy = 1; - else - unix_server = 1; - /* - * Set type to 0 (not specified by user), - * meaning binary by default, but don't bother - * telling server. We can use binary - * for text files unless changed by the user. - */ - type = 0; - (void) strcpy(typename, "binary"); - if (overbose) - printf("Using %s mode to transfer files.\n", - typename); - } else { - if (proxy) - unix_proxy = 0; - else - unix_server = 0; - if (overbose && - !strncmp(reply_string, "215 TOPS20", 10)) - printf( -"Remember to set tenex mode when transfering binary files from this machine.\n"); - } - verbose = overbose; -#endif /* unix */ - } -} - struct types { char *t_name; char *t_mode; @@ -320,7 +174,7 @@ char *stype[] = { void setbinary(argc, argv) int argc; - char **argv; + char *argv[]; { stype[1] = "binary"; @@ -416,9 +270,9 @@ put(argc, argv) } if (argc < 2 && !another(&argc, &argv, "local-file")) goto usage; - if (argc < 3 && !another(&argc, &argv, "remote-file")) { + if ((argc < 3 && !another(&argc, &argv, "remote-file")) || argc > 3) { usage: - printf("usage: %s local-file remote-file\n", argv[0]); + printf("usage: %s local-file [ remote-file ]\n", argv[0]); code = -1; return; } @@ -452,7 +306,7 @@ usage: void mput(argc, argv) int argc; - char **argv; + char *argv[]; { int i; sig_t oldintr; @@ -471,8 +325,8 @@ mput(argc, argv) if (proxy) { char *cp, *tp2, tmpbuf[MAXPATHLEN]; - while ((cp = remglob(argv,0)) != NULL) { - if (*cp == 0) { + while ((cp = remglob(argv, 0)) != NULL) { + if (*cp == '\0') { mflag = 0; continue; } @@ -506,7 +360,7 @@ mput(argc, argv) if (!mflag && fromatty) { ointer = interactive; interactive = 1; - if (confirm("Continue with","mput")) { + if (confirm("Continue with", "mput")) { mflag++; } interactive = ointer; @@ -518,7 +372,7 @@ mput(argc, argv) return; } for (i = 1; i < argc; i++) { - char **cpp, **gargs; + char **cpp; glob_t gl; int flags; @@ -531,7 +385,7 @@ mput(argc, argv) if (!mflag && fromatty) { ointer = interactive; interactive = 1; - if (confirm("Continue with","mput")) { + if (confirm("Continue with", "mput")) { mflag++; } interactive = ointer; @@ -556,7 +410,7 @@ mput(argc, argv) if (!mflag && fromatty) { ointer = interactive; interactive = 1; - if (confirm("Continue with","mput")) { + if (confirm("Continue with", "mput")) { mflag++; } interactive = ointer; @@ -594,8 +448,8 @@ int getit(argc, argv, restartit, mode) int argc; char *argv[]; - char *mode; int restartit; + const char *mode; { int loc = 0; char *oldargv1, *oldargv2; @@ -607,7 +461,7 @@ getit(argc, argv, restartit, mode) } if (argc < 2 && !another(&argc, &argv, "remote-file")) goto usage; - if (argc < 3 && !another(&argc, &argv, "local-file")) { + if ((argc < 3 && !another(&argc, &argv, "local-file")) || argc > 3) { usage: printf("usage: %s remote-file [ local-file ]\n", argv[0]); code = -1; @@ -655,38 +509,13 @@ usage: restart_point = stbuf.st_size; } else { if (ret == 0) { - int overbose; - - overbose = verbose; - if (debug == 0) - verbose = -1; - if (command("MDTM %s", argv[1]) == COMPLETE) { - int yy, mo, day, hour, min, sec; - struct tm *tm; - verbose = overbose; - sscanf(reply_string, - "%*s %04d%02d%02d%02d%02d%02d", - &yy, &mo, &day, &hour, &min, &sec); - tm = gmtime(&stbuf.st_mtime); - tm->tm_mon++; - if (tm->tm_year > yy%100) - return (1); - if ((tm->tm_year == yy%100 && - tm->tm_mon > mo) || - (tm->tm_mon == mo && - tm->tm_mday > day) || - (tm->tm_mday == day && - tm->tm_hour > hour) || - (tm->tm_hour == hour && - tm->tm_min > min) || - (tm->tm_min == min && - tm->tm_sec > sec)) - return (1); - } else { - printf("%s\n", reply_string); - verbose = overbose; + time_t mtime; + + mtime = remotemodtime(argv[1], 0); + if (mtime == -1) return (0); - } + if (stbuf.st_mtime >= mtime) + return (1); } } } @@ -702,21 +531,26 @@ void mabort(signo) int signo; { - int ointer; + int ointer, oconf; + alarmtimer(0); printf("\n"); (void) fflush(stdout); if (mflag && fromatty) { ointer = interactive; + oconf = confirmrest; interactive = 1; + confirmrest = 0; if (confirm("Continue with", mname)) { interactive = ointer; - longjmp(jabort,0); + confirmrest = oconf; + longjmp(jabort, 0); } interactive = ointer; + confirmrest = oconf; } mflag = 0; - longjmp(jabort,0); + longjmp(jabort, 0); } /* @@ -725,7 +559,7 @@ mabort(signo) void mget(argc, argv) int argc; - char **argv; + char *argv[]; { sig_t oldintr; int ch, ointer; @@ -740,7 +574,7 @@ mget(argc, argv) mflag = 1; oldintr = signal(SIGINT, mabort); (void) setjmp(jabort); - while ((cp = remglob(argv,proxy)) != NULL) { + while ((cp = remglob(argv, proxy)) != NULL) { if (*cp == '\0') { mflag = 0; continue; @@ -748,7 +582,7 @@ mget(argc, argv) if (mflag && confirm(argv[0], cp)) { tp = cp; if (mcase) { - for (tp2 = tmpbuf; ch = *tp++;) + for (tp2 = tmpbuf; (ch = *tp++) != NULL; ) *tp2++ = isupper(ch) ? tolower(ch) : ch; *tp2 = '\0'; tp = tmpbuf; @@ -764,84 +598,17 @@ mget(argc, argv) if (!mflag && fromatty) { ointer = interactive; interactive = 1; - if (confirm("Continue with","mget")) { + if (confirm("Continue with", "mget")) { mflag++; } interactive = ointer; } } } - (void) signal(SIGINT,oldintr); + (void) signal(SIGINT, oldintr); mflag = 0; } -char * -remglob(argv,doswitch) - char *argv[]; - int doswitch; -{ - char temp[16]; - static char buf[MAXPATHLEN]; - static FILE *ftemp = NULL; - int fd; - static char **args; - int oldverbose, oldhash; - char *cp, *mode; - - if (!mflag) { - if (!doglob) { - args = NULL; - } - else { - if (ftemp) { - (void) fclose(ftemp); - ftemp = NULL; - } - } - return (NULL); - } - if (!doglob) { - if (args == NULL) - args = argv; - if ((cp = *++args) == NULL) - args = NULL; - return (cp); - } - if (ftemp == NULL) { - (void) strcpy(temp, _PATH_TMPFILE); - fd = mkstemp(temp); - if (fd < 0) { - printf ("temporary file %s already exists\n", temp); - return NULL; - } - close (fd); - oldverbose = verbose, verbose = 0; - oldhash = hash, hash = 0; - if (doswitch) { - pswitch(!proxy); - } - for (mode = "w"; *++argv != NULL; mode = "a") - recvrequest ("NLST", temp, *argv, mode, 0); - if (doswitch) { - pswitch(!proxy); - } - verbose = oldverbose; hash = oldhash; - ftemp = fopen(temp, "r"); - (void) unlink(temp); - if (ftemp == NULL) { - printf("can't find list of remote files, oops\n"); - return (NULL); - } - } - if (fgets(buf, sizeof (buf), ftemp) == NULL) { - (void) fclose(ftemp), ftemp = NULL; - return (NULL); - } - if ((cp = strchr(buf, '\n')) != NULL) - *cp = '\0'; - return (buf); -} - char * onoff(bool) int bool; @@ -868,23 +635,26 @@ status(argc, argv) if (!proxy) { pswitch(1); if (connected) { - printf("Connected for proxy commands to %s.\n", hostname); + printf("Connected for proxy commands to %s.\n", + hostname); } else { printf("No proxy connection.\n"); } pswitch(0); } + printf("Passive mode: %s.\n", onoff(passivemode)); printf("Mode: %s; Type: %s; Form: %s; Structure: %s\n", modename, typename, formname, structname); - printf("Verbose: %s; Bell: %s; Prompting: %s; Globbing: %s\n", + printf("Verbose: %s; Bell: %s; Prompting: %s; Globbing: %s\n", onoff(verbose), onoff(bell), onoff(interactive), onoff(doglob)); printf("Store unique: %s; Receive unique: %s\n", onoff(sunique), onoff(runique)); - printf("Case: %s; CR stripping: %s\n",onoff(mcase),onoff(crflag)); + printf("Preserve modification times: %s\n", onoff(preserve)); + printf("Case: %s; CR stripping: %s\n", onoff(mcase), onoff(crflag)); if (ntflag) { - printf("Ntrans: (in) %s (out) %s\n", ntin,ntout); + printf("Ntrans: (in) %s (out) %s\n", ntin, ntout); } else { printf("Ntrans: off\n"); @@ -895,17 +665,45 @@ status(argc, argv) else { printf("Nmap: off\n"); } - printf("Hash mark printing: %s; Mark count: %d\n", onoff(hash), mark); + printf("Hash mark printing: %s; Mark count: %d; Progress bar: %s\n", + onoff(hash), mark, onoff(progress)); printf("Use of PORT cmds: %s\n", onoff(sendport)); +#ifndef SMALLFTP + printf("Command line editing: %s\n", onoff(editing)); +#endif /* !SMALLFTP */ if (macnum > 0) { printf("Macros:\n"); for (i=0; i 1) { - val = atoi(argv[1]); - if (val < 0) { - printf("%s: bad debugging value.\n", argv[1]); - code = -1; - return; + if (argc > 2) { + printf("usage: %s [ on | off | debuglevel ]\n", argv[0]); + code = -1; + return; + } else if (argc == 2) { + if (strcasecmp(argv[1], "on") == 0) + debug = 1; + else if (strcasecmp(argv[1], "off") == 0) + debug = 0; + else { + val = atoi(argv[1]); + if (val < 0) { + printf("%s: bad debugging value.\n", argv[1]); + code = -1; + return; + } + debug = val; } } else - val = !debug; - debug = val; + debug = !debug; if (debug) options |= SO_DEBUG; else @@ -1074,36 +905,27 @@ setdebug(argc, argv) * Set current working directory * on remote machine. */ -int -mcd(argc, argv) +void +cd(argc, argv) int argc; char *argv[]; { + int r; - if (argc < 2 && !another(&argc, &argv, "remote-directory")) { + if ((argc < 2 && !another(&argc, &argv, "remote-directory")) || + argc > 2) { printf("usage: %s remote-directory\n", argv[0]); code = -1; - return (-1); - } - if (command("CWD %s", argv[1]) == ERROR) { - if (code == 500) { - if (verbose) - printf("CWD command not recognized, " - "trying XCWD\n"); - return(command("XCWD %s", argv[1])); - } - else - return(-1); + return; } - return(0); -} - -void -cd(argc, argv) - int argc; - char *argv[]; -{ - mcd(argc, argv); + r = command("CWD %s", argv[1]); + if (r == ERROR && code == 500) { + if (verbose) + printf("CWD command not recognized, trying XCWD\n"); + r = command("XCWD %s", argv[1]); + } + if (r == COMPLETE) + dirchange = 1; } /* @@ -1136,7 +958,7 @@ lcd(argc, argv) if (getcwd(buf, sizeof(buf)) != NULL) printf("Local directory now %s\n", buf); else - warnx("getcwd: %s", buf); + warn("getcwd: %s", argv[1]); code = 0; } @@ -1149,7 +971,7 @@ delete(argc, argv) char *argv[]; { - if (argc < 2 && !another(&argc, &argv, "remote-file")) { + if ((argc < 2 && !another(&argc, &argv, "remote-file")) || argc > 2) { printf("usage: %s remote-file\n", argv[0]); code = -1; return; @@ -1163,7 +985,7 @@ delete(argc, argv) void mdelete(argc, argv) int argc; - char **argv; + char *argv[]; { sig_t oldintr; int ointer; @@ -1178,7 +1000,7 @@ mdelete(argc, argv) mflag = 1; oldintr = signal(SIGINT, mabort); (void) setjmp(jabort); - while ((cp = remglob(argv,0)) != NULL) { + while ((cp = remglob(argv, 0)) != NULL) { if (*cp == '\0') { mflag = 0; continue; @@ -1210,7 +1032,7 @@ renamefile(argc, argv) if (argc < 2 && !another(&argc, &argv, "from-name")) goto usage; - if (argc < 3 && !another(&argc, &argv, "to-name")) { + if ((argc < 3 && !another(&argc, &argv, "to-name")) || argc > 3) { usage: printf("%s from-name to-name\n", argv[0]); code = -1; @@ -1229,7 +1051,7 @@ ls(argc, argv) int argc; char *argv[]; { - char *cmd; + const char *cmd; if (argc < 2) argc++, argv[1] = NULL; @@ -1240,13 +1062,14 @@ ls(argc, argv) code = -1; return; } - cmd = argv[0][0] == 'n' ? "NLST" : "LIST"; + cmd = strcmp(argv[0], "dir") == 0 ? "LIST" : "NLST"; if (strcmp(argv[2], "-") && !globulize(&argv[2])) { code = -1; return; } if (strcmp(argv[2], "-") && *argv[2] != '|') - if (!globulize(&argv[2]) || !confirm("output to local-file:", argv[2])) { + if (!globulize(&argv[2]) || !confirm("output to local-file:", + argv[2])) { code = -1; return; } @@ -1263,11 +1086,12 @@ ls(argc, argv) void mls(argc, argv) int argc; - char **argv; + char *argv[]; { sig_t oldintr; int ointer, i; - char *cmd, mode[1], *dest; + const char *cmd; + char mode[1], *dest; if (argc < 2 && !another(&argc, &argv, "remote-files")) goto usage; @@ -1285,7 +1109,7 @@ usage: code = -1; return; } - cmd = argv[0][1] == 'l' ? "NLST" : "LIST"; + cmd = strcmp(argv[0], "mls") == 0 ? "NLST" : "LIST"; mname = argv[0]; mflag = 1; oldintr = signal(SIGINT, mabort); @@ -1313,11 +1137,11 @@ usage: void shell(argc, argv) int argc; - char **argv; + char *argv[]; { pid_t pid; sig_t old1, old2; - char shellnam[40], *shell, *namep; + char shellnam[MAXPATHLEN], *shell, *namep; union wait status; old1 = signal (SIGINT, SIG_IGN); @@ -1330,11 +1154,11 @@ shell(argc, argv) shell = getenv("SHELL"); if (shell == NULL) shell = _PATH_BSHELL; - namep = strrchr(shell,'/'); + namep = strrchr(shell, '/'); if (namep == NULL) namep = shell; - (void) strcpy(shellnam,"-"); - (void) strcat(shellnam, ++namep); + shellnam[0] = '-'; + (void) strncpy(shellnam + 1, ++namep, sizeof(shellnam) - 1); if (strcmp(namep, "sh") != 0) shellnam[0] = '+'; if (debug) { @@ -1342,10 +1166,10 @@ shell(argc, argv) (void) fflush (stdout); } if (argc > 1) { - execl(shell,shellnam,"-c",altarg,(char *)0); + execl(shell, shellnam, "-c", altarg, (char *)0); } else { - execl(shell,shellnam,(char *)0); + execl(shell, shellnam, (char *)0); } warn("%s", shell); code = -1; @@ -1371,7 +1195,7 @@ shell(argc, argv) void user(argc, argv) int argc; - char **argv; + char *argv[]; { char acct[80]; int n, aflag = 0; @@ -1409,7 +1233,7 @@ user(argc, argv) } /* - * Print working directory. + * Print working directory on remote machine. */ /*VARARGS*/ void @@ -1430,6 +1254,23 @@ pwd(argc, argv) verbose = oldverbose; } +/* + * Print working directory on local machine. + */ +void +lpwd(argc, argv) + int argc; + char *argv[]; +{ + char buf[MAXPATHLEN]; + + if (getcwd(buf, sizeof(buf)) != NULL) + printf("Local directory %s\n", buf); + else + warn("getcwd"); + code = 0; +} + /* * Make a directory. */ @@ -1439,7 +1280,8 @@ makedir(argc, argv) char *argv[]; { - if (argc < 2 && !another(&argc, &argv, "directory-name")) { + if ((argc < 2 && !another(&argc, &argv, "directory-name")) || + argc > 2) { printf("usage: %s directory-name\n", argv[0]); code = -1; return; @@ -1460,7 +1302,8 @@ removedir(argc, argv) char *argv[]; { - if (argc < 2 && !another(&argc, &argv, "directory-name")) { + if ((argc < 2 && !another(&argc, &argv, "directory-name")) || + argc > 2) { printf("usage: %s directory-name\n", argv[0]); code = -1; return; @@ -1514,20 +1357,21 @@ site(argc, argv) */ void quote1(initial, argc, argv) - char *initial; + const char *initial; int argc; - char **argv; + char *argv[]; { int i, len; char buf[BUFSIZ]; /* must be >= sizeof(line) */ - (void) strcpy(buf, initial); + (void) strncpy(buf, initial, sizeof(buf)); if (argc > 1) { len = strlen(buf); - len += strlen(strcpy(&buf[len], argv[1])); + len += strlen(strncpy(&buf[len], argv[1], sizeof(buf) - len)); for (i = 2; i < argc; i++) { buf[len++] = ' '; - len += strlen(strcpy(&buf[len], argv[i])); + len += strlen(strncpy(&buf[len], argv[i], + sizeof(buf) - len)); } } if (command(buf) == PRELIM) { @@ -1544,7 +1388,7 @@ do_chmod(argc, argv) if (argc < 2 && !another(&argc, &argv, "mode")) goto usage; - if (argc < 3 && !another(&argc, &argv, "file-name")) { + if ((argc < 3 && !another(&argc, &argv, "file-name")) || argc > 3) { usage: printf("usage: %s mode file-name\n", argv[0]); code = -1; @@ -1634,80 +1478,22 @@ disconnect(argc, argv) } } -int -confirm(cmd, file) - char *cmd, *file; -{ - char line[BUFSIZ]; - - if (!interactive) - return (1); - printf("%s %s? ", cmd, file); - (void) fflush(stdout); - if (fgets(line, sizeof line, stdin) == NULL) - return (0); - return (*line != 'n' && *line != 'N'); -} - -void -fatal(msg) - char *msg; -{ - - errx(1, "%s", msg); -} - -/* - * Glob a local file name specification with - * the expectation of a single return value. - * Can't control multiple values being expanded - * from the expression, we return only the first. - */ -int -globulize(cpp) - char **cpp; -{ - glob_t gl; - int flags; - - if (!doglob) - return (1); - - flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE; - memset(&gl, 0, sizeof(gl)); - if (glob(*cpp, flags, NULL, &gl) || - gl.gl_pathc == 0) { - warnx("%s: not found", *cpp); - globfree(&gl); - return (0); - } - *cpp = strdup(gl.gl_pathv[0]); /* XXX - wasted memory */ - globfree(&gl); - return (1); -} - void -account(argc,argv) +account(argc, argv) int argc; - char **argv; + char *argv[]; { - char acct[50], *ap; + char *ap; - if (argc > 1) { - ++argv; - --argc; - (void) strncpy(acct,*argv,49); - acct[49] = '\0'; - while (argc > 1) { - --argc; - ++argv; - (void) strncat(acct,*argv, 49-strlen(acct)); - } - ap = acct; + if (argc > 2) { + printf("usage: %s [password]\n", argv[0]); + code = -1; + return; } - else { + else if (argc == 2) + ap = argv[1]; + else ap = getpass("Account:"); - } (void) command("ACCT %s", ap); } @@ -1717,6 +1503,7 @@ void proxabort() { + alarmtimer(0); if (!proxy) { pswitch(1); } @@ -1727,7 +1514,7 @@ proxabort() proxflag = 0; } pswitch(0); - longjmp(abortprox,1); + longjmp(abortprox, 1); } void @@ -1736,6 +1523,7 @@ doproxy(argc, argv) char *argv[]; { struct cmd *c; + int cmdpos; sig_t oldintr; if (argc < 2 && !another(&argc, &argv, "command")) { @@ -1776,6 +1564,9 @@ doproxy(argc, argv) code = -1; return; } + cmdpos = strcspn(line, " \t"); + if (cmdpos > 0) /* remove leading "proxy " from input buffer */ + memmove(line, line + cmdpos + 1, strlen(line) - cmdpos + 1); (*c->c_handler)(argc-1, argv+1); if (connected) { proxflag = 1; @@ -1793,9 +1584,7 @@ setcase(argc, argv) char *argv[]; { - mcase = !mcase; - printf("Case mapping %s.\n", onoff(mcase)); - code = mcase; + code = togglevar(argc, argv, &mcase, "Case mapping"); } void @@ -1804,13 +1593,11 @@ setcr(argc, argv) char *argv[]; { - crflag = !crflag; - printf("Carriage Return stripping %s.\n", onoff(crflag)); - code = crflag; + code = togglevar(argc, argv, &crflag, "Carriage Return stripping"); } void -setntrans(argc,argv) +setntrans(argc, argv) int argc; char *argv[]; { @@ -1874,8 +1661,8 @@ setnmap(argc, argv) code = mapflag; return; } - if (argc < 3 && !another(&argc, &argv, "mapout")) { - printf("Usage: %s [mapin mapout]\n",argv[0]); + if ((argc < 3 && !another(&argc, &argv, "mapout")) || argc > 3) { + printf("Usage: %s [mapin mapout]\n", argv[0]); code = -1; return; } @@ -1957,7 +1744,7 @@ domap(name) break; case '[': LOOP: - if (*++cp2 == '$' && isdigit(*(cp2+1))) { + if (*++cp2 == '$' && isdigit(*(cp2+1))) { if (*++cp2 == '0') { char *cp3 = name; @@ -1976,7 +1763,7 @@ LOOP: } } else { - while (*cp2 && *cp2 != ',' && + while (*cp2 && *cp2 != ',' && *cp2 != ']') { if (*cp2 == '\\') { cp2++; @@ -2005,7 +1792,8 @@ LOOP: } } if (!*cp2) { - printf("nmap: unbalanced brackets\n"); + printf("nmap: unbalanced " + "brackets\n"); return (name); } match = 1; @@ -2018,7 +1806,8 @@ LOOP: } } if (!*cp2) { - printf("nmap: unbalanced brackets\n"); + printf("nmap: unbalanced " + "brackets\n"); return (name); } break; @@ -2071,10 +1860,7 @@ setpassive(argc, argv) char *argv[]; { - passivemode = !passivemode; - if (verbose) - printf("Passive mode %s.\n", onoff(passivemode)); - code = passivemode; + code = togglevar(argc, argv, &passivemode, "Passive mode"); } void @@ -2083,9 +1869,7 @@ setsunique(argc, argv) char *argv[]; { - sunique = !sunique; - printf("Store unique %s.\n", onoff(sunique)); - code = sunique; + code = togglevar(argc, argv, &sunique, "Store unique"); } void @@ -2094,23 +1878,25 @@ setrunique(argc, argv) char *argv[]; { - runique = !runique; - printf("Receive unique %s.\n", onoff(runique)); - code = runique; + code = togglevar(argc, argv, &runique, "Receive unique"); } -/* change directory to perent directory */ +/* change directory to parent directory */ void cdup(argc, argv) int argc; char *argv[]; { + int r; - if (command("CDUP") == ERROR && code == 500) { + r = command("CDUP"); + if (r == ERROR && code == 500) { if (verbose) printf("CDUP command not recognized, trying XCUP\n"); - (void) command("XCUP"); + r = command("XCUP"); } + if (r == COMPLETE) + dirchange = 1; } /* restart transfer at specific point */ @@ -2124,8 +1910,8 @@ restart(argc, argv) printf("restart: offset not specified\n"); else { restart_point = atol(argv[1]); - printf("restarting at %qd. %s\n", restart_point, - "execute get, put or append to initiate transfer"); + printf("Restarting at %qd. Execute get, put or append to" + "initiate transfer\n", restart_point); } } @@ -2152,13 +1938,14 @@ macdef(argc, argv) code = -1; return; } - if (argc < 2 && !another(&argc, &argv, "macro name")) { - printf("Usage: %s macro_name\n",argv[0]); + if ((argc < 2 && !another(&argc, &argv, "macro name")) || argc > 2) { + printf("Usage: %s macro_name\n", argv[0]); code = -1; return; } if (interactive) { - printf("Enter macro line by line, terminating it with a null line\n"); + printf("Enter macro line by line, terminating it with a " + "null line\n"); } (void) strncpy(macros[macnum].mac_name, argv[1], 8); if (macnum == 0) { @@ -2208,13 +1995,17 @@ sizecmd(argc, argv) int argc; char *argv[]; { + off_t size; - if (argc < 2 && !another(&argc, &argv, "filename")) { + if ((argc < 2 && !another(&argc, &argv, "filename")) || argc > 2) { printf("usage: %s filename\n", argv[0]); code = -1; return; } - (void) command("SIZE %s", argv[1]); + size = remotesize(argv[1], 1); + if (size != -1) + printf("%s\t%qd\n", argv[1], size); + code = size; } /* @@ -2225,30 +2016,21 @@ modtime(argc, argv) int argc; char *argv[]; { - int overbose; + time_t mtime; - if (argc < 2 && !another(&argc, &argv, "filename")) { + if ((argc < 2 && !another(&argc, &argv, "filename")) || argc > 2) { printf("usage: %s filename\n", argv[0]); code = -1; return; } - overbose = verbose; - if (debug == 0) - verbose = -1; - if (command("MDTM %s", argv[1]) == COMPLETE) { - int yy, mo, day, hour, min, sec; - sscanf(reply_string, "%*s %04d%02d%02d%02d%02d%02d", &yy, &mo, - &day, &hour, &min, &sec); - /* might want to print this in local time */ - printf("%s\t%02d/%02d/%04d %02d:%02d:%02d GMT\n", argv[1], - mo, day, yy, hour, min, sec); - } else - printf("%s\n", reply_string); - verbose = overbose; + mtime = remotemodtime(argv[1], 1); + if (mtime != -1) + printf("%s\t%s", argv[1], asctime(localtime(&mtime))); + code = mtime; } /* - * show status on reomte machine + * show status on remote machine */ void rmtstatus(argc, argv) diff --git a/usr.bin/ftp/cmdtab.c b/usr.bin/ftp/cmdtab.c index 669f364ac6e..db253004310 100644 --- a/usr.bin/ftp/cmdtab.c +++ b/usr.bin/ftp/cmdtab.c @@ -1,5 +1,4 @@ -/* $OpenBSD: cmdtab.c,v 1.4 1996/12/01 05:27:22 millert Exp $ */ -/* $NetBSD: cmdtab.c,v 1.6 1995/09/08 01:06:10 tls Exp $ */ +/* $NetBSD: cmdtab.c,v 1.12 1997/01/19 14:19:05 lukem Exp $ */ /* * Copyright (c) 1985, 1989, 1993, 1994 @@ -38,7 +37,7 @@ #if 0 static char sccsid[] = "@(#)cmdtab.c 8.4 (Berkeley) 10/9/94"; #else -static char rcsid[] = "$OpenBSD: cmdtab.c,v 1.4 1996/12/01 05:27:22 millert Exp $"; +static char rcsid[] = "$NetBSD: cmdtab.c,v 1.12 1997/01/19 14:19:05 lukem Exp $"; #endif #endif /* not lint */ @@ -56,21 +55,25 @@ char beephelp[] = "beep when command completed"; char binaryhelp[] = "set binary transfer type"; char casehelp[] = "toggle mget upper/lower case id mapping"; char cdhelp[] = "change remote working directory"; -char cduphelp[] = "change remote working directory to parent directory"; +char cduphelp[] = "change remote working directory to parent directory"; char chmodhelp[] = "change file permissions of remote file"; -char connecthelp[] = "connect to remote ftp"; +char connecthelp[] = "connect to remote ftp server"; char crhelp[] = "toggle carriage return stripping on ascii gets"; -char deletehelp[] = "delete remote file"; char debughelp[] = "toggle/set debugging mode"; +char deletehelp[] = "delete remote file"; char dirhelp[] = "list contents of remote directory"; char disconhelp[] = "terminate ftp session"; -char domachelp[] = "execute macro"; +char domachelp[] = "execute macro"; +#ifndef SMALLFTP +char edithelp[] = "toggle command line editing"; +#endif /* !SMALLFTP */ char formhelp[] = "set file transfer format"; char globhelp[] = "toggle metacharacter expansion of local file names"; char hashhelp[] = "toggle printing `#' marks; specify number to set size"; char helphelp[] = "print local help information"; char idlehelp[] = "get (set) idle timer on remote side"; char lcdhelp[] = "change local working directory"; +char lpwdhelp[] = "print local working directory"; char lshelp[] = "list contents of remote directory"; char macdefhelp[] = "define a macro"; char mdeletehelp[] = "delete multiple files"; @@ -78,14 +81,18 @@ char mdirhelp[] = "list contents of multiple remote directories"; char mgethelp[] = "get multiple files"; char mkdirhelp[] = "make directory on the remote machine"; char mlshelp[] = "list contents of multiple remote directories"; -char modtimehelp[] = "show last modification time of remote file"; char modehelp[] = "set file transfer mode"; +char modtimehelp[] = "show last modification time of remote file"; char mputhelp[] = "send multiple files"; char newerhelp[] = "get file if remote file is newer than local file "; char nlisthelp[] = "nlist contents of remote directory"; char nmaphelp[] = "set templates for default file name mapping"; char ntranshelp[] = "set translation table for default file name mapping"; +char passivehelp[] = "enter passive transfer mode"; char porthelp[] = "toggle use of PORT cmd for each data connection"; +char preservehelp[] ="toggle preservation of modification time of " + "retreived files"; +char progresshelp[] ="toggle transfer progress meter"; char prompthelp[] = "force interactive prompting on multiple commands"; char proxyhelp[] = "issue command on alternate connection"; char pwdhelp[] = "print working directory on remote machine"; @@ -95,15 +102,16 @@ char receivehelp[] = "receive file"; char regethelp[] = "get file restarting at end of local file"; char remotehelp[] = "get help from remote server"; char renamehelp[] = "rename file"; +char resethelp[] = "clear queued command replies"; char restarthelp[]= "restart file transfer at bytecount"; char rmdirhelp[] = "remove directory on the remote machine"; char rmtstatushelp[]="show status of remote machine"; char runiquehelp[] = "toggle store unique for local files"; -char resethelp[] = "clear queued command replies"; char sendhelp[] = "send one file"; -char passivehelp[] = "enter passive transfer mode"; -char sitehelp[] = "send site specific command to remote server\n\t\tTry \"rhelp site\" or \"site help\" for more information"; char shellhelp[] = "escape to the shell"; +char sitehelp[] = "send site specific command to remote server\n" + "\t\tTry \"rhelp site\" or \"site help\" " + "for more information"; char sizecmdhelp[] = "show size of remote file"; char statushelp[] = "show current status"; char structhelp[] = "set file transfer structure"; @@ -116,79 +124,96 @@ char umaskhelp[] = "get (set) umask on remote side"; char userhelp[] = "send new user information"; char verbosehelp[] = "toggle verbose mode"; +#ifdef SMALLFTP +#define CMPL(x) +#define CMPL0 +#else /* !SMALLFTP */ +#define CMPL(x) __STRING(x), +#define CMPL0 "", +#endif /* !SMALLFTP */ + struct cmd cmdtab[] = { - { "!", shellhelp, 0, 0, 0, shell }, - { "$", domachelp, 1, 0, 0, domacro }, - { "account", accounthelp, 0, 1, 1, account}, - { "append", appendhelp, 1, 1, 1, put }, - { "ascii", asciihelp, 0, 1, 1, setascii }, - { "bell", beephelp, 0, 0, 0, setbell }, - { "binary", binaryhelp, 0, 1, 1, setbinary }, - { "bye", quithelp, 0, 0, 0, quit }, - { "case", casehelp, 0, 0, 1, setcase }, - { "cd", cdhelp, 0, 1, 1, cd }, - { "cdup", cduphelp, 0, 1, 1, cdup }, - { "chmod", chmodhelp, 0, 1, 1, do_chmod }, - { "close", disconhelp, 0, 1, 1, disconnect }, - { "cr", crhelp, 0, 0, 0, setcr }, - { "delete", deletehelp, 0, 1, 1, delete }, - { "debug", debughelp, 0, 0, 0, setdebug }, - { "dir", dirhelp, 1, 1, 1, ls }, - { "disconnect", disconhelp, 0, 1, 1, disconnect }, - { "form", formhelp, 0, 1, 1, setform }, - { "get", receivehelp, 1, 1, 1, get }, - { "glob", globhelp, 0, 0, 0, setglob }, - { "hash", hashhelp, 0, 0, 0, sethash }, - { "help", helphelp, 0, 0, 1, help }, - { "idle", idlehelp, 0, 1, 1, idle }, - { "image", binaryhelp, 0, 1, 1, setbinary }, - { "lcd", lcdhelp, 0, 0, 0, lcd }, - { "ls", lshelp, 1, 1, 1, ls }, - { "macdef", macdefhelp, 0, 0, 0, macdef }, - { "mdelete", mdeletehelp, 1, 1, 1, mdelete }, - { "mdir", mdirhelp, 1, 1, 1, mls }, - { "mget", mgethelp, 1, 1, 1, mget }, - { "mkdir", mkdirhelp, 0, 1, 1, makedir }, - { "mls", mlshelp, 1, 1, 1, mls }, - { "mode", modehelp, 0, 1, 1, setftmode }, - { "modtime", modtimehelp, 0, 1, 1, modtime }, - { "mput", mputhelp, 1, 1, 1, mput }, - { "newer", newerhelp, 1, 1, 1, newer }, - { "nmap", nmaphelp, 0, 0, 1, setnmap }, - { "nlist", nlisthelp, 1, 1, 1, ls }, - { "ntrans", ntranshelp, 0, 0, 1, setntrans }, - { "open", connecthelp, 0, 0, 1, setpeer }, - { "passive", passivehelp, 0, 0, 0, setpassive }, - { "prompt", prompthelp, 0, 0, 0, setprompt }, - { "proxy", proxyhelp, 0, 0, 1, doproxy }, - { "sendport", porthelp, 0, 0, 0, setport }, - { "put", sendhelp, 1, 1, 1, put }, - { "pwd", pwdhelp, 0, 1, 1, pwd }, - { "quit", quithelp, 0, 0, 0, quit }, - { "quote", quotehelp, 1, 1, 1, quote }, - { "recv", receivehelp, 1, 1, 1, get }, - { "reget", regethelp, 1, 1, 1, reget }, - { "rstatus", rmtstatushelp, 0, 1, 1, rmtstatus }, - { "rhelp", remotehelp, 0, 1, 1, rmthelp }, - { "rename", renamehelp, 0, 1, 1, renamefile }, - { "reset", resethelp, 0, 1, 1, reset }, - { "restart", restarthelp, 1, 1, 1, restart }, - { "rmdir", rmdirhelp, 0, 1, 1, removedir }, - { "runique", runiquehelp, 0, 0, 1, setrunique }, - { "send", sendhelp, 1, 1, 1, put }, - { "site", sitehelp, 0, 1, 1, site }, - { "size", sizecmdhelp, 1, 1, 1, sizecmd }, - { "status", statushelp, 0, 0, 1, status }, - { "struct", structhelp, 0, 1, 1, setstruct }, - { "system", systemhelp, 0, 1, 1, syst }, - { "sunique", suniquehelp, 0, 0, 1, setsunique }, - { "tenex", tenexhelp, 0, 1, 1, settenex }, - { "trace", tracehelp, 0, 0, 0, settrace }, - { "type", typehelp, 0, 1, 1, settype }, - { "user", userhelp, 0, 1, 1, user }, - { "umask", umaskhelp, 0, 1, 1, do_umask }, - { "verbose", verbosehelp, 0, 0, 0, setverbose }, - { "?", helphelp, 0, 0, 1, help }, + { "!", shellhelp, 0, 0, 0, CMPL0 shell }, + { "$", domachelp, 1, 0, 0, CMPL0 domacro }, + { "account", accounthelp, 0, 1, 1, CMPL0 account}, + { "append", appendhelp, 1, 1, 1, CMPL(lr) put }, + { "ascii", asciihelp, 0, 1, 1, CMPL0 setascii }, + { "bell", beephelp, 0, 0, 0, CMPL0 setbell }, + { "binary", binaryhelp, 0, 1, 1, CMPL0 setbinary }, + { "bye", quithelp, 0, 0, 0, CMPL0 quit }, + { "case", casehelp, 0, 0, 1, CMPL0 setcase }, + { "cd", cdhelp, 0, 1, 1, CMPL(r) cd }, + { "cdup", cduphelp, 0, 1, 1, CMPL0 cdup }, + { "chmod", chmodhelp, 0, 1, 1, CMPL(nr) do_chmod }, + { "close", disconhelp, 0, 1, 1, CMPL0 disconnect }, + { "cr", crhelp, 0, 0, 0, CMPL0 setcr }, + { "debug", debughelp, 0, 0, 0, CMPL0 setdebug }, + { "delete", deletehelp, 0, 1, 1, CMPL(r) delete }, + { "dir", dirhelp, 1, 1, 1, CMPL(rl) ls }, + { "disconnect", disconhelp, 0, 1, 1, CMPL0 disconnect }, +#ifndef SMALLFTP + { "edit", edithelp, 0, 0, 0, CMPL0 setedit }, +#endif /* !SMALLFTP */ + { "exit", quithelp, 0, 0, 0, CMPL0 quit }, + { "form", formhelp, 0, 1, 1, CMPL0 setform }, + { "ftp", connecthelp, 0, 0, 1, CMPL0 setpeer }, + { "get", receivehelp, 1, 1, 1, CMPL(rl) get }, + { "glob", globhelp, 0, 0, 0, CMPL0 setglob }, + { "hash", hashhelp, 0, 0, 0, CMPL0 sethash }, + { "help", helphelp, 0, 0, 1, CMPL(C) help }, + { "idle", idlehelp, 0, 1, 1, CMPL0 idle }, + { "image", binaryhelp, 0, 1, 1, CMPL0 setbinary }, + { "lcd", lcdhelp, 0, 0, 0, CMPL(l) lcd }, + { "lpwd", lpwdhelp, 0, 0, 0, CMPL0 lpwd }, + { "ls", lshelp, 1, 1, 1, CMPL(rl) ls }, + { "macdef", macdefhelp, 0, 0, 0, CMPL0 macdef }, + { "mdelete", mdeletehelp, 1, 1, 1, CMPL(R) mdelete }, + { "mdir", mdirhelp, 1, 1, 1, CMPL(R) mls }, + { "mget", mgethelp, 1, 1, 1, CMPL(R) mget }, + { "mkdir", mkdirhelp, 0, 1, 1, CMPL(r) makedir }, + { "mls", mlshelp, 1, 1, 1, CMPL(R) mls }, + { "mode", modehelp, 0, 1, 1, CMPL0 setftmode }, + { "modtime", modtimehelp, 0, 1, 1, CMPL(r) modtime }, + { "mput", mputhelp, 1, 1, 1, CMPL(L) mput }, + { "msend", mputhelp, 1, 1, 1, CMPL(L) mput }, + { "newer", newerhelp, 1, 1, 1, CMPL(r) newer }, + { "nlist", nlisthelp, 1, 1, 1, CMPL(rl) ls }, + { "nmap", nmaphelp, 0, 0, 1, CMPL0 setnmap }, + { "ntrans", ntranshelp, 0, 0, 1, CMPL0 setntrans }, + { "open", connecthelp, 0, 0, 1, CMPL0 setpeer }, + { "passive", passivehelp, 0, 0, 0, CMPL0 setpassive }, + { "preserve", preservehelp, 0, 0, 0, CMPL0 setpreserve }, + { "progress", progresshelp, 0, 0, 0, CMPL0 setprogress }, + { "prompt", prompthelp, 0, 0, 0, CMPL0 setprompt }, + { "proxy", proxyhelp, 0, 0, 1, CMPL(c) doproxy }, + { "put", sendhelp, 1, 1, 1, CMPL(lr) put }, + { "pwd", pwdhelp, 0, 1, 1, CMPL0 pwd }, + { "quit", quithelp, 0, 0, 0, CMPL0 quit }, + { "quote", quotehelp, 1, 1, 1, CMPL0 quote }, + { "recv", receivehelp, 1, 1, 1, CMPL(rl) get }, + { "reget", regethelp, 1, 1, 1, CMPL(rl) reget }, + { "rename", renamehelp, 0, 1, 1, CMPL(rr) renamefile }, + { "reset", resethelp, 0, 1, 1, CMPL0 reset }, + { "restart", restarthelp, 1, 1, 1, CMPL0 restart }, + { "rhelp", remotehelp, 0, 1, 1, CMPL0 rmthelp }, + { "rmdir", rmdirhelp, 0, 1, 1, CMPL(r) removedir }, + { "rstatus", rmtstatushelp, 0, 1, 1, CMPL(r) rmtstatus }, + { "runique", runiquehelp, 0, 0, 1, CMPL0 setrunique }, + { "send", sendhelp, 1, 1, 1, CMPL(lr) put }, + { "sendport", porthelp, 0, 0, 0, CMPL0 setport }, + { "site", sitehelp, 0, 1, 1, CMPL0 site }, + { "size", sizecmdhelp, 1, 1, 1, CMPL(r) sizecmd }, + { "status", statushelp, 0, 0, 1, CMPL0 status }, + { "struct", structhelp, 0, 1, 1, CMPL0 setstruct }, + { "sunique", suniquehelp, 0, 0, 1, CMPL0 setsunique }, + { "system", systemhelp, 0, 1, 1, CMPL0 syst }, + { "tenex", tenexhelp, 0, 1, 1, CMPL0 settenex }, + { "trace", tracehelp, 0, 0, 0, CMPL0 settrace }, + { "type", typehelp, 0, 1, 1, CMPL0 settype }, + { "umask", umaskhelp, 0, 1, 1, CMPL0 do_umask }, + { "user", userhelp, 0, 1, 1, CMPL0 user }, + { "verbose", verbosehelp, 0, 0, 0, CMPL0 setverbose }, + { "?", helphelp, 0, 0, 1, CMPL(C) help }, { 0 }, }; diff --git a/usr.bin/ftp/complete.c b/usr.bin/ftp/complete.c new file mode 100644 index 00000000000..8faea95a548 --- /dev/null +++ b/usr.bin/ftp/complete.c @@ -0,0 +1,364 @@ +/* $NetBSD: complete.c,v 1.2 1997/02/01 10:44:57 lukem Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Luke Mewburn. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef lint +static char rcsid[] = "$NetBSD: complete.c,v 1.2 1997/02/01 10:44:57 lukem Exp $"; +#endif /* not lint */ + +/* + * FTP user program - command and file completion routines + */ + +#include +#include +#include +#include +#include +#include + +#include "ftp_var.h" + +static int +comparstr(a, b) + const void *a, *b; +{ + return strcmp(*(char **)a, *(char **)b); +} + +/* + * Determine if complete is ambiguous. If unique, insert. + * If no choices, error. If unambiguous prefix, insert that. + * Otherwise, list choices. words is assumed to be filtered + * to only contain possible choices. + * Args: + * word word which started the match + * list list by default + * words stringlist containing possible matches + */ +static unsigned char +complete_ambiguous(word, list, words) + char *word; + int list; + StringList *words; +{ + char insertstr[MAXPATHLEN + 1]; + char *lastmatch; + int i, j, matchlen, wordlen; + + wordlen = strlen(word); + if (words->sl_cur == 0) + return CC_ERROR; /* no choices available */ + + if (words->sl_cur == 1) { /* only once choice available */ + strcpy(insertstr, words->sl_str[0]); + if (el_insertstr(el, insertstr + wordlen) == -1) + return CC_ERROR; + else + return CC_REFRESH; + } + + if (!list) { + matchlen = 0; + lastmatch = words->sl_str[0]; + matchlen = strlen(lastmatch); + for (i = 1 ; i < words->sl_cur ; i++) { + for (j = wordlen ; j < strlen(words->sl_str[i]); j++) + if (lastmatch[j] != words->sl_str[i][j]) + break; + if (j < matchlen) + matchlen = j; + } + if (matchlen > wordlen) { + strncpy(insertstr, lastmatch, matchlen); + insertstr[matchlen] = '\0'; + if (el_insertstr(el, insertstr + wordlen) == -1) + return CC_ERROR; + else + /* + * XXX: really want CC_REFRESH_BEEP + */ + return CC_REFRESH; + } + } + + putchar('\n'); + qsort(words->sl_str, words->sl_cur, sizeof(char *), comparstr); + list_vertical(words); + return CC_REDISPLAY; +} + +/* + * Complete a command + */ +static unsigned char +complete_command(word, list) + char *word; + int list; +{ + struct cmd *c; + StringList *words; + int wordlen; + unsigned char rv; + + words = sl_init(); + wordlen = strlen(word); + + for (c = cmdtab; c->c_name != NULL; c++) { + if (wordlen > strlen(c->c_name)) + continue; + if (strncmp(word, c->c_name, wordlen) == 0) + sl_add(words, c->c_name); + } + + rv = complete_ambiguous(word, list, words); + sl_free(words, 0); + return rv; +} + +/* + * Complete a local file + */ +static unsigned char +complete_local(word, list) + char *word; + int list; +{ + StringList *words; + char dir[MAXPATHLEN + 1]; + char *file; + DIR *dd; + struct dirent *dp; + unsigned char rv; + + if ((file = strrchr(word, '/')) == NULL) { + strcpy(dir, "."); + file = word; + } else { + if (file == word) + strcpy(dir, "/"); + else { + strncpy(dir, word, file - word); + dir[file - word] = '\0'; + } + ++file; + } + + if ((dd = opendir(dir)) == NULL) + return CC_ERROR; + + words = sl_init(); + + for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) { + if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) + continue; + if (strlen(file) > dp->d_namlen) + continue; + if (strncmp(file, dp->d_name, strlen(file)) == 0) { + char *tcp; + + tcp = strdup(dp->d_name); + if (tcp == NULL) + errx(1, "Can't allocate memory for local dir"); + sl_add(words, tcp); + } + } + closedir(dd); + + rv = complete_ambiguous(file, list, words); + sl_free(words, 1); + return rv; +} + +/* + * Complete a remote file + */ +static unsigned char +complete_remote(word, list) + char *word; + int list; +{ + static StringList *dirlist; + static char lastdir[MAXPATHLEN + 1]; + static int ftpdslashbug; + StringList *words; + char dir[MAXPATHLEN + 1]; + char *file, *cp; + int i, offset; + unsigned char rv; + + char *dummyargv[] = { "complete", dir, NULL }; + + offset = 0; + if ((file = strrchr(word, '/')) == NULL) { + strcpy(dir, "."); + file = word; + } else { + if (file == word) + strcpy(dir, "/"); + else { + offset = file - word; + strncpy(dir, word, offset); + dir[offset] = '\0'; + offset++; + } + file++; + } + + if (dirchange || strcmp(dir, lastdir) != 0) { /* dir not cached */ + if (dirlist != NULL) + sl_free(dirlist, 1); + dirlist = sl_init(); + + ftpdslashbug = 0; + mflag = 1; + while ((cp = remglob(dummyargv, 0)) != NULL) { + char *tcp; + + if (!mflag) + continue; + if (*cp == '\0') { + mflag = 0; + continue; + } + /* + * Work around ftpd(1) bug, which puts a // instead + * of / in front of each filename returned by "NLST /". + * Without this, remote completes of / look ugly. + */ + if (dir[0] == '/' && dir[1] == '\0' && + cp[0] == '/' && cp[1] == '/') { + cp++; + ftpdslashbug = 1; + } + tcp = strdup(cp); + if (tcp == NULL) + errx(1, "Can't allocate memory for remote dir"); + sl_add(dirlist, tcp); + } + strcpy(lastdir, dir); + dirchange = 0; + } + + words = sl_init(); + for (i = 0; i < dirlist->sl_cur; i++) { + cp = dirlist->sl_str[i]; + if (strlen(word) > strlen(cp)) + continue; + if (strncmp(word, cp, strlen(word)) == 0) + sl_add(words, cp + offset + ftpdslashbug); + } + rv = complete_ambiguous(file, list, words); + sl_free(words, 0); + return rv; +} + +/* + * Generic complete routine + */ +unsigned char +complete(el, ch) + EditLine *el; + int ch; +{ + static char word[FTPBUFLEN]; + static int lastc_argc, lastc_argo; + + struct cmd *c; + const LineInfo *lf; + int len, celems, dolist; + + lf = el_line(el); + len = lf->lastchar - lf->buffer; + if (len >= sizeof(line)) + return CC_ERROR; + strncpy(line, lf->buffer, len); + line[len] = '\0'; + cursor_pos = line + (lf->cursor - lf->buffer); + lastc_argc = cursor_argc; /* remember last cursor pos */ + lastc_argo = cursor_argo; + makeargv(); /* build argc/argv of current line */ + + if (cursor_argo >= sizeof(word)) + return CC_ERROR; + + dolist = 0; + /* if cursor and word is same, list alternatives */ + if (lastc_argc == cursor_argc && lastc_argo == cursor_argo + && strncmp(word, margv[cursor_argc], cursor_argo) == 0) + dolist = 1; + else + strncpy(word, margv[cursor_argc], cursor_argo); + word[cursor_argo] = '\0'; + + if (cursor_argc == 0) + return complete_command(word, dolist); + + c = getcmd(margv[0]); + if (c == (struct cmd *)-1 || c == 0) + return CC_ERROR; + celems = strlen(c->c_complete); + + /* check for 'continuation' completes (which are uppercase) */ + if ((cursor_argc > celems) && (celems > 0) + && isupper(c->c_complete[celems-1])) + cursor_argc = celems; + + if (cursor_argc > celems) + return CC_ERROR; + + switch (c->c_complete[cursor_argc - 1]) { + case 'l': /* local complete */ + case 'L': + return complete_local(word, dolist); + case 'r': /* remote complete */ + case 'R': + if (!connected) { + printf("\nMust be connected to complete\n"); + return CC_REDISPLAY; + } + return complete_remote(word, dolist); + case 'c': /* command complete */ + case 'C': + return complete_command(word, dolist); + case 'n': /* no complete */ + default: + return CC_ERROR; + } + + return CC_ERROR; +} diff --git a/usr.bin/ftp/domacro.c b/usr.bin/ftp/domacro.c index abd717ddd02..9e389003594 100644 --- a/usr.bin/ftp/domacro.c +++ b/usr.bin/ftp/domacro.c @@ -1,5 +1,4 @@ -/* $OpenBSD: domacro.c,v 1.2 1996/06/26 05:33:34 deraadt Exp $ */ -/* $NetBSD: domacro.c,v 1.5 1995/09/08 01:06:14 tls Exp $ */ +/* $NetBSD: domacro.c,v 1.8 1997/01/19 14:19:08 lukem Exp $ */ /* * Copyright (c) 1985, 1993, 1994 @@ -38,14 +37,14 @@ #if 0 static char sccsid[] = "@(#)domacro.c 8.3 (Berkeley) 4/2/94"; #else -static char rcsid[] = "$OpenBSD: domacro.c,v 1.2 1996/06/26 05:33:34 deraadt Exp $"; +static char rcsid[] = "$NetBSD: domacro.c,v 1.8 1997/01/19 14:19:08 lukem Exp $"; #endif #endif /* not lint */ #include #include #include -#include +#include #include "ftp_var.h" @@ -134,7 +133,7 @@ TOP: } else { if (verbose) { - printf("%s\n",line); + printf("%s\n", line); } (*c->c_handler)(margc, margv); if (bell && c->c_bell) { diff --git a/usr.bin/ftp/extern.h b/usr.bin/ftp/extern.h index 5f369bccbd0..abe9d662104 100644 --- a/usr.bin/ftp/extern.h +++ b/usr.bin/ftp/extern.h @@ -1,5 +1,4 @@ -/* $OpenBSD: extern.h,v 1.5 1997/01/25 21:42:30 deraadt Exp $ */ -/* $NetBSD: extern.h,v 1.4 1995/09/08 01:06:19 tls Exp $ */ +/* $NetBSD: extern.h,v 1.11 1997/02/01 10:44:58 lukem Exp $ */ /*- * Copyright (c) 1994 The Regents of the University of California. @@ -36,25 +35,29 @@ * @(#)extern.h 8.3 (Berkeley) 10/9/94 */ -struct timeval; struct fd_set; void abort_remote __P((FILE *)); void abortpt __P(()); void abortrecv __P(()); void abortsend __P(()); +void aborthttp __P(()); void account __P((int, char **)); -int another __P((int *, char ***, char *)); +void alarmtimer __P((int)); +int another __P((int *, char ***, const char *)); +int auto_fetch __P((int, char **)); void blkfree __P((char **)); void cd __P((int, char **)); -int mcd __P((int, char **)); void cdup __P((int, char **)); void changetype __P((int, int)); void cmdabort __P(()); void cmdscanner __P((int)); int command __P(()); -int confirm __P((char *, char *)); -FILE *dataconn __P((char *)); +#ifndef SMALLFTP +unsigned char complete __P((EditLine *, int)); +#endif /* !SMALLFTP */ +int confirm __P((const char *, const char *)); +FILE *dataconn __P((const char *)); void delete __P((int, char **)); void disconnect __P((int, char **)); void do_chmod __P((int, char **)); @@ -64,25 +67,26 @@ char *domap __P((char *)); void doproxy __P((int, char **)); char *dotrans __P((char *)); int empty __P((struct fd_set *, int)); -void fatal __P((char *)); void get __P((int, char **)); -struct cmd *getcmd __P((char *)); -int getit __P((int, char **, int, char *)); +struct cmd *getcmd __P((const char *)); +int getit __P((int, char **, int, const char *)); int getreply __P((int)); int globulize __P((char **)); -char *gunique __P((char *)); +char *gunique __P((const char *)); void help __P((int, char **)); -char *hookup __P((char *, int)); +char *hookup __P((const char *, int)); void idle __P((int, char **)); int initconn __P((void)); void intr __P(()); +void list_vertical __P((StringList *)); void lcd __P((int, char **)); -int login __P((char *)); +int login __P((const char *)); void lostpeer __P(()); +void lpwd __P((int, char **)); void ls __P((int, char **)); void mabort __P((int)); void macdef __P((int, char **)); -void makeargv __P((void)); +void makeargv __P(()); void makedir __P((int, char **)); void mdelete __P((int, char **)); void mget __P((int, char **)); @@ -91,33 +95,40 @@ void modtime __P((int, char **)); void mput __P((int, char **)); char *onoff __P((int)); void newer __P((int, char **)); +void progressmeter __P((int)); +char *prompt __P(()); void proxabort __P(()); -void proxtrans __P((char *, char *, char *)); +void proxtrans __P((const char *, const char *, const char *)); void psabort __P(()); +void psummary __P((int)); void pswitch __P((int)); -void ptransfer __P((char *, long, struct timeval *, struct timeval *)); +void ptransfer __P((int)); void put __P((int, char **)); void pwd __P((int, char **)); void quit __P((int, char **)); void quote __P((int, char **)); -void quote1 __P((char *, int, char **)); -void recvrequest __P((char *, char *, char *, char *, int)); +void quote1 __P((const char *, int, char **)); +void recvrequest __P((const char *, const char *, const char *, + const char *, int)); void reget __P((int, char **)); char *remglob __P((char **, int)); +off_t remotesize __P((const char *, int)); +time_t remotemodtime __P((const char *, int)); void removedir __P((int, char **)); void renamefile __P((int, char **)); void reset __P((int, char **)); void restart __P((int, char **)); void rmthelp __P((int, char **)); void rmtstatus __P((int, char **)); -int ruserpass __P((char *, char **, char **, char **)); -void sendrequest __P((char *, char *, char *, int)); +int ruserpass __P((const char *, char **, char **, char **)); +void sendrequest __P((const char *, const char *, const char *, int)); void setascii __P((int, char **)); void setbell __P((int, char **)); void setbinary __P((int, char **)); void setcase __P((int, char **)); void setcr __P((int, char **)); void setdebug __P((int, char **)); +void setedit __P((int, char **)); void setform __P((int, char **)); void setftmode __P((int, char **)); void setglob __P((int, char **)); @@ -127,28 +138,32 @@ void setntrans __P((int, char **)); void setpassive __P((int, char **)); void setpeer __P((int, char **)); void setport __P((int, char **)); +void setpreserve __P((int, char **)); +void setprogress __P((int, char **)); void setprompt __P((int, char **)); void setrunique __P((int, char **)); void setstruct __P((int, char **)); void setsunique __P((int, char **)); void settenex __P((int, char **)); void settrace __P((int, char **)); +void setttywidth __P((int)); void settype __P((int, char **)); void setverbose __P((int, char **)); void shell __P((int, char **)); void site __P((int, char **)); void sizecmd __P((int, char **)); -char *slurpstring __P((void)); +char *slurpstring __P(()); void status __P((int, char **)); void syst __P((int, char **)); -void tvsub __P((struct timeval *, struct timeval *, struct timeval *)); +int togglevar __P((int, char **, int *, const char *)); +void usage __P(()); void user __P((int, char **)); -int http_fetch __P((char *)); + extern jmp_buf abortprox; extern int abrtflag; extern struct cmd cmdtab[]; -extern FILE *cout; +extern FILE *cout; extern int data; extern char *home; extern jmp_buf jabort; @@ -156,3 +171,6 @@ extern int proxy; extern char reply_string[]; extern off_t restart_point; extern int NCMDS; + +extern char *__progname; /* from crt0.o */ + diff --git a/usr.bin/ftp/fetch.c b/usr.bin/ftp/fetch.c new file mode 100644 index 00000000000..439f73c9aa7 --- /dev/null +++ b/usr.bin/ftp/fetch.c @@ -0,0 +1,508 @@ +/* $NetBSD: fetch.c,v 1.2 1997/02/01 10:45:00 lukem Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason Thorpe and Luke Mewburn. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef lint +static char rcsid[] = "$NetBSD: fetch.c,v 1.2 1997/02/01 10:45:00 lukem Exp $"; +#endif /* not lint */ + +/* + * FTP User Program -- Command line file retrieval + */ + +#include +#include +#include + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ftp_var.h" + +#define FTP_URL "ftp://" /* ftp URL prefix */ +#define HTTP_URL "http://" /* http URL prefix */ +#define HTTP_PROXY "http_proxy" /* env var with http proxy location */ + + +#define EMPTYSTRING(x) ((x) == NULL || (*(x) == '\0')) + +jmp_buf httpabort; + +/* + * Retrieve an http:// URL, via a proxy if necessary. + * Modifies the string argument given. + * Returns -1 on failure, 0 on success + */ +int +http_get(line) + char *line; +{ + struct sockaddr_in sin; + int i, out, port, s; + size_t buflen, len; + char c, *cp, *cp2, *savefile, *portnum, *path, buf[4096]; + char *proxyenv, *proxy, *host; + sig_t oldintr; + off_t hashbytes; + + s = -1; + proxy = NULL; + + host = line + sizeof(HTTP_URL) - 1; + path = strchr(host, '/'); /* find path */ + if (EMPTYSTRING(path)) + goto cleanup_http_get; + *path++ = '\0'; + if (EMPTYSTRING(path)) + goto cleanup_http_get; + + savefile = strrchr(path, '/'); /* find savefile */ + if (savefile != NULL) + savefile++; + else + savefile = path; + if (EMPTYSTRING(savefile)) + goto cleanup_http_get; + + proxyenv = getenv(HTTP_PROXY); + if (proxyenv != NULL) { /* use proxy */ + if (strncmp(proxyenv, HTTP_URL, sizeof(HTTP_URL) - 1) != 0) { + warnx("Malformed proxy url: %s", proxyenv); + goto cleanup_http_get; + } + proxy = strdup(proxyenv); + if (proxy == NULL) + errx(1, "Can't allocate memory for proxy url."); + host = proxy + sizeof(HTTP_URL) - 1; + if (EMPTYSTRING(host)) + goto cleanup_http_get; + *--path = '/'; /* add / back to real path */ + path = strchr(host, '/'); /* remove trailing / on host */ + if (! EMPTYSTRING(path)) + *path++ = '\0'; + path = line; + } + + portnum = strchr(host, ':'); /* find portnum */ + if (portnum != NULL) + *portnum++ = '\0'; + + if (debug) + printf("host %s, port %s, path %s, save as %s.\n", + host, portnum, path, savefile); + + memset(&sin, 0, sizeof(sin)); + sin.sin_family = AF_INET; + + if (isdigit(host[0])) { + if (inet_aton(host, &sin.sin_addr) == 0) { + warnx("invalid IP address: %s", host); + goto cleanup_http_get; + } + } else { + struct hostent *hp; + + hp = gethostbyname(host); + if (hp == NULL) { + warnx("%s: %s", host, hstrerror(h_errno)); + goto cleanup_http_get; + } + if (hp->h_addrtype != AF_INET) { + warnx("%s: not an Internet address?", host); + goto cleanup_http_get; + } + memcpy(&sin.sin_addr, hp->h_addr, hp->h_length); + } + + if (! EMPTYSTRING(portnum)) { + port = atoi(portnum); + if (port < 1 || (port & 0xffff) != port) { + warnx("invalid port: %s", portnum); + goto cleanup_http_get; + } + port = htons(port); + } else + port = httpport; + sin.sin_port = port; + + s = socket(AF_INET, SOCK_STREAM, 0); + if (s == -1) { + warnx("Can't create socket"); + goto cleanup_http_get; + } + + if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) { + warn("Can't connect to %s", host); + goto cleanup_http_get; + } + + /* + * Construct and send the request. We're expecting a return + * status of "200". Proxy requests don't want leading /. + */ + if (!proxy) + printf("Requesting %s:%d/%s\n", line, ntohs(port), path); + else + printf("Requesting %s (via %s)\n", line, proxyenv); + snprintf(buf, sizeof(buf), "GET %s%s HTTP/1.0\n\n", + proxy ? "" : "/", path); + buflen = strlen(buf); + if (write(s, buf, buflen) < buflen) { + warn("write"); + goto cleanup_http_get; + } + memset(buf, 0, sizeof(buf)); + for (i = 0, buflen = sizeof(buf), cp = buf; i < buflen; cp++, i++) { + if (read(s, cp, 1) != 1) + goto improper; + if (*cp == '\n') + break; + } + buf[buflen - 1] = '\0'; /* sanity */ + cp = strchr(buf, ' '); + if (cp == NULL) + goto improper; + else + cp++; + if (strncmp(cp, "200", 3)) { + warnx("Error retrieving file: %s", cp); + goto cleanup_http_get; + } + + /* + * Read the rest of the header. + */ + memset(buf, 0, sizeof(buf)); + c = '\0'; + for (i = 0, buflen = sizeof(buf), cp = buf; i < buflen; cp++, i++) { + if (read(s, cp, 1) != 1) + goto improper; + if (*cp == '\n' && c == '\n') + break; + c = *cp; + } + buf[buflen - 1] = '\0'; /* sanity */ + + /* + * Look for the "Content-length: " header. + */ +#define CONTENTLEN "Content-Length: " + for (cp = buf; *cp != '\0'; cp++) { + if (tolower(*cp) == 'c' && + strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0) + break; + } + if (*cp == '\0') + goto improper; + cp += sizeof(CONTENTLEN) - 1; + cp2 = strchr(cp, '\n'); + if (cp2 == NULL) + goto improper; + else + *cp2 = '\0'; + filesize = atoi(cp); + if (filesize < 1) + goto improper; + + /* Open the output file. */ + out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666); + if (out < 0) { + warn("Can't open %s", savefile); + goto cleanup_http_get; + } + + /* Trap signals */ + oldintr = NULL; + if (setjmp(httpabort)) { + if (oldintr) + (void) signal(SIGINT, oldintr); + goto cleanup_http_get; + } + oldintr = signal(SIGINT, aborthttp); + + bytes = 0; + hashbytes = mark; + progressmeter(-1); + + /* Finally, suck down the file. */ + i = 0; + while ((len = read(s, buf, sizeof(buf))) > 0) { + bytes += len; + for (cp = buf; len > 0; len -= i, cp += i) { + if ((i = write(out, cp, len)) == -1) { + warn("Writing %s", savefile); + goto cleanup_http_get; + } + else if (i == 0) + break; + } + if (hash && !progress) { + while (bytes >= hashbytes) { + (void) putchar('#'); + hashbytes += mark; + } + (void) fflush(stdout); + } + } + if (hash && !progress && bytes > 0) { + if (bytes < mark) + (void) putchar('#'); + (void) putchar('\n'); + (void) fflush(stdout); + } + if (len != 0) { + warn("Reading from socket"); + goto cleanup_http_get; + } + progressmeter(1); + if (verbose) + printf("Successfully retrieved file.\n"); + (void) signal(SIGINT, oldintr); + + close(s); + close(out); + if (proxy) + free(proxy); + return(0); + +improper: + warnx("improper response from %s", host); +cleanup_http_get: + if (s != -1) + close(s); + if (proxy) + free(proxy); + return(-1); +} + +/* + * Abort a http retrieval + */ +void +aborthttp() +{ + + alarmtimer(0); + printf("\nhttp fetch aborted\n"); + (void) fflush(stdout); + longjmp(httpabort, 1); +} + +/* + * Retrieve multiple files from the command line, transferring + * files of the form "host:path", "ftp://host/path" using the + * ftp protocol, and files of the form "http://host/path" using + * the http protocol. + * If path has a trailing "/", then return(-1); + * the path will be cd-ed into and the connection remains open, + * and the function will return -1 (to indicate the connection + * is alive). + * If an error occurs the return value will be the offset+1 in + * argv[] of the file that caused a problem (i.e, argv[x] + * returns x+1) + * Otherwise, 0 is returned if all files retrieved successfully. + */ +int +auto_fetch(argc, argv) + int argc; + char *argv[]; +{ + static char lasthost[MAXHOSTNAMELEN]; + char *xargv[5]; + char *cp, *line, *host, *dir, *file, *portnum; + int rval, xargc, argpos; + + argpos = 0; + + if (setjmp(toplevel)) { + if (connected) + disconnect(0, NULL); + return(argpos + 1); + } + (void) signal(SIGINT, intr); + (void) signal(SIGPIPE, lostpeer); + + /* + * Loop through as long as there's files to fetch. + */ + for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) { + if (strchr(argv[argpos], ':') == NULL) + break; + host = dir = file = portnum = NULL; + + /* + * We muck with the string, so we make a copy. + */ + line = strdup(argv[argpos]); + if (line == NULL) + errx(1, "Can't allocate memory for auto-fetch."); + + /* + * Try HTTP URL-style arguments first. + */ + if (strncmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0) { + if (http_get(line) == -1) + rval = argpos + 1; + continue; + } + + /* + * Try FTP URL-style arguments next, then host:file. + */ + host = line; + if (strncmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) { + host += sizeof(FTP_URL) - 1; + cp = strchr(host, '/'); + + /* Look for a port number after the host name. */ + portnum = strchr(host, ':'); + if (portnum != NULL) + *portnum++ = '\0'; + } else /* classic style `host:file' */ + cp = strchr(host, ':'); + if (EMPTYSTRING(host)) { + rval = argpos + 1; + continue; + } + + /* + * If cp is NULL, the file wasn't specified + * (URL looked something like ftp://host) + */ + if (cp != NULL) + *cp++ = '\0'; + + /* + * Extract the file and (if present) directory name. + */ + dir = cp; + if (! EMPTYSTRING(dir)) { + cp = strrchr(cp, '/'); + if (cp != NULL) { + *cp++ = '\0'; + file = cp; + } else { + file = dir; + dir = NULL; + } + } + if (debug) + printf("host '%s', dir '%s', file '%s'\n", + host, dir, file); + + /* + * Set up the connection if we don't have one. + */ + if (strcmp(host, lasthost) != 0) { + strcpy(lasthost, host); + if (connected) + disconnect(0, NULL); + xargv[0] = __progname; + xargv[1] = host; + xargv[2] = NULL; + xargc = 2; + if (portnum != NULL) { + xargv[2] = portnum; + xargv[3] = NULL; + xargc = 3; + } + setpeer(xargc, xargv); + if (connected == 0) { + warnx("Can't connect to host `%s'", host); + rval = argpos + 1; + continue; + } + + /* Always use binary transfers. */ + setbinary(0, NULL); + } + else /* already have connection, cd back to '/' */ + { + xargv[0] = "cd"; + xargv[1] = "/"; + xargv[2] = NULL; + cd(2, xargv); + if (! dirchange) { + rval = argpos + 1; + continue; + } + } + + /* Change directories, if necessary. */ + if (! EMPTYSTRING(dir)) { + xargv[0] = "cd"; + xargv[1] = dir; + xargv[2] = NULL; + cd(2, xargv); + if (! dirchange) { + rval = argpos + 1; + continue; + } + } + + if (EMPTYSTRING(file)) { + rval = -1; + continue; + } + + if (!verbose) + printf("Retrieving %s/%s\n", dir ? dir : "", file); + + /* Fetch the file. */ + xargv[0] = "get"; + xargv[1] = file; + xargv[2] = NULL; + get(2, xargv); + + if ((code / 100) != COMPLETE) /* XXX: is this valid? */ + rval = argpos + 1; + } + if (connected && rval != -1) + disconnect(0, NULL); + return (rval); +} diff --git a/usr.bin/ftp/ftp.1 b/usr.bin/ftp/ftp.1 index 956f111be6e..74c46246c65 100644 --- a/usr.bin/ftp/ftp.1 +++ b/usr.bin/ftp/ftp.1 @@ -1,5 +1,4 @@ -.\" $OpenBSD: ftp.1,v 1.5 1996/11/09 19:56:23 kstailey Exp $ -.\" $NetBSD: ftp.1,v 1.11 1995/09/08 01:06:24 tls Exp $ +.\" $NetBSD: ftp.1,v 1.17 1997/02/01 10:45:01 lukem Exp $ .\" .\" Copyright (c) 1985, 1989, 1990, 1993 .\" The Regents of the University of California. All rights reserved. @@ -34,7 +33,7 @@ .\" .\" @(#)ftp.1 8.3 (Berkeley) 10/9/94 .\" -.Dd October 9, 1994 +.Dd January 20, 1997 .Dt FTP 1 .Os BSD 4.2 .Sh NAME @@ -43,84 +42,95 @@ .Tn ARPANET file transfer program .Sh SYNOPSIS -.Nm ftp -.Op Fl t -.Op Fl v +.Nm +.Op Fl a .Op Fl d +.Op Fl g .Op Fl i .Op Fl n -.Op Fl g -.Op Fl r Ar seconds -.Op Ar host +.Op Fl p +.Op Fl P Ar port +.Op Fl t +.Op Fl v +.Op Fl V +.Op Ar host Op Ar port .Nm ftp -.Ar http://host/path/file.html +ftp://\fIhost\fR[:\fIport\fR]/\fIfile\fR[/] .Nm ftp -.Ar ftp://host/path/file.tar.gz +http://\fIhost\fR[:\fIport\fR]/\fIfile\fR .Nm ftp -.Ar host:/path/file.tar.gz +\fIhost\fR:\fIfile\fR[/] .Sh DESCRIPTION -.Nm Ftp +.Nm is the user interface to the .Tn ARPANET standard File Transfer Protocol. The program allows a user to transfer files to and from a remote network site. .Pp -The latter three usage formats will fetch a file using either the -HTTP or FTP protocols into the current directory. -This is ideal for scripts. -.Pp Options may be specified at the command line, or to the command interpreter. -.Bl -tag -width flag -.It Fl t -Enables packet tracing. -.It Fl v -Verbose option forces -.Nm ftp -to show all responses from the remote server, as well -as report on data transfer statistics. +.Bl -tag -width "port " +.It Fl a +Causes +.Nm +to bypass normal login procedure, and use an anonymous login instead. +.It Fl d +Enables debugging. +.It Fl g +Disables file name globbing. +.It Fl i +Turns off interactive prompting during +multiple file transfers. .It Fl n Restrains -.Nm ftp -from attempting \*(Lqauto-login\*(Rq upon initial connection. +.Nm +from attempting +.Dq auto-login +upon initial connection. If auto-login is enabled, -.Nm ftp +.Nm will check the .Pa .netrc (see below) file in the user's home directory for an entry describing an account on the remote machine. If no entry exists, -.Nm ftp +.Nm will prompt for the remote machine login name (default is the user identity on the local machine), and, if necessary, prompt for a password and an account with which to login. -.It Fl i -Turns off interactive prompting during -multiple file transfers. -.It Fl d -Enables debugging. -.It Fl g -Disables file name globbing. -.It Fl r Ar number -Retry to connect if failed, pausing for -.Ar number -of seconds. +.It Fl p +Enable passive mode operation for use behind connection filtering firewalls. +.It Fl P Ar port +Sets the port number to +.Ar port . +.It Fl t +Enables packet tracing. +.It Fl v +Enable verbose mode. +This is the default if input is from a terminal. +Forces +.Nm +to show all responses from the remote server, as well +as report on data transfer statistics. +.It Fl V +Disable verbose mode, overriding the default of enabled when input +is from a terminal. .El .Pp The client host with which -.Nm ftp +.Nm is to communicate may be specified on the command line. If this is done, -.Nm ftp +.Nm will immediately attempt to establish a connection to an .Tn FTP server on that host; otherwise, -.Nm ftp +.Nm will enter its command interpreter and await instructions from the user. When -.Nm ftp +.Nm is awaiting commands from the user the prompt .Ql ftp> is provided to the user. @@ -178,7 +188,7 @@ Terminate the .Tn FTP session with the remote server and exit -.Nm ftp . +.Nm ftp . An end of file will also terminate the session and exit. .It Ic case Toggle remote computer file name case mapping during @@ -192,7 +202,7 @@ to lower case. .It Ic \&cd Ar remote-directory Change the working directory on the remote machine to -.Ar remote-directory . +.Ar remote-directory . .It Ic cdup Change the remote machine working directory to the parent of the current remote machine working directory. @@ -200,8 +210,8 @@ current remote machine working directory. Change the permission modes of the file .Ar file-name on the remote -sytem to -.Ar mode . +system to +.Ar mode . .It Ic close Terminate the .Tn FTP @@ -237,42 +247,54 @@ If an optional .Ar debug-value is specified it is used to set the debugging level. When debugging is on, -.Nm ftp +.Nm prints each command sent to the remote machine, preceded by the string .Ql \-\-> -.It Xo -.Ic dir -.Op Ar remote-directory -.Op Ar local-file -.Xc -Print a listing of the directory contents in the -directory, -.Ar remote-directory , -and, optionally, placing the output in -.Ar local-file . +.It Ic dir Op Ar remote-directory Op Ar local-file +Print a listing of the contents of a +directory on the remote machine. +The listing includes any system-dependent information that the server +chooses to include; for example, most +.Ux +systems will produce +output from the command +.Ql ls \-l . +(See also +.Ic ls . ) +If +.Ar remote-directory +is left unspecified, the current working directory is used. If interactive prompting is on, -.Nm ftp +.Nm will prompt the user to verify that the last argument is indeed the target local file for receiving .Ic dir output. -If no directory is specified, the current working -directory on the remote machine is used. -If no local -file is specified, or +If no local file is specified, or if .Ar local-file is -.Fl , -output comes to the terminal. +.Sq Fl , +the output is sent to the terminal. .It Ic disconnect A synonym for -.Ar close . +.Ic close . +.It Ic edit +Toggle command line editing, and context sensitive command and file +completion. +This is automatically enabled if input is from a terminal, and +disabled otherwise. +.It Ic exit +A synonym for +.Ic bye . +.It Ic ftp Ar host Op Ar port +A synonym for +.Ic open . .It Ic form Ar format Set the file transfer .Ic form to -.Ar format . +.Ar format . The default format is \*(Lqfile\*(Rq. .It Ic get Ar remote-file Op Ar local-file Retrieve the @@ -299,7 +321,7 @@ Toggle filename expansion for .Ic mdelete , .Ic mget and -.Ic mput . +.Ic mput . If globbing is turned off with .Ic glob , the file name arguments @@ -333,12 +355,14 @@ archive of the subtree (in binary mode). Toggle hash-sign (``#'') printing for each data block transferred. The size of a data block defaults to 1024 bytes. -This can be changed by specifying a size in bytes. +This can be changed by specifying +.Ar size +in bytes. .It Ic help Op Ar command Print an informative message about the meaning of -.Ar command . +.Ar command . If no argument is given, -.Nm ftp +.Nm prints a list of the known commands. .It Ic idle Op Ar seconds Set the inactivity timer on the remote server to @@ -353,34 +377,24 @@ If no .Ar directory is specified, the user's home directory is used. -.It Xo -.Ic \&ls -.Op Ar remote-directory -.Op Ar local-file -.Xc -Print a listing of the contents of a +.It Ic lpwd +Print the working directory on the local machine. +.It Ic \&ls Op Ar remote-directory Op Ar local-file +Print a list of the files in a directory on the remote machine. -The listing includes any system-dependent information that the server -chooses to include; for example, most -.Ux -systems will produce -output from the command -.Ql ls \-l . -(See also -.Ic nlist . ) If .Ar remote-directory is left unspecified, the current working directory is used. If interactive prompting is on, -.Nm ftp +.Nm will prompt the user to verify that the last argument is indeed the target local file for receiving -.Ic \&ls +.Ic ls output. If no local file is specified, or if .Ar local-file is -.Sq Fl , +.Fl , the output is sent to the terminal. .It Ic macdef Ar macro-name Define a macro. @@ -413,7 +427,7 @@ Like .Ic dir , except multiple remote files may be specified. If interactive prompting is on, -.Nm ftp +.Nm will prompt the user to verify that the last argument is indeed the target local file for receiving .Ic mdir @@ -443,13 +457,13 @@ new local directories can be created with Make a directory on the remote machine. .It Ic mls Ar remote-files local-file Like -.Ic nlist , +.Ic ls , except multiple remote files may be specified, and the .Ar local-file must be specified. If interactive prompting is on, -.Nm ftp +.Nm will prompt the user to verify that the last argument is indeed the target local file for receiving .Ic mls @@ -458,7 +472,7 @@ output. Set the file transfer .Ic mode to -.Ar mode-name . +.Ar mode-name . The default mode is \*(Lqstream\*(Rq mode. .It Ic modtime Ar file-name Show the last modification time of the file on the remote machine. @@ -475,35 +489,20 @@ Resulting file names will then be processed according to and .Ic nmap settings. +.It Ic msend Ar local-files +A synonym for +.Ic mput . .It Ic newer Ar file-name Get the file only if the modification time of the remote file is more recent that the file on the current system. If the file does not exist on the current system, the remote file is considered -.Ic newer . +.Ic newer . Otherwise, this command is identical to -.Ar get . -.It Xo -.Ic nlist -.Op Ar remote-directory -.Op Ar local-file -.Xc -Print a list of the files in a -directory on the remote machine. -If -.Ar remote-directory -is left unspecified, the current working directory is used. -If interactive prompting is on, -.Nm ftp -will prompt the user to verify that the last argument is indeed the -target local file for receiving -.Ic nlist -output. -If no local file is specified, or if -.Ar local-file -is -.Fl , -the output is sent to the terminal. +.Ar get . +.It Ic nlist Op Ar remote-directory Op Ar local-file +A synonym for +.Ic ls . .It Ic nmap Op Ar inpattern outpattern Set or unset the filename mapping mechanism. If no arguments are specified, the filename mapping mechanism is unset. @@ -524,7 +523,7 @@ with different file naming conventions or practices. The mapping follows the pattern set by .Ar inpattern and -.Ar outpattern . +.Ar outpattern . .Op Ar Inpattern is a template for incoming filenames (which may have already been processed according to the @@ -534,7 +533,7 @@ and settings). Variable templating is accomplished by including the sequences `$1', `$2', ..., `$9' in -.Ar inpattern . +.Ar inpattern . Use `\\' to prevent this special treatment of the `$' character. All other characters are treated literally, and are used to determine the .Ic nmap @@ -598,7 +597,7 @@ with different file naming conventions or practices. Characters in a filename matching a character in .Ar inchars are replaced with the corresponding character in -.Ar outchars . +.Ar outchars . If the character's position in .Ar inchars is longer than the length of @@ -611,14 +610,14 @@ Establish a connection to the specified server. An optional port number may be supplied, in which case, -.Nm ftp +.Nm will attempt to contact an .Tn FTP server at that port. If the .Ic auto-login option is on (default), -.Nm ftp +.Nm will also attempt to automatically log the user in to the .Tn FTP @@ -638,12 +637,16 @@ port and the client connects to it. When using the more traditional .Dv PORT command, the client listens on a port and sends that address to the remote server, who connects back to it. Passive mode is useful when using -.Nm ftp +.Nm through a gateway router or host that controls the directionality of traffic. (Note that though ftp servers are required to support the .Dv PASV command by RFC 1123, some do not.) +.It Ic preserve +Toggle preservation of modification times on retrieved files. +.It Ic progress +Toggle display of transfer progress bar. .It Ic prompt Toggle interactive prompting. Interactive prompting @@ -656,6 +659,29 @@ or will transfer all files, and any .Ic mdelete will delete all files. +.Pp +When prompting is on, the following commands are available at a prompt: +.Bl -tag -width 2n -offset indent +.It Ic n +Do not transfer the file. +.It Ic a +Answer +.Sq yes +to the current file, and automatically answer +.Sq yes +to any remaining files for the current command. +.It Ic p +Answer +.Sq yes +to the current file, and turn off prompt mode +(as is +.Dq prompt off +had been given). +.El +.Pp +Any other reponse will answer +.Sq yes +to the current file. .It Ic proxy Ar ftp-command Execute an ftp command on a secondary control connection. This command allows simultaneous connection to two remote ftp @@ -704,19 +730,20 @@ current settings for .Ic format , .Ic mode , and -.Ic structure . +.Ic structure . .It Ic pwd Print the name of the current working directory on the remote machine. .It Ic quit A synonym for -.Ic bye . +.Ic bye . .It Ic quote Ar arg1 arg2 ... The arguments specified are sent, verbatim, to the remote .Tn FTP server. .It Ic recv Ar remote-file Op Ar local-file -A synonym for get. +A synonym for +.Ic get . .It Ic reget Ar remote-file Op Ar local-file Reget acts like get, except that if .Ar local-file @@ -739,22 +766,18 @@ server. If a .Ar command-name is specified it is supplied to the server as well. -.It Ic remotestatus Op Ar file-name +.It Ic rstatus Op Ar file-name With no arguments, show status of remote machine. If .Ar file-name is specified, show status of .Ar file-name on remote machine. -.It Xo -.Ic rename -.Op Ar from -.Op Ar to -.Xc +.It Ic rename Op Ar from Op Ar to Rename the file .Ar from on the remote machine, to the file -.Ar to . +.Ar to . .It Ic reset Clear reply queue. This command re-synchronizes command/reply sequencing with the remote @@ -768,7 +791,7 @@ or .Ic put at the indicated -.Ar marker . +.Ar marker . On .Ux systems, marker is usually a byte @@ -794,13 +817,14 @@ will not affect local files generated from a shell command (see below). The default value is off. .It Ic send Ar local-file Op Ar remote-file -A synonym for put. +A synonym for +.Ic put . .It Ic sendport Toggle the use of .Dv PORT commands. By default, -.Nm ftp +.Nm will attempt to use a .Dv PORT command when establishing @@ -812,7 +836,7 @@ when performing multiple file transfers. If the .Dv PORT command fails, -.Nm ftp +.Nm will use the default data port. When the use of .Dv PORT @@ -837,7 +861,7 @@ Return size of on remote machine. .It Ic status Show the current status of -.Nm ftp . +.Nm ftp . .It Ic struct Op Ar struct-name Set the file transfer .Ar structure @@ -865,21 +889,20 @@ Toggle packet tracing. Set the file transfer .Ic type to -.Ar type-name . +.Ar type-name . If no type is specified, the current type is printed. The default type is network .Tn ASCII . .It Ic umask Op Ar newmask Set the default umask on the remote server to -.Ar newmask . +.Ar newmask . If .Ar newmask is omitted, the current umask is printed. .It Xo .Ic user Ar user-name -.Op Ar password -.Op Ar account +.Op Ar password Op Ar account .Xc Identify yourself to the remote .Tn FTP @@ -887,7 +910,7 @@ server. If the .Ar password is not specified and the server requires it, -.Nm ftp +.Nm will prompt the user for it (after disabling local echo). If an .Ar account @@ -902,7 +925,7 @@ be relayed to the remote server after the login sequence is completed if the remote server did not require it for logging in. Unless -.Nm ftp +.Nm is invoked with \*(Lqauto-login\*(Rq disabled, this process is done automatically on initial connection to the @@ -920,11 +943,63 @@ regarding the efficiency of the transfer are reported. By default, verbose is on. .It Ic ? Op Ar command -A synonym for help. +A synonym for +.Ic help . .El .Pp Command arguments which have embedded spaces may be quoted with quote `"' marks. +.Pp +Commands which toggle settings can take an explicit +.Ic on +or +.Ic off +argument to force the setting appropriately. +.Pp +If +.Nm +receives a +.Dv SIGINFO +(see the +.Dq status +argument of +.Xr stty 1 ) +signal whilst a transfer is in progress, the current transfer rate +statistics will be written to the standard error output, in the +same format as the standard completion message. +.Sh AUTO-FETCHING FILES +In addition to standard commands, this version of +.Nm +supports an auto-fetch feature. +To enable auto-fetch, simply pass the list of hostnames/files +on the command line. +.Pp +The following formats are valid syntax for an auto-fetch element: +.Bl -tag -width "http://host[:port]/file" +.It host:/file +.Dq Classic +ftp format +.It ftp://host[:port]/file +FTP URL, using the ftp protocol. +.It http://host[:port]/file +HTTP URL, using the http protocol. +If +.Ev http_proxy +is defined, it is used as a URL to an HTTP proxy server. +.El +.Pp +If a classic format or a ftp URL format has a trailing +.Sq / , +then +.Nm +will connect to the site and +.Ic cd +to the directory given as the path, and leave the user in interactive +mode ready for further input. +.Pp +If successive auto-fetch ftp elements refer to the same host, then +the connection is maintained between transfers, reducing overhead on +connection creation and deletion. .Sh ABORTING A FILE TRANSFER To abort a file transfer, use the terminal interrupt key (usually Ctrl-C). @@ -944,18 +1019,18 @@ prompt will not appear until the remote server has completed sending the requested file. .Pp The terminal interrupt key sequence will be ignored when -.Nm ftp +.Nm has completed any local processing and is awaiting a reply from the remote server. A long delay in this mode may result from the ABOR processing described above, or from unexpected behavior by the remote server, including violations of the ftp protocol. If the delay results from unexpected remote server behavior, the local -.Nm ftp +.Nm program must be killed by hand. .Sh FILE NAMING CONVENTIONS Files specified as arguments to -.Nm ftp +.Nm commands are processed according to the following rules. .Bl -enum .It @@ -971,7 +1046,7 @@ If the first character of the file name is .Sq \&| , the remainder of the argument is interpreted as a shell command. -.Nm Ftp +.Nm then forks a shell, using .Xr popen 3 with the argument supplied, and reads (writes) from the stdout @@ -980,7 +1055,7 @@ If the shell command includes spaces, the argument must be quoted; e.g. \*(Lq" ls -lt"\*(Rq. A particularly -useful example of this mechanism is: \*(Lqdir more\*(Rq. +useful example of this mechanism is: \*(Lqdir \&|more\*(Rq. .It Failing the above checks, if ``globbing'' is enabled, local file names are expanded @@ -990,7 +1065,7 @@ c.f. the .Ic glob command. If the -.Nm ftp +.Nm command expects a single local file (.e.g. .Ic put ) , only the first filename generated by the "globbing" operation is used. @@ -1035,19 +1110,19 @@ may be one of \*(Lqascii\*(Rq, \*(Lqimage\*(Rq (binary), and .Tn PDP Ns -20's mostly). -.Nm Ftp +.Nm supports the ascii and image types of file transfer, plus local byte size 8 for .Ic tenex mode transfers. .Pp -.Nm Ftp +.Nm supports only the default values for the remaining file transfer parameters: .Ic mode , .Ic form , and -.Ic struct . +.Ic struct . .Sh THE .netrc FILE The .Pa .netrc @@ -1065,7 +1140,7 @@ The auto-login process searches the file for a .Ic machine token that matches the remote machine specified on the -.Nm ftp +.Nm command line or as an .Ic open command argument. @@ -1117,7 +1192,7 @@ Note that if this token is present in the file for any user other than .Ar anonymous , -.Nm ftp +.Nm will abort the auto-login process if the .Pa .netrc is readable by @@ -1132,7 +1207,7 @@ command if it does not. .It Ic macdef Ar name Define a macro. This token functions like the -.Nm ftp +.Nm .Ic macdef command functions. A macro is defined with the specified name; its contents begin with the @@ -1145,24 +1220,62 @@ If a macro named is defined, it is automatically executed as the last step in the auto-login process. .El +.Sh COMMAND LINE EDITING +.Nm +supports interactive command line editing, via the +.Xr editline 3 +library. +It is enabled with the +.Ic edit +command, and is enabled by default. +Previous lines can be recalled and edited with the arrow keys, +and other GNU Emacs-style editing keys may be used as well. +.Pp +The +.Xr editline 3 +library is configured with a +.Pa .editrc +file - refer to +.Xr editrc 5 +for more information. +.Pp +An extra key binding is available to +.Nm +to provide context sensitive command and filename completion +(including remote file completion). +To use this, bind a key to the +.Xr editline 3 +command +.Ic ftp-complete . +By default, this is bound to the TAB key. .Sh ENVIRONMENT -.Nm Ftp +.Nm utilizes the following environment variables. -.Bl -tag -width Fl +.Bl -tag -width "http_proxy" .It Ev HOME For default location of a .Pa .netrc file, if one exists. .It Ev SHELL For default shell. +.It Ev http_proxy +URL of HTTP proxy to use when making HTTP requests. .El .Sh SEE ALSO +.Xr editrc 5 , .Xr ftpd 8 .Sh HISTORY The -.Nm ftp +.Nm command appeared in .Bx 4.2 . +.Pp +Various features such as command line editing, context sensitive +command and file completion, dynamic progress bar, automatic +fetching of files, ftp and http URLs, and modification time +preservation were implemented in +.Nx 1.2b +by Luke Mewburn, with assistance from Jason Thorpe. .Sh BUGS Correct execution of many commands depends upon proper behavior by the remote server. diff --git a/usr.bin/ftp/ftp.c b/usr.bin/ftp/ftp.c index 802965c535d..98cc4107c28 100644 --- a/usr.bin/ftp/ftp.c +++ b/usr.bin/ftp/ftp.c @@ -1,5 +1,4 @@ -/* $OpenBSD: ftp.c,v 1.9 1997/01/08 13:19:11 niklas Exp $ */ -/* $NetBSD: ftp.c,v 1.13 1995/09/16 22:32:59 pk Exp $ */ +/* $NetBSD: ftp.c,v 1.22 1997/02/01 10:45:03 lukem Exp $ */ /* * Copyright (c) 1985, 1989, 1993, 1994 @@ -38,16 +37,13 @@ #if 0 static char sccsid[] = "@(#)ftp.c 8.6 (Berkeley) 10/27/94"; #else -static char rcsid[] = "$OpenBSD: ftp.c,v 1.9 1997/01/08 13:19:11 niklas Exp $"; +static char rcsid[] = "$NetBSD: ftp.c,v 1.22 1997/02/01 10:45:03 lukem Exp $"; #endif #endif /* not lint */ -#include +#include #include -#include #include -#include -#include #include #include @@ -59,10 +55,8 @@ static char rcsid[] = "$OpenBSD: ftp.c,v 1.9 1997/01/08 13:19:11 niklas Exp $"; #include #include #include -#include #include #include -#include #include #include #include @@ -71,8 +65,6 @@ static char rcsid[] = "$OpenBSD: ftp.c,v 1.9 1997/01/08 13:19:11 niklas Exp $"; #include "ftp_var.h" -extern int h_errno; - struct sockaddr_in hisctladdr; struct sockaddr_in data_addr; int data = -1; @@ -88,12 +80,12 @@ FILE *cin, *cout; char * hookup(host, port) - char *host; + const char *host; int port; { struct hostent *hp = 0; int s, len, tos; - static char hostnamebuf[80]; + static char hostnamebuf[MAXHOSTNAMELEN]; memset((char *)&hisctladdr, 0, sizeof (hisctladdr)); if (inet_aton(host, &hisctladdr.sin_addr) != 0) { @@ -120,7 +112,8 @@ hookup(host, port) return (0); } hisctladdr.sin_port = port; - while (connect(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0) { + while (connect(s, (struct sockaddr *)&hisctladdr, + sizeof (hisctladdr)) < 0) { if (hp && hp->h_addr_list[1]) { int oerrno = errno; char *ia; @@ -197,27 +190,50 @@ bad: int login(host) - char *host; + const char *host; { char tmp[80]; char *user, *pass, *acct; + char anonpass[MAXLOGNAME + MAXHOSTNAMELEN + 2]; /* "user@hostname\0" */ + char hostname[MAXHOSTNAMELEN + 1]; int n, aflag = 0; - char anonpass[64 + 1]; - user = pass = acct = 0; + user = pass = acct = NULL; if (ruserpass(host, &user, &pass, &acct) < 0) { code = -1; return (0); } - if (anonftp) { + + /* + * Set up arguments for an anonymous FTP session, if necessary. + */ + if ((user == NULL || pass == NULL) && anonftp) { + memset(anonpass, 0, sizeof(anonpass)); + memset(hostname, 0, sizeof(hostname)); + + /* + * Set up anonymous login password. + */ user = getlogin(); - strncpy(anonpass, user, sizeof anonpass - 1); - anonpass[sizeof anonpass - 1] = '\0'; - strncat(anonpass, "@anon.openbsd.org", /* XXX ugly */ - sizeof anonpass - strlen(anonpass) - 1); + gethostname(hostname, MAXHOSTNAMELEN); +#ifndef DONT_CHEAT_ANONPASS + /* + * Every anonymous FTP server I've encountered + * will accept the string "username@", and will + * append the hostname itself. We do this by default + * since many servers are picky about not having + * a FQDN in the anonymous password. - thorpej@netbsd.org + */ + snprintf(anonpass, sizeof(anonpass) - 1, "%s@", + user); +#else + snprintf(anonpass, sizeof(anonpass) - 1, "%s@%s", + user, hp->h_name); +#endif pass = anonpass; user = "anonymous"; } + while (user == NULL) { char *myname = getlogin(); @@ -272,11 +288,12 @@ void cmdabort() { + alarmtimer(0); printf("\n"); (void) fflush(stdout); abrtflag++; if (ptflag) - longjmp(ptabort,1); + longjmp(ptabort, 1); } /*VARARGS*/ @@ -296,7 +313,9 @@ va_dcl fmt = va_arg(ap, char *); if (strncmp("PASS ", fmt, 5) == 0) printf("PASS XXXX"); - else + else if (strncmp("ACCT ", fmt, 5) == 0) + printf("ACCT XXXX"); + else vfprintf(stdout, fmt, ap); va_end(ap); printf("\n"); @@ -322,13 +341,14 @@ va_dcl return (r); } -char reply_string[BUFSIZ]; /* last line of previous reply */ +char reply_string[BUFSIZ]; /* first line of previous reply */ int getreply(expecteof) int expecteof; { - int c, n; + char current_line[BUFSIZ]; /* last line of previous reply */ + int c, n, line; int dig; int originalcode = 0, continuation = 0; sig_t oldintr; @@ -336,9 +356,9 @@ getreply(expecteof) char *cp, *pt = pasv; oldintr = signal(SIGINT, cmdabort); - for (;;) { + for (line = 0 ;; line++) { dig = n = code = 0; - cp = reply_string; + cp = current_line; while ((c = getc(cin)) != '\n') { if (c == IAC) { /* handle telnet commands */ switch (c = getc(cin)) { @@ -362,26 +382,25 @@ getreply(expecteof) dig++; if (c == EOF) { if (expecteof) { - (void) signal(SIGINT,oldintr); + (void) signal(SIGINT, oldintr); code = 221; return (0); } lostpeer(); if (verbose) { - printf("421 Service not available, remote server has closed connection\n"); + printf("421 Service not available, " + "remote server has closed " + "connection\n"); (void) fflush(stdout); } code = 421; return (4); } - if (n == 0) - n = c; - if (c != '\r' && (n < '5' || !retry_connect) && - (verbose > 0 || - (verbose > -1 && n == '5' && dig > 4))) { + if (c != '\r' && (verbose > 0 || + (verbose > -1 && n == '5' && dig > 4))) { if (proxflag && - (dig == 1 || dig == 5 && verbose == 0)) - printf("%s:",hostname); + (dig == 1 || (dig == 5 && verbose == 0))) + printf("%s:", hostname); (void) putchar(c); } if (dig < 4 && isdigit(c)) @@ -403,14 +422,24 @@ getreply(expecteof) code = 0; continuation++; } - if (cp < &reply_string[sizeof(reply_string) - 1]) + if (n == 0) + n = c; + if (cp < ¤t_line[sizeof(current_line) - 1]) *cp++ = c; } - if ((verbose > 0 || (verbose > -1 && n == '5')) && - (n < '5' || !retry_connect)) { + if (verbose > 0 || (verbose > -1 && n == '5')) { (void) putchar(c); (void) fflush (stdout); } + if (line == 0) { + size_t len = cp - current_line; + + if (len > sizeof(reply_string)) + len = sizeof(reply_string); + + (void) strncpy(reply_string, current_line, len); + reply_string[len] = '\0'; + } if (continuation && code != originalcode) { if (originalcode == 0) originalcode = code; @@ -419,7 +448,7 @@ getreply(expecteof) *cp = '\0'; if (n != '1') cpend = 0; - (void) signal(SIGINT,oldintr); + (void) signal(SIGINT, oldintr); if (code == 421 || originalcode == 421) lostpeer(); if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN) @@ -446,6 +475,7 @@ void abortsend() { + alarmtimer(0); mflag = 0; abrtflag = 0; printf("\nsend aborted\nwaiting for remote to finish abort\n"); @@ -455,18 +485,21 @@ abortsend() void sendrequest(cmd, local, remote, printnames) - char *cmd, *local, *remote; + const char *cmd, *local, *remote; int printnames; { struct stat st; - struct timeval start, stop; int c, d; - FILE *fin, *dout = 0, *popen(); + FILE *fin, *dout = 0; int (*closefunc) __P((FILE *)); - sig_t oldintr, oldintp; - long bytes = 0, hashbytes = mark; + sig_t oldinti, oldintr, oldintp; + off_t hashbytes; char *lmode, buf[BUFSIZ], *bufp; + hashbytes = mark; + direction = "sent"; + bytes = 0; + filesize = -1; if (verbose && printnames) { if (local && *local != '-') printf("local: %s ", local); @@ -482,6 +515,7 @@ sendrequest(cmd, local, remote, printnames) closefunc = NULL; oldintr = NULL; oldintp = NULL; + oldinti = NULL; lmode = "w"; if (setjmp(sendabort)) { while (cpend) { @@ -492,22 +526,26 @@ sendrequest(cmd, local, remote, printnames) data = -1; } if (oldintr) - (void) signal(SIGINT,oldintr); + (void) signal(SIGINT, oldintr); if (oldintp) - (void) signal(SIGPIPE,oldintp); + (void) signal(SIGPIPE, oldintp); + if (oldinti) + (void) signal(SIGINFO, oldinti); code = -1; return; } oldintr = signal(SIGINT, abortsend); + oldinti = signal(SIGINFO, psummary); if (strcmp(local, "-") == 0) fin = stdin; else if (*local == '|') { - oldintp = signal(SIGPIPE,SIG_IGN); + oldintp = signal(SIGPIPE, SIG_IGN); fin = popen(local + 1, "r"); if (fin == NULL) { warn("%s", local + 1); (void) signal(SIGINT, oldintr); (void) signal(SIGPIPE, oldintp); + (void) signal(SIGINFO, oldinti); code = -1; return; } @@ -517,6 +555,7 @@ sendrequest(cmd, local, remote, printnames) if (fin == NULL) { warn("local: %s", local); (void) signal(SIGINT, oldintr); + (void) signal(SIGINFO, oldinti); code = -1; return; } @@ -525,13 +564,16 @@ sendrequest(cmd, local, remote, printnames) (st.st_mode&S_IFMT) != S_IFREG) { fprintf(stdout, "%s: not a plain file.\n", local); (void) signal(SIGINT, oldintr); + (void) signal(SIGINFO, oldinti); fclose(fin); code = -1; return; } + filesize = st.st_size; } if (initconn()) { (void) signal(SIGINT, oldintr); + (void) signal(SIGINFO, oldinti); if (oldintp) (void) signal(SIGPIPE, oldintp); code = -1; @@ -546,6 +588,7 @@ sendrequest(cmd, local, remote, printnames) (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) { int rc; + rc = -1; switch (curtype) { case TYPE_A: rc = fseek(fin, (long) restart_point, SEEK_SET); @@ -575,6 +618,7 @@ sendrequest(cmd, local, remote, printnames) if (remote) { if (command("%s %s", cmd, remote) != PRELIM) { (void) signal(SIGINT, oldintr); + (void) signal(SIGINFO, oldinti); if (oldintp) (void) signal(SIGPIPE, oldintp); if (closefunc != NULL) @@ -584,6 +628,7 @@ sendrequest(cmd, local, remote, printnames) } else if (command("%s", cmd) != PRELIM) { (void) signal(SIGINT, oldintr); + (void) signal(SIGINFO, oldinti); if (oldintp) (void) signal(SIGPIPE, oldintp); if (closefunc != NULL) @@ -593,7 +638,7 @@ sendrequest(cmd, local, remote, printnames) dout = dataconn(lmode); if (dout == NULL) goto abort; - (void) gettimeofday(&start, (struct timezone *)0); + progressmeter(-1); oldintp = signal(SIGPIPE, SIG_IGN); switch (curtype) { @@ -605,7 +650,7 @@ sendrequest(cmd, local, remote, printnames) for (bufp = buf; c > 0; c -= d, bufp += d) if ((d = write(fileno(dout), bufp, c)) <= 0) break; - if (hash) { + if (hash && (!progress || filesize < 0) ) { while (bytes >= hashbytes) { (void) putchar('#'); hashbytes += mark; @@ -613,7 +658,7 @@ sendrequest(cmd, local, remote, printnames) (void) fflush(stdout); } } - if (hash && bytes > 0) { + if (hash && (!progress || filesize < 0) && bytes > 0) { if (bytes < mark) (void) putchar('#'); (void) putchar('\n'); @@ -622,7 +667,7 @@ sendrequest(cmd, local, remote, printnames) if (c < 0) warn("local: %s", local); if (d < 0) { - if (errno != EPIPE) + if (errno != EPIPE) warn("netout"); bytes = -1; } @@ -631,7 +676,8 @@ sendrequest(cmd, local, remote, printnames) case TYPE_A: while ((c = getc(fin)) != EOF) { if (c == '\n') { - while (hash && (bytes >= hashbytes)) { + while (hash && (!progress || filesize < 0) && + (bytes >= hashbytes)) { (void) putchar('#'); (void) fflush(stdout); hashbytes += mark; @@ -643,12 +689,14 @@ sendrequest(cmd, local, remote, printnames) } (void) putc(c, dout); bytes++; - /* if (c == '\r') { */ - /* (void) putc('\0', dout); // this violates rfc */ - /* bytes++; */ - /* } */ +#if 0 /* this violates RFC */ + if (c == '\r') { + (void)putc('\0', dout); + bytes++; + } +#endif } - if (hash) { + if (hash && (!progress || filesize < 0)) { if (bytes < hashbytes) (void) putchar('#'); (void) putchar('\n'); @@ -663,19 +711,21 @@ sendrequest(cmd, local, remote, printnames) } break; } + progressmeter(1); if (closefunc != NULL) (*closefunc)(fin); (void) fclose(dout); - (void) gettimeofday(&stop, (struct timezone *)0); (void) getreply(0); (void) signal(SIGINT, oldintr); + (void) signal(SIGINFO, oldinti); if (oldintp) (void) signal(SIGPIPE, oldintp); if (bytes > 0) - ptransfer("sent", bytes, &start, &stop); + ptransfer(0); return; abort: (void) signal(SIGINT, oldintr); + (void) signal(SIGINFO, oldinti); if (oldintp) (void) signal(SIGPIPE, oldintp); if (!cpend) { @@ -692,9 +742,8 @@ abort: code = -1; if (closefunc != NULL && fin != NULL) (*closefunc)(fin); - (void) gettimeofday(&stop, (struct timezone *)0); if (bytes > 0) - ptransfer("sent", bytes, &start, &stop); + ptransfer(0); } jmp_buf recvabort; @@ -703,6 +752,7 @@ void abortrecv() { + alarmtimer(0); mflag = 0; abrtflag = 0; printf("\nreceive aborted\nwaiting for remote to finish abort\n"); @@ -712,19 +762,24 @@ abortrecv() void recvrequest(cmd, local, remote, lmode, printnames) - char *cmd, *local, *remote, *lmode; + const char *cmd, *local, *remote, *lmode; int printnames; { FILE *fout, *din = 0; int (*closefunc) __P((FILE *)); - sig_t oldintr, oldintp; + sig_t oldinti, oldintr, oldintp; int c, d, is_retr, tcrflag, bare_lfs = 0; static int bufsize; static char *buf; - long bytes = 0, hashbytes = mark; - struct timeval start, stop; + off_t hashbytes; struct stat st; + time_t mtime; + struct timeval tval[2]; + hashbytes = mark; + direction = "received"; + bytes = 0; + filesize = -1; is_retr = strcmp(cmd, "RETR") == 0; if (is_retr && verbose && printnames) { if (local && *local != '-') @@ -750,10 +805,13 @@ recvrequest(cmd, local, remote, lmode, printnames) } if (oldintr) (void) signal(SIGINT, oldintr); + if (oldinti) + (void) signal(SIGINFO, oldinti); code = -1; return; } oldintr = signal(SIGINT, abortrecv); + oldinti = signal(SIGINFO, psummary); if (strcmp(local, "-") && *local != '|') { if (access(local, 2) < 0) { char *dir = strrchr(local, '/'); @@ -761,17 +819,19 @@ recvrequest(cmd, local, remote, lmode, printnames) if (errno != ENOENT && errno != EACCES) { warn("local: %s", local); (void) signal(SIGINT, oldintr); + (void) signal(SIGINFO, oldinti); code = -1; return; } if (dir != NULL) *dir = 0; - d = access(dir ? local : ".", 2); + d = access(dir == local ? "/" : dir ? local : ".", 2); if (dir != NULL) *dir = '/'; if (d < 0) { warn("local: %s", local); (void) signal(SIGINT, oldintr); + (void) signal(SIGINFO, oldinti); code = -1; return; } @@ -779,19 +839,21 @@ recvrequest(cmd, local, remote, lmode, printnames) chmod(local, 0600) < 0) { warn("local: %s", local); (void) signal(SIGINT, oldintr); - (void) signal(SIGINT, oldintr); + (void) signal(SIGINFO, oldinti); code = -1; return; } if (runique && errno == EACCES && (local = gunique(local)) == NULL) { (void) signal(SIGINT, oldintr); + (void) signal(SIGINFO, oldinti); code = -1; return; } } else if (runique && (local = gunique(local)) == NULL) { (void) signal(SIGINT, oldintr); + (void) signal(SIGINFO, oldinti); code = -1; return; } @@ -799,10 +861,14 @@ recvrequest(cmd, local, remote, lmode, printnames) if (!is_retr) { if (curtype != TYPE_A) changetype(TYPE_A, 0); - } else if (curtype != type) - changetype(type, 0); + } else { + if (curtype != type) + changetype(type, 0); + filesize = remotesize(remote, 0); + } if (initconn()) { (void) signal(SIGINT, oldintr); + (void) signal(SIGINFO, oldinti); code = -1; return; } @@ -814,11 +880,13 @@ recvrequest(cmd, local, remote, lmode, printnames) if (remote) { if (command("%s %s", cmd, remote) != PRELIM) { (void) signal(SIGINT, oldintr); + (void) signal(SIGINFO, oldinti); return; } } else { if (command("%s", cmd) != PRELIM) { (void) signal(SIGINT, oldintr); + (void) signal(SIGINFO, oldinti); return; } } @@ -856,7 +924,7 @@ recvrequest(cmd, local, remote, lmode, printnames) } bufsize = st.st_blksize; } - (void) gettimeofday(&start, (struct timezone *)0); + progressmeter(-1); switch (curtype) { case TYPE_I: @@ -873,7 +941,7 @@ recvrequest(cmd, local, remote, lmode, printnames) if ((d = write(fileno(fout), buf, c)) != c) break; bytes += c; - if (hash) { + if (hash && (!progress || filesize < 0)) { while (bytes >= hashbytes) { (void) putchar('#'); hashbytes += mark; @@ -881,7 +949,7 @@ recvrequest(cmd, local, remote, lmode, printnames) (void) fflush(stdout); } } - if (hash && bytes > 0) { + if (hash && (!progress || filesize < 0) && bytes > 0) { if (bytes < mark) (void) putchar('#'); (void) putchar('\n'); @@ -925,7 +993,8 @@ done: if (c == '\n') bare_lfs++; while (c == '\r') { - while (hash && (bytes >= hashbytes)) { + while (hash && (!progress || filesize < 0) && + (bytes >= hashbytes)) { (void) putchar('#'); (void) fflush(stdout); hashbytes += mark; @@ -949,10 +1018,11 @@ done: } break2: if (bare_lfs) { - printf("WARNING! %d bare linefeeds received in ASCII mode\n", bare_lfs); + printf("WARNING! %d bare linefeeds received in ASCII " + "mode\n", bare_lfs); printf("File may not have transferred correctly.\n"); } - if (hash) { + if (hash && (!progress || filesize < 0)) { if (bytes < hashbytes) (void) putchar('#'); (void) putchar('\n'); @@ -967,27 +1037,45 @@ break2: warn("local: %s", local); break; } + progressmeter(1); if (closefunc != NULL) (*closefunc)(fout); (void) signal(SIGINT, oldintr); + (void) signal(SIGINFO, oldinti); if (oldintp) (void) signal(SIGPIPE, oldintp); (void) fclose(din); - (void) gettimeofday(&stop, (struct timezone *)0); (void) getreply(0); - if (bytes > 0 && is_retr) - ptransfer("received", bytes, &start, &stop); + if (bytes >= 0 && is_retr) { + if (bytes > 0) + ptransfer(0); + if (preserve && (closefunc == fclose)) { + mtime = remotemodtime(remote, 0); + if (mtime != -1) { + (void) gettimeofday(&tval[0], + (struct timezone *)0); + tval[1].tv_sec = mtime; + tval[1].tv_usec = 0; + if (utimes(local, tval) == -1) { + printf("Can't change modification time " + "on %s to %s", local, + asctime(localtime(&mtime))); + } + } + } + } return; abort: -/* abort using RFC959 recommended IP,SYNC sequence */ +/* abort using RFC959 recommended IP,SYNC sequence */ if (oldintp) - (void) signal(SIGPIPE, oldintr); + (void) signal(SIGPIPE, oldintp); (void) signal(SIGINT, SIG_IGN); if (!cpend) { code = -1; (void) signal(SIGINT, oldintr); + (void) signal(SIGINFO, oldinti); return; } @@ -1001,10 +1089,10 @@ abort: (*closefunc)(fout); if (din) (void) fclose(din); - (void) gettimeofday(&stop, (struct timezone *)0); if (bytes > 0) - ptransfer("received", bytes, &start, &stop); + ptransfer(0); (void) signal(SIGINT, oldintr); + (void) signal(SIGINFO, oldinti); } /* @@ -1022,13 +1110,13 @@ initconn() if (passivemode) { data = socket(AF_INET, SOCK_STREAM, 0); if (data < 0) { - perror("ftp: socket"); - return(1); + warn("socket"); + return (1); } if ((options & SO_DEBUG) && setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof (on)) < 0) - perror("ftp: setsockopt (ignored)"); + warn("setsockopt (ignored)"); if (command("PASV") != COMPLETE) { printf("Passive mode refused.\n"); goto bad; @@ -1042,14 +1130,14 @@ initconn() * From that we'll prepare a sockaddr_in. */ - if (sscanf(pasv,"%d,%d,%d,%d,%d,%d", + if (sscanf(pasv, "%d,%d,%d,%d,%d,%d", &a0, &a1, &a2, &a3, &p0, &p1) != 6) { printf("Passive mode address scan failure. " "Shouldn't happen!\n"); goto bad; } - bzero(&data_addr, sizeof(data_addr)); + memset(&data_addr, 0, sizeof(data_addr)); data_addr.sin_family = AF_INET; a = (char *)&data_addr.sin_addr.s_addr; a[0] = a0 & 0xff; @@ -1062,22 +1150,22 @@ initconn() if (connect(data, (struct sockaddr *)&data_addr, sizeof(data_addr)) < 0) { - perror("ftp: connect"); + warn("connect"); goto bad; } #ifdef IP_TOS on = IPTOS_THROUGHPUT; if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0) - perror("ftp: setsockopt TOS (ignored)"); + warn("setsockopt TOS (ignored)"); #endif - return(0); + return (0); } noport: data_addr = myctladdr; if (sendport) - data_addr.sin_port = 0; /* let system pick one */ + data_addr.sin_port = 0; /* let system pick one */ if (data != -1) (void) close(data); data = socket(AF_INET, SOCK_STREAM, 0); @@ -1088,7 +1176,8 @@ noport: return (1); } if (!sendport) - if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)) < 0) { + if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on, + sizeof (on)) < 0) { warn("setsockopt (reuse address)"); goto bad; } @@ -1097,7 +1186,8 @@ noport: goto bad; } if (options & SO_DEBUG && - setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof (on)) < 0) + setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on, + sizeof (on)) < 0) warn("setsockopt (ignored)"); len = sizeof (data_addr); if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) { @@ -1138,7 +1228,7 @@ bad: FILE * dataconn(lmode) - char *lmode; + const char *lmode; { struct sockaddr_in from; int s, fromlen = sizeof (from), tos; @@ -1163,29 +1253,19 @@ dataconn(lmode) } void -ptransfer(direction, bytes, t0, t1) - char *direction; - long bytes; - struct timeval *t0, *t1; +psummary(notused) + int notused; { - struct timeval td; - float s; - long bs; - - if (verbose) { - timersub(t1, t0, &td); - s = td.tv_sec + (td.tv_usec / 1000000.); -#define nz(x) ((x) == 0 ? 1 : (x)) - bs = bytes / nz(s); - printf("%ld bytes %s in %.3g seconds (%ld bytes/s)\n", - bytes, direction, s, bs); - } + + if (bytes > 0) + ptransfer(1); } void psabort() { + alarmtimer(0); abrtflag++; } @@ -1286,6 +1366,7 @@ void abortpt() { + alarmtimer(0); printf("\n"); (void) fflush(stdout); ptabflg++; @@ -1296,7 +1377,7 @@ abortpt() void proxtrans(cmd, local, remote) - char *cmd, *local, *remote; + const char *cmd, *local, *remote; { sig_t oldintr; int secndflag = 0, prox_type, nfnd; @@ -1316,7 +1397,8 @@ proxtrans(cmd, local, remote) if (curtype != prox_type) changetype(prox_type, 1); if (command("PASV") != COMPLETE) { - printf("proxy server does not support third party transfers.\n"); + printf("proxy server does not support third party " + "transfers.\n"); return; } pswitch(0); @@ -1424,7 +1506,7 @@ reset(argc, argv) FD_ZERO(&mask); while (nfnd > 0) { FD_SET(fileno(cin), &mask); - if ((nfnd = empty(&mask,0)) < 0) { + if ((nfnd = empty(&mask, 0)) < 0) { warn("reset"); code = -1; lostpeer(); @@ -1437,7 +1519,7 @@ reset(argc, argv) char * gunique(local) - char *local; + const char *local; { static char new[MAXPATHLEN]; char *cp = strrchr(local, '/'); @@ -1446,7 +1528,7 @@ gunique(local) if (cp) *cp = '\0'; - d = access(cp ? local : ".", 2); + d = access(cp == local ? "/" : cp ? local : ".", 2); if (cp) *cp = '/'; if (d < 0) { @@ -1496,11 +1578,11 @@ abort_remote(din) sprintf(buf, "%c%c%c", IAC, IP, IAC); if (send(fileno(cout), buf, 3, MSG_OOB) != 3) warn("abort"); - fprintf(cout,"%cABOR\r\n", DM); + fprintf(cout, "%cABOR\r\n", DM); (void) fflush(cout); FD_ZERO(&mask); FD_SET(fileno(cin), &mask); - if (din) { + if (din) { FD_SET(fileno(din), &mask); } if ((nfnd = empty(&mask, 10)) <= 0) { diff --git a/usr.bin/ftp/ftp_var.h b/usr.bin/ftp/ftp_var.h index 45570ef62ed..3132ccf6e6a 100644 --- a/usr.bin/ftp/ftp_var.h +++ b/usr.bin/ftp/ftp_var.h @@ -1,5 +1,4 @@ -/* $OpenBSD: ftp_var.h,v 1.5 1996/11/09 19:59:35 kstailey Exp $ */ -/* $NetBSD: ftp_var.h,v 1.7 1995/09/15 00:32:35 pk Exp $ */ +/* $NetBSD: ftp_var.h,v 1.13 1997/02/01 10:45:05 lukem Exp $ */ /* * Copyright (c) 1985, 1989, 1993, 1994 @@ -42,9 +41,22 @@ #include #include +#include + +#ifndef SMALLFTP +#include +#endif /* !SMALLFTP */ #include "extern.h" +#define HASHBYTES 1024 +#define FTPBUFLEN MAXPATHLEN + 200 + +#define STALLTIME 5 /* # of seconds of no xfer before "stalling" */ + +#define FTP_PORT 21 /* default if getservbyname("ftp/tcp") fails */ +#define HTTP_PORT 80 /* default if getservbyname("http/tcp") fails */ + /* * Options and other state info. */ @@ -56,6 +68,7 @@ int verbose; /* print messages coming back from server */ int connected; /* connected to server */ int fromatty; /* input is from a terminal */ int interactive; /* interactively prompt on m* cmds */ +int confirmrest; /* confirm rest of current m* cmd */ int debug; /* debugging level */ int bell; /* ring bell on cmd completion */ int doglob; /* glob local file names */ @@ -67,6 +80,8 @@ int runique; /* store local files with unique name */ int mcase; /* map upper to lower case for mget names */ int ntflag; /* use ntin ntout tables for name translation */ int mapflag; /* use mapin mapout templates on file names */ +int preserve; /* preserve modification time on files */ +int progress; /* display transfer progress bar */ int code; /* return/reply code for ftp command */ int crflag; /* if 1, strip car. rets. on ascii gets */ char pasv[64]; /* passive port for proxy data connection */ @@ -87,24 +102,38 @@ char modename[32]; /* name of file transfer mode */ int mode; /* file transfer mode */ char bytename[32]; /* local byte size in ascii */ int bytesize; /* local byte size in binary */ -int anonftp; /* force an anonftp login */ -int retry_connect; /* retry connect if failed */ +int anonftp; /* automatic anonymous login */ +int dirchange; /* remote directory changed by cd command */ +int ttywidth; /* width of tty */ + +#ifndef SMALLFTP +int editing; /* command line editing enabled */ +EditLine *el; /* editline(3) status structure */ +History *hist; /* editline(3) history structure */ +char *cursor_pos; /* cursor position we're looking for */ +int cursor_argc; /* location of cursor in margv */ +int cursor_argo; /* offset of cursor in margv[cursor_argc] */ +#endif /* !SMALLFTP */ + +off_t bytes; /* current # of bytes read */ +off_t filesize; /* size of file being transferred */ +char *direction; /* direction transfer is occurring */ char *hostname; /* name of host connected to */ int unix_server; /* server is unix, can use binary for ascii */ int unix_proxy; /* proxy is unix, can use binary for ascii */ - -struct servent *sp; /* service spec for tcp/ftp */ +int ftpport; /* port number to use for ftp connections */ +int httpport; /* port number to use for http connections */ jmp_buf toplevel; /* non-local goto stuff for cmd scanner */ -char line[200]; /* input line buffer */ +char line[FTPBUFLEN]; /* input line buffer */ char *stringbase; /* current scan point in line buffer */ -char argbuf[200]; /* argument storage buffer */ +char argbuf[FTPBUFLEN]; /* argument storage buffer */ char *argbase; /* current storage point in arg buffer */ +StringList *marg_sl; /* stringlist containing margv */ int margc; /* count of arguments on input line */ -char **margv; /* args parsed from input line */ -int margvlen; /* how large margv is currently */ +#define margv (marg_sl->sl_str) /* args parsed from input line */ int cpend; /* flag: if != 0, then pending server reply */ int mflag; /* flag: if != 0, then active multi command */ @@ -116,9 +145,12 @@ int options; /* used during socket creation */ struct cmd { char *c_name; /* name of command */ char *c_help; /* help string */ - char c_bell; /* give bell when command completes */ - char c_conn; /* must be connected to use command */ - char c_proxy; /* proxy server may execute */ + char c_bell; /* give bell when command completes */ + char c_conn; /* must be connected to use command */ + char c_proxy; /* proxy server may execute */ +#ifndef SMALLFTP + char *c_complete; /* context sensitive completion list */ +#endif /* !SMALLFTP */ void (*c_handler) __P((int, char **)); /* function to call */ }; diff --git a/usr.bin/ftp/main.c b/usr.bin/ftp/main.c index a10adcc5303..7896f9f1423 100644 --- a/usr.bin/ftp/main.c +++ b/usr.bin/ftp/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.16 1997/01/29 22:21:32 millert Exp $ */ +/* $NetBSD: main.c,v 1.17 1997/02/01 10:45:07 lukem Exp $ */ /* * Copyright (c) 1985, 1989, 1993, 1994 @@ -43,60 +43,77 @@ static char copyright[] = #if 0 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 10/9/94"; #else -static char rcsid[] = "$OpenBSD: main.c,v 1.16 1997/01/29 22:21:32 millert Exp $"; +static char rcsid[] = "$NetBSD: main.c,v 1.17 1997/02/01 10:45:07 lukem Exp $"; #endif #endif /* not lint */ /* * FTP User Program -- Command Interface. */ -/*#include */ #include -#include #include -#include -#include - -#include #include #include #include -#include #include -#include #include #include #include "ftp_var.h" -#define HASHBYTES 1024 /* default buffer size for drawing hash marks */ - int main(argc, argv) int argc; char *argv[]; { - int ch, top; + struct servent *sp; + int ch, top, port, rval; struct passwd *pw = NULL; char *cp, homedir[MAXPATHLEN]; - int force_port = 0; sp = getservbyname("ftp", "tcp"); if (sp == 0) - errx(1, "ftp/tcp: unknown service"); + ftpport = htons(FTP_PORT); /* good fallback */ + else + ftpport = sp->s_port; + sp = getservbyname("http", "tcp"); + if (sp == 0) + httpport = htons(HTTP_PORT); /* good fallback */ + else + httpport = sp->s_port; doglob = 1; interactive = 1; autologin = 1; + passivemode = 0; + preserve = 1; + verbose = 0; + progress = 0; mark = HASHBYTES; + marg_sl = sl_init(); + + cp = strrchr(argv[0], '/'); + cp = (cp == NULL) ? argv[0] : cp + 1; + if (strcmp(cp, "pftp") == 0) + passivemode = 1; + + fromatty = isatty(fileno(stdin)); + if (fromatty) + verbose = 1; /* verbose if from a tty */ + if (isatty(fileno(stdout))) + progress = 1; /* progress bar on if going to a tty */ - while ((ch = getopt(argc, argv, "p:r:dgintv")) != -1) { + while ((ch = getopt(argc, argv, "adginpP:tvV")) != EOF) { switch (ch) { + case 'a': + anonftp = 1; + break; + case 'd': options |= SO_DEBUG; debug++; break; - + case 'g': doglob = 0; break; @@ -110,44 +127,38 @@ main(argc, argv) break; case 'p': - force_port = atoi(optarg); + passivemode = 1; break; - case 'r': - if (isdigit(*optarg)) - retry_connect = atoi(optarg); - else { - extern char *__progname; - (void)fprintf(stderr, - "%s: -r requires numeric argument\n", - __progname); - exit(1); - } + case 'P': + port = atoi(optarg); + if (port <= 0) + warnx("bad port number: %s", optarg); + else + ftpport = htons(port); break; case 't': - trace++; + trace = 1; break; case 'v': - verbose++; + verbose = 1; + break; + + case 'V': + verbose = 0; break; default: - (void)fprintf(stderr, "usage: " - "ftp [-dgintv] [-r] [host [port]]\n"); - exit(1); + usage(); } } argc -= optind; argv += optind; - fromatty = isatty(fileno(stdin)); - if (fromatty) - verbose++; cpend = 0; /* no pending replies */ proxy = 0; /* proxy not active */ - passivemode = 0; /* passive mode not active */ crflag = 1; /* strip c.r. on ascii gets */ sendport = -1; /* not using ports */ /* @@ -164,137 +175,53 @@ main(argc, argv) (void) strcpy(home, pw->pw_dir); } - if (argc > 0 && strchr(argv[0], ':')) { - int ret = 0; - anonftp = 1; +#ifndef SMALLFTP + editing = 0; /* command line editing off */ + if (fromatty) { + editing = 1; /* editing mode on if a tty */ + el = el_init(__progname, stdin, stdout); /* init editline */ + + hist = history_init(); /* init the builtin history */ + history(hist, H_EVENT, 100); /* remember 100 events */ + el_set(el, EL_HIST, history, hist); /* use history */ + + el_set(el, EL_EDITOR, "emacs"); /* default editor is emacs */ + el_set(el, EL_PROMPT, prompt); /* set the prompt function */ + + /* add local file completion, bind to TAB */ + el_set(el, EL_ADDFN, "ftp-complete", + "Context sensitive argument completion", + complete); + el_set(el, EL_BIND, "^I", "ftp-complete", NULL); + + el_source(el, NULL); /* read ~/.editrc */ + } +#endif /* !SMALLFTP */ + + setttywidth(0); + (void) signal(SIGWINCH, setttywidth); - while (argc > 0 && strchr(argv[0], ':')) { + if (argc > 0) { + if (strchr(argv[0], ':') != NULL) { + anonftp = 1; /* Handle "automatic" transfers. */ + rval = auto_fetch(argc, argv); + if (rval >= 0) /* -1 == connected and cd-ed */ + exit(rval); + } else { char *xargv[5]; - extern char *__progname; - char portstr[20], *p, *bufp = NULL; - char *host = NULL, *dir = NULL, *file = NULL; - int xargc = 2, looping = 0, tmp; if (setjmp(toplevel)) exit(0); (void) signal(SIGINT, intr); (void) signal(SIGPIPE, lostpeer); xargv[0] = __progname; - - host = strdup(argv[0]); - if (host == NULL) { - ret = 1; - goto bail; - } - if (!strncmp(host, "http://", sizeof("http://") - 1)) { - ret = http_fetch(host); - argc--; - argv++; - goto bail; - } - if (strncmp(host, "ftp://", sizeof("ftp://") - 1) == - 0) { - host += sizeof("ftp://") - 1; - p = strchr(host, '/'); - } - else - p = strchr(host, ':'); - *p = '\0'; - - xargv[1] = host; - xargc = 2; - if (force_port) { - xargv[xargc++] = portstr; - snprintf(portstr, sizeof portstr, "%d", - force_port); - } - xargv[xargc] = NULL; - setpeer(xargc, xargv); - if (!connected) { - printf("failed to connect to %s\n", host); - ret = 1; - argc--; - argv++; - goto bail; - } - *argv = p + 1; - do { - dir = *argv; - p = strrchr(dir, '/'); - if (p != NULL) { - *p = '\0'; - file = ++p; - } else { - file = dir; - dir = NULL; - } - if (dir != NULL && *dir != '\0') { - xargv[1] = dir; - xargv[2] = NULL; - xargc = 2; - cd(xargc, xargv); - } - xargv[1] = *file == '\0' ? "/" : file; - xargv[2] = NULL; - xargc = 2; - tmp = verbose; - verbose = -1; - if (mcd(xargc, xargv) == 0) { - verbose = tmp; - goto CLINE_CD; - } - verbose = tmp; - if (!looping) { - setbinary(NULL, 0); - looping = 1; - } - /* fetch file */ - xargv[1] = file; - xargv[2] = NULL; - xargc = 2; - get(xargc, xargv); - if (code != 226) - ret = 1; - --argc; - argv++; - } while (argc > 0 && strchr(argv[0], ':') == NULL); - - /* get ready for the next file */ -bail: - if (bufp) { - free(bufp); - bufp = NULL; - } - if (connected) - disconnect(1, xargv); - } - exit(ret); - } - if (argc > 0) { - char *xargv[5]; - extern char *__progname; - - if (setjmp(toplevel)) - exit(0); - (void) signal(SIGINT, intr); - (void) signal(SIGPIPE, lostpeer); - xargv[0] = __progname; - xargv[1] = argv[0]; - xargv[2] = argv[1]; - xargv[3] = argv[2]; - xargv[4] = NULL; - do { + xargv[1] = argv[0]; + xargv[2] = argv[1]; + xargv[3] = argv[2]; + xargv[4] = NULL; setpeer(argc+1, xargv); - if (!retry_connect) - break; - if (!connected) { - macnum = 0; - printf("Retrying...\n"); - sleep(retry_connect); - } - } while (!connected); + } } -CLINE_CD: top = setjmp(toplevel) == 0; if (top) { (void) signal(SIGINT, intr); @@ -310,6 +237,7 @@ void intr() { + alarmtimer(0); longjmp(toplevel, 1); } @@ -317,6 +245,7 @@ void lostpeer() { + alarmtimer(0); if (connected) { if (cout != NULL) { (void) shutdown(fileno(cout), 1+1); @@ -344,23 +273,13 @@ lostpeer() } /* + * Generate a prompt + */ char * -tail(filename) - char *filename; +prompt() { - char *s; - - while (*filename) { - s = strrchr(filename, '/'); - if (s == NULL) - break; - if (s[1]) - return (s + 1); - *s = '\0'; - } - return (filename); + return ("ftp> "); } -*/ /* * Command parser. @@ -370,34 +289,68 @@ cmdscanner(top) int top; { struct cmd *c; - int l; + int num; - if (!top) + if (!top +#ifndef SMALLFTP + && !editing +#endif /* !SMALLFTP */ + ) (void) putchar('\n'); for (;;) { - if (fromatty) { - printf("ftp> "); - (void) fflush(stdout); - } - if (fgets(line, sizeof line, stdin) == NULL) - quit(0, 0); - l = strlen(line); - if (l == 0) - break; - if (line[--l] == '\n') { - if (l == 0) +#ifndef SMALLFTP + if (!editing) { +#endif /* !SMALLFTP */ + if (fromatty) { + printf("%s", prompt()); + (void) fflush(stdout); + } + if (fgets(line, sizeof(line), stdin) == NULL) + quit(0, 0); + num = strlen(line); + if (num == 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 */ + if (line[--num] == '\n') { + if (num == 0) + break; + line[num] = '\0'; + } else if (num == sizeof(line) - 2) { + printf("sorry, input line too long\n"); + while ((num = getchar()) != '\n' && num != EOF) + /* void */; + break; + } /* else it was a line without a newline */ +#ifndef SMALLFTP + } else { + const char *buf; + cursor_pos = NULL; + + if ((buf = el_gets(el, &num)) == NULL || num == 0) + quit(0, 0); + if (line[--num] == '\n') { + if (num == 0) + break; + } else if (num >= sizeof(line)) { + printf("sorry, input line too long\n"); + break; + } + memcpy(line, buf, num); + line[num] = '\0'; + history(hist, H_ENTER, buf); + } +#endif /* !SMALLFTP */ + makeargv(); - if (margc == 0) { + if (margc == 0) continue; - } +#if 0 && !defined(SMALLFTP) /* XXX: don't want el_parse */ + /* + * el_parse returns -1 to signal that it's not been handled + * internally. + */ + if (el_parse(el, margc, margv) != -1) + continue; +#endif /* !SMALLFTP */ c = getcmd(margv[0]); if (c == (struct cmd *)-1) { printf("?Ambiguous command\n"); @@ -411,6 +364,7 @@ cmdscanner(top) printf("Not connected.\n"); continue; } + confirmrest = 0; (*c->c_handler)(margc, margv); if (bell && c->c_bell) (void) putchar('\007'); @@ -423,9 +377,9 @@ cmdscanner(top) struct cmd * getcmd(name) - char *name; + const char *name; { - char *p, *q; + const char *p, *q; struct cmd *c, *found; int nmatches, longest; @@ -435,7 +389,7 @@ getcmd(name) longest = 0; nmatches = 0; found = 0; - for (c = cmdtab; p = c->c_name; c++) { + for (c = cmdtab; (p = c->c_name) != NULL; c++) { for (q = name; *q == *p++; q++) if (*q == 0) /* exact match? */ return (c); @@ -462,31 +416,41 @@ int slrflag; void makeargv() { - char **argp; + char *argp; - argp = margv; stringbase = line; /* scan from first of buffer */ argbase = argbuf; /* store from first of buffer */ slrflag = 0; + marg_sl->sl_cur = 0; /* reset to start of marg_sl */ for (margc = 0; ; margc++) { - /* Expand array if necessary */ - if (margc == margvlen) { - margv = (margvlen == 0) - ? (char **)malloc(20 * sizeof(char *)) - : (char **)realloc(margv, - (margvlen + 20)*sizeof(char *)); - if (margv == NULL) - errx(1, "cannot realloc argv array"); - margvlen += 20; - argp = margv + margc; - } - - if ((*argp++ = slurpstring()) == NULL) + argp = slurpstring(); + sl_add(marg_sl, argp); + if (argp == NULL) break; } - +#ifndef SMALLFTP + if (cursor_pos == line) { + cursor_argc = 0; + cursor_argo = 0; + } else if (cursor_pos != NULL) { + cursor_argc = margc; + cursor_argo = strlen(margv[margc-1]); + } +#endif /* !SMALLFTP */ } +#ifdef SMALLFTP +#define INC_CHKCURSOR(x) (x)++ +#else /* !SMALLFTP */ +#define INC_CHKCURSOR(x) { (x)++ ; \ + if (x == cursor_pos) { \ + cursor_argc = margc; \ + cursor_argo = ap-argbase; \ + cursor_pos = NULL; \ + } } + +#endif /* !SMALLFTP */ + /* * Parse string into argbuf; * implemented with FSM to @@ -504,7 +468,7 @@ slurpstring() switch (slrflag) { /* and $ as token for macro invoke */ case 0: slrflag++; - stringbase++; + INC_CHKCURSOR(stringbase); return ((*sb == '!') ? "!" : "$"); /* NOTREACHED */ case 1: @@ -524,7 +488,8 @@ S0: case ' ': case '\t': - sb++; goto S0; + INC_CHKCURSOR(sb); + goto S0; default: switch (slrflag) { @@ -550,13 +515,17 @@ S1: goto OUT; /* end of token */ case '\\': - sb++; goto S2; /* slurp next character */ + INC_CHKCURSOR(sb); + goto S2; /* slurp next character */ case '"': - sb++; goto S3; /* slurp quoted string */ + INC_CHKCURSOR(sb); + goto S3; /* slurp quoted string */ default: - *ap++ = *sb++; /* add character to token */ + *ap = *sb; /* add character to token */ + ap++; + INC_CHKCURSOR(sb); got_one = 1; goto S1; } @@ -568,7 +537,9 @@ S2: goto OUT; default: - *ap++ = *sb++; + *ap = *sb; + ap++; + INC_CHKCURSOR(sb); got_one = 1; goto S1; } @@ -580,10 +551,13 @@ S3: goto OUT; case '"': - sb++; goto S1; + INC_CHKCURSOR(sb); + goto S1; default: - *ap++ = *sb++; + *ap = *sb; + ap++; + INC_CHKCURSOR(sb); got_one = 1; goto S3; } @@ -610,8 +584,6 @@ OUT: return ((char *)0); } -#define HELPINDENT ((int) sizeof ("directory")) - /* * Help command. * Call each command handler with argc == 0 and argv[0] == name. @@ -624,47 +596,24 @@ help(argc, argv) struct cmd *c; if (argc == 1) { - int i, j, w, k; - int columns, width = 0, lines; - - printf("Commands may be abbreviated. Commands are:\n\n"); - for (c = cmdtab; c < &cmdtab[NCMDS]; c++) { - int len = strlen(c->c_name); - - if (len > width) - width = len; - } - width = (width + 8) &~ 7; - columns = 80 / width; - if (columns == 0) - columns = 1; - lines = (NCMDS + columns - 1) / columns; - for (i = 0; i < lines; i++) { - for (j = 0; j < columns; j++) { - c = cmdtab + j * lines + i; - if (c->c_name && (!proxy || c->c_proxy)) { - printf("%s", c->c_name); - } - else if (c->c_name) { - for (k=0; k < strlen(c->c_name); k++) { - (void) putchar(' '); - } - } - if (c + lines >= &cmdtab[NCMDS]) { - printf("\n"); - break; - } - w = strlen(c->c_name); - while (w < width) { - w = (w + 8) &~ 7; - (void) putchar('\t'); - } - } - } + StringList *buf; + + buf = sl_init(); + printf("%sommands may be abbreviated. Commands are:\n\n", + proxy ? "Proxy c" : "C"); + for (c = cmdtab; c < &cmdtab[NCMDS]; c++) + if (c->c_name && (!proxy || c->c_proxy)) + sl_add(buf, c->c_name); + list_vertical(buf); + sl_free(buf, 0); return; } + +#define HELPINDENT ((int) sizeof ("disconnect")) + while (--argc > 0) { char *arg; + arg = *++argv; c = getcmd(arg); if (c == (struct cmd *)-1) @@ -676,3 +625,15 @@ help(argc, argv) c->c_name, c->c_help); } } + +void +usage() +{ + (void)fprintf(stderr, + "usage: %s [-adginptvV] [host [port]]\n" + " %s host:path[/]\n" + " %s ftp://host[:port]/path[/]\n" + " %s http://host[:port]/file\n", + __progname, __progname, __progname, __progname); + exit(1); +} diff --git a/usr.bin/ftp/pathnames.h b/usr.bin/ftp/pathnames.h index c3ffcbce4d2..e028658e736 100644 --- a/usr.bin/ftp/pathnames.h +++ b/usr.bin/ftp/pathnames.h @@ -1,5 +1,4 @@ -/* $OpenBSD: pathnames.h,v 1.3 1996/09/16 02:26:07 deraadt Exp $ */ -/* $NetBSD: pathnames.h,v 1.5 1995/09/08 01:06:40 tls Exp $ */ +/* $NetBSD: pathnames.h,v 1.7 1997/01/09 20:19:40 tls Exp $ */ /* * Copyright (c) 1989, 1993 @@ -38,4 +37,4 @@ #include -#define _PATH_TMPFILE "/tmp/ftpXXXXXX" +#define TMPFILE "ftpXXXXXX" diff --git a/usr.bin/ftp/ruserpass.c b/usr.bin/ftp/ruserpass.c index 0ddd3d6689e..e8312df2d31 100644 --- a/usr.bin/ftp/ruserpass.c +++ b/usr.bin/ftp/ruserpass.c @@ -1,5 +1,4 @@ -/* $OpenBSD: ruserpass.c,v 1.3 1996/10/28 00:32:30 millert Exp $ */ -/* $NetBSD: ruserpass.c,v 1.6 1995/09/08 01:06:43 tls Exp $ */ +/* $NetBSD: ruserpass.c,v 1.11 1997/01/19 14:19:16 lukem Exp $ */ /* * Copyright (c) 1985, 1993, 1994 @@ -35,7 +34,11 @@ */ #ifndef lint +#if 0 static char sccsid[] = "@(#)ruserpass.c 8.4 (Berkeley) 4/27/95"; +#else +static char rcsid[] = "$NetBSD: ruserpass.c,v 1.11 1997/01/19 14:19:16 lukem Exp $"; +#endif #endif /* not lint */ #include @@ -80,7 +83,8 @@ static struct toktab { int ruserpass(host, aname, apass, aacct) - char *host, **aname, **apass, **aacct; + const char *host; + char **aname, **apass, **aacct; { char *hdir, buf[BUFSIZ], *tmp; char myname[MAXHOSTNAMELEN], *mydomain; @@ -90,7 +94,7 @@ ruserpass(host, aname, apass, aacct) hdir = getenv("HOME"); if (hdir == NULL) hdir = "."; - if (strlen(hdir) + 7 < sizeof(buf)) { + if (strlen(hdir) + sizeof(".netrc") < sizeof(buf)) { (void) sprintf(buf, "%s/.netrc", hdir); } else { warnx("%s/.netrc: %s", hdir, strerror(ENAMETOOLONG)); @@ -119,7 +123,7 @@ next: continue; /* * Allow match either for user's input host name - * or official hostname. Also allow match of + * or official hostname. Also allow match of * incompletely-specified host in local domain. */ if (strcasecmp(host, tokval) == 0) @@ -143,8 +147,9 @@ next: case LOGIN: if (token()) - if (*aname == 0) { - *aname = malloc((unsigned) strlen(tokval) + 1); + if (*aname == 0) { + *aname = malloc((unsigned) + strlen(tokval) + 1); (void) strcpy(*aname, tokval); } else { if (strcmp(*aname, tokval)) @@ -181,13 +186,16 @@ next: (void) fclose(cfile); return (0); } - while ((c=getc(cfile)) != EOF && c == ' ' || c == '\t'); + while ((c=getc(cfile)) != EOF) + if (c != ' ' && c != '\t') + break; if (c == EOF || c == '\n') { printf("Missing macdef name argument.\n"); goto bad; } if (macnum == 16) { - printf("Limit of 16 macros have already been defined\n"); + printf( +"Limit of 16 macros have already been defined\n"); goto bad; } tmp = macros[macnum].mac_name; @@ -197,7 +205,8 @@ next: *tmp++ = c; } if (c == EOF) { - printf("Macro definition missing null line terminator.\n"); + printf( +"Macro definition missing null line terminator.\n"); goto bad; } *tmp = '\0'; @@ -205,19 +214,22 @@ next: while ((c=getc(cfile)) != EOF && c != '\n'); } if (c == EOF) { - printf("Macro definition missing null line terminator.\n"); + printf( +"Macro definition missing null line terminator.\n"); goto bad; } if (macnum == 0) { macros[macnum].mac_start = macbuf; } else { - macros[macnum].mac_start = macros[macnum-1].mac_end + 1; + macros[macnum].mac_start = + macros[macnum-1].mac_end + 1; } tmp = macros[macnum].mac_start; while (tmp != macbuf + 4096) { if ((c=getc(cfile)) == EOF) { - printf("Macro definition missing null line terminator.\n"); + printf( +"Macro definition missing null line terminator.\n"); goto bad; } *tmp = c; diff --git a/usr.bin/ftp/util.c b/usr.bin/ftp/util.c new file mode 100644 index 00000000000..5d9c7ecd1fc --- /dev/null +++ b/usr.bin/ftp/util.c @@ -0,0 +1,614 @@ +/* $NetBSD: util.c,v 1.4 1997/02/01 11:26:34 lukem Exp $ */ + +/* + * Copyright (c) 1985, 1989, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +static char rcsid[] = "$NetBSD: util.c,v 1.4 1997/02/01 11:26:34 lukem Exp $"; +#endif /* not lint */ + +/* + * FTP User Program -- Misc support routines + */ +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ftp_var.h" +#include "pathnames.h" + +/* + * Connect to peer server and + * auto-login, if possible. + */ +void +setpeer(argc, argv) + int argc; + char *argv[]; +{ + char *host; + short port; + + if (connected) { + printf("Already connected to %s, use close first.\n", + hostname); + code = -1; + return; + } + if (argc < 2) + (void) another(&argc, &argv, "to"); + if (argc < 2 || argc > 3) { + printf("usage: %s host-name [port]\n", argv[0]); + code = -1; + return; + } + port = ftpport; + if (argc > 2) { + port = atoi(argv[2]); + if (port <= 0) { + printf("%s: bad port number-- %s\n", argv[1], argv[2]); + printf ("usage: %s host-name [port]\n", argv[0]); + code = -1; + return; + } + port = htons(port); + } + host = hookup(argv[1], port); + if (host) { + int overbose; + + connected = 1; + /* + * Set up defaults for FTP. + */ + (void) strcpy(typename, "ascii"), type = TYPE_A; + curtype = TYPE_A; + (void) strcpy(formname, "non-print"), form = FORM_N; + (void) strcpy(modename, "stream"), mode = MODE_S; + (void) strcpy(structname, "file"), stru = STRU_F; + (void) strcpy(bytename, "8"), bytesize = 8; + if (autologin) + (void) login(argv[1]); + + overbose = verbose; + if (debug == 0) + verbose = -1; + if (command("SYST") == COMPLETE && overbose) { + char *cp, c; + c = 0; + cp = strchr(reply_string+4, ' '); + if (cp == NULL) + cp = strchr(reply_string+4, '\r'); + if (cp) { + if (cp[-1] == '.') + cp--; + c = *cp; + *cp = '\0'; + } + + printf("Remote system type is %s.\n", + reply_string+4); + if (cp) + *cp = c; + } + if (!strncmp(reply_string, "215 UNIX Type: L8", 17)) { + if (proxy) + unix_proxy = 1; + else + unix_server = 1; + /* + * Set type to 0 (not specified by user), + * meaning binary by default, but don't bother + * telling server. We can use binary + * for text files unless changed by the user. + */ + type = 0; + (void) strcpy(typename, "binary"); + if (overbose) + printf("Using %s mode to transfer files.\n", + typename); + } else { + if (proxy) + unix_proxy = 0; + else + unix_server = 0; + if (overbose && + !strncmp(reply_string, "215 TOPS20", 10)) + printf("Remember to set tenex mode when " + "transferring binary files from this " + "machine.\n"); + } + verbose = overbose; + } +} + +/* + * `Another' gets another argument, and stores the new argc and argv. + * It reverts to the top level (via main.c's intr()) on EOF/error. + * + * Returns false if no new arguments have been added. + */ +int +another(pargc, pargv, prompt) + int *pargc; + char ***pargv; + const char *prompt; +{ + int len = strlen(line), ret; + + if (len >= sizeof(line) - 3) { + printf("sorry, arguments too long\n"); + intr(); + } + printf("(%s) ", prompt); + line[len++] = ' '; + if (fgets(&line[len], sizeof(line) - len, stdin) == NULL) + intr(); + len += strlen(&line[len]); + if (len > 0 && line[len - 1] == '\n') + line[len - 1] = '\0'; + makeargv(); + ret = margc > *pargc; + *pargc = margc; + *pargv = margv; + return (ret); +} + +char * +remglob(argv, doswitch) + char *argv[]; + int doswitch; +{ + char temp[MAXPATHLEN]; + static char buf[MAXPATHLEN]; + static FILE *ftemp = NULL; + static char **args; + int oldverbose, oldhash, fd; + char *cp, *mode; + + if (!mflag) { + if (!doglob) { + args = NULL; + } + else { + if (ftemp) { + (void) fclose(ftemp); + ftemp = NULL; + } + } + return (NULL); + } + if (!doglob) { + if (args == NULL) + args = argv; + if ((cp = *++args) == NULL) + args = NULL; + return (cp); + } + if (ftemp == NULL) { + (void) snprintf(temp, sizeof(temp), "%s%s", _PATH_TMP, TMPFILE) +; + fd = mkstemp(temp); + if (fd < 0) { + warn("unable to create temporary file %s", temp); + return (NULL); + } + close(fd); + oldverbose = verbose, verbose = 0; + oldhash = hash, hash = 0; + if (doswitch) { + pswitch(!proxy); + } + for (mode = "w"; *++argv != NULL; mode = "a") + recvrequest ("NLST", temp, *argv, mode, 0); + if (doswitch) { + pswitch(!proxy); + } + verbose = oldverbose; hash = oldhash; + ftemp = fopen(temp, "r"); + (void) unlink(temp); + if (ftemp == NULL) { + printf("can't find list of remote files, oops\n"); + return (NULL); + } + } + if (fgets(buf, sizeof (buf), ftemp) == NULL) { + (void) fclose(ftemp), ftemp = NULL; + return (NULL); + } + if ((cp = strchr(buf, '\n')) != NULL) + *cp = '\0'; + return (buf); +} + +int +confirm(cmd, file) + const char *cmd, *file; +{ + char line[BUFSIZ]; + + if (!interactive || confirmrest) + return (1); + printf("%s %s? ", cmd, file); + (void) fflush(stdout); + if (fgets(line, sizeof(line), stdin) == NULL) + return (0); + switch (tolower(*line)) { + case 'n': + return (0); + case 'p': + interactive = 0; + printf("Interactive mode: off\n"); + break; + case 'a': + confirmrest = 1; + printf("Prompting off for duration of %s\n", cmd); + break; + } + return (1); +} + +/* + * Glob a local file name specification with + * the expectation of a single return value. + * Can't control multiple values being expanded + * from the expression, we return only the first. + */ +int +globulize(cpp) + char **cpp; +{ + glob_t gl; + int flags; + + if (!doglob) + return (1); + + flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE; + memset(&gl, 0, sizeof(gl)); + if (glob(*cpp, flags, NULL, &gl) || + gl.gl_pathc == 0) { + warnx("%s: not found", *cpp); + globfree(&gl); + return (0); + } + *cpp = strdup(gl.gl_pathv[0]); /* XXX - wasted memory */ + globfree(&gl); + return (1); +} + +/* + * determine size of remote file + */ +off_t +remotesize(file, noisy) + const char *file; + int noisy; +{ + int overbose; + off_t size; + + overbose = verbose; + size = -1; + if (debug == 0) + verbose = -1; + if (command("SIZE %s", file) == COMPLETE) + sscanf(reply_string, "%*s %qd", &size); + else if (noisy && debug == 0) + printf("%s\n", reply_string); + verbose = overbose; + return (size); +} + +/* + * determine last modification time (in GMT) of remote file + */ +time_t +remotemodtime(file, noisy) + const char *file; + int noisy; +{ + int overbose; + time_t rtime; + + overbose = verbose; + rtime = -1; + if (debug == 0) + verbose = -1; + if (command("MDTM %s", file) == COMPLETE) { + struct tm timebuf; + int yy, mo, day, hour, min, sec; + sscanf(reply_string, "%*s %04d%02d%02d%02d%02d%02d", &yy, &mo, + &day, &hour, &min, &sec); + memset(&timebuf, 0, sizeof(timebuf)); + timebuf.tm_sec = sec; + timebuf.tm_min = min; + timebuf.tm_hour = hour; + timebuf.tm_mday = day; + timebuf.tm_mon = mo - 1; + timebuf.tm_year = yy - 1900; + timebuf.tm_isdst = -1; + rtime = mktime(&timebuf); + if (rtime == -1 && (noisy || debug != 0)) + printf("Can't convert %s to a time\n", reply_string); + else + rtime += timebuf.tm_gmtoff; /* conv. local -> GMT */ + } else if (noisy && debug == 0) + printf("%s\n", reply_string); + verbose = overbose; + return (rtime); +} + +void +updateprogressmeter() +{ + + progressmeter(0); +} + +/* + * Display a transfer progress bar if progress is non-zero. + * SIGALRM is hijacked for use by this function. + * - Before the transfer, set filesize to size of file (or -1 if unknown), + * and call with flag = -1. This starts the once per second timer, + * and a call to updateprogressmeter() upon SIGALRM. + * - During the transfer, updateprogressmeter will call progressmeter + * with flag = 0 + * - After the transfer, call with flag = 1 + */ +static struct timeval start; + +void +progressmeter(flag) + int flag; +{ + /* + * List of order of magnitude prefixes. + * The last is `P', as 2^64 = 16384 Petabytes + */ + static const char prefixes[] = " KMGTP"; + + static struct timeval lastupdate; + static off_t lastsize; + struct timeval now, td, wait; + off_t cursize, abbrevsize; + double elapsed; + int ratio, barlength, i, remaining; + char buf[256]; + + if (flag == -1) { + (void) gettimeofday(&start, (struct timezone *)0); + lastupdate = start; + lastsize = restart_point; + } + (void) gettimeofday(&now, (struct timezone *)0); + if (!progress || filesize <= 0) + return; + cursize = bytes + restart_point; + + ratio = cursize * 100 / filesize; + ratio = MAX(ratio, 0); + ratio = MIN(ratio, 100); + snprintf(buf, sizeof(buf), "\r%3d%% ", ratio); + + barlength = ttywidth - 30; + if (barlength > 0) { + i = barlength * ratio / 100; + snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), + "|%.*s%*s|", i, +"*****************************************************************************" +"*****************************************************************************", + barlength - i, ""); + } + + i = 0; + abbrevsize = cursize; + while (abbrevsize >= 100000 && i < sizeof(prefixes)) { + i++; + abbrevsize >>= 10; + } + snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), + " %5qd %c%c ", abbrevsize, prefixes[i], + prefixes[i] == ' ' ? ' ' : 'B'); + + timersub(&now, &lastupdate, &wait); + if (cursize > lastsize) { + lastupdate = now; + lastsize = cursize; + if (wait.tv_sec >= STALLTIME) { /* fudge out stalled time */ + start.tv_sec += wait.tv_sec; + start.tv_usec += wait.tv_usec; + } + wait.tv_sec = 0; + } + + timersub(&now, &start, &td); + elapsed = td.tv_sec + (td.tv_usec / 1000000.0); + + if (bytes <= 0 || elapsed <= 0.0) { + snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), + " --:-- ETA"); + } else if (wait.tv_sec >= STALLTIME) { + snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), + " - stalled -"); + } else { + remaining = (int)((filesize - restart_point) / + (bytes / elapsed) - elapsed); + i = remaining / 3600; + if (i) + snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), + "%2d:", i); + else + snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), + " "); + i = remaining % 3600; + snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), + "%02d:%02d ETA", i / 60, i % 60); + } + (void)write(STDOUT_FILENO, buf, strlen(buf)); + + if (flag == -1) { + (void) signal(SIGALRM, updateprogressmeter); + alarmtimer(1); /* set alarm timer for 1 Hz */ + } else if (flag == 1) { + alarmtimer(0); + (void) putchar('\n'); + } + fflush(stdout); +} + +/* + * Display transfer statistics. + * Requires start to be initialised by progressmeter(-1), + * direction to be defined by xfer routines, and filesize and bytes + * to be updated by xfer routines + * If siginfo is nonzero, an ETA is displayed, and the output goes to STDERR + * instead of STDOUT. + */ +void +ptransfer(siginfo) + int siginfo; +{ + struct timeval now, td; + double elapsed; + off_t bs; + int meg, remaining, hh; + char buf[100]; + + if (!verbose && !siginfo) + return; + + (void) gettimeofday(&now, (struct timezone *)0); + timersub(&now, &start, &td); + elapsed = td.tv_sec + (td.tv_usec / 1000000.0); + bs = bytes / (elapsed == 0.0 ? 1 : elapsed); + meg = 0; + if (bs > (1024 * 1024)) + meg = 1; + (void)snprintf(buf, sizeof(buf), + "%qd byte%s %s in %.2f seconds (%.2f %sB/s)\n", + bytes, bytes == 1 ? "" : "s", direction, elapsed, + bs / (1024.0 * (meg ? 1024.0 : 1.0)), meg ? "M" : "K"); + if (siginfo && bytes > 0 && elapsed > 0.0 && filesize >= 0) { + remaining = (int)((filesize - restart_point) / + (bytes / elapsed) - elapsed); + hh = remaining / 3600; + remaining %= 3600; + snprintf(buf + strlen(buf) - 1, sizeof(buf) - strlen(buf), + " ETA: %02d:%02d:%02d\n", hh, remaining / 60, + remaining % 60); + } + (void)write(siginfo ? STDERR_FILENO : STDOUT_FILENO, buf, strlen(buf)); +} + +/* + * List words in stringlist, vertically arranged + */ +void +list_vertical(sl) + StringList *sl; +{ + int i, j, w; + int columns, width, lines, items; + char *p; + + width = items = 0; + + for (i = 0 ; i < sl->sl_cur ; i++) { + w = strlen(sl->sl_str[i]); + if (w > width) + width = w; + } + width = (width + 8) &~ 7; + + columns = ttywidth / width; + if (columns == 0) + columns = 1; + lines = (sl->sl_cur + columns - 1) / columns; + for (i = 0; i < lines; i++) { + for (j = 0; j < columns; j++) { + p = sl->sl_str[j * lines + i]; + if (p) + printf("%s", p); + if (j * lines + i + lines >= sl->sl_cur) { + printf("\n"); + break; + } + w = strlen(p); + while (w < width) { + w = (w + 8) &~ 7; + (void) putchar('\t'); + } + } + } +} + +/* + * Update the global ttywidth value, using TIOCGWINSZ. + */ +void +setttywidth(a) + int a; +{ + struct winsize winsize; + + if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1) + ttywidth = winsize.ws_col; + else + ttywidth = 80; +} + +/* + * Set the SIGALRM interval timer for wait seconds, 0 to disable. + */ + +void +alarmtimer(wait) + int wait; +{ + struct itimerval itv; + + itv.it_value.tv_sec = wait; + itv.it_value.tv_usec = 0; + itv.it_interval = itv.it_value; + setitimer(ITIMER_REAL, &itv, NULL); +} -- 2.20.1