/*
- * Copyright (c) 1985 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1985, 1989, 1993, 1994
+ * The Regents of the University of California. All rights reserved.
*
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley. The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * %sccs.include.redist.c%
*/
#ifndef lint
-static char sccsid[] = "@(#)cmds.c 5.10 (Berkeley) %G%";
+static char sccsid[] = "@(#)cmds.c 8.4 (Berkeley) %G%";
#endif /* not lint */
/*
* FTP User Program -- Command Routines.
*/
-#include "ftp_var.h"
+#include <sys/param.h>
+#include <sys/wait.h>
+#include <sys/stat.h>
#include <sys/socket.h>
-
+#include <netinet/in.h>
#include <arpa/ftp.h>
+#include <ctype.h>
+#include <err.h>
+#include <netdb.h>
#include <signal.h>
#include <stdio.h>
-#include <errno.h>
-#include <netdb.h>
-#include <ctype.h>
-#include <sys/wait.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include "ftp_var.h"
+#include "pathnames.h"
-extern char *globerr;
-extern char **glob();
-extern char *home;
-extern short gflag;
-extern char *remglob();
-extern char *getenv();
-extern char *index();
-extern char *rindex();
-char *mname;
-jmp_buf jabort;
-char *dotrans(), *domap();
+jmp_buf jabort;
+char *mname;
+
+/*
+ * `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, *hookup();
- int port;
+ char *host;
+ short port;
if (connected) {
printf("Already connected to %s, use close first.\n",
code = -1;
return;
}
- if (argc < 2) {
- (void) strcat(line, " ");
- printf("(to) ");
- (void) gets(&line[strlen(line)]);
- makeargv();
- argc = margc;
- argv = margv;
- }
- if (argc > 3) {
+ 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;
}
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 */
}
}
{ "image", "I", TYPE_I, 0 },
{ "ebcdic", "E", TYPE_E, 0 },
{ "tenex", "L", TYPE_L, bytename },
- 0
+ { NULL }
};
/*
* Set transfer type.
*/
+void
settype(argc, argv)
+ int argc;
char *argv[];
{
- register struct types *p;
+ struct types *p;
int comret;
if (argc > 2) {
sep = " ";
for (p = types; p->t_name; p++) {
printf("%s%s", sep, p->t_name);
- if (*sep == ' ')
- sep = " | ";
+ sep = " | ";
}
printf(" ]\n");
code = -1;
comret = command("TYPE %s", p->t_mode);
if (comret == COMPLETE) {
(void) strcpy(typename, p->t_name);
- type = p->t_type;
+ curtype = type = p->t_type;
}
}
/*
- * Set binary transfer type.
+ * Internal form of settype; changes current type in use with server
+ * without changing our notion of the type for data transfers.
+ * Used to change to and from ascii for listings.
*/
-/*VARARGS*/
-setbinary()
+void
+changetype(newtype, show)
+ int newtype, show;
{
+ struct types *p;
+ int comret, oldverbose = verbose;
- call(settype, "type", "binary", 0);
+ if (newtype == 0)
+ newtype = TYPE_I;
+ if (newtype == curtype)
+ return;
+ if (debug == 0 && show == 0)
+ verbose = 0;
+ for (p = types; p->t_name; p++)
+ if (newtype == p->t_type)
+ break;
+ if (p->t_name == 0) {
+ printf("ftp: internal error: unknown type %d\n", newtype);
+ return;
+ }
+ if (newtype == TYPE_L && bytename[0] != '\0')
+ comret = command("TYPE %s %s", p->t_mode, bytename);
+ else
+ comret = command("TYPE %s", p->t_mode);
+ if (comret == COMPLETE)
+ curtype = newtype;
+ verbose = oldverbose;
}
+char *stype[] = {
+ "type",
+ "",
+ 0
+};
+
/*
- * Set ascii transfer type.
+ * Set binary transfer type.
*/
/*VARARGS*/
-setascii()
+void
+setbinary(argc, argv)
+ int argc;
+ char **argv;
{
- call(settype, "type", "ascii", 0);
+ stype[1] = "binary";
+ settype(2, stype);
}
/*
- * Set tenex transfer type.
+ * Set ascii transfer type.
*/
/*VARARGS*/
-settenex()
+void
+setascii(argc, argv)
+ int argc;
+ char *argv[];
{
- call(settype, "type", "tenex", 0);
+ stype[1] = "ascii";
+ settype(2, stype);
}
/*
- * Set ebcdic transfer type.
+ * Set tenex transfer type.
*/
/*VARARGS*/
-setebcdic()
+void
+settenex(argc, argv)
+ int argc;
+ char *argv[];
{
- call(settype, "type", "ebcdic", 0);
+ stype[1] = "tenex";
+ settype(2, stype);
}
/*
* Set file transfer mode.
*/
/*ARGSUSED*/
-setmode(argc, argv)
+void
+setftmode(argc, argv)
+ int argc;
char *argv[];
{
* Set file transfer format.
*/
/*ARGSUSED*/
+void
setform(argc, argv)
+ int argc;
char *argv[];
{
* Set file transfer structure.
*/
/*ARGSUSED*/
+void
setstruct(argc, argv)
+ int argc;
char *argv[];
{
/*
* Send a single file.
*/
+void
put(argc, argv)
int argc;
char *argv[];
{
char *cmd;
int loc = 0;
- char *oldargv1;
+ char *oldargv1, *oldargv2;
if (argc == 2) {
argc++;
argv[2] = argv[1];
loc++;
}
- if (argc < 2) {
- (void) strcat(line, " ");
- printf("(local-file) ");
- (void) gets(&line[strlen(line)]);
- makeargv();
- argc = margc;
- argv = margv;
- }
- if (argc < 2) {
+ if (argc < 2 && !another(&argc, &argv, "local-file"))
+ goto usage;
+ if (argc < 3 && !another(&argc, &argv, "remote-file")) {
usage:
- printf("usage:%s local-file remote-file\n", argv[0]);
+ printf("usage: %s local-file remote-file\n", argv[0]);
code = -1;
return;
}
- if (argc < 3) {
- (void) strcat(line, " ");
- printf("(remote-file) ");
- (void) gets(&line[strlen(line)]);
- makeargv();
- argc = margc;
- argv = margv;
- }
- if (argc < 3)
- goto usage;
oldargv1 = argv[1];
+ oldargv2 = argv[2];
if (!globulize(&argv[1])) {
code = -1;
return;
if (loc && mapflag) {
argv[2] = domap(argv[2]);
}
- sendrequest(cmd, argv[1], argv[2]);
+ sendrequest(cmd, argv[1], argv[2],
+ argv[1] != oldargv1 || argv[2] != oldargv2);
}
/*
* Send multiple files.
*/
+void
mput(argc, argv)
- char *argv[];
+ int argc;
+ char **argv;
{
- register int i;
- int ointer, (*oldintr)(), mabort();
- extern jmp_buf jabort;
+ int i;
+ sig_t oldintr;
+ int ointer;
char *tp;
- if (argc < 2) {
- (void) strcat(line, " ");
- printf("(local-files) ");
- (void) gets(&line[strlen(line)]);
- makeargv();
- argc = margc;
- argv = margv;
- }
- if (argc < 2) {
- printf("usage:%s local-files\n", argv[0]);
+ if (argc < 2 && !another(&argc, &argv, "local-files")) {
+ printf("usage: %s local-files\n", argv[0]);
code = -1;
return;
}
if (mapflag) {
tp = domap(tp);
}
- sendrequest((sunique) ? "STOU" : "STOR", cp,tp);
+ sendrequest((sunique) ? "STOU" : "STOR",
+ cp, tp, cp != tp || !interactive);
if (!mflag && fromatty) {
ointer = interactive;
interactive = 1;
return;
}
for (i = 1; i < argc; i++) {
- register char **cpp, **gargs;
+ char **cpp, **gargs;
if (!doglob) {
if (mflag && confirm(argv[0], argv[i])) {
tp = (ntflag) ? dotrans(argv[i]) : argv[i];
tp = (mapflag) ? domap(tp) : tp;
sendrequest((sunique) ? "STOU" : "STOR",
- argv[i], tp);
+ argv[i], tp, tp != argv[i] || !interactive);
if (!mflag && fromatty) {
ointer = interactive;
interactive = 1;
}
continue;
}
- gargs = glob(argv[i]);
+ gargs = ftpglob(argv[i]);
if (globerr != NULL) {
printf("%s\n", globerr);
- if (gargs)
+ if (gargs) {
blkfree(gargs);
+ free((char *)gargs);
+ }
continue;
}
for (cpp = gargs; cpp && *cpp != NULL; cpp++) {
tp = (ntflag) ? dotrans(*cpp) : *cpp;
tp = (mapflag) ? domap(tp) : tp;
sendrequest((sunique) ? "STOU" : "STOR",
- *cpp, tp);
+ *cpp, tp, *cpp != tp || !interactive);
if (!mflag && fromatty) {
ointer = interactive;
interactive = 1;
}
}
}
- if (gargs != NULL)
+ if (gargs != NULL) {
blkfree(gargs);
+ free((char *)gargs);
+ }
}
(void) signal(SIGINT, oldintr);
mflag = 0;
}
+void
+
+void
/*
* Receive one file.
*/
+int
get(argc, argv)
char *argv[];
{
int loc = 0;
+ char *oldargv1, *oldargv2;
if (argc == 2) {
argc++;
argv[2] = argv[1];
loc++;
}
- if (argc < 2) {
- (void) strcat(line, " ");
- printf("(remote-file) ");
- (void) gets(&line[strlen(line)]);
- makeargv();
- argc = margc;
- argv = margv;
- }
- if (argc < 2) {
+ if (argc < 2 && !another(&argc, &argv, "remote-file"))
+ goto usage;
+ if (argc < 3 && !another(&argc, &argv, "local-file")) {
usage:
printf("usage: %s remote-file [ local-file ]\n", argv[0]);
code = -1;
return;
}
- if (argc < 3) {
- (void) strcat(line, " ");
- printf("(local-file) ");
- (void) gets(&line[strlen(line)]);
- makeargv();
- argc = margc;
- argv = margv;
- }
- if (argc < 3)
- goto usage;
+ oldargv1 = argv[1];
+ oldargv2 = argv[2];
if (!globulize(&argv[2])) {
code = -1;
return;
argv[2] = tmpbuf;
}
}
- if (loc && ntflag) {
+ if (loc && ntflag)
argv[2] = dotrans(argv[2]);
- }
- if (loc && mapflag) {
+ if (loc && mapflag)
argv[2] = domap(argv[2]);
- }
recvrequest("RETR", argv[2], argv[1], "w");
}
-mabort()
+/* ARGSUSED */
+void
+mabort(signo)
+ int signo;
{
int ointer;
- extern jmp_buf jabort;
printf("\n");
(void) fflush(stdout);
/*
* Get multiple files.
*/
+void
mget(argc, argv)
- char *argv[];
+ int argc;
+ char **argv;
{
+ sig_t oldintr;
+ int ch, ointer;
char *cp, *tp, *tp2, tmpbuf[MAXPATHLEN];
- int ointer, (*oldintr)(), mabort();
- extern jmp_buf jabort;
- if (argc < 2) {
- (void) strcat(line, " ");
- printf("(remote-files) ");
- (void) gets(&line[strlen(line)]);
- makeargv();
- argc = margc;
- argv = margv;
- }
- if (argc < 2) {
- printf("usage:%s remote-files\n", argv[0]);
+ if (argc < 2 && !another(&argc, &argv, "remote-files")) {
+ printf("usage: %s remote-files\n", argv[0]);
code = -1;
return;
}
mname = argv[0];
mflag = 1;
- oldintr = signal(SIGINT,mabort);
+ oldintr = signal(SIGINT, mabort);
(void) setjmp(jabort);
while ((cp = remglob(argv,proxy)) != NULL) {
if (*cp == '\0') {
if (mflag && confirm(argv[0], cp)) {
tp = cp;
if (mcase) {
- while (*tp && !islower(*tp)) {
- tp++;
- }
- if (!*tp) {
- tp = cp;
- tp2 = tmpbuf;
- while ((*tp2 = *tp) != NULL) {
- if (isupper(*tp2)) {
- *tp2 = 'a' + *tp2 - 'A';
- }
- tp++;
- tp2++;
- }
- }
+ for (tp2 = tmpbuf; ch = *tp++;)
+ *tp2++ = isupper(ch) ? tolower(ch) : ch;
tp = tmpbuf;
}
if (ntflag) {
if (mapflag) {
tp = domap(tp);
}
- recvrequest("RETR", tp, cp, "w");
+ recvrequest("RETR", tp, cp, "w",
+ tp != cp || !interactive);
if (!mflag && fromatty) {
ointer = interactive;
interactive = 1;
ftemp = NULL;
}
}
- return(NULL);
+ return (NULL);
}
if (!doglob) {
if (args == NULL)
return (cp);
}
if (ftemp == NULL) {
- (void) strcpy(temp, "/tmp/ftpXXXXXX");
+ (void) strcpy(temp, _PATH_TMP);
(void) mktemp(temp);
oldverbose = verbose, verbose = 0;
oldhash = hash, hash = 0;
pswitch(!proxy);
}
for (mode = "w"; *++argv != NULL; mode = "a")
- recvrequest ("NLST", temp, *argv, mode);
+ recvrequest ("NLST", temp, *argv, mode, 0);
if (doswitch) {
pswitch(!proxy);
}
(void) fclose(ftemp), ftemp = NULL;
return (NULL);
}
- if ((cp = index(buf, '\n')) != NULL)
+ if ((cp = strchr(buf, '\n')) != NULL)
*cp = '\0';
return (buf);
}
* Show status.
*/
/*ARGSUSED*/
+void
status(argc, argv)
+ int argc;
char *argv[];
{
int i;
* Set beep on cmd completed mode.
*/
/*VARARGS*/
-setbell()
+void
+setbell(argc, argv)
+ int argc;
+ char *argv[];
{
bell = !bell;
* Turn on packet tracing.
*/
/*VARARGS*/
-settrace()
+void
+settrace(argc, argv)
+ int argc;
+ char *argv[];
{
trace = !trace;
* Toggle hash mark printing during transfers.
*/
/*VARARGS*/
-sethash()
+void
+sethash(argc, argv)
+ int argc;
+ char *argv[];
{
hash = !hash;
printf("Hash mark printing %s", onoff(hash));
code = hash;
if (hash)
- printf(" (%d bytes/hash mark)", BUFSIZ);
+ printf(" (%d bytes/hash mark)", 1024);
printf(".\n");
}
* Turn on printing of server echo's.
*/
/*VARARGS*/
-setverbose()
+void
+setverbose(argc, argv)
+ int argc;
+ char *argv[];
{
verbose = !verbose;
* Toggle PORT cmd use before each data connection.
*/
/*VARARGS*/
-setport()
+void
+setport(argc, argv)
+ int argc;
+ char *argv[];
{
sendport = !sendport;
* during mget, mput, and mdelete.
*/
/*VARARGS*/
-setprompt()
+void
+setprompt(argc, argv)
+ int argc;
+ char *argv[];
{
interactive = !interactive;
* on local file names.
*/
/*VARARGS*/
-setglob()
+void
+setglob(argc, argv)
+ int argc;
+ char *argv[];
{
doglob = !doglob;
* set level of debugging.
*/
/*VARARGS*/
+void
setdebug(argc, argv)
+ int argc;
char *argv[];
{
int val;
* Set current working directory
* on remote machine.
*/
+void
cd(argc, argv)
+ int argc;
char *argv[];
{
- if (argc < 2) {
- (void) strcat(line, " ");
- printf("(remote-directory) ");
- (void) gets(&line[strlen(line)]);
- makeargv();
- argc = margc;
- argv = margv;
- }
- if (argc < 2) {
- printf("usage:%s remote-directory\n", argv[0]);
+ if (argc < 2 && !another(&argc, &argv, "remote-directory")) {
+ printf("usage: %s remote-directory\n", argv[0]);
code = -1;
return;
}
- (void) command("CWD %s", argv[1]);
+ if (command("CWD %s", argv[1]) == ERROR && code == 500) {
+ if (verbose)
+ printf("CWD command not recognized, trying XCWD\n");
+ (void) command("XCWD %s", argv[1]);
+ }
}
/*
* Set current working directory
* on local machine.
*/
+void
lcd(argc, argv)
+ int argc;
char *argv[];
{
char buf[MAXPATHLEN];
if (argc < 2)
argc++, argv[1] = home;
if (argc != 2) {
- printf("usage:%s local-directory\n", argv[0]);
+ printf("usage: %s local-directory\n", argv[0]);
code = -1;
return;
}
return;
}
if (chdir(argv[1]) < 0) {
- perror(argv[1]);
+ warn("local: %s", argv[1]);
code = -1;
return;
}
/*
* Delete a single file.
*/
+void
delete(argc, argv)
+ int argc;
char *argv[];
{
- if (argc < 2) {
- (void) strcat(line, " ");
- printf("(remote-file) ");
- (void) gets(&line[strlen(line)]);
- makeargv();
- argc = margc;
- argv = margv;
- }
- if (argc < 2) {
- printf("usage:%s remote-file\n", argv[0]);
+ if (argc < 2 && !another(&argc, &argv, "remote-file")) {
+ printf("usage: %s remote-file\n", argv[0]);
code = -1;
return;
}
/*
* Delete multiple files.
*/
+void
mdelete(argc, argv)
- char *argv[];
+ int argc;
+ char **argv;
{
+ sig_t oldintr;
+ int ointer;
char *cp;
- int ointer, (*oldintr)(), mabort();
- extern jmp_buf jabort;
- if (argc < 2) {
- (void) strcat(line, " ");
- printf("(remote-files) ");
- (void) gets(&line[strlen(line)]);
- makeargv();
- argc = margc;
- argv = margv;
- }
- if (argc < 2) {
- printf("usage:%s remote-files\n", argv[0]);
+ if (argc < 2 && !another(&argc, &argv, "remote-files")) {
+ printf("usage: %s remote-files\n", argv[0]);
code = -1;
return;
}
/*
* Rename a remote file.
*/
+void
renamefile(argc, argv)
+ int argc;
char *argv[];
{
- if (argc < 2) {
- (void) strcat(line, " ");
- printf("(from-name) ");
- (void) gets(&line[strlen(line)]);
- makeargv();
- argc = margc;
- argv = margv;
- }
- if (argc < 2) {
+ if (argc < 2 && !another(&argc, &argv, "from-name"))
+ goto usage;
+ if (argc < 3 && !another(&argc, &argv, "to-name")) {
usage:
printf("%s from-name to-name\n", argv[0]);
code = -1;
return;
}
- if (argc < 3) {
- (void) strcat(line, " ");
- printf("(to-name) ");
- (void) gets(&line[strlen(line)]);
- makeargv();
- argc = margc;
- argv = margv;
- }
- if (argc < 3)
- goto usage;
if (command("RNFR %s", argv[1]) == CONTINUE)
(void) command("RNTO %s", argv[2]);
}
* Get a directory listing
* of remote files.
*/
+void
ls(argc, argv)
+ int argc;
char *argv[];
{
char *cmd;
code = -1;
return;
}
- cmd = argv[0][0] == 'l' ? "NLST" : "LIST";
+ cmd = argv[0][0] == 'n' ? "NLST" : "LIST";
if (strcmp(argv[2], "-") && !globulize(&argv[2])) {
code = -1;
return;
code = -1;
return;
}
- recvrequest(cmd, argv[2], argv[1], "w");
+ recvrequest(cmd, argv[2], argv[1], "w", 0);
}
/*
* Get a directory listing
* of multiple remote files.
*/
+void
mls(argc, argv)
- char *argv[];
+ int argc;
+ char **argv;
{
+ sig_t oldintr;
+ int ointer, i;
char *cmd, mode[1], *dest;
- int ointer, i, (*oldintr)(), mabort();
- extern jmp_buf jabort;
- if (argc < 2) {
- (void) strcat(line, " ");
- printf("(remote-files) ");
- (void) gets(&line[strlen(line)]);
- makeargv();
- argc = margc;
- argv = margv;
- }
- if (argc < 3) {
- (void) strcat(line, " ");
- printf("(local-file) ");
- (void) gets(&line[strlen(line)]);
- makeargv();
- argc = margc;
- argv = margv;
- }
- if (argc < 3) {
- printf("usage:%s remote-files local-file\n", argv[0]);
+ if (argc < 2 && !another(&argc, &argv, "remote-files"))
+ goto usage;
+ if (argc < 3 && !another(&argc, &argv, "local-file")) {
+usage:
+ printf("usage: %s remote-files local-file\n", argv[0]);
code = -1;
return;
}
dest = argv[argc - 1];
argv[argc - 1] = NULL;
if (strcmp(dest, "-") && *dest != '|')
- if (!globulize(&dest) || !confirm("output to local-file:", dest)) {
+ if (!globulize(&dest) ||
+ !confirm("output to local-file:", dest)) {
code = -1;
return;
}
(void) setjmp(jabort);
for (i = 1; mflag && i < argc-1; ++i) {
*mode = (i == 1) ? 'w' : 'a';
- recvrequest(cmd, dest, argv[i], mode);
+ recvrequest(cmd, dest, argv[i], mode, 0);
if (!mflag && fromatty) {
ointer = interactive;
interactive = 1;
* Do a shell escape
*/
/*ARGSUSED*/
+void
shell(argc, argv)
- char *argv[];
+ int argc;
+ char **argv;
{
- int pid, (*old1)(), (*old2)();
+ pid_t pid;
+ sig_t old1, old2;
char shellnam[40], *shell, *namep;
union wait status;
(void) signal(SIGQUIT, SIG_DFL);
shell = getenv("SHELL");
if (shell == NULL)
- shell = "/bin/sh";
- namep = rindex(shell,'/');
+ shell = _PATH_BSHELL;
+ namep = strrchr(shell,'/');
if (namep == NULL)
namep = shell;
(void) strcpy(shellnam,"-");
else {
execl(shell,shellnam,(char *)0);
}
- perror(shell);
+ warn("%s", shell);
code = -1;
exit(1);
- }
+ }
if (pid > 0)
- while (wait(&status) != pid)
+ while (wait((int *)&status) != pid)
;
(void) signal(SIGINT, old1);
(void) signal(SIGQUIT, old2);
if (pid == -1) {
- perror("Try again later");
+ warn("%s", "Try again later");
code = -1;
}
else {
code = 0;
}
- return (0);
}
/*
* Send new user information (re-login)
*/
+void
user(argc, argv)
int argc;
char **argv;
{
- char acct[80], *getpass();
+ char acct[80];
int n, aflag = 0;
- if (argc < 2) {
- (void) strcat(line, " ");
- printf("(username) ");
- (void) gets(&line[strlen(line)]);
- makeargv();
- argc = margc;
- argv = margv;
- }
- if (argc > 4) {
+ if (argc < 2)
+ (void) another(&argc, &argv, "username");
+ if (argc < 2 || argc > 4) {
printf("usage: %s username [password] [account]\n", argv[0]);
code = -1;
- return (0);
+ return;
}
n = command("USER %s", argv[1]);
if (n == CONTINUE) {
}
if (n != COMPLETE) {
fprintf(stdout, "Login failed.\n");
- return (0);
+ return;
}
if (!aflag && argc == 4) {
(void) command("ACCT %s", argv[3]);
}
- return (1);
}
/*
* Print working directory.
*/
/*VARARGS*/
-pwd()
+void
+pwd(argc, argv)
+ int argc;
+ char *argv[];
{
+ int oldverbose = verbose;
- (void) command("PWD");
+ /*
+ * If we aren't verbose, this doesn't do anything!
+ */
+ verbose = 1;
+ if (command("PWD") == ERROR && code == 500) {
+ printf("PWD command not recognized, trying XPWD\n");
+ (void) command("XPWD");
+ }
+ verbose = oldverbose;
}
/*
* Make a directory.
*/
+void
makedir(argc, argv)
+ int argc;
char *argv[];
{
- if (argc < 2) {
- (void) strcat(line, " ");
- printf("(directory-name) ");
- (void) gets(&line[strlen(line)]);
- makeargv();
- argc = margc;
- argv = margv;
- }
- if (argc < 2) {
+ if (argc < 2 && !another(&argc, &argv, "directory-name")) {
printf("usage: %s directory-name\n", argv[0]);
code = -1;
return;
}
- (void) command("MKD %s", argv[1]);
+ 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]);
+ }
}
/*
* Remove a directory.
*/
+void
removedir(argc, argv)
+ int argc;
char *argv[];
{
- if (argc < 2) {
- (void) strcat(line, " ");
- printf("(directory-name) ");
- (void) gets(&line[strlen(line)]);
- makeargv();
- argc = margc;
- argv = margv;
- }
- if (argc < 2) {
+ if (argc < 2 && !another(&argc, &argv, "directory-name")) {
printf("usage: %s directory-name\n", argv[0]);
code = -1;
return;
}
- (void) command("RMD %s", argv[1]);
+ 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]);
+ }
}
/*
* Send a line, verbatim, to the remote machine.
*/
+void
quote(argc, argv)
+ int argc;
char *argv[];
{
- int i;
- char buf[BUFSIZ];
- if (argc < 2) {
- (void) strcat(line, " ");
- printf("(command line to send) ");
- (void) gets(&line[strlen(line)]);
- makeargv();
- argc = margc;
- argv = margv;
+ if (argc < 2 && !another(&argc, &argv, "command line to send")) {
+ printf("usage: %s line-to-send\n", argv[0]);
+ code = -1;
+ return;
}
- if (argc < 2) {
+ quote1("", argc, argv);
+}
+
+/*
+ * Send a SITE command to the remote machine. The line
+ * is sent verbatim to the remote machine, except that the
+ * word "SITE" is added at the front.
+ */
+void
+site(argc, argv)
+ int argc;
+ char *argv[];
+{
+
+ if (argc < 2 && !another(&argc, &argv, "arguments to SITE command")) {
printf("usage: %s line-to-send\n", argv[0]);
code = -1;
return;
}
- (void) strcpy(buf, argv[1]);
- for (i = 2; i < argc; i++) {
- (void) strcat(buf, " ");
- (void) strcat(buf, argv[i]);
+ quote1("SITE ", argc, argv);
+}
+
+/*
+ * Turn argv[1..argc) into a space-separated string, then prepend initial text.
+ * Send the result as a one-line command and get response.
+ */
+void
+quote1(initial, argc, argv)
+ char *initial;
+ int argc;
+ char **argv;
+{
+ int i, len;
+ char buf[BUFSIZ]; /* must be >= sizeof(line) */
+
+ (void) strcpy(buf, initial);
+ if (argc > 1) {
+ len = strlen(buf);
+ len += strlen(strcpy(&buf[len], argv[1]));
+ for (i = 2; i < argc; i++) {
+ buf[len++] = ' ';
+ len += strlen(strcpy(&buf[len], argv[i]));
+ }
}
if (command(buf) == PRELIM) {
- while (getreply(0) == PRELIM);
+ while (getreply(0) == PRELIM)
+ continue;
+ }
+}
+
+void
+do_chmod(argc, argv)
+ int argc;
+ char *argv[];
+{
+
+ if (argc < 2 && !another(&argc, &argv, "mode"))
+ goto usage;
+ if (argc < 3 && !another(&argc, &argv, "file-name")) {
+usage:
+ printf("usage: %s mode file-name\n", argv[0]);
+ code = -1;
+ return;
}
+ (void) command("SITE CHMOD %s %s", argv[1], argv[2]);
+}
+
+void
+do_umask(argc, argv)
+ int argc;
+ char *argv[];
+{
+ int oldverbose = verbose;
+
+ verbose = 1;
+ (void) command(argc == 1 ? "SITE UMASK" : "SITE UMASK %s", argv[1]);
+ verbose = oldverbose;
+}
+
+void
+idle(argc, argv)
+ int argc;
+ char *argv[];
+{
+ int oldverbose = verbose;
+
+ verbose = 1;
+ (void) command(argc == 1 ? "SITE IDLE" : "SITE IDLE %s", argv[1]);
+ verbose = oldverbose;
}
/*
* Ask the other side for help.
*/
+void
rmthelp(argc, argv)
+ int argc;
char *argv[];
{
int oldverbose = verbose;
* Terminate session and exit.
*/
/*VARARGS*/
-quit()
+void
+quit(argc, argv)
+ int argc;
+ char *argv[];
{
if (connected)
- disconnect();
+ disconnect(0, 0);
pswitch(1);
if (connected) {
- disconnect();
+ disconnect(0, 0);
}
exit(0);
}
/*
* Terminate session, but don't exit.
*/
-disconnect()
+void
+disconnect(argc, argv)
+ int argc;
+ char *argv[];
{
- extern FILE *cout;
- extern int data;
if (!connected)
return;
}
}
+int
confirm(cmd, file)
char *cmd, *file;
{
return (1);
printf("%s %s? ", cmd, file);
(void) fflush(stdout);
- (void) gets(line);
+ if (fgets(line, sizeof line, stdin) == NULL)
+ return (0);
return (*line != 'n' && *line != 'N');
}
+void
fatal(msg)
char *msg;
{
- fprintf(stderr, "ftp: %s\n", msg);
- exit(1);
+ errx(1, "%s", msg);
}
/*
* Can't control multiple values being expanded
* from the expression, we return only the first.
*/
+int
globulize(cpp)
char **cpp;
{
if (!doglob)
return (1);
- globbed = glob(*cpp);
+ globbed = ftpglob(*cpp);
if (globerr != NULL) {
printf("%s: %s\n", *cpp, globerr);
- if (globbed)
+ if (globbed) {
blkfree(globbed);
+ free((char *)globbed);
+ }
return (0);
}
if (globbed) {
*cpp = *globbed++;
/* don't waste too much memory */
- if (*globbed)
+ if (globbed) {
blkfree(globbed);
+ free((char *)*globbed);
+ }
}
return (1);
}
+void
account(argc,argv)
int argc;
char **argv;
jmp_buf abortprox;
+void
proxabort()
{
- extern int proxy;
if (!proxy) {
pswitch(1);
longjmp(abortprox,1);
}
-doproxy(argc,argv)
+void
+doproxy(argc, argv)
int argc;
char *argv[];
{
- int (*oldintr)(), proxabort();
- register struct cmd *c;
- struct cmd *getcmd();
- extern struct cmd cmdtab[];
- extern jmp_buf abortprox;
+ struct cmd *c;
+ sig_t oldintr;
- if (argc < 2) {
- (void) strcat(line, " ");
- printf("(command) ");
- (void) gets(&line[strlen(line)]);
- makeargv();
- argc = margc;
- argv = margv;
- }
- if (argc < 2) {
- printf("usage:%s command\n", argv[0]);
+ if (argc < 2 && !another(&argc, &argv, "command")) {
+ printf("usage: %s command\n", argv[0]);
code = -1;
return;
}
(void) signal(SIGINT, oldintr);
}
-setcase()
+void
+setcase(argc, argv)
+ int argc;
+ char *argv[];
{
+
mcase = !mcase;
printf("Case mapping %s.\n", onoff(mcase));
code = mcase;
}
-setcr()
+void
+setcr(argc, argv)
+ int argc;
+ char *argv[];
{
+
crflag = !crflag;
printf("Carriage Return stripping %s.\n", onoff(crflag));
code = crflag;
}
+void
setntrans(argc,argv)
int argc;
char *argv[];
{
static char new[MAXPATHLEN];
char *cp1, *cp2 = new;
- register int i, ostop, found;
+ int i, ostop, found;
- for (ostop = 0; *(ntout + ostop) && ostop < 16; ostop++);
+ for (ostop = 0; *(ntout + ostop) && ostop < 16; ostop++)
+ continue;
for (cp1 = name; *cp1; cp1++) {
found = 0;
for (i = 0; *(ntin + i) && i < 16; i++) {
}
}
*cp2 = '\0';
- return(new);
+ return (new);
}
+void
setnmap(argc, argv)
int argc;
char *argv[];
code = mapflag;
return;
}
- if (argc < 3) {
- (void) strcat(line, " ");
- printf("(mapout) ");
- (void) gets(&line[strlen(line)]);
- makeargv();
- argc = margc;
- argv = margv;
- }
- if (argc < 3) {
+ if (argc < 3 && !another(&argc, &argv, "mapout")) {
printf("Usage: %s [mapin mapout]\n",argv[0]);
code = -1;
return;
}
mapflag = 1;
code = 1;
- cp = index(altarg, ' ');
+ cp = strchr(altarg, ' ');
if (proxy) {
- while(*++cp == ' ');
+ while(*++cp == ' ')
+ continue;
altarg = cp;
- cp = index(altarg, ' ');
+ cp = strchr(altarg, ' ');
}
*cp = '\0';
(void) strncpy(mapin, altarg, MAXPATHLEN - 1);
- while (*++cp == ' ');
+ while (*++cp == ' ')
+ continue;
(void) strncpy(mapout, cp, MAXPATHLEN - 1);
}
char *name;
{
static char new[MAXPATHLEN];
- register char *cp1 = name, *cp2 = mapin;
+ char *cp1 = name, *cp2 = mapin;
char *tp[9], *te[9];
- int i, toks[9], toknum, match = 1;
+ int i, toks[9], toknum = 0, match = 1;
for (i=0; i < 9; ++i) {
toks[i] = 0;
cp2++;
break;
}
- /* intentional drop through */
+ /* FALLTHROUGH */
default:
if (*cp2 != *cp1) {
match = 0;
}
break;
}
- if (*cp1) {
+ if (match && *cp1) {
cp1++;
}
- if (*cp2) {
+ if (match && *cp2) {
cp2++;
}
}
+ if (!match && *cp1) /* last token mismatch */
+ {
+ toks[toknum] = 0;
+ }
cp1 = new;
*cp1 = '\0';
cp2 = mapout;
}
if (!*cp2) {
printf("nmap: unbalanced brackets\n");
- return(name);
+ return (name);
}
match = 1;
cp2--;
}
if (!*cp2) {
printf("nmap: unbalanced brackets\n");
- return(name);
+ return (name);
}
break;
}
}
*cp1 = '\0';
if (!*new) {
- return(name);
+ return (name);
}
- return(new);
+ return (new);
}
-setsunique()
+void
+setsunique(argc, argv)
+ int argc;
+ char *argv[];
{
+
sunique = !sunique;
printf("Store unique %s.\n", onoff(sunique));
code = sunique;
}
-setrunique()
+void
+setrunique(argc, argv)
+ int argc;
+ char *argv[];
{
+
runique = !runique;
printf("Receive unique %s.\n", onoff(runique));
code = runique;
}
/* change directory to perent directory */
-cdup()
+void
+cdup(argc, argv)
+ int argc;
+ char *argv[];
{
- (void) command("CDUP");
+
+ if (command("CDUP") == ERROR && code == 500) {
+ if (verbose)
+ printf("CDUP command not recognized, trying XCUP\n");
+ (void) command("XCUP");
+ }
}
+
+/* show remote system type */
+void
+syst(argc, argv)
+ int argc;
+ char *argv[];
+{
+
+ (void) command("SYST");
+}
+
+void
macdef(argc, argv)
int argc;
char *argv[];
code = -1;
return;
}
- if (argc < 2) {
- (void) strcat(line, " ");
- printf("(macro name) ");
- (void) gets(&line[strlen(line)]);
- makeargv();
- argc = margc;
- argv = margv;
- }
- if (argc != 2) {
+ if (argc < 2 && !another(&argc, &argv, "macro name")) {
printf("Usage: %s macro_name\n",argv[0]);
code = -1;
return;
tmp++;
}
while (1) {
- while ((c = getchar()) != '\n' && c != EOF);
+ while ((c = getchar()) != '\n' && c != EOF)
+ /* LOOP */;
if (c == EOF || getchar() == '\n') {
printf("Macro not defined - 4k buffer exceeded\n");
code = -1;
}
}
}
+
+/*
+ * get size of file on remote machine
+ */
+void
+sizecmd(argc, argv)
+ int argc;
+ char *argv[];
+{
+
+ if (argc < 2 && !another(&argc, &argv, "filename")) {
+ printf("usage: %s filename\n", argv[0]);
+ code = -1;
+ return;
+ }
+ (void) command("SIZE %s", argv[1]);
+}
+
+/*
+ * get last modification time of file on remote machine
+ */
+void
+modtime(argc, argv)
+ int argc;
+ char *argv[];
+{
+ int overbose;
+
+ if (argc < 2 && !another(&argc, &argv, "filename")) {
+ 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;
+}
+
+/*
+ * show status on remote machine
+ */
+void
+rmtstatus(argc, argv)
+ int argc;
+ char *argv[];
+{
+
+ (void) command(argc > 1 ? "STAT %s" : "STAT" , argv[1]);
+}