2 * Copyright (c) 1985, 1989 Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 static char sccsid
[] = "@(#)cmds.c 5.18 (Berkeley) 4/20/89";
23 * FTP User Program -- Command Routines.
25 //#include <sys/param.h>
26 //#include <sys/wait.h>
29 #include <sys/socket.h>
31 #include <netinet/in.h>
45 #include "pathnames.h"
46 #include "prototypes.h"
50 extern char *remglob();
52 extern off_t restart_point
;
53 extern char reply_string
[];
57 const char *dotrans(), *domap();
60 extern char *hostname
;
63 * Connect to peer server and
64 * auto-login, if possible.
66 void setpeer(int argc
, const char *argv
[])
71 printf("Already connected to %s, use close first.\n",
73 (void) fflush(stdout
);
78 (void) strcat(line
, " ");
80 (void) fflush(stdout
);
81 (void) gets(&line
[strlen(line
)]);
87 printf("usage: %s host-name [port]\n", argv
[0]);
88 (void) fflush(stdout
);
93 portnum
= atoi(argv
[2]);
95 printf("%s: bad port number-- %s\n", argv
[1], argv
[2]);
96 printf ("usage: %s host-name [port]\n", argv
[0]);
97 (void) fflush(stdout
);
101 portnum
= htons(portnum
);
103 host
= hookup(argv
[1], portnum
);
105 #if defined(unix) && NBBY == 8
110 (void) login(argv
[1]);
112 #if defined(unix) && NBBY == 8
114 * this ifdef is to keep someone form "porting" this to an incompatible
115 * system and not checking this out. This way they have to think about it.
121 if (command("SYST") == COMPLETE
&& overbose
) {
122 register char *cp
, c
;
123 cp
= index(reply_string
+4, ' ');
125 cp
= index(reply_string
+4, '\r');
133 printf("Remote system type is %s.\n",
138 if (!strncmp(reply_string
, "215 UNIX Type: L8", 17)) {
140 /* allbinary = 1; this violates the RFC */
142 printf("Using %s mode to transfer files.\n",
144 } else if (overbose
&&
145 !strncmp(reply_string
, "215 TOPS20", 10)) {
147 "Remember to set tenex mode when transfering binary files from this machine.\n");
152 (void) fflush(stdout
);
161 { "ascii", "A", TYPE_A
, 0 },
162 { "binary", "I", TYPE_I
, 0 },
163 { "image", "I", TYPE_I
, 0 },
164 { "ebcdic", "E", TYPE_E
, 0 },
165 { "tenex", "L", TYPE_L
, bytename
},
172 void settype(argc
, argv
)
175 register struct types
*p
;
181 printf("usage: %s [", argv
[0]);
183 for (p
= types
; p
->t_name
; p
++) {
184 printf("%s%s", sep
, p
->t_name
);
189 (void) fflush(stdout
);
194 printf("Using %s mode to transfer files.\n", typename
);
195 (void) fflush(stdout
);
199 for (p
= types
; p
->t_name
; p
++)
200 if (strcmp(argv
[1], p
->t_name
) == 0)
202 if (p
->t_name
== 0) {
203 printf("%s: unknown mode\n", argv
[1]);
204 (void) fflush(stdout
);
208 if ((p
->t_arg
!= NULL
) && (*(p
->t_arg
) != '\0'))
209 comret
= command ("TYPE %s %s", p
->t_mode
, p
->t_arg
);
211 comret
= command("TYPE %s", p
->t_mode
);
212 if (comret
== COMPLETE
) {
213 (void) strcpy(typename
, p
->t_name
);
218 const char *stype
[] = {
225 * Set binary transfer type.
235 * Set ascii transfer type.
245 * Set tenex transfer type.
255 * Set ebcdic transfer type.
265 * Set file transfer mode.
269 void fsetmode(argc
, argv
)
273 printf("We only support %s mode, sorry.\n", modename
);
274 (void) fflush(stdout
);
280 * Set file transfer format.
283 void setform(argc
, argv
)
287 printf("We only support %s format, sorry.\n", formname
);
288 (void) fflush(stdout
);
293 * Set file transfer structure.
296 void setstruct(argc
, argv
)
300 printf("We only support %s structure, sorry.\n", structname
);
301 (void) fflush(stdout
);
306 * Send a single file.
314 const char *oldargv1
, *oldargv2
;
322 (void) strcat(line
, " ");
323 printf("(local-file) ");
324 (void) fflush(stdout
);
325 (void) gets(&line
[strlen(line
)]);
332 printf("usage:%s local-file remote-file\n", argv
[0]);
333 (void) fflush(stdout
);
338 (void) strcat(line
, " ");
339 printf("(remote-file) ");
340 (void) fflush(stdout
);
341 (void) gets(&line
[strlen(line
)]);
350 if (!globulize(&argv
[1])) {
355 * If "globulize" modifies argv[1], and argv[2] is a copy of
356 * the old argv[1], make it a copy of the new argv[1].
358 if (argv
[1] != oldargv1
&& argv
[2] == oldargv1
) {
361 cmd
= (argv
[0][0] == 'a') ? "APPE" : ((sunique
) ? "STOU" : "STOR");
363 argv
[2] = dotrans(argv
[2]);
365 if (loc
&& mapflag
) {
366 argv
[2] = domap(argv
[2]);
368 sendrequest(cmd
, argv
[1], argv
[2],
369 argv
[1] != oldargv1
|| argv
[2] != oldargv2
);
373 * Send multiple files.
375 void mput(argc
, argv
)
380 extern jmp_buf jabort
;
384 (void) strcat(line
, " ");
385 printf("(local-files) ");
386 (void) fflush(stdout
);
387 (void) gets(&line
[strlen(line
)]);
393 printf("usage:%s local-files\n", argv
[0]);
394 (void) fflush(stdout
);
400 // oldintr = signal(SIGINT, mabort);
401 (void) setjmp(jabort
);
403 char *cp
, *tp2
, tmpbuf
[MAXPATHLEN
];
405 while ((cp
= remglob(argv
,0)) != NULL
) {
410 if (mflag
&& confirm(argv
[0], cp
)) {
413 while (*tp
&& !islower(*tp
)) {
419 while ((*tp2
= *tp
)) {
421 *tp2
= 'a' + *tp2
- 'A';
435 sendrequest((sunique
) ? "STOU" : "STOR",
436 cp
, tp
, cp
!= tp
|| !interactive
);
437 if (!mflag
&& fromatty
) {
438 ointer
= interactive
;
440 if (confirm("Continue with","mput")) {
443 interactive
= ointer
;
447 // (void) signal(SIGINT, oldintr);
451 for (i
= 1; i
< argc
; i
++) {
452 register char **cpp
, **gargs
;
455 if (mflag
&& confirm(argv
[0], argv
[i
])) {
456 tp
= (ntflag
) ? dotrans(argv
[i
]) : argv
[i
];
457 tp
= (mapflag
) ? domap(tp
) : tp
;
458 sendrequest((sunique
) ? "STOU" : "STOR",
459 argv
[i
], tp
, tp
!= argv
[i
] || !interactive
);
460 if (!mflag
&& fromatty
) {
461 ointer
= interactive
;
463 if (confirm("Continue with","mput")) {
466 interactive
= ointer
;
471 gargs
= glob(argv
[i
]);
472 if (globerr
!= NULL
) {
473 printf("%s\n", globerr
);
474 (void) fflush(stdout
);
481 for (cpp
= gargs
; cpp
&& *cpp
!= NULL
; cpp
++) {
482 if (mflag
&& confirm(argv
[0], *cpp
)) {
483 tp
= (ntflag
) ? dotrans(*cpp
) : *cpp
;
484 tp
= (mapflag
) ? domap(tp
) : tp
;
485 sendrequest((sunique
) ? "STOU" : "STOR",
486 *cpp
, tp
, *cpp
!= tp
|| !interactive
);
487 if (!mflag
&& fromatty
) {
488 ointer
= interactive
;
490 if (confirm("Continue with","mput")) {
493 interactive
= ointer
;
502 // (void) signal(SIGINT, oldintr);
506 void reget(argc
, argv
)
509 (void) getit(argc
, argv
, 1, "r+w");
515 (void) getit(argc
, argv
, 0, restart_point
? "r+w" : "w" );
521 int getit(argc
, argv
, restartit
, mode
)
526 const char *oldargv1
, *oldargv2
;
534 (void) strcat(line
, " ");
535 printf("(remote-file) ");
536 (void) fflush(stdout
);
537 (void) gets(&line
[strlen(line
)]);
544 printf("usage: %s remote-file [ local-file ]\n", argv
[0]);
545 (void) fflush(stdout
);
550 (void) strcat(line
, " ");
551 printf("(local-file) ");
552 (void) fflush(stdout
);
553 (void) gets(&line
[strlen(line
)]);
562 if (!globulize(&argv
[2])) {
567 const char *tp
= argv
[1];
568 char *tp2
, tmpbuf
[MAXPATHLEN
];
570 while (*tp
&& !islower(*tp
)) {
576 while ((*tp2
= *tp
)) {
578 *tp2
= 'a' + *tp2
- 'A';
587 argv
[2] = dotrans(argv
[2]);
589 argv
[2] = domap(argv
[2]);
594 ret
= stat(argv
[2], &stbuf
);
595 if (restartit
== 1) {
600 restart_point
= stbuf
.st_size
;
608 if (command("MDTM %s", argv
[1]) == COMPLETE
) {
609 int yy
, mo
, day
, hour
, min
, sec
;
613 "%*s %04d%02d%02d%02d%02d%02d",
614 &yy
, &mo
, &day
, &hour
, &min
, &sec
);
615 tm
= gmtime(&stbuf
.st_mtime
);
617 if (tm
->tm_year
> yy
%100)
619 else if (tm
->tm_year
== yy
%100) {
622 } else if (tm
->tm_mon
== mo
) {
623 if (tm
->tm_mday
> day
)
625 } else if (tm
->tm_mday
== day
) {
626 if (tm
->tm_hour
> hour
)
628 } else if (tm
->tm_hour
== hour
) {
629 if (tm
->tm_min
> min
)
631 } else if (tm
->tm_min
== min
) {
632 if (tm
->tm_sec
> sec
)
636 printf("%s\n", reply_string
);
637 (void) fflush(stdout
);
645 recvrequest("RETR", argv
[2], argv
[1], mode
,
646 argv
[1] != oldargv1
|| argv
[2] != oldargv2
);
656 extern jmp_buf jabort
;
659 (void) fflush(stdout
);
660 if (mflag
&& fromatty
) {
661 ointer
= interactive
;
663 if (confirm("Continue with", mname
)) {
664 interactive
= ointer
;
667 interactive
= ointer
;
675 * Get multiple files.
677 void mget(argc
, argv
)
681 char *tp2
, tmpbuf
[MAXPATHLEN
];
683 extern jmp_buf jabort
;
686 (void) strcat(line
, " ");
687 printf("(remote-files) ");
688 (void) fflush(stdout
);
689 (void) gets(&line
[strlen(line
)]);
695 printf("usage:%s remote-files\n", argv
[0]);
696 (void) fflush(stdout
);
702 // oldintr = signal(SIGINT,mabort);
703 (void) setjmp(jabort
);
704 while ((cp
= remglob(argv
,proxy
)) != NULL
) {
709 if (mflag
&& confirm(argv
[0], cp
)) {
712 while (*tp
&& !islower(*tp
)) {
718 while ((*tp2
= *tp
)) {
720 *tp2
= 'a' + *tp2
- 'A';
734 recvrequest("RETR", tp
, cp
, "w",
735 tp
!= cp
|| !interactive
);
736 if (!mflag
&& fromatty
) {
737 ointer
= interactive
;
739 if (confirm("Continue with","mget")) {
742 interactive
= ointer
;
746 // (void) signal(SIGINT,oldintr);
751 remglob(argv
,doswitch
)
756 static char buf
[MAXPATHLEN
];
757 static FILE *ftemp
= NULL
;
759 int oldverbose
, oldhash
;
769 (void) fclose(ftemp
);
778 if ((cp
= *++args
) == NULL
)
783 (void) strcpy(temp
, _PATH_TMP
);
785 oldverbose
= verbose
, verbose
= 0;
786 oldhash
= hash
, hash
= 0;
790 for (mode
= "w"; *++argv
!= NULL
; mode
= "a")
791 recvrequest ("NLST", temp
, *argv
, mode
, 0);
795 verbose
= oldverbose
; hash
= oldhash
;
796 ftemp
= fopen(temp
, "r");
799 printf("can't find list of remote files, oops\n");
800 (void) fflush(stdout
);
804 if (fgets(buf
, sizeof (buf
), ftemp
) == NULL
) {
805 (void) fclose(ftemp
), ftemp
= NULL
;
808 if ((cp
= index(buf
, '\n')) != NULL
)
818 return (bool ? "on" : "off");
825 void status(argc
, argv
)
831 printf("Connected to %s.\n", hostname
);
833 printf("Not connected.\n");
837 printf("Connected for proxy commands to %s.\n", hostname
);
840 printf("No proxy connection.\n");
844 printf("Mode: %s; Type: %s; Form: %s; Structure: %s\n",
845 modename
, typename
, formname
, structname
);
846 printf("Verbose: %s; Bell: %s; Prompting: %s; Globbing: %s\n",
847 onoff(verbose
), onoff(bell
), onoff(interactive
),
849 printf("Store unique: %s; Receive unique: %s\n", onoff(sunique
),
851 printf("Case: %s; CR stripping: %s\n",onoff(mcase
),onoff(crflag
));
853 printf("Ntrans: (in) %s (out) %s\n", ntin
,ntout
);
856 printf("Ntrans: off\n");
859 printf("Nmap: (in) %s (out) %s\n", mapin
, mapout
);
862 printf("Nmap: off\n");
864 printf("Hash mark printing: %s; Use of PORT cmds: %s\n",
865 onoff(hash
), onoff(sendport
));
868 for (i
=0; i
<macnum
; i
++) {
869 printf("\t%s\n",macros
[i
].mac_name
);
872 (void) fflush(stdout
);
877 * Set beep on cmd completed mode.
884 printf("Bell mode %s.\n", onoff(bell
));
885 (void) fflush(stdout
);
890 * Turn on packet tracing.
897 printf("Packet tracing %s.\n", onoff(trace
));
898 (void) fflush(stdout
);
903 * Toggle hash mark printing during transfers.
910 printf("Hash mark printing %s", onoff(hash
));
913 printf(" (%d bytes/hash mark)", 1024);
915 (void) fflush(stdout
);
919 * Turn on printing of server echo's.
926 printf("Verbose mode %s.\n", onoff(verbose
));
927 (void) fflush(stdout
);
932 * Toggle PORT cmd use before each data connection.
938 sendport
= !sendport
;
939 printf("Use of PORT cmds %s.\n", onoff(sendport
));
940 (void) fflush(stdout
);
945 * Turn on interactive prompting
946 * during mget, mput, and mdelete.
952 interactive
= !interactive
;
953 printf("Interactive mode %s.\n", onoff(interactive
));
954 (void) fflush(stdout
);
959 * Toggle metacharacter interpretation
960 * on local file names.
967 printf("Globbing %s.\n", onoff(doglob
));
968 (void) fflush(stdout
);
973 * Set debugging mode on/off and/or
974 * set level of debugging.
977 void setdebug(argc
, argv
)
985 printf("%s: bad debugging value.\n", argv
[1]);
986 (void) fflush(stdout
);
996 options
&= ~SO_DEBUG
;
997 printf("Debugging %s (debug=%d).\n", onoff(debug
), debug
);
998 (void) fflush(stdout
);
1003 * Set current working directory
1004 * on remote machine.
1011 (void) strcat(line
, " ");
1012 printf("(remote-directory) ");
1013 (void) fflush(stdout
);
1014 (void) gets(&line
[strlen(line
)]);
1020 printf("usage:%s remote-directory\n", argv
[0]);
1021 (void) fflush(stdout
);
1025 if (command("CWD %s", argv
[1]) == ERROR
&& code
== 500) {
1027 printf("CWD command not recognized, trying XCWD\n");
1028 (void) fflush(stdout
);
1030 (void) command("XCWD %s", argv
[1]);
1035 * Set current working directory
1038 void lcd(argc
, argv
)
1041 char buf
[MAXPATHLEN
];
1044 argc
++, argv
[1] = home
;
1046 printf("usage:%s local-directory\n", argv
[0]);
1047 (void) fflush(stdout
);
1051 if (!globulize(&argv
[1])) {
1055 if (chdir(argv
[1]) < 0) {
1060 printf("Local directory now %s\n", getcwd(buf
,sizeof(buf
)));
1061 (void) fflush(stdout
);
1066 * Delete a single file.
1068 void delete(argc
, argv
)
1073 (void) strcat(line
, " ");
1074 printf("(remote-file) ");
1075 (void) fflush(stdout
);
1076 (void) gets(&line
[strlen(line
)]);
1082 printf("usage:%s remote-file\n", argv
[0]);
1083 (void) fflush(stdout
);
1087 (void) command("DELE %s", argv
[1]);
1091 * Delete multiple files.
1093 void mdelete(argc
, argv
)
1098 extern jmp_buf jabort
;
1101 (void) strcat(line
, " ");
1102 printf("(remote-files) ");
1103 (void) fflush(stdout
);
1104 (void) gets(&line
[strlen(line
)]);
1110 printf("usage:%s remote-files\n", argv
[0]);
1111 (void) fflush(stdout
);
1117 // oldintr = signal(SIGINT, mabort);
1118 (void) setjmp(jabort
);
1119 while ((cp
= remglob(argv
,0)) != NULL
) {
1124 if (mflag
&& confirm(argv
[0], cp
)) {
1125 (void) command("DELE %s", cp
);
1126 if (!mflag
&& fromatty
) {
1127 ointer
= interactive
;
1129 if (confirm("Continue with", "mdelete")) {
1132 interactive
= ointer
;
1136 // (void) signal(SIGINT, oldintr);
1141 * Rename a remote file.
1143 void renamefile(argc
, argv
)
1148 (void) strcat(line
, " ");
1149 printf("(from-name) ");
1150 (void) fflush(stdout
);
1151 (void) gets(&line
[strlen(line
)]);
1158 printf("%s from-name to-name\n", argv
[0]);
1159 (void) fflush(stdout
);
1164 (void) strcat(line
, " ");
1165 printf("(to-name) ");
1166 (void) fflush(stdout
);
1167 (void) gets(&line
[strlen(line
)]);
1174 if (command("RNFR %s", argv
[1]) == CONTINUE
)
1175 (void) command("RNTO %s", argv
[2]);
1179 * Get a directory listing
1188 argc
++, argv
[1] = NULL
;
1190 argc
++, argv
[2] = "-";
1192 printf("usage: %s remote-directory local-file\n", argv
[0]);
1193 (void) fflush(stdout
);
1197 cmd
= argv
[0][0] == 'n' ? "NLST" : "LIST";
1198 // cmd = argv[0][0] == 'n' ? "NLST -CF" : "NLST -CF";
1199 if (strcmp(argv
[2], "-") && !globulize(&argv
[2])) {
1203 if (strcmp(argv
[2], "-") && *argv
[2] != '|')
1204 if (!globulize(&argv
[2]) || !confirm("output to local-file:", argv
[2])) {
1208 recvrequest(cmd
, argv
[2], argv
[1], "w", 0);
1212 * Get a directory listing
1213 * of multiple remote files.
1215 void mls(argc
, argv
)
1218 const char *cmd
, *dest
;
1221 extern jmp_buf jabort
;
1224 (void) strcat(line
, " ");
1225 printf("(remote-files) ");
1226 (void) fflush(stdout
);
1227 (void) gets(&line
[strlen(line
)]);
1233 (void) strcat(line
, " ");
1234 printf("(local-file) ");
1235 (void) fflush(stdout
);
1236 (void) gets(&line
[strlen(line
)]);
1242 printf("usage:%s remote-files local-file\n", argv
[0]);
1243 (void) fflush(stdout
);
1247 dest
= argv
[argc
- 1];
1248 argv
[argc
- 1] = NULL
;
1249 if (strcmp(dest
, "-") && *dest
!= '|')
1250 if (!globulize(&dest
) || !confirm("output to local-file:", dest
)) {
1254 cmd
= argv
[0][1] == 'l' ? "NLST" : "LIST";
1257 // oldintr = signal(SIGINT, mabort);
1258 (void) setjmp(jabort
);
1259 for (i
= 1; mflag
&& i
< argc
-1; ++i
) {
1260 *mode
= (i
== 1) ? 'w' : 'a';
1261 recvrequest(cmd
, dest
, argv
[i
], mode
, 0);
1262 if (!mflag
&& fromatty
) {
1263 ointer
= interactive
;
1265 if (confirm("Continue with", argv
[0])) {
1268 interactive
= ointer
;
1271 // (void) signal(SIGINT, oldintr);
1279 void shell(argc
, argv
)
1284 sig_t (*old1
)(), (*old2
)();
1285 char shellnam
[40], *shell
, *namep
;
1288 old1
= signal (SIGINT
, SIG_IGN
);
1289 old2
= signal (SIGQUIT
, SIG_IGN
);
1290 if ((pid
= fork()) == 0) {
1291 for (pid
= 3; pid
< 20; pid
++)
1293 (void) signal(SIGINT
, SIG_DFL
);
1294 (void) signal(SIGQUIT
, SIG_DFL
);
1295 shell
= getenv("SHELL");
1297 shell
= _PATH_BSHELL
;
1298 namep
= rindex(shell
,'/');
1301 (void) strcpy(shellnam
,"-");
1302 (void) strcat(shellnam
, ++namep
);
1303 if (strcmp(namep
, "sh") != 0)
1306 printf ("%s\n", shell
);
1307 (void) fflush (stdout
);
1310 execl(shell
,shellnam
,"-c",altarg
,(char *)0);
1313 execl(shell
,shellnam
,(char *)0);
1320 while (wait(&status
) != pid
)
1322 (void) signal(SIGINT
, old1
);
1323 (void) signal(SIGQUIT
, old2
);
1325 perror("Try again later");
1334 char ShellCmd
[MAX_PATH
];
1335 char CmdLine
[MAX_PATH
];
1337 PROCESS_INFORMATION ProcessInformation
;
1339 STARTUPINFO StartupInfo
;
1340 char ShellName
[] = "COMSPEC";
1343 NumBytes
= GetEnvironmentVariable( ShellName
, ShellCmd
, MAX_PATH
);
1351 strcpy( CmdLine
, ShellCmd
);
1355 strncat(CmdLine
, " /C", MAX_PATH
);
1358 for (i
=1; i
<argc
; i
++)
1360 strncat(CmdLine
, " ", MAX_PATH
);
1361 strncat(CmdLine
, argv
[i
], MAX_PATH
);
1364 StartupInfo
.cb
= sizeof( StartupInfo
);
1365 StartupInfo
.lpReserved
= NULL
;
1366 StartupInfo
.lpDesktop
= NULL
;
1367 StartupInfo
.lpTitle
= NULL
;
1368 StartupInfo
.dwX
= 0;
1369 StartupInfo
.dwY
= 0;
1370 StartupInfo
.dwXSize
= 0;
1371 StartupInfo
.dwYSize
= 0;
1372 StartupInfo
.dwFlags
= 0;
1373 StartupInfo
.wShowWindow
= 0;
1374 StartupInfo
.cbReserved2
= 0;
1375 StartupInfo
.lpReserved2
= NULL
;
1377 Result
= CreateProcess( AppName
, // cmd name
1378 CmdLine
, // cmd line arguments
1380 NULL
, // security attributes
1381 FALSE
, // inherit flags
1382 0, // Creation flags
1383 NULL
, // Environment
1384 NULL
, // Current directory
1385 &StartupInfo
, // Startup info structure
1386 &ProcessInformation
); // processInfo structure
1390 WaitForSingleObject( ProcessInformation
.hProcess
, 0xffffffff);
1392 CloseHandle( ProcessInformation
.hProcess
);
1397 * Send new user information (re-login)
1399 void user(argc
, argv
)
1403 char acct
[80], *getpass();
1407 (void) strcat(line
, " ");
1408 printf("(username) ");
1409 (void) fflush(stdout
);
1410 (void) gets(&line
[strlen(line
)]);
1416 printf("usage: %s username [password] [account]\n", argv
[0]);
1417 (void) fflush(stdout
);
1421 n
= command("USER %s", argv
[1]);
1422 if (n
== CONTINUE
) {
1424 argv
[2] = getpass("Password: "), argc
++;
1425 n
= command("PASS %s", argv
[2]);
1427 if (n
== CONTINUE
) {
1429 printf("Account: "); (void) fflush(stdout
);
1430 (void) fflush(stdout
);
1431 (void) fgets(acct
, sizeof(acct
) - 1, stdin
);
1432 acct
[strlen(acct
) - 1] = '\0';
1433 argv
[3] = acct
; argc
++;
1435 n
= command("ACCT %s", argv
[3]);
1438 if (n
!= COMPLETE
) {
1439 fprintf(stdout
, "Login failed.\n");
1440 (void) fflush(stdout
);
1443 if (!aflag
&& argc
== 4) {
1444 (void) command("ACCT %s", argv
[3]);
1449 * Print working directory.
1454 int oldverbose
= verbose
;
1457 * If we aren't verbose, this doesn't do anything!
1460 if (command("PWD") == ERROR
&& code
== 500) {
1461 printf("PWD command not recognized, trying XPWD\n");
1462 (void) fflush(stdout
);
1463 (void) command("XPWD");
1465 verbose
= oldverbose
;
1471 void makedir(argc
, argv
)
1476 (void) strcat(line
, " ");
1477 printf("(directory-name) ");
1478 (void) fflush(stdout
);
1479 (void) gets(&line
[strlen(line
)]);
1485 printf("usage: %s directory-name\n", argv
[0]);
1486 (void) fflush(stdout
);
1490 if (command("MKD %s", argv
[1]) == ERROR
&& code
== 500) {
1492 printf("MKD command not recognized, trying XMKD\n");
1493 (void) fflush(stdout
);
1495 (void) command("XMKD %s", argv
[1]);
1500 * Remove a directory.
1502 void removedir(argc
, argv
)
1507 (void) strcat(line
, " ");
1508 printf("(directory-name) ");
1509 (void) fflush(stdout
);
1510 (void) gets(&line
[strlen(line
)]);
1516 printf("usage: %s directory-name\n", argv
[0]);
1517 (void) fflush(stdout
);
1521 if (command("RMD %s", argv
[1]) == ERROR
&& code
== 500) {
1523 printf("RMD command not recognized, trying XRMD\n");
1524 (void) fflush(stdout
);
1526 (void) command("XRMD %s", argv
[1]);
1531 * Send a line, verbatim, to the remote machine.
1533 void quote(argc
, argv
)
1540 (void) strcat(line
, " ");
1541 printf("(command line to send) ");
1542 (void) fflush(stdout
);
1543 (void) gets(&line
[strlen(line
)]);
1549 printf("usage: %s line-to-send\n", argv
[0]);
1550 (void) fflush(stdout
);
1554 (void) strcpy(buf
, argv
[1]);
1555 for (i
= 2; i
< argc
; i
++) {
1556 (void) strcat(buf
, " ");
1557 (void) strcat(buf
, argv
[i
]);
1559 if (command(buf
) == PRELIM
) {
1560 while (getreply(0) == PRELIM
);
1565 * Send a SITE command to the remote machine. The line
1566 * is sent almost verbatim to the remote machine, the
1567 * first argument is changed to SITE.
1570 void site(argc
, argv
)
1577 (void) strcat(line
, " ");
1578 printf("(arguments to SITE command) ");
1579 (void) fflush(stdout
);
1580 (void) gets(&line
[strlen(line
)]);
1586 printf("usage: %s line-to-send\n", argv
[0]);
1587 (void) fflush(stdout
);
1591 (void) strcpy(buf
, "SITE ");
1592 (void) strcat(buf
, argv
[1]);
1593 for (i
= 2; i
< argc
; i
++) {
1594 (void) strcat(buf
, " ");
1595 (void) strcat(buf
, argv
[i
]);
1597 if (command(buf
) == PRELIM
) {
1598 while (getreply(0) == PRELIM
);
1602 void do_chmod(argc
, argv
)
1606 printf("usage: %s mode file-name\n", argv
[0]);
1607 (void) fflush(stdout
);
1612 (void) strcat(line
, " ");
1613 printf("(mode and file-name) ");
1614 (void) fflush(stdout
);
1615 (void) gets(&line
[strlen(line
)]);
1621 printf("usage: %s mode file-name\n", argv
[0]);
1622 (void) fflush(stdout
);
1626 (void)command("SITE CHMOD %s %s", argv
[1], argv
[2]);
1629 void do_umask(argc
, argv
)
1632 int oldverbose
= verbose
;
1635 (void) command(argc
== 1 ? "SITE UMASK" : "SITE UMASK %s", argv
[1]);
1636 verbose
= oldverbose
;
1639 void idle(argc
, argv
)
1642 int oldverbose
= verbose
;
1645 (void) command(argc
== 1 ? "SITE IDLE" : "SITE IDLE %s", argv
[1]);
1646 verbose
= oldverbose
;
1650 * Ask the other side for help.
1652 void rmthelp(argc
, argv
)
1655 int oldverbose
= verbose
;
1658 (void) command(argc
== 1 ? "HELP" : "HELP %s", argv
[1]);
1659 verbose
= oldverbose
;
1663 * Terminate session and exit.
1679 * Terminate session, but don't exit.
1688 (void) command("QUIT");
1697 int confirm(cmd
, file
)
1698 const char *cmd
, *file
;
1704 printf("%s %s? ", cmd
, file
);
1705 (void) fflush(stdout
);
1707 return (*line
!= 'n' && *line
!= 'N');
1711 static void fatal(msg
)
1715 fprintf(stderr
, "ftp: %s\n", msg
);
1721 * Glob a local file name specification with
1722 * the expectation of a single return value.
1723 * Can't control multiple values being expanded
1724 * from the expression, we return only the first.
1733 globbed
= glob(*cpp
);
1734 if (globerr
!= NULL
) {
1735 printf("%s: %s\n", *cpp
, globerr
);
1736 (void) fflush(stdout
);
1739 free((char *)globbed
);
1745 /* don't waste too much memory */
1748 free((char *)globbed
);
1754 void account(argc
,argv
)
1758 char acct
[50], *getpass(), *ap
;
1763 (void) strncpy(acct
,*argv
,49);
1768 (void) strncat(acct
,*argv
, 49-strlen(acct
));
1773 ap
= getpass("Account:");
1775 (void) command("ACCT %s", ap
);
1796 longjmp(abortprox
,1);
1800 void doproxy(argc
,argv
)
1804 register struct cmd
*c
;
1805 struct cmd
*getcmd();
1806 // extern struct cmd cmdtab[];
1807 extern jmp_buf abortprox
;
1810 (void) strcat(line
, " ");
1811 printf("(command) ");
1812 (void) fflush(stdout
);
1813 (void) gets(&line
[strlen(line
)]);
1819 printf("usage:%s command\n", argv
[0]);
1820 (void) fflush(stdout
);
1824 c
= getcmd(argv
[1]);
1825 if (c
== (struct cmd
*) -1) {
1826 printf("?Ambiguous command\n");
1827 (void) fflush(stdout
);
1832 printf("?Invalid command\n");
1833 (void) fflush(stdout
);
1838 printf("?Invalid proxy command\n");
1839 (void) fflush(stdout
);
1843 if (setjmp(abortprox
)) {
1847 // oldintr = signal(SIGINT, proxabort);
1849 if (c
->c_conn
&& !connected
) {
1850 printf("Not connected\n");
1851 (void) fflush(stdout
);
1853 // (void) signal(SIGINT, oldintr);
1857 (*c
->c_handler
)(argc
-1, argv
+1);
1865 // (void) signal(SIGINT, oldintr);
1871 printf("Case mapping %s.\n", onoff(mcase
));
1872 (void) fflush(stdout
);
1879 printf("Carriage Return stripping %s.\n", onoff(crflag
));
1880 (void) fflush(stdout
);
1884 void setntrans(argc
,argv
)
1890 printf("Ntrans off.\n");
1891 (void) fflush(stdout
);
1897 (void) strncpy(ntin
, argv
[1], 16);
1903 (void) strncpy(ntout
, argv
[2], 16);
1911 static char new[MAXPATHLEN
];
1914 register int i
, ostop
, found
;
1916 for (ostop
= 0; *(ntout
+ ostop
) && ostop
< 16; ostop
++);
1917 for (cp1
= name
; *cp1
; cp1
++) {
1919 for (i
= 0; *(ntin
+ i
) && i
< 16; i
++) {
1920 if (*cp1
== *(ntin
+ i
)) {
1923 *cp2
++ = *(ntout
+ i
);
1938 setpassive(argc
, argv
)
1942 passivemode
= !passivemode
;
1943 printf("Passive mode %s.\n", onoff(passivemode
));
1944 (void) fflush(stdout
);
1948 void setnmap(argc
, argv
)
1956 printf("Nmap off.\n");
1957 (void) fflush(stdout
);
1962 (void) strcat(line
, " ");
1963 printf("(mapout) ");
1964 (void) fflush(stdout
);
1965 (void) gets(&line
[strlen(line
)]);
1971 printf("Usage: %s [mapin mapout]\n",argv
[0]);
1972 (void) fflush(stdout
);
1978 cp
= index(altarg
, ' ');
1980 while(*++cp
== ' ');
1982 cp
= index(altarg
, ' ');
1985 (void) strncpy(mapin
, altarg
, MAXPATHLEN
- 1);
1986 while (*++cp
== ' ');
1987 (void) strncpy(mapout
, cp
, MAXPATHLEN
- 1);
1994 static char new[MAXPATHLEN
];
1995 const char *cp1
= name
;
1996 char *cpn
, *cp2
= mapin
;
1997 const char *tp
[9], *te
[9];
1998 int i
, toks
[9], toknum
= 0, match
= 1;
2000 for (i
=0; i
< 9; ++i
) {
2003 while (match
&& *cp1
&& *cp2
) {
2006 if (*++cp2
!= *cp1
) {
2011 if (*(cp2
+1) >= '1' && (*cp2
+1) <= '9') {
2012 if (*cp1
!= *(++cp2
+1)) {
2013 toks
[toknum
= *cp2
- '1']++;
2015 while (*++cp1
&& *(cp2
+1)
2029 if (match
&& *cp1
) {
2032 if (match
&& *cp2
) {
2036 if (!match
&& *cp1
) /* last token mismatch */
2054 if (*++cp2
== '$' && isdigit(*(cp2
+1))) {
2055 if (*++cp2
== '0') {
2056 const char *cp3
= name
;
2063 else if (toks
[toknum
= *cp2
- '1']) {
2064 const char *cp3
= tp
[toknum
];
2066 while (cp3
!= te
[toknum
]) {
2073 while (*cp2
&& *cp2
!= ',' &&
2078 else if (*cp2
== '$' &&
2079 isdigit(*(cp2
+1))) {
2080 if (*++cp2
== '0') {
2081 const char *cp3
= name
;
2087 else if (toks
[toknum
=
2089 const char *cp3
=tp
[toknum
];
2102 printf("nmap: unbalanced brackets\n");
2103 (void) fflush(stdout
);
2110 while (*++cp2
&& *cp2
!= ']') {
2111 if (*cp2
== '\\' && *(cp2
+ 1)) {
2116 printf("nmap: unbalanced brackets\n");
2117 (void) fflush(stdout
);
2133 if (isdigit(*(cp2
+ 1))) {
2134 if (*++cp2
== '0') {
2135 const char *cp3
= name
;
2141 else if (toks
[toknum
= *cp2
- '1']) {
2142 const char *cp3
= tp
[toknum
];
2144 while (cp3
!= te
[toknum
]) {
2150 /* intentional drop through */
2167 printf("Store unique %s.\n", onoff(sunique
));
2168 (void) fflush(stdout
);
2175 printf("Receive unique %s.\n", onoff(runique
));
2176 (void) fflush(stdout
);
2180 /* change directory to perent directory */
2183 if (command("CDUP") == ERROR
&& code
== 500) {
2185 printf("CDUP command not recognized, trying XCUP\n");
2186 (void) fflush(stdout
);
2188 (void) command("XCUP");
2192 /* restart transfer at specific point */
2193 void restart(argc
, argv
)
2198 printf("restart: offset not specified\n");
2200 restart_point
= atol(argv
[1]);
2201 printf("restarting at %ld. %s\n", restart_point
,
2202 "execute get, put or append to initiate transfer");
2204 (void) fflush(stdout
);
2207 /* show remote system type */
2210 (void) command("SYST");
2213 void macdef(argc
, argv
)
2221 printf("Limit of 16 macros have already been defined\n");
2222 (void) fflush(stdout
);
2227 (void) strcat(line
, " ");
2228 printf("(macro name) ");
2229 (void) fflush(stdout
);
2230 (void) gets(&line
[strlen(line
)]);
2236 printf("Usage: %s macro_name\n",argv
[0]);
2237 (void) fflush(stdout
);
2242 printf("Enter macro line by line, terminating it with a null line\n");
2243 (void) fflush(stdout
);
2245 (void) strncpy(macros
[macnum
].mac_name
, argv
[1], 8);
2247 macros
[macnum
].mac_start
= macbuf
;
2250 macros
[macnum
].mac_start
= macros
[macnum
- 1].mac_end
+ 1;
2252 tmp
= macros
[macnum
].mac_start
;
2253 while (tmp
!= macbuf
+4096) {
2254 if ((c
= getchar()) == EOF
) {
2255 printf("macdef:end of file encountered\n");
2256 (void) fflush(stdout
);
2260 if ((*tmp
= c
) == '\n') {
2261 if (tmp
== macros
[macnum
].mac_start
) {
2262 macros
[macnum
++].mac_end
= tmp
;
2266 if (*(tmp
-1) == '\0') {
2267 macros
[macnum
++].mac_end
= tmp
- 1;
2276 while ((c
= getchar()) != '\n' && c
!= EOF
)
2278 if (c
== EOF
|| getchar() == '\n') {
2279 printf("Macro not defined - 4k buffer exceeded\n");
2280 (void) fflush(stdout
);
2288 * get size of file on remote machine
2290 void sizecmd(argc
, argv
)
2295 (void) strcat(line
, " ");
2296 printf("(filename) ");
2297 (void) fflush(stdout
);
2298 (void) gets(&line
[strlen(line
)]);
2304 printf("usage:%s filename\n", argv
[0]);
2305 (void) fflush(stdout
);
2309 (void) command("SIZE %s", argv
[1]);
2313 * get last modification time of file on remote machine
2315 void modtime(argc
, argv
)
2321 (void) strcat(line
, " ");
2322 printf("(filename) ");
2323 (void) fflush(stdout
);
2324 (void) gets(&line
[strlen(line
)]);
2330 printf("usage:%s filename\n", argv
[0]);
2331 (void) fflush(stdout
);
2338 if (command("MDTM %s", argv
[1]) == COMPLETE
) {
2339 int yy
, mo
, day
, hour
, min
, sec
;
2340 sscanf(reply_string
, "%*s %04d%02d%02d%02d%02d%02d", &yy
, &mo
,
2341 &day
, &hour
, &min
, &sec
);
2342 /* might want to print this in local time */
2343 printf("%s\t%02d/%02d/%04d %02d:%02d:%02d GMT\n", argv
[1],
2344 mo
, day
, yy
, hour
, min
, sec
);
2346 printf("%s\n", reply_string
);
2348 (void) fflush(stdout
);
2352 * show status on reomte machine
2354 void rmtstatus(argc
, argv
)
2357 (void) command(argc
> 1 ? "STAT %s" : "STAT" , argv
[1]);
2361 * get file if modtime is more recent than current file
2363 void newer(argc
, argv
)
2366 if (getit(argc
, argv
, -1, "w")) {
2367 printf("Local file \"%s\" is newer than remote file \"%s\"\n",
2369 (void) fflush(stdout
);