summaryrefslogtreecommitdiffstats
path: root/usr.bin
diff options
context:
space:
mode:
authormsmith <msmith@FreeBSD.org>1997-06-25 08:56:46 +0000
committermsmith <msmith@FreeBSD.org>1997-06-25 08:56:46 +0000
commit89334bc82c5b7ad1189c9a5fd805f53e80ad65b1 (patch)
treeda509a34c25bd98d42aa22dab0f6cd5363f3c6a8 /usr.bin
parent556be62658f72a2060b34dc2e2d5ec070e75dd41 (diff)
downloadFreeBSD-src-89334bc82c5b7ad1189c9a5fd805f53e80ad65b1.zip
FreeBSD-src-89334bc82c5b7ad1189c9a5fd805f53e80ad65b1.tar.gz
Import substantial improvements to ftp(1) from NetBSD, largely the
work of Luke Mewburn. This includes, but is not limited to : - commandline editing and history. - local and remote filename completion. - a new progress display. - the ability to access files using either the ftp or http protocols, and use http proxies for ftp transfers. The FreeeBSD "restricted ports" functionality was preserved. Obtained from: NetBSD
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/ftp/Makefile15
-rw-r--r--usr.bin/ftp/cmds.c993
-rw-r--r--usr.bin/ftp/cmdtab.c206
-rw-r--r--usr.bin/ftp/complete.c367
-rw-r--r--usr.bin/ftp/domacro.c32
-rw-r--r--usr.bin/ftp/extern.h88
-rw-r--r--usr.bin/ftp/fetch.c608
-rw-r--r--usr.bin/ftp/ftp.1515
-rw-r--r--usr.bin/ftp/ftp.c815
-rw-r--r--usr.bin/ftp/ftp_var.h62
-rw-r--r--usr.bin/ftp/main.c410
-rw-r--r--usr.bin/ftp/pathnames.h6
-rw-r--r--usr.bin/ftp/ruserpass.c57
-rw-r--r--usr.bin/ftp/util.c779
14 files changed, 3555 insertions, 1398 deletions
diff --git a/usr.bin/ftp/Makefile b/usr.bin/ftp/Makefile
index dc8197f..4bb9dc0 100644
--- a/usr.bin/ftp/Makefile
+++ b/usr.bin/ftp/Makefile
@@ -1,8 +1,15 @@
-# @(#)Makefile 8.2 (Berkeley) 4/3/94
+# $Id$
+# $NetBSD: Makefile,v 1.11 1997/03/24 21:59:36 christos Exp $
+# from: @(#)Makefile 8.2 (Berkeley) 4/3/94
PROG= ftp
-SRCS= cmds.c cmdtab.c ftp.c main.c ruserpass.c domacro.c
-LINKS= ${BINDIR}/ftp ${BINDIR}/pftp
-MLINKS= ftp.1 pftp.1
+SRCS= cmds.c cmdtab.c complete.c domacro.c fetch.c ftp.c main.c ruserpass.c \
+ util.c
+
+LDADD+= -ledit -ltermcap
+DPADD+= ${LIBEDIT} ${LIBTERMCAP}
+
+LINKS= ${BINDIR}/ftp ${BINDIR}/pftp
+MLINKS= ftp.1 pftp.1
.include <bsd.prog.mk>
diff --git a/usr.bin/ftp/cmds.c b/usr.bin/ftp/cmds.c
index 371ad2e..f083f95 100644
--- a/usr.bin/ftp/cmds.c
+++ b/usr.bin/ftp/cmds.c
@@ -1,3 +1,6 @@
+/* $Id$ */
+/* $NetBSD: cmds.c,v 1.24 1997/05/17 19:44:36 pk Exp $ */
+
/*
* Copyright (c) 1985, 1989, 1993, 1994
* The Regents of the University of California. All rights reserved.
@@ -32,28 +35,29 @@
*/
#ifndef lint
-static char sccsid[] = "@(#)cmds.c 8.5 (Berkeley) 4/6/94";
+#if 0
+static char sccsid[] = "@(#)cmds.c 8.6 (Berkeley) 10/9/94";
+#else
+static char rcsid[] = "$Id$";
+#endif
#endif /* not lint */
/*
* FTP User Program -- Command Routines.
*/
-#include <sys/param.h>
-#include <sys/wait.h>
-#include <sys/stat.h>
+#include <sys/types.h>
#include <sys/socket.h>
-#include <netinet/in.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
#include <arpa/ftp.h>
#include <ctype.h>
#include <err.h>
#include <glob.h>
#include <netdb.h>
-#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <time.h>
#include <unistd.h>
#include "ftp_var.h"
@@ -63,147 +67,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;
@@ -238,7 +101,7 @@ settype(argc, argv)
printf("%s%s", sep, p->t_name);
sep = " | ";
}
- printf(" ]\n");
+ puts(" ]");
code = -1;
return;
}
@@ -251,16 +114,16 @@ settype(argc, argv)
if (strcmp(argv[1], p->t_name) == 0)
break;
if (p->t_name == 0) {
- printf("%s: unknown mode\n", argv[1]);
+ printf("%s: unknown mode.\n", argv[1]);
code = -1;
return;
}
if ((p->t_arg != NULL) && (*(p->t_arg) != '\0'))
- comret = command ("TYPE %s %s", p->t_mode, p->t_arg);
+ comret = command("TYPE %s %s", p->t_mode, p->t_arg);
else
comret = command("TYPE %s", p->t_mode);
if (comret == COMPLETE) {
- (void) strcpy(typename, p->t_name);
+ (void)strcpy(typename, p->t_name);
curtype = type = p->t_type;
}
}
@@ -287,7 +150,7 @@ changetype(newtype, show)
if (newtype == p->t_type)
break;
if (p->t_name == 0) {
- printf("ftp: internal error: unknown type %d\n", newtype);
+ warnx("internal error: unknown type %d.", newtype);
return;
}
if (newtype == TYPE_L && bytename[0] != '\0')
@@ -312,7 +175,7 @@ char *stype[] = {
void
setbinary(argc, argv)
int argc;
- char **argv;
+ char *argv[];
{
stype[1] = "binary";
@@ -408,9 +271,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;
}
@@ -444,7 +307,7 @@ usage:
void
mput(argc, argv)
int argc;
- char **argv;
+ char *argv[];
{
int i;
sig_t oldintr;
@@ -459,12 +322,12 @@ mput(argc, argv)
mname = argv[0];
mflag = 1;
oldintr = signal(SIGINT, mabort);
- (void) setjmp(jabort);
+ (void)setjmp(jabort);
if (proxy) {
char *cp, *tp2, tmpbuf[MAXPATHLEN];
- while ((cp = remglob(argv,0)) != NULL) {
- if (*cp == 0) {
+ while ((cp = remglob(argv, 0, NULL)) != NULL) {
+ if (*cp == '\0') {
mflag = 0;
continue;
}
@@ -477,7 +340,7 @@ mput(argc, argv)
if (!*tp) {
tp = cp;
tp2 = tmpbuf;
- while ((*tp2 = *tp) != NULL) {
+ while ((*tp2 = *tp) != '\0') {
if (isupper(*tp2)) {
*tp2 = 'a' + *tp2 - 'A';
}
@@ -498,19 +361,19 @@ mput(argc, argv)
if (!mflag && fromatty) {
ointer = interactive;
interactive = 1;
- if (confirm("Continue with","mput")) {
+ if (confirm("Continue with", "mput")) {
mflag++;
}
interactive = ointer;
}
}
}
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
mflag = 0;
return;
}
for (i = 1; i < argc; i++) {
- char **cpp, **gargs;
+ char **cpp;
glob_t gl;
int flags;
@@ -523,7 +386,7 @@ mput(argc, argv)
if (!mflag && fromatty) {
ointer = interactive;
interactive = 1;
- if (confirm("Continue with","mput")) {
+ if (confirm("Continue with", "mput")) {
mflag++;
}
interactive = ointer;
@@ -548,7 +411,7 @@ mput(argc, argv)
if (!mflag && fromatty) {
ointer = interactive;
interactive = 1;
- if (confirm("Continue with","mput")) {
+ if (confirm("Continue with", "mput")) {
mflag++;
}
interactive = ointer;
@@ -557,7 +420,7 @@ mput(argc, argv)
}
globfree(&gl);
}
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
mflag = 0;
}
@@ -567,7 +430,7 @@ reget(argc, argv)
char *argv[];
{
- (void) getit(argc, argv, 1, "r+w");
+ (void)getit(argc, argv, 1, "r+w");
}
void
@@ -576,7 +439,7 @@ get(argc, argv)
char *argv[];
{
- (void) getit(argc, argv, 0, restart_point ? "r+w" : "w" );
+ (void)getit(argc, argv, 0, restart_point ? "r+w" : "w" );
}
/*
@@ -586,8 +449,8 @@ int
getit(argc, argv, restartit, mode)
int argc;
char *argv[];
- char *mode;
int restartit;
+ const char *mode;
{
int loc = 0;
char *oldargv1, *oldargv2;
@@ -599,7 +462,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;
@@ -620,7 +483,7 @@ usage:
if (!*tp) {
tp = argv[2];
tp2 = tmpbuf;
- while ((*tp2 = *tp) != NULL) {
+ while ((*tp2 = *tp) != '\0') {
if (isupper(*tp2)) {
*tp2 = 'a' + *tp2 - 'A';
}
@@ -647,38 +510,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);
}
}
}
@@ -694,21 +532,26 @@ void
mabort(signo)
int signo;
{
- int ointer;
+ int ointer, oconf;
- printf("\n");
- (void) fflush(stdout);
+ alarmtimer(0);
+ putchar('\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);
}
/*
@@ -717,7 +560,7 @@ mabort(signo)
void
mget(argc, argv)
int argc;
- char **argv;
+ char *argv[];
{
sig_t oldintr;
int ch, ointer;
@@ -731,8 +574,8 @@ mget(argc, argv)
mname = argv[0];
mflag = 1;
oldintr = signal(SIGINT, mabort);
- (void) setjmp(jabort);
- while ((cp = remglob(argv,proxy)) != NULL) {
+ (void)setjmp(jabort);
+ while ((cp = remglob(argv, proxy, NULL)) != NULL) {
if (*cp == '\0') {
mflag = 0;
continue;
@@ -740,7 +583,7 @@ mget(argc, argv)
if (mflag && confirm(argv[0], cp)) {
tp = cp;
if (mcase) {
- for (tp2 = tmpbuf; ch = *tp++;)
+ for (tp2 = tmpbuf; (ch = *tp++) != 0; )
*tp2++ = isupper(ch) ? tolower(ch) : ch;
*tp2 = '\0';
tp = tmpbuf;
@@ -756,79 +599,18 @@ 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;
- 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_TMP);
- (void) mktemp(temp);
- 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;
{
@@ -848,51 +630,84 @@ status(argc, argv)
int i;
if (connected)
- printf("Connected to %s.\n", hostname);
+ printf("Connected %sto %s.\n",
+ connected == -1 ? "and logged in" : "", hostname);
else
- printf("Not connected.\n");
+ puts("Not connected.");
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");
+ puts("No proxy connection.");
}
pswitch(0);
}
- printf("Mode: %s; Type: %s; Form: %s; Structure: %s\n",
+ 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),
+ 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");
+ puts("Ntrans: off.");
}
if (mapflag) {
printf("Nmap: (in) %s (out) %s\n", mapin, mapout);
}
else {
- printf("Nmap: off\n");
- }
- printf("Hash mark printing: %s; Use of PORT cmds: %s\n",
- onoff(hash), onoff(sendport));
+ puts("Nmap: off.");
+ }
+ 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 SMALL
+ printf("Command line editing: %s.\n", onoff(editing));
+#endif /* !SMALL */
if (macnum > 0) {
- printf("Macros:\n");
+ puts("Macros:");
for (i=0; i<macnum; i++) {
- printf("\t%s\n",macros[i].mac_name);
+ printf("\t%s\n", macros[i].mac_name);
}
}
code = 0;
}
/*
+ * Toggle a variable
+ */
+int
+togglevar(argc, argv, var, mesg)
+ int argc;
+ char *argv[];
+ int *var;
+ const char *mesg;
+{
+ if (argc < 2) {
+ *var = !*var;
+ } else if (argc == 2 && strcasecmp(argv[1], "on") == 0) {
+ *var = 1;
+ } else if (argc == 2 && strcasecmp(argv[1], "off") == 0) {
+ *var = 0;
+ } else {
+ printf("usage: %s [ on | off ]\n", argv[0]);
+ return (-1);
+ }
+ if (mesg)
+ printf("%s %s.\n", mesg, onoff(*var));
+ return (*var);
+}
+
+/*
* Set beep on cmd completed mode.
*/
/*VARARGS*/
@@ -902,11 +717,25 @@ setbell(argc, argv)
char *argv[];
{
- bell = !bell;
- printf("Bell mode %s.\n", onoff(bell));
- code = bell;
+ code = togglevar(argc, argv, &bell, "Bell mode");
}
+#ifndef SMALL
+/*
+ * Set command line editing
+ */
+/*VARARGS*/
+void
+setedit(argc, argv)
+ int argc;
+ char *argv[];
+{
+
+ code = togglevar(argc, argv, &editing, "Editing mode");
+ controlediting();
+}
+#endif /* !SMALL */
+
/*
* Turn on packet tracing.
*/
@@ -917,13 +746,11 @@ settrace(argc, argv)
char *argv[];
{
- trace = !trace;
- printf("Packet tracing %s.\n", onoff(trace));
- code = trace;
+ code = togglevar(argc, argv, &trace, "Packet tracing");
}
/*
- * Toggle hash mark printing during transfers.
+ * Toggle hash mark printing during transfers, or set hash mark bytecount.
*/
/*VARARGS*/
void
@@ -931,13 +758,31 @@ sethash(argc, argv)
int argc;
char *argv[];
{
-
- hash = !hash;
+ if (argc == 1)
+ hash = !hash;
+ else if (argc != 2) {
+ printf("usage: %s [ on | off | bytecount ]\n", argv[0]);
+ code = -1;
+ return;
+ } else if (strcasecmp(argv[1], "on") == 0)
+ hash = 1;
+ else if (strcasecmp(argv[1], "off") == 0)
+ hash = 0;
+ else {
+ int nmark = atol(argv[1]);
+ if (nmark < 1) {
+ printf("%s: bad bytecount value.\n", argv[1]);
+ code = -1;
+ return;
+ }
+ mark = nmark;
+ hash = 1;
+ }
printf("Hash mark printing %s", onoff(hash));
- code = hash;
if (hash)
- printf(" (%d bytes/hash mark)", 1024);
- printf(".\n");
+ printf(" (%d bytes/hash mark)", mark);
+ puts(".");
+ code = hash;
}
/*
@@ -950,9 +795,7 @@ setverbose(argc, argv)
char *argv[];
{
- verbose = !verbose;
- printf("Verbose mode %s.\n", onoff(verbose));
- code = verbose;
+ code = togglevar(argc, argv, &verbose, "Verbose mode");
}
/*
@@ -965,9 +808,20 @@ setport(argc, argv)
char *argv[];
{
- sendport = !sendport;
- printf("Use of PORT cmds %s.\n", onoff(sendport));
- code = sendport;
+ code = togglevar(argc, argv, &sendport, "Use of PORT cmds");
+}
+
+/*
+ * Toggle transfer progress bar.
+ */
+/*VARARGS*/
+void
+setprogress(argc, argv)
+ int argc;
+ char *argv[];
+{
+
+ code = togglevar(argc, argv, &progress, "Progress bar");
}
/*
@@ -981,9 +835,7 @@ setprompt(argc, argv)
char *argv[];
{
- interactive = !interactive;
- printf("Interactive mode %s.\n", onoff(interactive));
- code = interactive;
+ code = togglevar(argc, argv, &interactive, "Interactive mode");
}
/*
@@ -997,9 +849,20 @@ setglob(argc, argv)
char *argv[];
{
- doglob = !doglob;
- printf("Globbing %s.\n", onoff(doglob));
- code = doglob;
+ code = togglevar(argc, argv, &doglob, "Globbing");
+}
+
+/*
+ * Toggle preserving modification times on retreived files.
+ */
+/*VARARGS*/
+void
+setpreserve(argc, argv)
+ int argc;
+ char *argv[];
+{
+
+ code = togglevar(argc, argv, &preserve, "Preserve modification times");
}
/*
@@ -1014,16 +877,26 @@ setdebug(argc, argv)
{
int val;
- if (argc > 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
@@ -1041,17 +914,22 @@ 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;
}
- if (command("CWD %s", argv[1]) == ERROR && code == 500) {
+ r = command("CWD %s", argv[1]);
+ if (r == ERROR && code == 500) {
if (verbose)
- printf("CWD command not recognized, trying XCWD\n");
- (void) command("XCWD %s", argv[1]);
+ puts("CWD command not recognized, trying XCWD.");
+ r = command("XCWD %s", argv[1]);
}
+ if (r == COMPLETE)
+ dirchange = 1;
}
/*
@@ -1081,10 +959,10 @@ lcd(argc, argv)
code = -1;
return;
}
- if (getwd(buf) != NULL)
+ if (getcwd(buf, sizeof(buf)) != NULL)
printf("Local directory now %s\n", buf);
else
- warnx("getwd: %s", buf);
+ warn("getcwd: %s", argv[1]);
code = 0;
}
@@ -1097,12 +975,12 @@ 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;
}
- (void) command("DELE %s", argv[1]);
+ (void)command("DELE %s", argv[1]);
}
/*
@@ -1111,7 +989,7 @@ delete(argc, argv)
void
mdelete(argc, argv)
int argc;
- char **argv;
+ char *argv[];
{
sig_t oldintr;
int ointer;
@@ -1125,14 +1003,14 @@ mdelete(argc, argv)
mname = argv[0];
mflag = 1;
oldintr = signal(SIGINT, mabort);
- (void) setjmp(jabort);
- while ((cp = remglob(argv,0)) != NULL) {
+ (void)setjmp(jabort);
+ while ((cp = remglob(argv, 0, NULL)) != NULL) {
if (*cp == '\0') {
mflag = 0;
continue;
}
if (mflag && confirm(argv[0], cp)) {
- (void) command("DELE %s", cp);
+ (void)command("DELE %s", cp);
if (!mflag && fromatty) {
ointer = interactive;
interactive = 1;
@@ -1143,7 +1021,7 @@ mdelete(argc, argv)
}
}
}
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
mflag = 0;
}
@@ -1158,14 +1036,14 @@ 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]);
+ printf("usage: %s from-name to-name\n", argv[0]);
code = -1;
return;
}
if (command("RNFR %s", argv[1]) == CONTINUE)
- (void) command("RNTO %s", argv[2]);
+ (void)command("RNTO %s", argv[2]);
}
/*
@@ -1177,7 +1055,7 @@ ls(argc, argv)
int argc;
char *argv[];
{
- char *cmd;
+ const char *cmd;
if (argc < 2)
argc++, argv[1] = NULL;
@@ -1188,17 +1066,21 @@ 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;
}
recvrequest(cmd, argv[2], argv[1], "w", 0);
+
+ /* flush results in case commands are coming from a pipe */
+ fflush(stdout);
}
/*
@@ -1208,11 +1090,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;
@@ -1230,11 +1113,11 @@ 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);
- (void) setjmp(jabort);
+ (void)setjmp(jabort);
for (i = 1; mflag && i < argc-1; ++i) {
*mode = (i == 1) ? 'w' : 'a';
recvrequest(cmd, dest, argv[i], mode, 0);
@@ -1247,7 +1130,7 @@ usage:
interactive = ointer;
}
}
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
mflag = 0;
}
@@ -1258,39 +1141,40 @@ 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);
old2 = signal (SIGQUIT, SIG_IGN);
if ((pid = fork()) == 0) {
for (pid = 3; pid < 20; pid++)
- (void) close(pid);
- (void) signal(SIGINT, SIG_DFL);
- (void) signal(SIGQUIT, SIG_DFL);
+ (void)close(pid);
+ (void)signal(SIGINT, SIG_DFL);
+ (void)signal(SIGQUIT, SIG_DFL);
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) - 2);
+ shellnam[sizeof(shellnam) - 1] = '\0';
if (strcmp(namep, "sh") != 0)
shellnam[0] = '+';
if (debug) {
- printf ("%s\n", shell);
- (void) fflush (stdout);
+ puts(shell);
+ (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;
@@ -1299,10 +1183,10 @@ shell(argc, argv)
if (pid > 0)
while (wait((int *)&status) != pid)
;
- (void) signal(SIGINT, old1);
- (void) signal(SIGQUIT, old2);
+ (void)signal(SIGINT, old1);
+ (void)signal(SIGQUIT, old2);
if (pid == -1) {
- warn("%s", "Try again later");
+ warn("Try again later");
code = -1;
}
else {
@@ -1316,13 +1200,13 @@ shell(argc, argv)
void
user(argc, argv)
int argc;
- char **argv;
+ char *argv[];
{
char acct[80];
int n, aflag = 0;
if (argc < 2)
- (void) another(&argc, &argv, "username");
+ (void)another(&argc, &argv, "username");
if (argc < 2 || argc > 4) {
printf("usage: %s username [password] [account]\n", argv[0]);
code = -1;
@@ -1336,8 +1220,9 @@ user(argc, argv)
}
if (n == CONTINUE) {
if (argc < 4) {
- printf("Account: "); (void) fflush(stdout);
- (void) fgets(acct, sizeof(acct) - 1, stdin);
+ (void)fputs("Account: ", stdout);
+ (void)fflush(stdout);
+ (void)fgets(acct, sizeof(acct) - 1, stdin);
acct[strlen(acct) - 1] = '\0';
argv[3] = acct; argc++;
}
@@ -1345,16 +1230,17 @@ user(argc, argv)
aflag++;
}
if (n != COMPLETE) {
- fprintf(stdout, "Login failed.\n");
+ puts("Login failed.");
return;
}
if (!aflag && argc == 4) {
- (void) command("ACCT %s", argv[3]);
+ (void)command("ACCT %s", argv[3]);
}
+ connected = -1;
}
/*
- * Print working directory.
+ * Print working directory on remote machine.
*/
/*VARARGS*/
void
@@ -1369,13 +1255,30 @@ pwd(argc, argv)
*/
verbose = 1;
if (command("PWD") == ERROR && code == 500) {
- printf("PWD command not recognized, trying XPWD\n");
- (void) command("XPWD");
+ puts("PWD command not recognized, trying XPWD.");
+ (void)command("XPWD");
}
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.
*/
void
@@ -1384,15 +1287,16 @@ 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;
}
if (command("MKD %s", argv[1]) == ERROR && code == 500) {
if (verbose)
- printf("MKD command not recognized, trying XMKD\n");
- (void) command("XMKD %s", argv[1]);
+ puts("MKD command not recognized, trying XMKD.");
+ (void)command("XMKD %s", argv[1]);
}
}
@@ -1405,15 +1309,16 @@ 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;
}
if (command("RMD %s", argv[1]) == ERROR && code == 500) {
if (verbose)
- printf("RMD command not recognized, trying XRMD\n");
- (void) command("XRMD %s", argv[1]);
+ puts("RMD command not recognized, trying XRMD.");
+ (void)command("XRMD %s", argv[1]);
}
}
@@ -1459,20 +1364,23 @@ 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) - 1);
+ buf[sizeof(buf) - 1] = '\0';
if (argc > 1) {
len = strlen(buf);
- len += strlen(strcpy(&buf[len], argv[1]));
- for (i = 2; i < argc; i++) {
+ len += strlen(strncpy(&buf[len], argv[1],
+ sizeof(buf) - len - 1));
+ for (i = 2; i < argc && len < sizeof(buf); i++) {
buf[len++] = ' ';
- len += strlen(strcpy(&buf[len], argv[i]));
+ len += strlen(strncpy(&buf[len], argv[i],
+ sizeof(buf) - len) - 1);
}
}
if (command(buf) == PRELIM) {
@@ -1489,13 +1397,13 @@ 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;
return;
}
- (void) command("SITE CHMOD %s %s", argv[1], argv[2]);
+ (void)command("SITE CHMOD %s %s", argv[1], argv[2]);
}
void
@@ -1506,7 +1414,7 @@ do_umask(argc, argv)
int oldverbose = verbose;
verbose = 1;
- (void) command(argc == 1 ? "SITE UMASK" : "SITE UMASK %s", argv[1]);
+ (void)command(argc == 1 ? "SITE UMASK" : "SITE UMASK %s", argv[1]);
verbose = oldverbose;
}
@@ -1518,7 +1426,7 @@ idle(argc, argv)
int oldverbose = verbose;
verbose = 1;
- (void) command(argc == 1 ? "SITE IDLE" : "SITE IDLE %s", argv[1]);
+ (void)command(argc == 1 ? "SITE IDLE" : "SITE IDLE %s", argv[1]);
verbose = oldverbose;
}
@@ -1533,7 +1441,7 @@ rmthelp(argc, argv)
int oldverbose = verbose;
verbose = 1;
- (void) command(argc == 1 ? "HELP" : "HELP %s", argv[1]);
+ (void)command(argc == 1 ? "HELP" : "HELP %s", argv[1]);
verbose = oldverbose;
}
@@ -1567,9 +1475,9 @@ disconnect(argc, argv)
if (!connected)
return;
- (void) command("QUIT");
+ (void)command("QUIT");
if (cout) {
- (void) fclose(cout);
+ (void)fclose(cout);
}
cout = NULL;
connected = 0;
@@ -1579,89 +1487,33 @@ 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);
+ (void)command("ACCT %s", ap);
}
jmp_buf abortprox;
void
-proxabort()
+proxabort(notused)
+ int notused;
{
+ alarmtimer(0);
if (!proxy) {
pswitch(1);
}
@@ -1672,7 +1524,7 @@ proxabort()
proxflag = 0;
}
pswitch(0);
- longjmp(abortprox,1);
+ longjmp(abortprox, 1);
}
void
@@ -1681,6 +1533,7 @@ doproxy(argc, argv)
char *argv[];
{
struct cmd *c;
+ int cmdpos;
sig_t oldintr;
if (argc < 2 && !another(&argc, &argv, "command")) {
@@ -1690,20 +1543,20 @@ doproxy(argc, argv)
}
c = getcmd(argv[1]);
if (c == (struct cmd *) -1) {
- printf("?Ambiguous command\n");
- (void) fflush(stdout);
+ puts("?Ambiguous command.");
+ (void)fflush(stdout);
code = -1;
return;
}
if (c == 0) {
- printf("?Invalid command\n");
- (void) fflush(stdout);
+ puts("?Invalid command.");
+ (void)fflush(stdout);
code = -1;
return;
}
if (!c->c_proxy) {
- printf("?Invalid proxy command\n");
- (void) fflush(stdout);
+ puts("?Invalid proxy command.");
+ (void)fflush(stdout);
code = -1;
return;
}
@@ -1714,13 +1567,16 @@ doproxy(argc, argv)
oldintr = signal(SIGINT, proxabort);
pswitch(1);
if (c->c_conn && !connected) {
- printf("Not connected\n");
- (void) fflush(stdout);
+ puts("Not connected.");
+ (void)fflush(stdout);
pswitch(0);
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
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;
@@ -1729,7 +1585,7 @@ doproxy(argc, argv)
proxflag = 0;
}
pswitch(0);
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
}
void
@@ -1738,9 +1594,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
@@ -1749,32 +1603,30 @@ 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[];
{
if (argc == 1) {
ntflag = 0;
- printf("Ntrans off.\n");
+ puts("Ntrans off.");
code = ntflag;
return;
}
ntflag++;
code = ntflag;
- (void) strncpy(ntin, argv[1], 16);
- ntin[16] = '\0';
+ (void)strncpy(ntin, argv[1], sizeof(ntin) - 1);
+ ntin[sizeof(ntin) - 1] = '\0';
if (argc == 2) {
ntout[0] = '\0';
return;
}
- (void) strncpy(ntout, argv[2], 16);
- ntout[16] = '\0';
+ (void)strncpy(ntout, argv[2], sizeof(ntout) - 1);
+ ntout[sizeof(ntout) - 1] = '\0';
}
char *
@@ -1815,12 +1667,12 @@ setnmap(argc, argv)
if (argc == 1) {
mapflag = 0;
- printf("Nmap off.\n");
+ puts("Nmap off.");
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;
}
@@ -1834,10 +1686,10 @@ setnmap(argc, argv)
cp = strchr(altarg, ' ');
}
*cp = '\0';
- (void) strncpy(mapin, altarg, MAXPATHLEN - 1);
+ (void)strncpy(mapin, altarg, MAXPATHLEN - 1);
while (*++cp == ' ')
continue;
- (void) strncpy(mapout, cp, MAXPATHLEN - 1);
+ (void)strncpy(mapout, cp, MAXPATHLEN - 1);
}
char *
@@ -1950,7 +1802,8 @@ LOOP:
}
}
if (!*cp2) {
- printf("nmap: unbalanced brackets\n");
+ puts(
+"nmap: unbalanced brackets.");
return (name);
}
match = 1;
@@ -1963,7 +1816,8 @@ LOOP:
}
}
if (!*cp2) {
- printf("nmap: unbalanced brackets\n");
+ puts(
+"nmap: unbalanced brackets.");
return (name);
}
break;
@@ -2011,14 +1865,22 @@ LOOP:
}
void
+setpassive(argc, argv)
+ int argc;
+ char *argv[];
+{
+
+ code = togglevar(argc, argv, &passivemode,
+ verbose ? "Passive mode" : NULL);
+}
+
+void
setsunique(argc, argv)
int argc;
char *argv[];
{
- sunique = !sunique;
- printf("Store unique %s.\n", onoff(sunique));
- code = sunique;
+ code = togglevar(argc, argv, &sunique, "Store unique");
}
void
@@ -2027,23 +1889,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");
+ puts("CDUP command not recognized, trying XCUP.");
+ r = command("XCUP");
}
+ if (r == COMPLETE)
+ dirchange = 1;
}
/* restart transfer at specific point */
@@ -2054,11 +1918,11 @@ restart(argc, argv)
{
if (argc != 2)
- printf("restart: offset not specified\n");
+ puts("restart: offset not specified.");
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);
}
}
@@ -2069,7 +1933,7 @@ syst(argc, argv)
char *argv[];
{
- (void) command("SYST");
+ (void)command("SYST");
}
void
@@ -2081,29 +1945,29 @@ macdef(argc, argv)
int c;
if (macnum == 16) {
- printf("Limit of 16 macros have already been defined\n");
+ puts("Limit of 16 macros have already been defined.");
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");
- }
- (void) strncpy(macros[macnum].mac_name, argv[1], 8);
- if (macnum == 0) {
+ if (interactive)
+ puts(
+"Enter macro line by line, terminating it with a null line.");
+ (void)strncpy(macros[macnum].mac_name, argv[1],
+ sizeof(macros[macnum].mac_name) - 1);
+ macros[macnum].mac_name[sizeof(macros[macnum].mac_name) - 1] = '\0';
+ if (macnum == 0)
macros[macnum].mac_start = macbuf;
- }
- else {
+ else
macros[macnum].mac_start = macros[macnum - 1].mac_end + 1;
- }
tmp = macros[macnum].mac_start;
while (tmp != macbuf+4096) {
if ((c = getchar()) == EOF) {
- printf("macdef:end of file encountered\n");
+ puts("macdef: end of file encountered.");
code = -1;
return;
}
@@ -2126,7 +1990,7 @@ macdef(argc, argv)
while ((c = getchar()) != '\n' && c != EOF)
/* LOOP */;
if (c == EOF || getchar() == '\n') {
- printf("Macro not defined - 4k buffer exceeded\n");
+ puts("Macro not defined - 4K buffer exceeded.");
code = -1;
return;
}
@@ -2134,44 +1998,36 @@ macdef(argc, argv)
}
/*
- * Start up passive mode interaction
- */
-void
-setpassive()
-{
- passivemode = !passivemode;
- printf("Passive mode %s.\n", onoff(passivemode));
- code = passivemode;
-}
-
-/*
* Restrict FTP data port range to a high group of "safe" ports
*/
void
-setrestrict()
+setrestrict(argc, argv)
+ int argc;
+ char *argv[];
{
- restricted_data_ports = !restricted_data_ports;
- printf("Data port range restrictions %s.\n",
- onoff(restricted_data_ports));
- code = restricted_data_ports;
+ code = togglevar(argc, argv, &restricted_data_ports,
+ verbose ? "Restricted data ports" : NULL);
}
/*
* get size of file on remote machine
*/
-/*VARARGS*/
void
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;
}
/*
@@ -2182,30 +2038,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)
@@ -2213,7 +2060,7 @@ rmtstatus(argc, argv)
char *argv[];
{
- (void) command(argc > 1 ? "STAT %s" : "STAT" , argv[1]);
+ (void)command(argc > 1 ? "STAT %s" : "STAT" , argv[1]);
}
/*
@@ -2226,6 +2073,44 @@ newer(argc, argv)
{
if (getit(argc, argv, -1, "w"))
- printf("Local file \"%s\" is newer than remote file \"%s\"\n",
+ printf("Local file \"%s\" is newer than remote file \"%s\".\n",
argv[2], argv[1]);
}
+
+/*
+ * Display one file through $PAGER (defaults to "more").
+ */
+void
+page(argc, argv)
+ int argc;
+ char *argv[];
+{
+ int orestart_point, ohash, overbose;
+ char *p, *pager;
+
+ if ((argc < 2 && !another(&argc, &argv, "filename")) || argc > 2) {
+ printf("usage: %s filename\n", argv[0]);
+ code = -1;
+ return;
+ }
+ if (!globulize(&argv[1])) {
+ code = -1;
+ return;
+ }
+ p = getenv("PAGER");
+ if (p == NULL)
+ p = PAGER;
+ if ((pager = malloc(strlen(p) + 2)) == NULL)
+ errx(1, "Can't allocate memory for $PAGER");
+ (void)sprintf(pager, "|%s", p);
+
+ orestart_point = restart_point;
+ ohash = hash;
+ overbose = verbose;
+ restart_point = hash = verbose = 0;
+ recvrequest("RETR", pager, argv[1], "r+w", 1);
+ (void)free(pager);
+ restart_point = orestart_point;
+ hash = ohash;
+ verbose = overbose;
+}
diff --git a/usr.bin/ftp/cmdtab.c b/usr.bin/ftp/cmdtab.c
index 6c8f001..f67eeb0 100644
--- a/usr.bin/ftp/cmdtab.c
+++ b/usr.bin/ftp/cmdtab.c
@@ -1,3 +1,6 @@
+/* $Id$ */
+/* $NetBSD: cmdtab.c,v 1.15 1997/04/05 03:27:33 lukem Exp $ */
+
/*
* Copyright (c) 1985, 1989, 1993, 1994
* The Regents of the University of California. All rights reserved.
@@ -32,7 +35,11 @@
*/
#ifndef lint
-static char sccsid[] = "@(#)cmdtab.c 8.3 (Berkeley) 4/2/94";
+#if 0
+static char sccsid[] = "@(#)cmdtab.c 8.4 (Berkeley) 10/9/94";
+#else
+static char rcsid[] = "$Id$";
+#endif
#endif /* not lint */
#include <stdio.h>
@@ -49,21 +56,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 tftp";
+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 SMALL
+char edithelp[] = "toggle command line editing";
+#endif /* !SMALL */
char formhelp[] = "set file transfer format";
char globhelp[] = "toggle metacharacter expansion of local file names";
-char hashhelp[] = "toggle printing `#' for each buffer transferred";
+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";
@@ -71,15 +82,19 @@ 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 pagehelp[] = "view a remote file through your pager";
+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";
@@ -89,15 +104,17 @@ 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 restricthelp[]= "toggle restriction of data port range";
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 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";
@@ -110,82 +127,101 @@ char umaskhelp[] = "get (set) umask on remote side";
char userhelp[] = "send new user information";
char verbosehelp[] = "toggle verbose mode";
+#ifdef SMALL
+#define CMPL(x)
+#define CMPL0
+#else /* !SMALL */
+#define CMPL(x) __STRING(x),
+#define CMPL0 "",
+#endif /* !SMALL */
+
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 },
- { "ftp", connecthelp, 0, 0, 1, setpeer },
- { "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 },
- { "prompt", prompthelp, 0, 0, 0, setprompt },
- { "passive", passivehelp, 0, 0, 0, setpassive },
- { "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 },
- { "restrict", restricthelp, 0, 0, 0, setrestrict },
- { "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 SMALL
+ { "edit", edithelp, 0, 0, 0, CMPL0 setedit },
+#endif /* !SMALL */
+ { "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 },
+ { "less", pagehelp, 1, 1, 1, CMPL(r) page },
+ { "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 },
+ { "more", pagehelp, 1, 1, 1, CMPL(r) page },
+ { "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 },
+ { "page", pagehelp, 1, 1, 1, CMPL(r) page },
+ { "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 },
+ { "restrict", restricthelp, 0, 0, 0, CMPL0 setrestrict },
+ { "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 },
};
-int NCMDS = (sizeof (cmdtab) / sizeof (cmdtab[0])) - 1;
+int NCMDS = (sizeof(cmdtab) / sizeof(cmdtab[0])) - 1;
diff --git a/usr.bin/ftp/complete.c b/usr.bin/ftp/complete.c
new file mode 100644
index 0000000..e20c27c
--- /dev/null
+++ b/usr.bin/ftp/complete.c
@@ -0,0 +1,367 @@
+/* $Id$ */
+/* $NetBSD: complete.c,v 1.8 1997/05/24 16:34:30 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 SMALL
+#ifndef lint
+static char rcsid[] = "$Id$";
+#endif /* not lint */
+
+/*
+ * FTP user program - command and file completion routines
+ */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <err.h>
+#include <dirent.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#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];
+ 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 */
+ (void)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) {
+ (void)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];
+ char *file;
+ DIR *dd;
+ struct dirent *dp;
+ unsigned char rv;
+
+ if ((file = strrchr(word, '/')) == NULL) {
+ dir[0] = '.';
+ dir[1] = '\0';
+ file = word;
+ } else {
+ if (file == word) {
+ dir[0] = '/';
+ dir[1] = '\0';
+ } else {
+ (void)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];
+ StringList *words;
+ char dir[MAXPATHLEN];
+ char *file, *cp;
+ int i;
+ unsigned char rv;
+
+ char *dummyargv[] = { "complete", dir, NULL };
+
+ if ((file = strrchr(word, '/')) == NULL) {
+ dir[0] = '.';
+ dir[1] = '\0';
+ file = word;
+ } else {
+ cp = file;
+ while (*cp == '/' && cp > word)
+ cp--;
+ (void)strncpy(dir, word, cp - word + 1);
+ dir[cp - word + 1] = '\0';
+ file++;
+ }
+
+ if (dirchange || strcmp(dir, lastdir) != 0) { /* dir not cached */
+ char *emesg;
+
+ if (dirlist != NULL)
+ sl_free(dirlist, 1);
+ dirlist = sl_init();
+
+ mflag = 1;
+ emesg = NULL;
+ while ((cp = remglob(dummyargv, 0, &emesg)) != NULL) {
+ char *tcp;
+
+ if (!mflag)
+ continue;
+ if (*cp == '\0') {
+ mflag = 0;
+ continue;
+ }
+ tcp = strrchr(cp, '/');
+ if (tcp)
+ tcp++;
+ else
+ tcp = cp;
+ tcp = strdup(tcp);
+ if (tcp == NULL)
+ errx(1, "Can't allocate memory for remote dir");
+ sl_add(dirlist, tcp);
+ }
+ if (emesg != NULL) {
+ printf("\n%s\n", emesg);
+ return (CC_REDISPLAY);
+ }
+ (void)strcpy(lastdir, dir);
+ dirchange = 0;
+ }
+
+ words = sl_init();
+ for (i = 0; i < dirlist->sl_cur; i++) {
+ cp = dirlist->sl_str[i];
+ if (strlen(file) > strlen(cp))
+ continue;
+ if (strncmp(file, cp, strlen(file)) == 0)
+ sl_add(words, cp);
+ }
+ 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);
+ (void)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
+ (void)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 != -1) {
+ puts("\nMust be logged in to complete.");
+ 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);
+}
+#endif
diff --git a/usr.bin/ftp/domacro.c b/usr.bin/ftp/domacro.c
index 9644487..cb42f7e 100644
--- a/usr.bin/ftp/domacro.c
+++ b/usr.bin/ftp/domacro.c
@@ -1,3 +1,6 @@
+/* $Id$ */
+/* $NetBSD: domacro.c,v 1.9 1997/03/13 06:23:14 lukem Exp $ */
+
/*
* Copyright (c) 1985, 1993, 1994
* The Regents of the University of California. All rights reserved.
@@ -32,13 +35,17 @@
*/
#ifndef lint
+#if 0
static char sccsid[] = "@(#)domacro.c 8.3 (Berkeley) 4/2/94";
+#else
+static char rcsid[] = "$Id$";
+#endif
#endif /* not lint */
#include <ctype.h>
#include <signal.h>
#include <stdio.h>
-#include <strings.h>
+#include <string.h>
#include "ftp_var.h"
@@ -52,7 +59,7 @@ domacro(argc, argv)
struct cmd *c;
if (argc < 2 && !another(&argc, &argv, "macro name")) {
- printf("Usage: %s macro_name.\n", argv[0]);
+ printf("usage: %s macro_name\n", argv[0]);
code = -1;
return;
}
@@ -66,7 +73,7 @@ domacro(argc, argv)
code = -1;
return;
}
- (void) strcpy(line2, line);
+ (void)strcpy(line2, line);
TOP:
cp1 = macros[i].mac_start;
while (cp1 != macros[i].mac_end) {
@@ -87,7 +94,7 @@ TOP:
}
cp1--;
if (argc - 2 >= j) {
- (void) strcpy(cp2, argv[j+1]);
+ (void)strcpy(cp2, argv[j+1]);
cp2 += strlen(argv[j+1]);
}
break;
@@ -96,7 +103,7 @@ TOP:
loopflg = 1;
cp1++;
if (count < argc) {
- (void) strcpy(cp2, argv[count]);
+ (void)strcpy(cp2, argv[count]);
cp2 += strlen(argv[count]);
}
break;
@@ -114,26 +121,25 @@ TOP:
makeargv();
c = getcmd(margv[0]);
if (c == (struct cmd *)-1) {
- printf("?Ambiguous command\n");
+ puts("?Ambiguous command.");
code = -1;
}
else if (c == 0) {
- printf("?Invalid command\n");
+ puts("?Invalid command.");
code = -1;
}
else if (c->c_conn && !connected) {
- printf("Not connected.\n");
+ puts("Not connected.");
code = -1;
}
else {
- if (verbose) {
- printf("%s\n",line);
- }
+ if (verbose)
+ puts(line);
(*c->c_handler)(margc, margv);
if (bell && c->c_bell) {
- (void) putchar('\007');
+ (void)putchar('\007');
}
- (void) strcpy(line, line2);
+ (void)strcpy(line, line2);
makeargv();
argc = margc;
argv = margv;
diff --git a/usr.bin/ftp/extern.h b/usr.bin/ftp/extern.h
index fb9aecf..3acc671 100644
--- a/usr.bin/ftp/extern.h
+++ b/usr.bin/ftp/extern.h
@@ -1,3 +1,6 @@
+/* $Id$ */
+/* $NetBSD: extern.h,v 1.15 1997/04/14 09:09:17 lukem Exp $ */
+
/*-
* Copyright (c) 1994 The Regents of the University of California.
* All rights reserved.
@@ -30,27 +33,33 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * @(#)extern.h 8.2 (Berkeley) 4/3/94
+ * @(#)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 abortpt __P((int));
+void abortrecv __P((int));
+void abortsend __P((int));
+void aborthttp __P((int));
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 **));
void cdup __P((int, char **));
void changetype __P((int, int));
-void cmdabort __P(());
+void cmdabort __P((int));
void cmdscanner __P((int));
-int command __P(());
-int confirm __P((char *, char *));
-FILE *dataconn __P((char *));
+int command __P((const char *, ...));
+#ifndef SMALL
+unsigned char complete __P((EditLine *, int));
+void controlediting __P((void));
+#endif /* !SMALL */
+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 **));
@@ -60,21 +69,22 @@ 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 intr __P((void));
+void list_vertical __P((StringList *));
void lcd __P((int, char **));
-int login __P((char *));
-void lostpeer __P(());
+int login __P((const char *, char *, char *));
+void lostpeer __P((void));
+void lpwd __P((int, char **));
void ls __P((int, char **));
void mabort __P((int));
void macdef __P((int, char **));
@@ -87,49 +97,60 @@ void modtime __P((int, char **));
void mput __P((int, char **));
char *onoff __P((int));
void newer __P((int, char **));
-void proxabort __P(());
-void proxtrans __P((char *, char *, char *));
-void psabort __P(());
+void page __P((int, char **));
+void progressmeter __P((int));
+char *prompt __P((void));
+void proxabort __P((int));
+void proxtrans __P((const char *, const char *, const char *));
+void psabort __P((int));
+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));
+char *remglob __P((char **, int, char **));
+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 **));
void sethash __P((int, char **));
void setnmap __P((int, char **));
void setntrans __P((int, char **));
-void setpassive __P(());
+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 setrestrict __P(());
+void setrestrict __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 **));
@@ -138,13 +159,15 @@ void sizecmd __P((int, char **));
char *slurpstring __P((void));
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));
void user __P((int, 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;
@@ -152,3 +175,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 0000000..5774e77
--- /dev/null
+++ b/usr.bin/ftp/fetch.c
@@ -0,0 +1,608 @@
+/* $Id$ */
+/* $NetBSD: fetch.c,v 1.10 1997/05/23 18:54:18 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[] = "$Id$";
+#endif /* not lint */
+
+/*
+ * FTP User Program -- Command line file retrieval
+ */
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+
+#include <netinet/in.h>
+
+#include <arpa/ftp.h>
+#include <arpa/inet.h>
+
+#include <ctype.h>
+#include <err.h>
+#include <netdb.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "ftp_var.h"
+
+#define FTP_URL "ftp://" /* ftp URL prefix */
+#define HTTP_URL "http://" /* http URL prefix */
+#define FTP_PROXY "ftp_proxy" /* env var with ftp proxy location */
+#define HTTP_PROXY "http_proxy" /* env var with http proxy location */
+
+
+#define EMPTYSTRING(x) ((x) == NULL || (*(x) == '\0'))
+
+jmp_buf httpabort;
+
+/*
+ * Retrieve URL, via the proxy in $proxyvar if necessary.
+ * Modifies the string argument given.
+ * Returns -1 on failure, 0 on success
+ */
+int
+url_get(origline, proxyenv)
+ const char *origline;
+ const char *proxyenv;
+{
+ struct sockaddr_in sin;
+ int i, out, port, s;
+ size_t buflen, len;
+ char c, *cp, *cp2, *savefile, *portnum, *path, buf[4096];
+ char *line, *proxy, *host;
+ sig_t oldintr;
+ off_t hashbytes;
+
+ s = -1;
+ proxy = NULL;
+
+ line = strdup(origline);
+ if (line == NULL)
+ errx(1, "Can't allocate memory to parse URL");
+ if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
+ host = line + sizeof(HTTP_URL) - 1;
+ else if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0)
+ host = line + sizeof(FTP_URL) - 1;
+ else
+ errx(1, "url_get: Invalid URL '%s'", line);
+
+ path = strchr(host, '/'); /* find path */
+ if (EMPTYSTRING(path)) {
+ warnx("Invalid URL: %s", origline);
+ goto cleanup_url_get;
+ }
+ *path++ = '\0';
+ if (EMPTYSTRING(path)) {
+ warnx("Invalid URL: %s", origline);
+ goto cleanup_url_get;
+ }
+
+ savefile = strrchr(path, '/'); /* find savefile */
+ if (savefile != NULL)
+ savefile++;
+ else
+ savefile = path;
+ if (EMPTYSTRING(savefile)) {
+ warnx("Invalid URL: %s", origline);
+ goto cleanup_url_get;
+ }
+
+ if (proxyenv != NULL) { /* use proxy */
+ proxy = strdup(proxyenv);
+ if (proxy == NULL)
+ errx(1, "Can't allocate memory for proxy URL.");
+ if (strncasecmp(proxy, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
+ host = proxy + sizeof(HTTP_URL) - 1;
+ else if (strncasecmp(proxy, FTP_URL, sizeof(FTP_URL) - 1) == 0)
+ host = proxy + sizeof(FTP_URL) - 1;
+ else {
+ warnx("Malformed proxy URL: %s", proxyenv);
+ goto cleanup_url_get;
+ }
+ if (EMPTYSTRING(host)) {
+ warnx("Malformed proxy URL: %s", proxyenv);
+ goto cleanup_url_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_url_get;
+ }
+ } else {
+ struct hostent *hp;
+
+ hp = gethostbyname(host);
+ if (hp == NULL) {
+ warnx("%s: %s", host, hstrerror(h_errno));
+ goto cleanup_url_get;
+ }
+ if (hp->h_addrtype != AF_INET) {
+ warnx("%s: not an Internet address?", host);
+ goto cleanup_url_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_url_get;
+ }
+ port = htons(port);
+ } else
+ port = httpport;
+ sin.sin_port = port;
+
+ s = socket(AF_INET, SOCK_STREAM, 0);
+ if (s == -1) {
+ warn("Can't create socket");
+ goto cleanup_url_get;
+ }
+
+ if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
+ warn("Can't connect to %s", host);
+ goto cleanup_url_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\n", origline);
+ else
+ printf("Requesting %s (via %s)\n", origline, 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("Writing HTTP request");
+ goto cleanup_url_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 == '\r')
+ continue;
+ 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_url_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 == '\r')
+ continue;
+ 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_url_get;
+ }
+
+ /* Trap signals */
+ oldintr = NULL;
+ if (setjmp(httpabort)) {
+ if (oldintr)
+ (void)signal(SIGINT, oldintr);
+ goto cleanup_url_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_url_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_url_get;
+ }
+ progressmeter(1);
+ if (verbose)
+ puts("Successfully retrieved file.");
+ (void)signal(SIGINT, oldintr);
+
+ close(s);
+ close(out);
+ if (proxy)
+ free(proxy);
+ free(line);
+ return (0);
+
+improper:
+ warnx("Improper response from %s", host);
+cleanup_url_get:
+ if (s != -1)
+ close(s);
+ if (proxy)
+ free(proxy);
+ free(line);
+ return (-1);
+}
+
+/*
+ * Abort a http retrieval
+ */
+void
+aborthttp(notused)
+ int notused;
+{
+
+ alarmtimer(0);
+ puts("\nhttp fetch aborted.");
+ (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;
+ char *user, *pass;
+ char *ftpproxy, *httpproxy;
+ int rval, xargc, argpos;
+ int dirhasglob, filehasglob;
+ char rempath[MAXPATHLEN];
+
+ argpos = 0;
+
+ if (setjmp(toplevel)) {
+ if (connected)
+ disconnect(0, NULL);
+ return (argpos + 1);
+ }
+ (void)signal(SIGINT, (sig_t)intr);
+ (void)signal(SIGPIPE, (sig_t)lostpeer);
+
+ ftpproxy = getenv(FTP_PROXY);
+ httpproxy = getenv(HTTP_PROXY);
+
+ /*
+ * 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 = user = pass = 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 (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0) {
+ if (url_get(line, httpproxy) == -1)
+ rval = argpos + 1;
+ continue;
+ }
+
+ /*
+ * Try FTP URL-style arguments next. If ftpproxy is
+ * set, use url_get() instead of standard ftp.
+ * Finally, try host:file.
+ */
+ host = line;
+ if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
+ if (ftpproxy) {
+ if (url_get(line, ftpproxy) == -1)
+ rval = argpos + 1;
+ continue;
+ }
+ host += sizeof(FTP_URL) - 1;
+ dir = strchr(host, '/');
+
+ /* look for [user:pass@]host[:port] */
+ pass = strpbrk(host, ":@/");
+ if (pass == NULL || *pass == '/') {
+ pass = NULL;
+ goto parsed_url;
+ }
+ if (pass == host || *pass == '@') {
+bad_ftp_url:
+ warnx("Invalid URL: %s", argv[argpos]);
+ rval = argpos + 1;
+ continue;
+ }
+ *pass++ = '\0';
+ cp = strpbrk(pass, ":@/");
+ if (cp == NULL || *cp == '/') {
+ portnum = pass;
+ pass = NULL;
+ goto parsed_url;
+ }
+ if (EMPTYSTRING(cp) || *cp == ':')
+ goto bad_ftp_url;
+ *cp++ = '\0';
+ user = host;
+ if (EMPTYSTRING(user))
+ goto bad_ftp_url;
+ host = cp;
+ portnum = strchr(host, ':');
+ if (portnum != NULL)
+ *portnum++ = '\0';
+parsed_url:
+ } else { /* classic style `host:file' */
+ dir = 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 (dir != NULL)
+ *dir++ = '\0';
+
+ /*
+ * Extract the file and (if present) directory name.
+ */
+ if (! EMPTYSTRING(dir)) {
+ cp = strrchr(dir, '/');
+ if (cp != NULL) {
+ *cp++ = '\0';
+ file = cp;
+ } else {
+ file = dir;
+ dir = NULL;
+ }
+ }
+ if (debug)
+ printf("user %s:%s host %s port %s dir %s file %s\n",
+ user, pass, host, portnum, dir, file);
+
+ /*
+ * Set up the connection if we don't have one.
+ */
+ if (strcmp(host, lasthost) != 0) {
+ int oautologin;
+
+ (void)strcpy(lasthost, host);
+ if (connected)
+ disconnect(0, NULL);
+ xargv[0] = __progname;
+ xargv[1] = host;
+ xargv[2] = NULL;
+ xargc = 2;
+ if (! EMPTYSTRING(portnum)) {
+ xargv[2] = portnum;
+ xargv[3] = NULL;
+ xargc = 3;
+ }
+ oautologin = autologin;
+ if (user != NULL)
+ autologin = 0;
+ setpeer(xargc, xargv);
+ autologin = oautologin;
+ if ((connected == 0)
+ || ((connected == 1) && !login(host, user, pass)) ) {
+ warnx("Can't connect or login to host `%s'",
+ host);
+ rval = argpos + 1;
+ continue;
+ }
+
+ /* Always use binary transfers. */
+ setbinary(0, NULL);
+ }
+ /* cd back to '/' */
+ xargv[0] = "cd";
+ xargv[1] = "/";
+ xargv[2] = NULL;
+ cd(2, xargv);
+ if (! dirchange) {
+ rval = argpos + 1;
+ continue;
+ }
+
+ dirhasglob = filehasglob = 0;
+ if (doglob) {
+ if (! EMPTYSTRING(dir) &&
+ strpbrk(dir, "*?[]{}") != NULL)
+ dirhasglob = 1;
+ if (! EMPTYSTRING(file) &&
+ strpbrk(file, "*?[]{}") != NULL)
+ filehasglob = 1;
+ }
+
+ /* Change directories, if necessary. */
+ if (! EMPTYSTRING(dir) && !dirhasglob) {
+ 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);
+
+ if (dirhasglob) {
+ snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
+ file = rempath;
+ }
+
+ /* Fetch the file(s). */
+ xargv[0] = "get";
+ xargv[1] = file;
+ xargv[2] = NULL;
+ if (dirhasglob || filehasglob) {
+ int ointeractive;
+
+ ointeractive = interactive;
+ interactive = 0;
+ xargv[0] = "mget";
+ mget(2, xargv);
+ interactive = ointeractive;
+ } else
+ get(2, xargv);
+
+ if ((code / 100) != COMPLETE)
+ 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 bc0b27f..c288d48 100644
--- a/usr.bin/ftp/ftp.1
+++ b/usr.bin/ftp/ftp.1
@@ -1,3 +1,6 @@
+.\" $Id$
+.\" $NetBSD: ftp.1,v 1.21 1997/06/10 21:59:58 lukem Exp $
+.\"
.\" Copyright (c) 1985, 1989, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
@@ -29,9 +32,9 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.\" @(#)ftp.1 8.2 (Berkeley) 12/30/93
+.\" @(#)ftp.1 8.3 (Berkeley) 10/9/94
.\"
-.Dd December 30, 1993
+.Dd February 23, 1997
.Dt FTP 1
.Os BSD 4.2
.Sh NAME
@@ -40,72 +43,108 @@
.Tn ARPANET
file transfer program
.Sh SYNOPSIS
-.Nm ftp
-.Op Fl v
+.Nm
+.Op Fl a
.Op Fl d
+.Op Fl e
+.Op Fl g
.Op Fl i
.Op Fl n
.Op Fl U
.Op Fl p
-.Op Fl g
-.Op Ar host
+.Op Fl P Ar port
+.Op Fl t
+.Op Fl v
+.Op Fl V
+.Op Ar host Op Ar port
+.Nm ftp
+ftp://[\fIuser\fR:\fIpassword\fR@]\fIhost\fR[:\fIport\fR]/\fIfile\fR[/]
+.Nm ftp
+http://\fIhost\fR[:\fIport\fR]/\fIfile\fR
+.Nm ftp
+\fIhost\fR:[/\fIpath\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.
+Refer to
+.Sx AUTO-FETCHING FILES
+below for more information.
+.Pp
Options may be specified at the command line, or to the
command interpreter.
-.Bl -tag -width flag
-.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 e
+Disables command line editing.
+.It Fl g
+Disables file name globbing.
+.It Fl U
+Disable data port range restrictions.
+.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 U
-Disable data port range restrictions.
.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.
@@ -163,7 +202,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
@@ -177,7 +216,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.
@@ -186,7 +225,7 @@ Change the permission modes of the file
.Ar file-name
on the remote
system to
-.Ar mode .
+.Ar mode .
.It Ic close
Terminate the
.Tn FTP
@@ -222,42 +261,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
@@ -284,7 +335,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
@@ -314,15 +365,18 @@ That can be done by
transferring a
.Xr tar 1
archive of the subtree (in binary mode).
-.It Ic hash
+.It Ic hash Op Ar size
Toggle hash-sign (``#'') printing for each data block
transferred.
-The size of a data block is 1024 bytes.
+The size of a data block defaults to 1024 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
@@ -337,36 +391,29 @@ 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 less Ar file
+A synonym for
+.Ic page .
+.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 macdefNs Ar macro-name
+.It Ic macdef Ar macro-name
Define a macro.
Subsequent lines are stored as the macro
.Ar macro-name ;
@@ -397,7 +444,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
@@ -427,13 +474,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
@@ -442,10 +489,13 @@ 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.
+.It Ic more Ar file
+A synonym for
+.Ic page .
.It Ic mput Ar local-files
Expand wild cards in the list of local files given as arguments
and do a
@@ -459,35 +509,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.
@@ -508,7 +543,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
@@ -518,7 +553,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
@@ -582,7 +617,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
@@ -595,18 +630,59 @@ 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
server (see below).
+.It Ic page Ar file
+Retrieve
+.Ic file
+and display with the program defined in
+.Ev PAGER
+(which defaults to
+.Xr less 1 ).
+.It Ic passive
+Toggle passive mode. If passive mode is turned on
+(default is off), the ftp client will
+send a
+.Dv PASV
+command for all data connections instead of the usual
+.Dv PORT
+command. The
+.Dv PASV
+command requests that the remote server open a port for the data connection
+and return the address of that port. The remote server listens on that
+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
+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.
+The progress bar will be disabled for a transfer that has
+.Ar local-file
+as
+.Sq Fl
+or a command that starts with
+.Sq \&| .
+Refer to
+.Sx FILE NAMING CONVENTIONS
+for more information.
.It Ic prompt
Toggle interactive prompting.
Interactive prompting
@@ -619,6 +695,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
@@ -667,19 +766,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
@@ -702,22 +802,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
@@ -731,12 +827,12 @@ or
.Ic put
at the
indicated
-.Ar marker .
+.Ar marker .
On
.Ux
systems, marker is usually a byte
offset into the file.
-.It Ic restrict
++.It Ic restrict
Toggle data port range restrictions.
When not operating in passive mode, the
.Nm ftp ,
@@ -769,13 +865,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
@@ -787,7 +884,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
@@ -812,7 +909,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
@@ -840,21 +937,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
@@ -862,7 +958,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
@@ -877,7 +973,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
@@ -895,11 +991,93 @@ 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 "host:/file"
+.It host:/file
+.Dq Classic
+ftp format
+.It ftp://[user:password@]host[:port]/file
+An ftp URL, retrieved using the ftp protocol if
+.Ev ftp_proxy
+isn't defined.
+Otherwise, transfer using http via the proxy defined in
+.Ev ftp_proxy .
+If
+.Ar user:password@
+is given and
+.Ev ftp_proxy
+isn't defined, login as
+.Ar user
+with a password of
+.Ar password .
+.It http://host[:port]/file
+An HTTP URL, retrieved 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.
+.Pp
+If
+.Ic file
+contains a glob character and globbing is enabled,
+(see
+.Ic glob ) ,
+then the equivalent of
+.Ic "mget file"
+is performed.
+.Pp
+If the directory component of
+.Ic file
+contains no globbing characters,
+it is stored in the current directory as the
+.Xr basename 1
+of
+.Ic file .
+Otherwise, the remote name is used as the local name.
.Sh ABORTING A FILE TRANSFER
To abort a file transfer, use the terminal interrupt key
(usually Ctrl-C).
@@ -919,18 +1097,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
@@ -946,7 +1124,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
@@ -955,7 +1133,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
@@ -965,7 +1143,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.
@@ -1010,19 +1188,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
@@ -1040,7 +1218,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.
@@ -1092,7 +1270,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
@@ -1107,7 +1285,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
@@ -1120,24 +1298,69 @@ 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 if input is from a tty.
+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 PAGER
+Used by
+.Ic page
+to display files.
.It Ev SHELL
For default shell.
+.It Ev ftp_proxy
+URL of FTP proxy to use when making FTP URL requests
+(if not defined, use the standard ftp protocol).
+.It Ev http_proxy
+URL of HTTP proxy to use when making HTTP URL 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.3
+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 f37798e..f3914c1 100644
--- a/usr.bin/ftp/ftp.c
+++ b/usr.bin/ftp/ftp.c
@@ -1,3 +1,6 @@
+/* $Id$ */
+/* $NetBSD: ftp.c,v 1.25 1997/04/14 09:09:22 lukem Exp $ */
+
/*
* Copyright (c) 1985, 1989, 1993, 1994
* The Regents of the University of California. All rights reserved.
@@ -32,15 +35,16 @@
*/
#ifndef lint
-static char sccsid[] = "@(#)ftp.c 8.4 (Berkeley) 4/6/94";
+#if 0
+static char sccsid[] = "@(#)ftp.c 8.6 (Berkeley) 10/27/94";
+#else
+static char rcsid[] = "$Id$";
+#endif
#endif /* not lint */
-#include <sys/param.h>
+#include <sys/types.h>
#include <sys/stat.h>
-#include <sys/ioctl.h>
#include <sys/socket.h>
-#include <sys/time.h>
-#include <sys/file.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
@@ -52,20 +56,19 @@ static char sccsid[] = "@(#)ftp.c 8.4 (Berkeley) 4/6/94";
#include <ctype.h>
#include <err.h>
#include <errno.h>
-#include <fcntl.h>
#include <netdb.h>
-#include <pwd.h>
-#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#ifdef __STDC__
+#include <stdarg.h>
+#else
#include <varargs.h>
+#endif
#include "ftp_var.h"
-extern int h_errno;
-
struct sockaddr_in hisctladdr;
struct sockaddr_in data_addr;
int data = -1;
@@ -81,18 +84,18 @@ 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));
- hisctladdr.sin_addr.s_addr = inet_addr(host);
- if (hisctladdr.sin_addr.s_addr != -1) {
+ memset((void *)&hisctladdr, 0, sizeof(hisctladdr));
+ if (inet_aton(host, &hisctladdr.sin_addr) != 0) {
hisctladdr.sin_family = AF_INET;
- (void) strncpy(hostnamebuf, host, sizeof(hostnamebuf));
+ (void)strncpy(hostnamebuf, host, sizeof(hostnamebuf) - 1);
+ hostnamebuf[sizeof(hostnamebuf) - 1] = '\0';
} else {
hp = gethostbyname(host);
if (hp == NULL) {
@@ -101,9 +104,9 @@ hookup(host, port)
return ((char *) 0);
}
hisctladdr.sin_family = hp->h_addrtype;
- memmove((caddr_t)&hisctladdr.sin_addr,
- hp->h_addr_list[0], hp->h_length);
- (void) strncpy(hostnamebuf, hp->h_name, sizeof(hostnamebuf));
+ memcpy(&hisctladdr.sin_addr, hp->h_addr, hp->h_length);
+ (void)strncpy(hostnamebuf, hp->h_name, sizeof(hostnamebuf) - 1);
+ hostnamebuf[sizeof(hostnamebuf) - 1] = '\0';
}
hostname = hostnamebuf;
s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
@@ -113,7 +116,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;
@@ -122,11 +126,10 @@ hookup(host, port)
errno = oerrno;
warn("connect to address %s", ia);
hp->h_addr_list++;
- memmove((caddr_t)&hisctladdr.sin_addr,
- hp->h_addr_list[0], hp->h_length);
- fprintf(stdout, "Trying %s...\n",
- inet_ntoa(hisctladdr.sin_addr));
- (void) close(s);
+ memcpy(&hisctladdr.sin_addr, hp->h_addr, hp->h_length);
+ printf("Trying %s...\n",
+ inet_ntoa(hisctladdr.sin_addr));
+ (void)close(s);
s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
if (s < 0) {
warn("socket");
@@ -139,7 +142,7 @@ hookup(host, port)
code = -1;
goto bad;
}
- len = sizeof (myctladdr);
+ len = sizeof(myctladdr);
if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
warn("getsockname");
code = -1;
@@ -155,9 +158,9 @@ hookup(host, port)
if (cin == NULL || cout == NULL) {
warnx("fdopen failed.");
if (cin)
- (void) fclose(cin);
+ (void)fclose(cin);
if (cout)
- (void) fclose(cout);
+ (void)fclose(cout);
code = -1;
goto bad;
}
@@ -165,9 +168,9 @@ hookup(host, port)
printf("Connected to %s.\n", hostname);
if (getreply(0) > 2) { /* read startup message from server */
if (cin)
- (void) fclose(cin);
+ (void)fclose(cin);
if (cout)
- (void) fclose(cout);
+ (void)fclose(cout);
code = -1;
goto bad;
}
@@ -184,134 +187,90 @@ hookup(host, port)
return (hostname);
bad:
- (void) close(s);
+ (void)close(s);
return ((char *)0);
}
-int
-login(host)
- char *host;
-{
- char tmp[80];
- char *user, *pass, *acct;
- int n, aflag = 0;
-
- user = pass = acct = 0;
- if (ruserpass(host, &user, &pass, &acct) < 0) {
- code = -1;
- return (0);
- }
- while (user == NULL) {
- char *myname = getlogin();
-
- if (myname == NULL) {
- struct passwd *pp = getpwuid(getuid());
-
- if (pp != NULL)
- myname = pp->pw_name;
- }
- if (myname)
- printf("Name (%s:%s): ", host, myname);
- else
- printf("Name (%s): ", host);
- (void) fgets(tmp, sizeof(tmp) - 1, stdin);
- tmp[strlen(tmp) - 1] = '\0';
- if (*tmp == '\0')
- user = myname;
- else
- user = tmp;
- }
- n = command("USER %s", user);
- if (n == CONTINUE) {
- if (pass == NULL)
- pass = getpass("Password:");
- n = command("PASS %s", pass);
- }
- if (n == CONTINUE) {
- aflag++;
- acct = getpass("Account:");
- n = command("ACCT %s", acct);
- }
- if (n != COMPLETE) {
- warnx("Login failed.");
- return (0);
- }
- if (!aflag && acct != NULL)
- (void) command("ACCT %s", acct);
- if (proxy)
- return (1);
- for (n = 0; n < macnum; ++n) {
- if (!strcmp("init", macros[n].mac_name)) {
- (void) strcpy(line, "$init");
- makeargv();
- domacro(margc, margv);
- break;
- }
- }
- return (1);
-}
-
void
-cmdabort()
+cmdabort(notused)
+ int notused;
{
- printf("\n");
- (void) fflush(stdout);
+ alarmtimer(0);
+ putchar('\n');
+ (void)fflush(stdout);
abrtflag++;
if (ptflag)
- longjmp(ptabort,1);
+ longjmp(ptabort, 1);
}
/*VARARGS*/
int
+#ifdef __STDC__
+command(const char *fmt, ...)
+#else
command(va_alist)
-va_dcl
+ va_dcl
+#endif
{
va_list ap;
- char *fmt;
int r;
sig_t oldintr;
+#ifndef __STDC__
+ const char *fmt;
+#endif
abrtflag = 0;
if (debug) {
- printf("---> ");
+ fputs("---> ", stdout);
+#ifdef __STDC__
+ va_start(ap, fmt);
+#else
va_start(ap);
- fmt = va_arg(ap, char *);
+ fmt = va_arg(ap, const char *);
+#endif
if (strncmp("PASS ", fmt, 5) == 0)
- printf("PASS XXXX");
+ fputs("PASS XXXX", stdout);
+ else if (strncmp("ACCT ", fmt, 5) == 0)
+ fputs("ACCT XXXX", stdout);
else
- vfprintf(stdout, fmt, ap);
+ vprintf(fmt, ap);
va_end(ap);
- printf("\n");
- (void) fflush(stdout);
+ putchar('\n');
+ (void)fflush(stdout);
}
if (cout == NULL) {
- warn("No control connection for command");
+ warnx("No control connection for command.");
code = -1;
return (0);
}
oldintr = signal(SIGINT, cmdabort);
+#ifdef __STDC__
+ va_start(ap, fmt);
+#else
va_start(ap);
fmt = va_arg(ap, char *);
+#endif
vfprintf(cout, fmt, ap);
va_end(ap);
- fprintf(cout, "\r\n");
- (void) fflush(cout);
+ fputs("\r\n", cout);
+ (void)fflush(cout);
cpend = 1;
r = getreply(!strcmp(fmt, "QUIT"));
if (abrtflag && oldintr != SIG_IGN)
(*oldintr)(SIGINT);
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
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;
@@ -319,9 +278,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)) {
@@ -329,13 +288,13 @@ getreply(expecteof)
case WONT:
c = getc(cin);
fprintf(cout, "%c%c%c", IAC, DONT, c);
- (void) fflush(cout);
+ (void)fflush(cout);
break;
case DO:
case DONT:
c = getc(cin);
fprintf(cout, "%c%c%c", IAC, WONT, c);
- (void) fflush(cout);
+ (void)fflush(cout);
break;
default:
break;
@@ -345,14 +304,15 @@ 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");
- (void) fflush(stdout);
+ puts(
+"421 Service not available, remote server has closed connection.");
+ (void)fflush(stdout);
}
code = 421;
return (4);
@@ -360,9 +320,9 @@ getreply(expecteof)
if (c != '\r' && (verbose > 0 ||
(verbose > -1 && n == '5' && dig > 4))) {
if (proxflag &&
- (dig == 1 || dig == 5 && verbose == 0))
- printf("%s:",hostname);
- (void) putchar(c);
+ (dig == 1 || (dig == 5 && verbose == 0)))
+ printf("%s:", hostname);
+ (void)putchar(c);
}
if (dig < 4 && isdigit(c))
code = code * 10 + (c - '0');
@@ -385,12 +345,21 @@ getreply(expecteof)
}
if (n == 0)
n = c;
- if (cp < &reply_string[sizeof(reply_string) - 1])
+ if (cp < &current_line[sizeof(current_line) - 1])
*cp++ = c;
}
- if (verbose > 0 || verbose > -1 && n == '5') {
- (void) putchar(c);
- (void) fflush (stdout);
+ 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)
@@ -400,7 +369,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)
@@ -424,32 +393,37 @@ empty(mask, sec)
jmp_buf sendabort;
void
-abortsend()
+abortsend(notused)
+ int notused;
{
+ alarmtimer(0);
mflag = 0;
abrtflag = 0;
- printf("\nsend aborted\nwaiting for remote to finish abort\n");
- (void) fflush(stdout);
+ puts("\nsend aborted\nwaiting for remote to finish abort.");
+ (void)fflush(stdout);
longjmp(sendabort, 1);
}
-#define HASHBYTES 1024
-
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 = HASHBYTES;
+ sig_t oldinti, oldintr, oldintp;
+ off_t hashbytes;
char *lmode, buf[BUFSIZ], *bufp;
+ int oprogress;
+ hashbytes = mark;
+ direction = "sent";
+ bytes = 0;
+ filesize = -1;
+ oprogress = progress;
if (verbose && printnames) {
if (local && *local != '-')
printf("local: %s ", local);
@@ -465,59 +439,72 @@ sendrequest(cmd, local, remote, printnames)
closefunc = NULL;
oldintr = NULL;
oldintp = NULL;
+ oldinti = NULL;
lmode = "w";
if (setjmp(sendabort)) {
while (cpend) {
- (void) getreply(0);
+ (void)getreply(0);
}
if (data >= 0) {
- (void) close(data);
+ (void)close(data);
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);
+ progress = oprogress;
code = -1;
return;
}
oldintr = signal(SIGINT, abortsend);
- if (strcmp(local, "-") == 0)
+ oldinti = signal(SIGINFO, psummary);
+ if (strcmp(local, "-") == 0) {
fin = stdin;
- else if (*local == '|') {
- oldintp = signal(SIGPIPE,SIG_IGN);
+ progress = 0;
+ } else if (*local == '|') {
+ 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(SIGINT, oldintr);
+ (void)signal(SIGPIPE, oldintp);
+ (void)signal(SIGINFO, oldinti);
code = -1;
return;
}
+ progress = 0;
closefunc = pclose;
} else {
fin = fopen(local, "r");
if (fin == NULL) {
warn("local: %s", local);
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
+ (void)signal(SIGINFO, oldinti);
code = -1;
return;
}
closefunc = fclose;
if (fstat(fileno(fin), &st) < 0 ||
- (st.st_mode&S_IFMT) != S_IFREG) {
- fprintf(stdout, "%s: not a plain file.\n", local);
- (void) signal(SIGINT, oldintr);
+ (st.st_mode & S_IFMT) != S_IFREG) {
+ printf("%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(SIGINT, oldintr);
+ (void)signal(SIGINFO, oldinti);
if (oldintp)
- (void) signal(SIGPIPE, oldintp);
+ (void)signal(SIGPIPE, oldintp);
code = -1;
+ progress = oprogress;
if (closefunc != NULL)
(*closefunc)(fin);
return;
@@ -529,6 +516,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);
@@ -541,6 +529,7 @@ sendrequest(cmd, local, remote, printnames)
if (rc < 0) {
warn("local: %s", local);
restart_point = 0;
+ progress = oprogress;
if (closefunc != NULL)
(*closefunc)(fin);
return;
@@ -548,6 +537,7 @@ sendrequest(cmd, local, remote, printnames)
if (command("REST %ld", (long) restart_point)
!= CONTINUE) {
restart_point = 0;
+ progress = oprogress;
if (closefunc != NULL)
(*closefunc)(fin);
return;
@@ -557,18 +547,22 @@ sendrequest(cmd, local, remote, printnames)
}
if (remote) {
if (command("%s %s", cmd, remote) != PRELIM) {
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
+ (void)signal(SIGINFO, oldinti);
+ progress = oprogress;
if (oldintp)
- (void) signal(SIGPIPE, oldintp);
+ (void)signal(SIGPIPE, oldintp);
if (closefunc != NULL)
(*closefunc)(fin);
return;
}
} else
if (command("%s", cmd) != PRELIM) {
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
+ (void)signal(SIGINFO, oldinti);
+ progress = oprogress;
if (oldintp)
- (void) signal(SIGPIPE, oldintp);
+ (void)signal(SIGPIPE, oldintp);
if (closefunc != NULL)
(*closefunc)(fin);
return;
@@ -576,31 +570,31 @@ 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) {
case TYPE_I:
case TYPE_L:
errno = d = 0;
- while ((c = read(fileno(fin), buf, sizeof (buf))) > 0) {
+ while ((c = read(fileno(fin), buf, sizeof(buf))) > 0) {
bytes += c;
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 += HASHBYTES;
+ (void)putchar('#');
+ hashbytes += mark;
}
- (void) fflush(stdout);
+ (void)fflush(stdout);
}
}
- if (hash && bytes > 0) {
- if (bytes < HASHBYTES)
- (void) putchar('#');
- (void) putchar('\n');
- (void) fflush(stdout);
+ if (hash && (!progress || filesize < 0) && bytes > 0) {
+ if (bytes < mark)
+ (void)putchar('#');
+ (void)putchar('\n');
+ (void)fflush(stdout);
}
if (c < 0)
warn("local: %s", local);
@@ -614,28 +608,31 @@ sendrequest(cmd, local, remote, printnames)
case TYPE_A:
while ((c = getc(fin)) != EOF) {
if (c == '\n') {
- while (hash && (bytes >= hashbytes)) {
- (void) putchar('#');
- (void) fflush(stdout);
- hashbytes += HASHBYTES;
+ while (hash && (!progress || filesize < 0) &&
+ (bytes >= hashbytes)) {
+ (void)putchar('#');
+ (void)fflush(stdout);
+ hashbytes += mark;
}
if (ferror(dout))
break;
- (void) putc('\r', dout);
+ (void)putc('\r', dout);
bytes++;
}
- (void) putc(c, dout);
+ (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');
- (void) fflush(stdout);
+ (void)putchar('#');
+ (void)putchar('\n');
+ (void)fflush(stdout);
}
if (ferror(fin))
warn("local: %s", local);
@@ -646,68 +643,82 @@ sendrequest(cmd, local, remote, printnames)
}
break;
}
- (void) gettimeofday(&stop, (struct timezone *)0);
+ progressmeter(1);
+ progress = oprogress;
if (closefunc != NULL)
(*closefunc)(fin);
- (void) fclose(dout);
- (void) getreply(0);
- (void) signal(SIGINT, oldintr);
+ (void)fclose(dout);
+ (void)getreply(0);
+ (void)signal(SIGINT, oldintr);
+ (void)signal(SIGINFO, oldinti);
if (oldintp)
- (void) signal(SIGPIPE, oldintp);
+ (void)signal(SIGPIPE, oldintp);
if (bytes > 0)
- ptransfer("sent", bytes, &start, &stop);
+ ptransfer(0);
return;
abort:
- (void) gettimeofday(&stop, (struct timezone *)0);
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
+ (void)signal(SIGINFO, oldinti);
+ progress = oprogress;
if (oldintp)
- (void) signal(SIGPIPE, oldintp);
+ (void)signal(SIGPIPE, oldintp);
if (!cpend) {
code = -1;
return;
}
if (data >= 0) {
- (void) close(data);
+ (void)close(data);
data = -1;
}
if (dout)
- (void) fclose(dout);
- (void) getreply(0);
+ (void)fclose(dout);
+ (void)getreply(0);
code = -1;
if (closefunc != NULL && fin != NULL)
(*closefunc)(fin);
if (bytes > 0)
- ptransfer("sent", bytes, &start, &stop);
+ ptransfer(0);
}
jmp_buf recvabort;
void
-abortrecv()
+abortrecv(notused)
+ int notused;
{
+ alarmtimer(0);
mflag = 0;
abrtflag = 0;
- printf("\nreceive aborted\nwaiting for remote to finish abort\n");
- (void) fflush(stdout);
+ puts("\nreceive aborted\nwaiting for remote to finish abort.");
+ (void)fflush(stdout);
longjmp(recvabort, 1);
}
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 = HASHBYTES;
- struct timeval start, stop;
+ off_t hashbytes;
struct stat st;
-
+ time_t mtime;
+ struct timeval tval[2];
+ int oprogress;
+ int opreserve;
+
+ hashbytes = mark;
+ direction = "received";
+ bytes = 0;
+ filesize = -1;
+ oprogress = progress;
+ opreserve = preserve;
is_retr = strcmp(cmd, "RETR") == 0;
if (is_retr && verbose && printnames) {
if (local && *local != '-')
@@ -725,25 +736,31 @@ recvrequest(cmd, local, remote, lmode, printnames)
tcrflag = !crflag && is_retr;
if (setjmp(recvabort)) {
while (cpend) {
- (void) getreply(0);
+ (void)getreply(0);
}
if (data >= 0) {
- (void) close(data);
+ (void)close(data);
data = -1;
}
if (oldintr)
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
+ if (oldinti)
+ (void)signal(SIGINFO, oldinti);
+ progress = oprogress;
+ preserve = opreserve;
code = -1;
return;
}
oldintr = signal(SIGINT, abortrecv);
+ oldinti = signal(SIGINFO, psummary);
if (strcmp(local, "-") && *local != '|') {
if (access(local, 2) < 0) {
char *dir = strrchr(local, '/');
if (errno != ENOENT && errno != EACCES) {
warn("local: %s", local);
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
+ (void)signal(SIGINFO, oldinti);
code = -1;
return;
}
@@ -754,27 +771,30 @@ recvrequest(cmd, local, remote, lmode, printnames)
*dir = '/';
if (d < 0) {
warn("local: %s", local);
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
+ (void)signal(SIGINFO, oldinti);
code = -1;
return;
}
if (!runique && errno == EACCES &&
chmod(local, 0600) < 0) {
warn("local: %s", local);
- (void) signal(SIGINT, oldintr);
- (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(SIGINT, oldintr);
+ (void)signal(SIGINFO, oldinti);
code = -1;
return;
}
}
else if (runique && (local = gunique(local)) == NULL) {
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
+ (void)signal(SIGINFO, oldinti);
code = -1;
return;
}
@@ -782,10 +802,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(SIGINT, oldintr);
+ (void)signal(SIGINFO, oldinti);
code = -1;
return;
}
@@ -796,27 +820,33 @@ recvrequest(cmd, local, remote, lmode, printnames)
return;
if (remote) {
if (command("%s %s", cmd, remote) != PRELIM) {
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
+ (void)signal(SIGINFO, oldinti);
return;
}
} else {
if (command("%s", cmd) != PRELIM) {
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
+ (void)signal(SIGINFO, oldinti);
return;
}
}
din = dataconn("r");
if (din == NULL)
goto abort;
- if (strcmp(local, "-") == 0)
+ if (strcmp(local, "-") == 0) {
fout = stdout;
- else if (*local == '|') {
+ progress = 0;
+ preserve = 0;
+ } else if (*local == '|') {
oldintp = signal(SIGPIPE, SIG_IGN);
fout = popen(local + 1, "w");
if (fout == NULL) {
warn("%s", local+1);
goto abort;
}
+ progress = 0;
+ preserve = 0;
closefunc = pclose;
} else {
fout = fopen(local, lmode);
@@ -830,7 +860,7 @@ recvrequest(cmd, local, remote, lmode, printnames)
st.st_blksize = BUFSIZ;
if (st.st_blksize > bufsize) {
if (buf)
- (void) free(buf);
+ (void)free(buf);
buf = malloc((unsigned)st.st_blksize);
if (buf == NULL) {
warn("malloc");
@@ -839,7 +869,11 @@ recvrequest(cmd, local, remote, lmode, printnames)
}
bufsize = st.st_blksize;
}
- (void) gettimeofday(&start, (struct timezone *)0);
+ if ((st.st_mode & S_IFMT) != S_IFREG) {
+ progress = 0;
+ preserve = 0;
+ }
+ progressmeter(-1);
switch (curtype) {
case TYPE_I:
@@ -847,6 +881,8 @@ recvrequest(cmd, local, remote, lmode, printnames)
if (restart_point &&
lseek(fileno(fout), restart_point, SEEK_SET) < 0) {
warn("local: %s", local);
+ progress = oprogress;
+ preserve = opreserve;
if (closefunc != NULL)
(*closefunc)(fout);
return;
@@ -856,19 +892,19 @@ 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 += HASHBYTES;
+ (void)putchar('#');
+ hashbytes += mark;
}
- (void) fflush(stdout);
+ (void)fflush(stdout);
}
}
- if (hash && bytes > 0) {
- if (bytes < HASHBYTES)
- (void) putchar('#');
- (void) putchar('\n');
- (void) fflush(stdout);
+ if (hash && (!progress || filesize < 0) && bytes > 0) {
+ if (bytes < mark)
+ (void)putchar('#');
+ (void)putchar('\n');
+ (void)fflush(stdout);
}
if (c < 0) {
if (errno != EPIPE)
@@ -899,6 +935,8 @@ recvrequest(cmd, local, remote, lmode, printnames)
if (fseek(fout, 0L, SEEK_CUR) < 0) {
done:
warn("local: %s", local);
+ progress = oprogress;
+ preserve = opreserve;
if (closefunc != NULL)
(*closefunc)(fout);
return;
@@ -908,16 +946,17 @@ done:
if (c == '\n')
bare_lfs++;
while (c == '\r') {
- while (hash && (bytes >= hashbytes)) {
- (void) putchar('#');
- (void) fflush(stdout);
- hashbytes += HASHBYTES;
+ while (hash && (!progress || filesize < 0) &&
+ (bytes >= hashbytes)) {
+ (void)putchar('#');
+ (void)fflush(stdout);
+ hashbytes += mark;
}
bytes++;
if ((c = getc(din)) != '\n' || tcrflag) {
if (ferror(fout))
goto break2;
- (void) putc('\r', fout);
+ (void)putc('\r', fout);
if (c == '\0') {
bytes++;
goto contin2;
@@ -926,20 +965,21 @@ done:
goto contin2;
}
}
- (void) putc(c, fout);
+ (void)putc(c, fout);
bytes++;
contin2: ;
}
break2:
if (bare_lfs) {
- printf("WARNING! %d bare linefeeds received in ASCII mode\n", bare_lfs);
- printf("File may not have transferred correctly.\n");
+ printf(
+"WARNING! %d bare linefeeds received in ASCII mode.\n", bare_lfs);
+ puts("File may not have transferred correctly.");
}
- if (hash) {
+ if (hash && (!progress || filesize < 0)) {
if (bytes < hashbytes)
- (void) putchar('#');
- (void) putchar('\n');
- (void) fflush(stdout);
+ (void)putchar('#');
+ (void)putchar('\n');
+ (void)fflush(stdout);
}
if (ferror(din)) {
if (errno != EPIPE)
@@ -950,44 +990,67 @@ break2:
warn("local: %s", local);
break;
}
+ progressmeter(1);
+ progress = oprogress;
+ preserve = opreserve;
if (closefunc != NULL)
(*closefunc)(fout);
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
+ (void)signal(SIGINFO, oldinti);
if (oldintp)
- (void) signal(SIGPIPE, oldintp);
- (void) gettimeofday(&stop, (struct timezone *)0);
- (void) fclose(din);
- (void) getreply(0);
- if (bytes > 0 && is_retr)
- ptransfer("received", bytes, &start, &stop);
+ (void)signal(SIGPIPE, oldintp);
+ (void)fclose(din);
+ (void)getreply(0);
+ 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 */
- (void) gettimeofday(&stop, (struct timezone *)0);
+ progress = oprogress;
+ preserve = opreserve;
if (oldintp)
- (void) signal(SIGPIPE, oldintr);
- (void) signal(SIGINT, SIG_IGN);
+ (void)signal(SIGPIPE, oldintp);
+ (void)signal(SIGINT, SIG_IGN);
if (!cpend) {
code = -1;
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
+ (void)signal(SIGINFO, oldinti);
return;
}
abort_remote(din);
code = -1;
if (data >= 0) {
- (void) close(data);
+ (void)close(data);
data = -1;
}
if (closefunc != NULL && fout != NULL)
(*closefunc)(fout);
if (din)
- (void) fclose(din);
+ (void)fclose(din);
if (bytes > 0)
- ptransfer("received", bytes, &start, &stop);
- (void) signal(SIGINT, oldintr);
+ ptransfer(0);
+ (void)signal(SIGINT, oldintr);
+ (void)signal(SIGINFO, oldinti);
}
/*
@@ -1001,57 +1064,61 @@ initconn()
int result, len, tmpno = 0;
int on = 1;
int tos, ports;
- u_long a1,a2,a3,a4,p1,p2;
+ int a0, a1, a2, a3, p0, p1;
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 &&
+ if ((options & SO_DEBUG) &&
setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
- sizeof (on)) < 0)
- perror("ftp: setsockopt (ignored)");
+ sizeof(on)) < 0)
+ warn("setsockopt (ignored)");
if (command("PASV") != COMPLETE) {
- printf("Passive mode refused.\n");
- close(data);
- return(1);
+ puts("Passive mode refused.");
+ goto bad;
}
/*
- * What we've got at this point is a string of comma separated
- * one-byte unsigned integer values, separated by commas.
- * The first four are the an IP address. The fifth is the MSB
- * of the port number, the sixth is the LSB. From that we'll
- * prepare a sockaddr_in.
+ * What we've got at this point is a string of comma
+ * separated one-byte unsigned integer values.
+ * The first four are the an IP address. The fifth is
+ * the MSB of the port number, the sixth is the LSB.
+ * From that we'll prepare a sockaddr_in.
*/
- if (sscanf(pasv,"%d,%d,%d,%d,%d,%d",&a1,&a2,&a3,&a4,&p1,&p2)
- != 6) {
- printf("Passive mode address scan failure. Shouldn't happen!\n");
- close(data);
- return(1);
- };
+ if (sscanf(pasv, "%d,%d,%d,%d,%d,%d",
+ &a0, &a1, &a2, &a3, &p0, &p1) != 6) {
+ puts(
+"Passive mode address scan failure. Shouldn't happen!");
+ goto bad;
+ }
+ memset(&data_addr, 0, sizeof(data_addr));
data_addr.sin_family = AF_INET;
- data_addr.sin_addr.s_addr = htonl((a1 << 24) | (a2 << 16) |
- (a3 << 8) | a4);
- data_addr.sin_port = htons((p1 << 8) | p2);
-
- if (connect(data, (struct sockaddr *) &data_addr,
- sizeof(data_addr))<0) {
- perror("ftp: connect");
- close(data);
- return(1);
+ a = (char *)&data_addr.sin_addr.s_addr;
+ a[0] = a0 & 0xff;
+ a[1] = a1 & 0xff;
+ a[2] = a2 & 0xff;
+ a[3] = a3 & 0xff;
+ p = (char *)&data_addr.sin_port;
+ p[0] = p0 & 0xff;
+ p[1] = p1 & 0xff;
+
+ if (connect(data, (struct sockaddr *)&data_addr,
+ sizeof(data_addr)) < 0) {
+ warn("connect");
+ goto bad;
}
#ifdef IP_TOS
tos = IPTOS_THROUGHPUT;
if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&tos,
- sizeof(tos)) < 0)
- perror("ftp: setsockopt TOS (ignored)");
+ sizeof(int)) < 0)
+ warn("setsockopt TOS (ignored)");
#endif
- return(0);
+ return (0);
}
noport:
@@ -1059,7 +1126,7 @@ noport:
if (sendport)
data_addr.sin_port = 0; /* let system pick one */
if (data != -1)
- (void) close(data);
+ (void)close(data);
data = socket(AF_INET, SOCK_STREAM, 0);
if (data < 0) {
warn("socket");
@@ -1069,24 +1136,25 @@ noport:
}
if (!sendport)
if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
- sizeof (on)) < 0) {
+ sizeof(on)) < 0) {
warn("setsockopt (reuse address)");
goto bad;
}
#ifdef IP_PORTRANGE
ports = restricted_data_ports ? IP_PORTRANGE_HIGH : IP_PORTRANGE_DEFAULT;
if (setsockopt(data, IPPROTO_IP, IP_PORTRANGE, (char *)&ports,
- sizeof (ports)) < 0)
- warn("setsockopt PORTRANGE (ignored)");
+ sizeof(ports)) < 0)
+ warn("setsockopt PORTRANGE (ignored)");
#endif
- if (bind(data, (struct sockaddr *)&data_addr, sizeof (data_addr)) < 0) {
+ if (bind(data, (struct sockaddr *)&data_addr, sizeof(data_addr)) < 0) {
warn("bind");
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);
+ len = sizeof(data_addr);
if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
warn("getsockname");
goto bad;
@@ -1117,7 +1185,7 @@ noport:
#endif
return (0);
bad:
- (void) close(data), data = -1;
+ (void)close(data), data = -1;
if (tmpno)
sendport = 1;
return (1);
@@ -1125,21 +1193,23 @@ bad:
FILE *
dataconn(lmode)
- char *lmode;
+ const char *lmode;
{
struct sockaddr_in from;
- int s, fromlen = sizeof (from), tos;
+ int s, fromlen, tos;
+
+ fromlen = sizeof(from);
if (passivemode)
- return (fdopen(data, lmode));
+ return (fdopen(data, lmode));
s = accept(data, (struct sockaddr *) &from, &fromlen);
if (s < 0) {
warn("accept");
- (void) close(data), data = -1;
+ (void)close(data), data = -1;
return (NULL);
}
- (void) close(data);
+ (void)close(data);
data = s;
#ifdef IP_TOS
tos = IPTOS_THROUGHPUT;
@@ -1150,56 +1220,20 @@ dataconn(lmode)
}
void
-ptransfer(direction, bytes, t0, t1)
- char *direction;
- long bytes;
- struct timeval *t0, *t1;
-{
- struct timeval td;
- float s, bs;
-
- if (verbose) {
- tvsub(&td, t1, t0);
- s = td.tv_sec + (td.tv_usec / 1000000.);
-#define nz(x) ((x) == 0 ? 1 : (x))
- bs = bytes / nz(s);
- if(bs > ( 1024 * 1024 ))
- printf("%ld bytes %s in %.2f seconds (%.2f Mbytes/s)\n",
- bytes, direction, s, bs / (1024. * 1024.));
- else
- printf("%ld bytes %s in %.2f seconds (%.2f Kbytes/s)\n",
- bytes, direction, s, bs / 1024.);
- }
-}
-
-/*
-void
-tvadd(tsum, t0)
- struct timeval *tsum, *t0;
+psummary(notused)
+ int notused;
{
- tsum->tv_sec += t0->tv_sec;
- tsum->tv_usec += t0->tv_usec;
- if (tsum->tv_usec > 1000000)
- tsum->tv_sec++, tsum->tv_usec -= 1000000;
-}
-*/
-
-void
-tvsub(tdiff, t1, t0)
- struct timeval *tdiff, *t1, *t0;
-{
-
- tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
- tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
- if (tdiff->tv_usec < 0)
- tdiff->tv_sec--, tdiff->tv_usec += 1000000;
+ if (bytes > 0)
+ ptransfer(1);
}
void
-psabort()
+psabort(notused)
+ int notused;
{
+ alarmtimer(0);
abrtflag++;
}
@@ -1248,10 +1282,10 @@ pswitch(flag)
ip->connect = connected;
connected = op->connect;
if (hostname) {
- (void) strncpy(ip->name, hostname, sizeof(ip->name) - 1);
- ip->name[strlen(ip->name)] = '\0';
+ (void)strncpy(ip->name, hostname, sizeof(ip->name) - 1);
+ ip->name[sizeof(ip->name) - 1] = '\0';
} else
- ip->name[0] = 0;
+ ip->name[0] = '\0';
hostname = op->name;
ip->hctl = hisctladdr;
hisctladdr = op->hctl;
@@ -1275,21 +1309,21 @@ pswitch(flag)
mcase = op->mcse;
ip->ntflg = ntflag;
ntflag = op->ntflg;
- (void) strncpy(ip->nti, ntin, 16);
- (ip->nti)[strlen(ip->nti)] = '\0';
- (void) strcpy(ntin, op->nti);
- (void) strncpy(ip->nto, ntout, 16);
- (ip->nto)[strlen(ip->nto)] = '\0';
- (void) strcpy(ntout, op->nto);
+ (void)strncpy(ip->nti, ntin, sizeof(ip->nti) - 1);
+ (ip->nti)[sizeof(ip->nti) - 1] = '\0';
+ (void)strcpy(ntin, op->nti);
+ (void)strncpy(ip->nto, ntout, sizeof(ip->nto) - 1);
+ (ip->nto)[sizeof(ip->nto) - 1] = '\0';
+ (void)strcpy(ntout, op->nto);
ip->mapflg = mapflag;
mapflag = op->mapflg;
- (void) strncpy(ip->mi, mapin, MAXPATHLEN - 1);
- (ip->mi)[strlen(ip->mi)] = '\0';
- (void) strcpy(mapin, op->mi);
- (void) strncpy(ip->mo, mapout, MAXPATHLEN - 1);
- (ip->mo)[strlen(ip->mo)] = '\0';
- (void) strcpy(mapout, op->mo);
- (void) signal(SIGINT, oldintr);
+ (void)strncpy(ip->mi, mapin, sizeof(ip->mi) - 1);
+ (ip->mi)[sizeof(ip->mi) - 1] = '\0';
+ (void)strcpy(mapin, op->mi);
+ (void)strncpy(ip->mo, mapout, sizeof(ip->mo) - 1);
+ (ip->mo)[sizeof(ip->mo) - 1] = '\0';
+ (void)strcpy(mapout, op->mo);
+ (void)signal(SIGINT, oldintr);
if (abrtflag) {
abrtflag = 0;
(*oldintr)(SIGINT);
@@ -1297,11 +1331,13 @@ pswitch(flag)
}
void
-abortpt()
+abortpt(notused)
+ int notused;
{
- printf("\n");
- (void) fflush(stdout);
+ alarmtimer(0);
+ putchar('\n');
+ (void)fflush(stdout);
ptabflg++;
mflag = 0;
abrtflag = 0;
@@ -1310,7 +1346,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;
@@ -1330,12 +1366,12 @@ 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");
+ puts("proxy server does not support third party transfers.");
return;
}
pswitch(0);
if (!connected) {
- printf("No primary connection\n");
+ puts("No primary connection.");
pswitch(1);
code = -1;
return;
@@ -1350,7 +1386,7 @@ proxtrans(cmd, local, remote)
goto abort;
oldintr = signal(SIGINT, abortpt);
if (command("%s %s", cmd, remote) != PRELIM) {
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
pswitch(1);
return;
}
@@ -1360,16 +1396,16 @@ proxtrans(cmd, local, remote)
if (command("%s %s", cmd2, local) != PRELIM)
goto abort;
ptflag++;
- (void) getreply(0);
+ (void)getreply(0);
pswitch(0);
- (void) getreply(0);
- (void) signal(SIGINT, oldintr);
+ (void)getreply(0);
+ (void)signal(SIGINT, oldintr);
pswitch(1);
ptflag = 0;
printf("local: %s remote: %s\n", local, remote);
return;
abort:
- (void) signal(SIGINT, SIG_IGN);
+ (void)signal(SIGINT, SIG_IGN);
ptflag = 0;
if (strcmp(cmd, "RETR") && !proxy)
pswitch(1);
@@ -1384,7 +1420,7 @@ abort:
pswitch(1);
if (ptabflg)
code = -1;
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
return;
}
if (cpend)
@@ -1398,7 +1434,7 @@ abort:
pswitch(1);
if (ptabflg)
code = -1;
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
return;
}
}
@@ -1416,15 +1452,15 @@ abort:
code = -1;
lostpeer();
}
- (void) getreply(0);
- (void) getreply(0);
+ (void)getreply(0);
+ (void)getreply(0);
}
if (proxy)
pswitch(0);
pswitch(1);
if (ptabflg)
code = -1;
- (void) signal(SIGINT, oldintr);
+ (void)signal(SIGINT, oldintr);
}
void
@@ -1438,20 +1474,20 @@ 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();
}
else if (nfnd) {
- (void) getreply(0);
+ (void)getreply(0);
}
}
}
char *
gunique(local)
- char *local;
+ const char *local;
{
static char new[MAXPATHLEN];
char *cp = strrchr(local, '/');
@@ -1467,12 +1503,12 @@ gunique(local)
warn("local: %s", local);
return ((char *) 0);
}
- (void) strcpy(new, local);
+ (void)strcpy(new, local);
cp = new + strlen(new);
*cp++ = '.';
while (!d) {
if (++count == 100) {
- printf("runique: can't find unique file name.\n");
+ puts("runique: can't find unique file name.");
return ((char *) 0);
}
*cp++ = ext;
@@ -1503,6 +1539,13 @@ abort_remote(din)
int nfnd;
struct fd_set mask;
+ if (cout == NULL) {
+ warnx("Lost control connection for abort.");
+ if (ptabflg)
+ code = -1;
+ lostpeer();
+ return;
+ }
/*
* send IAC in urgent mode instead of DM because 4.3BSD places oob mark
* after urgent byte rather than before as is protocol now
@@ -1510,8 +1553,8 @@ 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);
- (void) fflush(cout);
+ fprintf(cout, "%cABOR\r\n", DM);
+ (void)fflush(cout);
FD_ZERO(&mask);
FD_SET(fileno(cin), &mask);
if (din) {
@@ -1531,7 +1574,7 @@ abort_remote(din)
}
if (getreply(0) == ERROR && code == 552) {
/* 552 needed for nic style abort */
- (void) getreply(0);
+ (void)getreply(0);
}
- (void) getreply(0);
+ (void)getreply(0);
}
diff --git a/usr.bin/ftp/ftp_var.h b/usr.bin/ftp/ftp_var.h
index 85895a8..3a7423f 100644
--- a/usr.bin/ftp/ftp_var.h
+++ b/usr.bin/ftp/ftp_var.h
@@ -1,3 +1,6 @@
+/* $Id$ */
+/* $NetBSD: ftp_var.h,v 1.16 1997/04/14 09:09:23 lukem Exp $ */
+
/*
* Copyright (c) 1985, 1989, 1993, 1994
* The Regents of the University of California. All rights reserved.
@@ -30,7 +33,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * @(#)ftp_var.h 8.3 (Berkeley) 4/2/94
+ * @(#)ftp_var.h 8.4 (Berkeley) 10/9/94
*/
/*
@@ -39,19 +42,36 @@
#include <sys/param.h>
#include <setjmp.h>
+#include <stringlist.h>
+
+#ifndef SMALL
+#include <histedit.h>
+#endif /* !SMALL */
#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 */
+
+#define PAGER "less" /* default pager if $PAGER isn't set */
+
/*
* Options and other state info.
*/
int trace; /* trace packets exchanged */
int hash; /* print # for each buffer transferred */
+int mark; /* number of bytes between hashes */
int sendport; /* use PORT cmd for each data connection */
int verbose; /* print messages coming back from server */
-int connected; /* connected to server */
+int connected; /* 1 = connected to server, -1 = logged in */
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 */
@@ -63,6 +83,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 */
@@ -84,21 +106,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; /* automatic anonymous login */
+int dirchange; /* remote directory changed by cd command */
+int ttywidth; /* width of tty */
+
+#ifndef SMALL
+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 /* !SMALL */
+
+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[20]; /* args parsed from input line */
+#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 */
@@ -110,9 +149,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 SMALL
+ char *c_complete; /* context sensitive completion list */
+#endif /* !SMALL */
void (*c_handler) __P((int, char **)); /* function to call */
};
diff --git a/usr.bin/ftp/main.c b/usr.bin/ftp/main.c
index 7237c06..1705021 100644
--- a/usr.bin/ftp/main.c
+++ b/usr.bin/ftp/main.c
@@ -1,3 +1,6 @@
+/* $Id$ */
+/* $NetBSD: main.c,v 1.22 1997/06/10 07:04:43 lukem Exp $ */
+
/*
* Copyright (c) 1985, 1989, 1993, 1994
* The Regents of the University of California. All rights reserved.
@@ -38,27 +41,26 @@ static char copyright[] =
#endif /* not lint */
#ifndef lint
-static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 4/3/94";
+#if 0
+static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 10/9/94";
+#else
+static char rcsid[] = "$Id$";
+#endif
#endif /* not lint */
/*
* FTP User Program -- Command Interface.
*/
-/*#include <sys/ioctl.h>*/
#include <sys/types.h>
#include <sys/socket.h>
-#include <arpa/ftp.h>
-
-#include <ctype.h>
#include <err.h>
#include <netdb.h>
#include <pwd.h>
-#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
#include <string.h>
+#include <unistd.h>
#include "ftp_var.h"
@@ -67,35 +69,76 @@ 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];
- struct servent sp_default;
+ int dumbterm;
sp = getservbyname("ftp", "tcp");
- if (sp == 0) {
- sp = &sp_default;
- memset(sp, 0, sizeof *sp);
- sp->s_port = htons(21);
- }
+ if (sp == 0)
+ 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;
restricted_data_ports = 1;
+ preserve = 1;
+ verbose = 0;
+ progress = 0;
+#ifndef SMALL
+ editing = 0;
+ el = NULL;
+ hist = NULL;
+#endif
+ mark = HASHBYTES;
+ marg_sl = sl_init();
cp = strrchr(argv[0], '/');
- cp = (cp == NULL) ? argv[0] : cp+1;
+ cp = (cp == NULL) ? argv[0] : cp + 1;
if (getenv("FTP_PASSIVE_MODE") || strcmp(cp, "pftp") == 0)
- passivemode = 1;
+ passivemode = 1;
+
+ cp = getenv("TERM");
+ if (cp == NULL || strcmp(cp, "dumb") == 0)
+ dumbterm = 1;
+ else
+ dumbterm = 0;
+ fromatty = isatty(fileno(stdin));
+ if (fromatty) {
+ verbose = 1; /* verbose if from a tty */
+#ifndef SMALL
+ if (! dumbterm)
+ editing = 1; /* editing mode on if tty is usable */
+#endif
+ }
+ if (isatty(fileno(stdout)) && !dumbterm)
+ progress = 1; /* progress bar on if tty is usable */
- while ((ch = getopt(argc, argv, "dginptvU")) != -1) {
+ while ((ch = getopt(argc, argv, "adeginpP:tvVU")) != -1) {
switch (ch) {
+ case 'a':
+ anonftp = 1;
+ break;
+
case 'd':
options |= SO_DEBUG;
debug++;
break;
+ case 'e':
+#ifndef SMALL
+ editing = 0;
+#endif
+ break;
+
case 'g':
doglob = 0;
break;
@@ -112,30 +155,37 @@ main(argc, argv)
passivemode = 1;
break;
+ case 'P':
+ port = atoi(optarg);
+ if (port <= 0)
+ warnx("bad port number: %s (ignored)", optarg);
+ else
+ ftpport = htons(port);
+ break;
+
case 't':
- trace++;
+ trace = 1;
break;
case 'v':
- verbose++;
+ verbose = 1;
+ break;
+
+ case 'V':
+ verbose = 0;
break;
case 'U':
- restricted_data_ports = 0;
+ restricted_data_ports = 0;
break;
default:
- (void)fprintf(stderr,
- "usage: ftp [-dginptv] [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 */
crflag = 1; /* strip c.r. on ascii gets */
@@ -151,27 +201,40 @@ main(argc, argv)
pw = getpwuid(getuid());
if (pw != NULL) {
home = homedir;
- (void) strcpy(home, pw->pw_dir);
+ (void)strcpy(home, pw->pw_dir);
}
+
+ setttywidth(0);
+ (void)signal(SIGWINCH, setttywidth);
+
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;
- setpeer(argc+1, xargv);
+ 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];
+
+ if (setjmp(toplevel))
+ exit(0);
+ (void)signal(SIGINT, (sig_t)intr);
+ (void)signal(SIGPIPE, (sig_t)lostpeer);
+ xargv[0] = __progname;
+ xargv[1] = argv[0];
+ xargv[2] = argv[1];
+ xargv[3] = argv[2];
+ xargv[4] = NULL;
+ setpeer(argc+1, xargv);
+ }
}
+#ifndef SMALL
+ controlediting();
+#endif /* !SMALL */
top = setjmp(toplevel) == 0;
if (top) {
- (void) signal(SIGINT, intr);
- (void) signal(SIGPIPE, lostpeer);
+ (void)signal(SIGINT, (sig_t)intr);
+ (void)signal(SIGPIPE, (sig_t)lostpeer);
}
for (;;) {
cmdscanner(top);
@@ -183,6 +246,7 @@ void
intr()
{
+ alarmtimer(0);
longjmp(toplevel, 1);
}
@@ -190,15 +254,16 @@ void
lostpeer()
{
+ alarmtimer(0);
if (connected) {
if (cout != NULL) {
- (void) shutdown(fileno(cout), 1+1);
- (void) fclose(cout);
+ (void)shutdown(fileno(cout), 1+1);
+ (void)fclose(cout);
cout = NULL;
}
if (data >= 0) {
- (void) shutdown(data, 1+1);
- (void) close(data);
+ (void)shutdown(data, 1+1);
+ (void)close(data);
data = -1;
}
connected = 0;
@@ -206,8 +271,8 @@ lostpeer()
pswitch(1);
if (connected) {
if (cout != NULL) {
- (void) shutdown(fileno(cout), 1+1);
- (void) fclose(cout);
+ (void)shutdown(fileno(cout), 1+1);
+ (void)fclose(cout);
cout = NULL;
}
connected = 0;
@@ -217,25 +282,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);
- if (s == filename)
- break; XXX
- *s = '\0';
- }
- return (filename);
+ return ("ftp> ");
}
-*/
/*
* Command parser.
@@ -245,69 +298,107 @@ cmdscanner(top)
int top;
{
struct cmd *c;
- int l;
-
- if (!top)
- (void) putchar('\n');
+ int num;
+
+ if (!top
+#ifndef SMALL
+ && !editing
+#endif /* !SMALL */
+ )
+ (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 SMALL
+ if (!editing) {
+#endif /* !SMALL */
+ if (fromatty) {
+ fputs(prompt(), stdout);
+ (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) {
+ puts("sorry, input line too long.");
+ while ((num = getchar()) != '\n' && num != EOF)
+ /* void */;
+ break;
+ } /* else it was a line without a newline */
+#ifndef SMALL
+ } 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)) {
+ puts("sorry, input line too long.");
+ break;
+ }
+ memcpy(line, buf, num);
+ line[num] = '\0';
+ history(hist, H_ENTER, buf);
+ }
+#endif /* !SMALL */
+
makeargv();
- if (margc == 0) {
+ if (margc == 0)
continue;
- }
+#if 0 && !defined(SMALL) /* 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 /* !SMALL */
c = getcmd(margv[0]);
if (c == (struct cmd *)-1) {
- printf("?Ambiguous command\n");
+ puts("?Ambiguous command.");
continue;
}
if (c == 0) {
- printf("?Invalid command\n");
+ puts("?Invalid command.");
continue;
}
if (c->c_conn && !connected) {
- printf("Not connected.\n");
+ puts("Not connected.");
continue;
}
+ confirmrest = 0;
(*c->c_handler)(margc, margv);
if (bell && c->c_bell)
- (void) putchar('\007');
+ (void)putchar('\007');
if (c->c_handler != help)
break;
}
- (void) signal(SIGINT, intr);
- (void) signal(SIGPIPE, lostpeer);
+ (void)signal(SIGINT, (sig_t)intr);
+ (void)signal(SIGPIPE, (sig_t)lostpeer);
}
struct cmd *
getcmd(name)
- char *name;
+ const char *name;
{
- char *p, *q;
+ const char *p, *q;
struct cmd *c, *found;
int nmatches, longest;
+ if (name == NULL)
+ return (0);
+
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);
@@ -334,17 +425,41 @@ int slrflag;
void
makeargv()
{
- char **argp;
+ char *argp;
- margc = 0;
- argp = margv;
stringbase = line; /* scan from first of buffer */
argbase = argbuf; /* store from first of buffer */
slrflag = 0;
- while (*argp++ = slurpstring())
- margc++;
+ marg_sl->sl_cur = 0; /* reset to start of marg_sl */
+ for (margc = 0; ; margc++) {
+ argp = slurpstring();
+ sl_add(marg_sl, argp);
+ if (argp == NULL)
+ break;
+ }
+#ifndef SMALL
+ 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 /* !SMALL */
}
+#ifdef SMALL
+#define INC_CHKCURSOR(x) (x)++
+#else /* !SMALL */
+#define INC_CHKCURSOR(x) { (x)++ ; \
+ if (x == cursor_pos) { \
+ cursor_argc = margc; \
+ cursor_argo = ap-argbase; \
+ cursor_pos = NULL; \
+ } }
+
+#endif /* !SMALL */
+
/*
* Parse string into argbuf;
* implemented with FSM to
@@ -362,7 +477,7 @@ slurpstring()
switch (slrflag) { /* and $ as token for macro invoke */
case 0:
slrflag++;
- stringbase++;
+ INC_CHKCURSOR(stringbase);
return ((*sb == '!') ? "!" : "$");
/* NOTREACHED */
case 1:
@@ -382,7 +497,8 @@ S0:
case ' ':
case '\t':
- sb++; goto S0;
+ INC_CHKCURSOR(sb);
+ goto S0;
default:
switch (slrflag) {
@@ -408,13 +524,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;
}
@@ -426,7 +546,9 @@ S2:
goto OUT;
default:
- *ap++ = *sb++;
+ *ap = *sb;
+ ap++;
+ INC_CHKCURSOR(sb);
got_one = 1;
goto S1;
}
@@ -438,10 +560,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;
}
@@ -468,8 +593,6 @@ OUT:
return ((char *)0);
}
-#define HELPINDENT ((int) sizeof ("directory"))
-
/*
* Help command.
* Call each command handler with argc == 0 and argv[0] == name.
@@ -482,47 +605,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)
@@ -534,3 +634,15 @@ help(argc, argv)
c->c_name, c->c_help);
}
}
+
+void
+usage()
+{
+ (void)fprintf(stderr,
+ "usage: %s [-adeginptvV] [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 be72b7e..c764add 100644
--- a/usr.bin/ftp/pathnames.h
+++ b/usr.bin/ftp/pathnames.h
@@ -1,3 +1,6 @@
+/* $Id$ */
+/* $NetBSD: pathnames.h,v 1.7 1997/01/09 20:19:40 tls Exp $ */
+
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
@@ -35,5 +38,4 @@
#include <paths.h>
-#undef _PATH_TMP
-#define _PATH_TMP "/tmp/ftpXXXXXX"
+#define TMPFILE "ftpXXXXXX"
diff --git a/usr.bin/ftp/ruserpass.c b/usr.bin/ftp/ruserpass.c
index 85a190e..315cd79 100644
--- a/usr.bin/ftp/ruserpass.c
+++ b/usr.bin/ftp/ruserpass.c
@@ -1,3 +1,6 @@
+/* $Id$ */
+/* $NetBSD: ruserpass.c,v 1.13 1997/04/01 14:20:34 mrg Exp $ */
+
/*
* Copyright (c) 1985, 1993, 1994
* The Regents of the University of California. All rights reserved.
@@ -32,7 +35,11 @@
*/
#ifndef lint
-static char sccsid[] = "@(#)ruserpass.c 8.3 (Berkeley) 4/2/94";
+#if 0
+static char sccsid[] = "@(#)ruserpass.c 8.4 (Berkeley) 4/27/95";
+#else
+static char rcsid[] = "$Id$";
+#endif
#endif /* not lint */
#include <sys/types.h>
@@ -77,7 +84,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;
@@ -87,7 +95,12 @@ ruserpass(host, aname, apass, aacct)
hdir = getenv("HOME");
if (hdir == NULL)
hdir = ".";
- (void) snprintf(buf, sizeof(buf), "%s/.netrc", hdir);
+ if (strlen(hdir) + sizeof(".netrc") < sizeof(buf)) {
+ (void)snprintf(buf, sizeof buf, "%s/.netrc", hdir);
+ } else {
+ warnx("%s/.netrc: %s", hdir, strerror(ENAMETOOLONG));
+ return (0);
+ }
cfile = fopen(buf, "r");
if (cfile == NULL) {
if (errno != ENOENT)
@@ -136,8 +149,9 @@ next:
case LOGIN:
if (token())
if (*aname == 0) {
- *aname = malloc((unsigned) strlen(tokval) + 1);
- (void) strcpy(*aname, tokval);
+ *aname = malloc((unsigned)
+ strlen(tokval) + 1);
+ (void)strcpy(*aname, tokval);
} else {
if (strcmp(*aname, tokval))
goto next;
@@ -153,7 +167,7 @@ next:
}
if (token() && *apass == 0) {
*apass = malloc((unsigned) strlen(tokval) + 1);
- (void) strcpy(*apass, tokval);
+ (void)strcpy(*apass, tokval);
}
break;
case ACCOUNT:
@@ -165,21 +179,24 @@ next:
}
if (token() && *aacct == 0) {
*aacct = malloc((unsigned) strlen(tokval) + 1);
- (void) strcpy(*aacct, tokval);
+ (void)strcpy(*aacct, tokval);
}
break;
case MACDEF:
if (proxy) {
- (void) fclose(cfile);
+ (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");
+ puts("Missing macdef name argument.");
goto bad;
}
if (macnum == 16) {
- printf("Limit of 16 macros have already been defined\n");
+ puts(
+"Limit of 16 macros have already been defined.");
goto bad;
}
tmp = macros[macnum].mac_name;
@@ -189,7 +206,8 @@ next:
*tmp++ = c;
}
if (c == EOF) {
- printf("Macro definition missing null line terminator.\n");
+ puts(
+"Macro definition missing null line terminator.");
goto bad;
}
*tmp = '\0';
@@ -197,19 +215,22 @@ next:
while ((c=getc(cfile)) != EOF && c != '\n');
}
if (c == EOF) {
- printf("Macro definition missing null line terminator.\n");
+ puts(
+"Macro definition missing null line terminator.");
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");
+ puts(
+"Macro definition missing null line terminator.");
goto bad;
}
*tmp = c;
@@ -223,7 +244,7 @@ next:
tmp++;
}
if (tmp == macbuf + 4096) {
- printf("4K macro buffer exceeded\n");
+ puts("4K macro buffer exceeded.");
goto bad;
}
break;
@@ -234,10 +255,10 @@ next:
goto done;
}
done:
- (void) fclose(cfile);
+ (void)fclose(cfile);
return (0);
bad:
- (void) fclose(cfile);
+ (void)fclose(cfile);
return (-1);
}
diff --git a/usr.bin/ftp/util.c b/usr.bin/ftp/util.c
new file mode 100644
index 0000000..43c9c38
--- /dev/null
+++ b/usr.bin/ftp/util.c
@@ -0,0 +1,779 @@
+/* $Id$ */
+/* $NetBSD: util.c,v 1.9 1997/06/10 22:00:01 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[] = "$Id$";
+#endif /* not lint */
+
+/*
+ * FTP User Program -- Misc support routines
+ */
+#include <sys/ioctl.h>
+#include <sys/time.h>
+#include <arpa/ftp.h>
+
+#include <ctype.h>
+#include <err.h>
+#include <fcntl.h>
+#include <glob.h>
+#include <pwd.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#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], NULL, NULL);
+
+ 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))
+ puts(
+"Remember to set tenex mode when transferring binary files from this machine.");
+ }
+ verbose = overbose;
+ }
+}
+
+
+/*
+ * login to remote host, using given username & password if supplied
+ */
+int
+login(host, user, pass)
+ const char *host;
+ char *user, *pass;
+{
+ char tmp[80];
+ char *acct;
+ char anonpass[MAXLOGNAME + 1 + MAXHOSTNAMELEN]; /* "user@hostname" */
+ char hostname[MAXHOSTNAMELEN];
+ int n, aflag = 0;
+
+ acct = NULL;
+ if (user == NULL) {
+ if (ruserpass(host, &user, &pass, &acct) < 0) {
+ code = -1;
+ return (0);
+ }
+ }
+
+ /*
+ * 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();
+ 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"; /* as per RFC 1635 */
+ }
+
+ while (user == NULL) {
+ char *myname = getlogin();
+
+ if (myname == NULL) {
+ struct passwd *pp = getpwuid(getuid());
+
+ if (pp != NULL)
+ myname = pp->pw_name;
+ }
+ if (myname)
+ printf("Name (%s:%s): ", host, myname);
+ else
+ printf("Name (%s): ", host);
+ (void)fgets(tmp, sizeof(tmp) - 1, stdin);
+ tmp[strlen(tmp) - 1] = '\0';
+ if (*tmp == '\0')
+ user = myname;
+ else
+ user = tmp;
+ }
+ n = command("USER %s", user);
+ if (n == CONTINUE) {
+ if (pass == NULL)
+ pass = getpass("Password:");
+ n = command("PASS %s", pass);
+ }
+ if (n == CONTINUE) {
+ aflag++;
+ if (acct == NULL)
+ acct = getpass("Account:");
+ n = command("ACCT %s", acct);
+ }
+ if ((n != COMPLETE) ||
+ (!aflag && acct != NULL && command("ACCT %s", acct) != COMPLETE)) {
+ warnx("Login failed.");
+ return (0);
+ }
+ if (proxy)
+ return (1);
+ connected = -1;
+ for (n = 0; n < macnum; ++n) {
+ if (!strcmp("init", macros[n].mac_name)) {
+ (void)strcpy(line, "$init");
+ makeargv();
+ domacro(margc, margv);
+ break;
+ }
+ }
+ return (1);
+}
+
+/*
+ * `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) {
+ puts("sorry, arguments too long.");
+ 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);
+}
+
+/*
+ * glob files given in argv[] from the remote server.
+ * if errbuf isn't NULL, store error messages there instead
+ * of writing to the screen.
+ */
+char *
+remglob(argv, doswitch, errbuf)
+ char *argv[];
+ int doswitch;
+ char **errbuf;
+{
+ 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);
+ if ((fd = mkstemp(temp)) < 0) {
+ warn("unable to create temporary file %s", temp);
+ return (NULL);
+ }
+ close(fd);
+ oldverbose = verbose;
+ verbose = (errbuf != NULL) ? -1 : 0;
+ oldhash = hash;
+ hash = 0;
+ if (doswitch)
+ pswitch(!proxy);
+ for (mode = "w"; *++argv != NULL; mode = "a")
+ recvrequest("NLST", temp, *argv, mode, 0);
+ if ((code / 100) != COMPLETE) {
+ if (errbuf != NULL)
+ *errbuf = reply_string;
+ }
+ if (doswitch)
+ pswitch(!proxy);
+ verbose = oldverbose;
+ hash = oldhash;
+ ftemp = fopen(temp, "r");
+ (void)unlink(temp);
+ if (ftemp == NULL) {
+ if (errbuf == NULL)
+ puts("can't find list of remote files, oops.");
+ else
+ *errbuf =
+ "can't find list of remote files, oops.";
+ 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;
+ puts("Interactive mode: off.");
+ 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)
+ puts(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)
+ puts(reply_string);
+ verbose = overbose;
+ return (rtime);
+}
+
+void
+updateprogressmeter()
+{
+ static pid_t pgrp = -1;
+ int ctty_pgrp;
+
+ if (pgrp == -1)
+ pgrp = getpgrp();
+
+ /*
+ * print progress bar only if we are foreground process.
+ */
+ if (ioctl(STDOUT_FILENO, TIOCGPGRP, &ctty_pgrp) != -1 &&
+ ctty_pgrp == (int)pgrp)
+ 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 || cursize > filesize) {
+ 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
+ && bytes + restart_point <= filesize) {
+ remaining = (int)((filesize - restart_point) /
+ (bytes / elapsed) - elapsed);
+ hh = remaining / 3600;
+ remaining %= 3600;
+ /* "buf+len(buf) -1" to overwrite \n */
+ 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)
+ fputs(p, stdout);
+ if (j * lines + i + lines >= sl->sl_cur) {
+ putchar('\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);
+}
+
+/*
+ * Setup or cleanup EditLine structures
+ */
+#ifndef SMALL
+void
+controlediting()
+{
+ if (editing && el == NULL && hist == NULL) {
+ 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 */
+ el_set(el, EL_SIGNAL, 1);
+ } else if (!editing) {
+ if (hist) {
+ history_end(hist);
+ hist = NULL;
+ }
+ if (el) {
+ el_end(el);
+ el = NULL;
+ }
+ }
+}
+#endif /* !SMALL */
OpenPOWER on IntegriCloud