* Copyright (c) 1985, 1989 Regents of the University of California.
* 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 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
static char sccsid
[] = "@(#)cmds.c 5.14.1.2 (Berkeley) 3/1/89";
* FTP User Program -- Command Routines.
extern char reply_string
[];
char *dotrans(), *domap();
* Connect to peer server and
* auto-login, if possible.
printf("Already connected to %s, use close first.\n",
(void) strcat(line
, " ");
(void) gets(&line
[strlen(line
)]);
printf("usage: %s host-name [port]\n", argv
[0]);
printf("%s: bad port number-- %s\n", argv
[1], argv
[2]);
printf ("usage: %s host-name [port]\n", argv
[0]);
host
= hookup(argv
[1], port
);
#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.
if (command("SYST") == COMPLETE
&& overbose
) {
cp
= index(reply_string
+4, ' ');
cp
= index(reply_string
+4, '\r');
printf("Remote system type is %s.\n",
if (!strncmp(reply_string
, "215 UNIX Type: L8", 17)) {
printf("Using %s mode to transfer files.\n",
!strncmp(reply_string
, "215 TOPS20", 10)) {
"Remember to set tenex mode when transfering binary files from this machine.\n");
{ "ascii", "A", TYPE_A
, 0 },
{ "binary", "I", TYPE_I
, 0 },
{ "image", "I", TYPE_I
, 0 },
{ "ebcdic", "E", TYPE_E
, 0 },
{ "tenex", "L", TYPE_L
, bytename
},
register struct types
*p
;
printf("usage: %s [", argv
[0]);
for (p
= types
; p
->t_name
; p
++) {
printf("%s%s", sep
, p
->t_name
);
printf("Using %s mode to transfer files.\n", typename
);
for (p
= types
; p
->t_name
; p
++)
if (strcmp(argv
[1], p
->t_name
) == 0)
printf("%s: unknown mode\n", argv
[1]);
if ((p
->t_arg
!= NULL
) && (*(p
->t_arg
) != '\0'))
comret
= command ("TYPE %s %s", p
->t_mode
, p
->t_arg
);
comret
= command("TYPE %s", p
->t_mode
);
if (comret
== COMPLETE
) {
(void) strcpy(typename
, p
->t_name
);
* Set binary transfer type.
call(settype
, "type", "binary", 0);
* Set ascii transfer type.
call(settype
, "type", "ascii", 0);
* Set tenex transfer type.
call(settype
, "type", "tenex", 0);
* Set ebcdic transfer type.
call(settype
, "type", "ebcdic", 0);
* Set file transfer mode.
printf("We only support %s mode, sorry.\n", modename
);
* Set file transfer format.
printf("We only support %s format, sorry.\n", formname
);
* Set file transfer structure.
printf("We only support %s structure, sorry.\n", structname
);
(void) strcat(line
, " ");
(void) gets(&line
[strlen(line
)]);
printf("usage:%s local-file remote-file\n", argv
[0]);
(void) strcat(line
, " ");
printf("(remote-file) ");
(void) gets(&line
[strlen(line
)]);
if (!globulize(&argv
[1])) {
* If "globulize" modifies argv[1], and argv[2] is a copy of
* the old argv[1], make it a copy of the new argv[1].
if (argv
[1] != oldargv1
&& argv
[2] == oldargv1
) {
cmd
= (argv
[0][0] == 'a') ? "APPE" : ((sunique
) ? "STOU" : "STOR");
argv
[2] = dotrans(argv
[2]);
argv
[2] = domap(argv
[2]);
sendrequest(cmd
, argv
[1], argv
[2]);
int ointer
, (*oldintr
)(), mabort();
(void) strcat(line
, " ");
printf("(local-files) ");
(void) gets(&line
[strlen(line
)]);
printf("usage:%s local-files\n", argv
[0]);
oldintr
= signal(SIGINT
, mabort
);
char *cp
, *tp2
, tmpbuf
[MAXPATHLEN
];
while ((cp
= remglob(argv
,0)) != NULL
) {
if (mflag
&& confirm(argv
[0], cp
)) {
while (*tp
&& !islower(*tp
)) {
while ((*tp2
= *tp
) != NULL
) {
sendrequest((sunique
) ? "STOU" : "STOR", cp
,tp
);
if (!mflag
&& fromatty
) {
if (confirm("Continue with","mput")) {
(void) signal(SIGINT
, oldintr
);
for (i
= 1; i
< argc
; i
++) {
register char **cpp
, **gargs
;
if (mflag
&& confirm(argv
[0], argv
[i
])) {
tp
= (ntflag
) ? dotrans(argv
[i
]) : argv
[i
];
tp
= (mapflag
) ? domap(tp
) : tp
;
sendrequest((sunique
) ? "STOU" : "STOR",
if (!mflag
&& fromatty
) {
if (confirm("Continue with","mput")) {
for (cpp
= gargs
; cpp
&& *cpp
!= NULL
; cpp
++) {
if (mflag
&& confirm(argv
[0], *cpp
)) {
tp
= (ntflag
) ? dotrans(*cpp
) : *cpp
;
tp
= (mapflag
) ? domap(tp
) : tp
;
sendrequest((sunique
) ? "STOU" : "STOR",
if (!mflag
&& fromatty
) {
if (confirm("Continue with","mput")) {
(void) signal(SIGINT
, oldintr
);
(void) strcat(line
, " ");
printf("(remote-file) ");
(void) gets(&line
[strlen(line
)]);
printf("usage: %s remote-file [ local-file ]\n", argv
[0]);
(void) strcat(line
, " ");
(void) gets(&line
[strlen(line
)]);
if (!globulize(&argv
[2])) {
char *tp
= argv
[1], *tp2
, tmpbuf
[MAXPATHLEN
];
while (*tp
&& !islower(*tp
)) {
while ((*tp2
= *tp
) != NULL
) {
argv
[2] = dotrans(argv
[2]);
argv
[2] = domap(argv
[2]);
recvrequest("RETR", argv
[2], argv
[1], "w");
if (confirm("Continue with", mname
)) {
char *cp
, *tp
, *tp2
, tmpbuf
[MAXPATHLEN
];
int ointer
, (*oldintr
)(), mabort();
(void) strcat(line
, " ");
printf("(remote-files) ");
(void) gets(&line
[strlen(line
)]);
printf("usage:%s remote-files\n", argv
[0]);
oldintr
= signal(SIGINT
,mabort
);
while ((cp
= remglob(argv
,proxy
)) != NULL
) {
if (mflag
&& confirm(argv
[0], cp
)) {
while (*tp
&& !islower(*tp
)) {
while ((*tp2
= *tp
) != NULL
) {
recvrequest("RETR", tp
, cp
, "w");
if (!mflag
&& fromatty
) {
if (confirm("Continue with","mget")) {
(void) signal(SIGINT
,oldintr
);
static char buf
[MAXPATHLEN
];
static FILE *ftemp
= NULL
;
if ((cp
= *++args
) == NULL
)
(void) strcpy(temp
, "/tmp/ftpXXXXXX");
oldverbose
= verbose
, verbose
= 0;
oldhash
= hash
, hash
= 0;
for (mode
= "w"; *++argv
!= NULL
; mode
= "a")
recvrequest ("NLST", temp
, *argv
, mode
);
verbose
= oldverbose
; hash
= oldhash
;
ftemp
= fopen(temp
, "r");
printf("can't find list of remote files, oops\n");
if (fgets(buf
, sizeof (buf
), ftemp
) == NULL
) {
(void) fclose(ftemp
), ftemp
= NULL
;
if ((cp
= index(buf
, '\n')) != NULL
)
return (bool ? "on" : "off");
printf("Connected to %s.\n", hostname
);
printf("Not connected.\n");
printf("Connected for proxy commands to %s.\n", hostname
);
printf("No proxy connection.\n");
printf("Mode: %s; Type: %s; Form: %s; Structure: %s\n",
modename
, typename
, formname
, structname
);
printf("Verbose: %s; Bell: %s; Prompting: %s; Globbing: %s\n",
onoff(verbose
), onoff(bell
), onoff(interactive
),
printf("Store unique: %s; Receive unique: %s\n", onoff(sunique
),
printf("Case: %s; CR stripping: %s\n",onoff(mcase
),onoff(crflag
));
printf("Ntrans: (in) %s (out) %s\n", ntin
,ntout
);
printf("Nmap: (in) %s (out) %s\n", mapin
, mapout
);
printf("Hash mark printing: %s; Use of PORT cmds: %s\n",
onoff(hash
), onoff(sendport
));
for (i
=0; i
<macnum
; i
++) {
printf("\t%s\n",macros
[i
].mac_name
);
* Set beep on cmd completed mode.
printf("Bell mode %s.\n", onoff(bell
));
* Turn on packet tracing.
printf("Packet tracing %s.\n", onoff(trace
));
* Toggle hash mark printing during transfers.
printf("Hash mark printing %s", onoff(hash
));
printf(" (%d bytes/hash mark)", BUFSIZ
);
* Turn on printing of server echo's.
printf("Verbose mode %s.\n", onoff(verbose
));
* Toggle PORT cmd use before each data connection.
printf("Use of PORT cmds %s.\n", onoff(sendport
));
* Turn on interactive prompting
* during mget, mput, and mdelete.
interactive
= !interactive
;
printf("Interactive mode %s.\n", onoff(interactive
));
* Toggle metacharacter interpretation
printf("Globbing %s.\n", onoff(doglob
));
* Set debugging mode on/off and/or
* set level of debugging.
printf("%s: bad debugging value.\n", argv
[1]);
printf("Debugging %s (debug=%d).\n", onoff(debug
), debug
);
* Set current working directory
(void) strcat(line
, " ");
printf("(remote-directory) ");
(void) gets(&line
[strlen(line
)]);
printf("usage:%s remote-directory\n", argv
[0]);
(void) command("CWD %s", argv
[1]);
* Set current working directory
printf("usage:%s local-directory\n", argv
[0]);
if (!globulize(&argv
[1])) {
if (chdir(argv
[1]) < 0) {
printf("Local directory now %s\n", getwd(buf
));
(void) strcat(line
, " ");
printf("(remote-file) ");
(void) gets(&line
[strlen(line
)]);
printf("usage:%s remote-file\n", argv
[0]);
(void) command("DELE %s", argv
[1]);
int ointer
, (*oldintr
)(), mabort();
(void) strcat(line
, " ");
printf("(remote-files) ");
(void) gets(&line
[strlen(line
)]);
printf("usage:%s remote-files\n", argv
[0]);
oldintr
= signal(SIGINT
, mabort
);
while ((cp
= remglob(argv
,0)) != NULL
) {
if (mflag
&& confirm(argv
[0], cp
)) {
(void) command("DELE %s", cp
);
if (!mflag
&& fromatty
) {
if (confirm("Continue with", "mdelete")) {
(void) signal(SIGINT
, oldintr
);
(void) strcat(line
, " ");
(void) gets(&line
[strlen(line
)]);
printf("%s from-name to-name\n", argv
[0]);
(void) strcat(line
, " ");
(void) gets(&line
[strlen(line
)]);
if (command("RNFR %s", argv
[1]) == CONTINUE
)
(void) command("RNTO %s", argv
[2]);
* Get a directory listing
printf("usage: %s remote-directory local-file\n", argv
[0]);
cmd
= argv
[0][0] == 'n' ? "NLST" : "LIST";
if (strcmp(argv
[2], "-") && !globulize(&argv
[2])) {
if (strcmp(argv
[2], "-") && *argv
[2] != '|')
if (!globulize(&argv
[2]) || !confirm("output to local-file:", argv
[2])) {
recvrequest(cmd
, argv
[2], argv
[1], "w");
* Get a directory listing
* of multiple remote files.
char *cmd
, mode
[1], *dest
;
int ointer
, i
, (*oldintr
)(), mabort();
(void) strcat(line
, " ");
printf("(remote-files) ");
(void) gets(&line
[strlen(line
)]);
(void) strcat(line
, " ");
(void) gets(&line
[strlen(line
)]);
printf("usage:%s remote-files local-file\n", argv
[0]);
if (strcmp(dest
, "-") && *dest
!= '|')
if (!globulize(&dest
) || !confirm("output to local-file:", dest
)) {
cmd
= argv
[0][1] == 'l' ? "NLST" : "LIST";
oldintr
= signal(SIGINT
, mabort
);
for (i
= 1; mflag
&& i
< argc
-1; ++i
) {
*mode
= (i
== 1) ? 'w' : 'a';
recvrequest(cmd
, dest
, argv
[i
], mode
);
if (!mflag
&& fromatty
) {
if (confirm("Continue with", argv
[0])) {
(void) signal(SIGINT
, oldintr
);
int pid
, (*old1
)(), (*old2
)();
char shellnam
[40], *shell
, *namep
;
old1
= signal (SIGINT
, SIG_IGN
);
old2
= signal (SIGQUIT
, SIG_IGN
);
if ((pid
= fork()) == 0) {
for (pid
= 3; pid
< 20; pid
++)
(void) signal(SIGINT
, SIG_DFL
);
(void) signal(SIGQUIT
, SIG_DFL
);
namep
= rindex(shell
,'/');
(void) strcpy(shellnam
,"-");
(void) strcat(shellnam
, ++namep
);
if (strcmp(namep
, "sh") != 0)
execl(shell
,shellnam
,"-c",altarg
,(char *)0);
execl(shell
,shellnam
,(char *)0);
while (wait(&status
) != pid
)
(void) signal(SIGINT
, old1
);
(void) signal(SIGQUIT
, old2
);
perror("Try again later");
* Send new user information (re-login)
char acct
[80], *getpass();
(void) strcat(line
, " ");
(void) gets(&line
[strlen(line
)]);
printf("usage: %s username [password] [account]\n", argv
[0]);
n
= command("USER %s", argv
[1]);
argv
[2] = getpass("Password: "), argc
++;
n
= command("PASS %s", argv
[2]);
printf("Account: "); (void) fflush(stdout
);
(void) fgets(acct
, sizeof(acct
) - 1, stdin
);
acct
[strlen(acct
) - 1] = '\0';
n
= command("ACCT %s", argv
[3]);
fprintf(stdout
, "Login failed.\n");
if (!aflag
&& argc
== 4) {
(void) command("ACCT %s", argv
[3]);
* Print working directory.
(void) strcat(line
, " ");
printf("(directory-name) ");
(void) gets(&line
[strlen(line
)]);
printf("usage: %s directory-name\n", argv
[0]);
(void) command("MKD %s", argv
[1]);
(void) strcat(line
, " ");
printf("(directory-name) ");
(void) gets(&line
[strlen(line
)]);
printf("usage: %s directory-name\n", argv
[0]);
(void) command("RMD %s", argv
[1]);
* Send a line, verbatim, to the remote machine.
(void) strcat(line
, " ");
printf("(command line to send) ");
(void) gets(&line
[strlen(line
)]);
printf("usage: %s line-to-send\n", argv
[0]);
(void) strcpy(buf
, argv
[1]);
for (i
= 2; i
< argc
; i
++) {
(void) strcat(buf
, argv
[i
]);
if (command(buf
) == PRELIM
) {
while (getreply(0) == PRELIM
);
* Ask the other side for help.
int oldverbose
= verbose
;
(void) command(argc
== 1 ? "HELP" : "HELP %s", argv
[1]);
* Terminate session and exit.
* Terminate session, but don't exit.
printf("%s %s? ", cmd
, file
);
return (*line
!= 'n' && *line
!= 'N');
fprintf(stderr
, "ftp: %s\n", 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.
printf("%s: %s\n", *cpp
, globerr
);
/* don't waste too much memory */
char acct
[50], *getpass(), *ap
;
(void) strncpy(acct
,*argv
,49);
(void) strncat(acct
,*argv
, 49-strlen(acct
));
ap
= getpass("Account:");
(void) command("ACCT %s", ap
);
int (*oldintr
)(), proxabort();
extern struct cmd cmdtab
[];
extern jmp_buf abortprox
;
(void) strcat(line
, " ");
(void) gets(&line
[strlen(line
)]);
printf("usage:%s command\n", argv
[0]);
if (c
== (struct cmd
*) -1) {
printf("?Ambiguous command\n");
printf("?Invalid command\n");
printf("?Invalid proxy command\n");
oldintr
= signal(SIGINT
, proxabort
);
if (c
->c_conn
&& !connected
) {
printf("Not connected\n");
(void) signal(SIGINT
, oldintr
);
(*c
->c_handler
)(argc
-1, argv
+1);
(void) signal(SIGINT
, oldintr
);
printf("Case mapping %s.\n", onoff(mcase
));
printf("Carriage Return stripping %s.\n", onoff(crflag
));
(void) strncpy(ntin
, argv
[1], 16);
(void) strncpy(ntout
, argv
[2], 16);
static char new[MAXPATHLEN
];
register int i
, ostop
, found
;
for (ostop
= 0; *(ntout
+ ostop
) && ostop
< 16; ostop
++);
for (cp1
= name
; *cp1
; cp1
++) {
for (i
= 0; *(ntin
+ i
) && i
< 16; i
++) {
if (*cp1
== *(ntin
+ i
)) {
(void) strcat(line
, " ");
(void) gets(&line
[strlen(line
)]);
printf("Usage: %s [mapin mapout]\n",argv
[0]);
(void) strncpy(mapin
, altarg
, MAXPATHLEN
- 1);
(void) strncpy(mapout
, cp
, MAXPATHLEN
- 1);
static char new[MAXPATHLEN
];
register char *cp1
= name
, *cp2
= mapin
;
int i
, toks
[9], toknum
= 0, match
= 1;
while (match
&& *cp1
&& *cp2
) {
if (*(cp2
+1) >= '1' && (*cp2
+1) <= '9') {
if (*cp1
!= *(++cp2
+1)) {
toks
[toknum
= *cp2
- '1']++;
while (*++cp1
&& *(cp2
+1)
if (!match
&& *cp1
) /* last token mismatch */
if (*++cp2
== '$' && isdigit(*(cp2
+1))) {
else if (toks
[toknum
= *cp2
- '1']) {
while (cp3
!= te
[toknum
]) {
while (*cp2
&& *cp2
!= ',' &&
printf("nmap: unbalanced brackets\n");
while (*++cp2
&& *cp2
!= ']') {
if (*cp2
== '\\' && *(cp2
+ 1)) {
printf("nmap: unbalanced brackets\n");
if (isdigit(*(cp2
+ 1))) {
else if (toks
[toknum
= *cp2
- '1']) {
while (cp3
!= te
[toknum
]) {
/* intentional drop through */
printf("Store unique %s.\n", onoff(sunique
));
printf("Receive unique %s.\n", onoff(runique
));
/* change directory to perent directory */
/* show remote system type */
printf("Limit of 16 macros have already been defined\n");
(void) strcat(line
, " ");
(void) gets(&line
[strlen(line
)]);
printf("Usage: %s macro_name\n",argv
[0]);
printf("Enter macro line by line, terminating it with a null line\n");
(void) strncpy(macros
[macnum
].mac_name
, argv
[1], 8);
macros
[macnum
].mac_start
= macbuf
;
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");
if ((*tmp
= c
) == '\n') {
if (tmp
== macros
[macnum
].mac_start
) {
macros
[macnum
++].mac_end
= tmp
;
macros
[macnum
++].mac_end
= tmp
- 1;
while ((c
= getchar()) != '\n' && c
!= EOF
)
if (c
== EOF
|| getchar() == '\n') {
printf("Macro not defined - 4k buffer exceeded\n");
* get size of file on remote machine
(void) strcat(line
, " ");
(void) gets(&line
[strlen(line
)]);
printf("usage:%s filename\n", argv
[0]);
(void) command("SIZE %s", argv
[1]);
* get last modification time of file on remote machine
(void) strcat(line
, " ");
(void) gets(&line
[strlen(line
)]);
printf("usage:%s filename\n", argv
[0]);
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
);
fputs(reply_string
, stdout
);
* show status on remote machine
(void) command(argc
> 1 ? "STAT %s" : "STAT" , argv
[1]);