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.
20 "@(#) Copyright (c) 1985, 1989 Regents of the University of California.\n\
21 All rights reserved.\n";
25 static char sccsid
[] = "@(#)main.c based on 5.13 (Berkeley) 3/14/89";
29 * FTP User Program -- Command Interface.
33 #include <sys/socket.h>
34 #include <sys/ioctl.h>
40 #include "prototypes.h"
41 #include <sys/types.h>
52 #if defined(sun) && !defined(FD_SET)
69 /* Lot's of options... */
71 * Options and other state info.
73 int trace
; /* trace packets exchanged */
74 int hash
; /* print # for each buffer transferred */
75 int sendport
; /* use PORT cmd for each data connection */
76 int verbose
; /* print messages coming back from server */
77 int connected
; /* connected to server */
78 int fromatty
; /* input is from a terminal */
79 int interactive
; /* interactively prompt on m* cmds */
80 int debug
; /* debugging level */
81 int bell
; /* ring bell on cmd completion */
82 int doglob
; /* glob local file names */
83 int proxy
; /* proxy server connection active */
85 int proxflag
; /* proxy connection exists */
86 int sunique
; /* store files on server with unique name */
87 int runique
; /* store local files with unique name */
88 int mcase
; /* map upper to lower case for mget names */
89 int ntflag
; /* use ntin ntout tables for name translation */
90 int mapflag
; /* use mapin mapout templates on file names */
91 int code
; /* return/reply code for ftp command */
92 int crflag
; /* if 1, strip car. rets. on ascii gets */
93 char pasv
[64]; /* passive port for proxy data connection */
94 char *altarg
; /* argv[1] with no shell-like preprocessing */
95 char ntin
[17]; /* input translation table */
96 char ntout
[17]; /* output translation table */
97 // #include <sys/param.h>
98 char mapin
[MAXPATHLEN
]; /* input map template */
99 char mapout
[MAXPATHLEN
]; /* output map template */
100 char typename
[32]; /* name of file transfer type */
101 int type
; /* file transfer type */
102 char structname
[32]; /* name of file transfer structure */
103 int stru
; /* file transfer structure */
104 char formname
[32]; /* name of file transfer format */
105 int form
; /* file transfer format */
106 char modename
[32]; /* name of file transfer mode */
107 int mode
; /* file transfer mode */
108 char bytename
[32]; /* local byte size in ascii */
109 int bytesize
; /* local byte size in binary */
111 jmp_buf toplevel
; /* non-local goto stuff for cmd scanner */
113 char line
[200]; /* input line buffer */
114 char *stringbase
; /* current scan point in line buffer */
115 char argbuf
[200]; /* argument storage buffer */
116 char *argbase
; /* current storage point in arg buffer */
117 int margc
; /* count of arguments on input line */
118 const char *margv
[20]; /* args parsed from input line */
119 int cpend
; /* flag: if != 0, then pending server reply */
120 int mflag
; /* flag: if != 0, then active multi command */
122 int options
; /* used during socket creation */
124 static const char *slurpstring();
127 int main(int argc
, const char *argv
[])
132 char homedir
[MAXPATHLEN
];
139 struct servent
*sp
; /* service spec for tcp/ftp */
141 /* Disable output buffering, for the benefit of Emacs. */
142 //setbuf(stdout, NULL);
144 _fmode
= O_BINARY
; // This causes an error somewhere.
146 wVerReq
= MAKEWORD(1,1);
148 err
= WSAStartup(wVerReq
, &WSAData
);
151 fprintf(stderr
, "Could not initialize Windows socket interface.");
155 sp
= getservbyname("ftp", "tcp");
157 fprintf(stderr
, "ftp: ftp/tcp: unknown service\n");
161 portnum
= sp
->s_port
;
168 while (argc
> 0 && **argv
== '-') {
169 for (cp
= *argv
+ 1; *cp
; cp
++)
199 "ftp: %c: unknown option\n", *cp
);
204 // fromatty = isatty(fileno(stdin));
205 fromatty
= 1; // Strengthen this test
207 * Set up defaults for FTP.
209 (void) strcpy(typename
, "ascii"), type
= TYPE_A
;
210 (void) strcpy(formname
, "non-print"), form
= FORM_N
;
211 (void) strcpy(modename
, "stream"), mode
= MODE_S
;
212 (void) strcpy(structname
, "file"), stru
= STRU_F
;
213 (void) strcpy(bytename
, "8"), bytesize
= 8;
216 cpend
= 0; /* no pending replies */
217 proxy
= 0; /* proxy not active */
218 passivemode
= 1; /* passive mode *is* active */
219 crflag
= 1; /* strip c.r. on ascii gets */
221 * Set up the home directory in case we're globbing.
229 pw
= getpwuid(getuid());
232 (void) strcpy(home
, pw
->pw_dir
);
237 if (setjmp(toplevel
))
239 // (void) signal(SIGINT, intr);
240 // (void) signal(SIGPIPE, lostpeer);
241 setpeer(argc
+ 1, argv
- 1);
243 top
= setjmp(toplevel
) == 0;
245 // (void) signal(SIGINT, intr);
246 // (void) signal(SIGPIPE, lostpeer);
258 longjmp(toplevel
, 1);
272 (void) shutdown(data
, 1+1);
297 s = rindex(filename, '/');
313 register struct cmd
*c
;
316 (void) putchar('\n');
318 (void) fflush(stdout
);
321 (void) fflush(stdout
);
323 if (gets(line
) == 0) {
324 if (feof(stdin
) || ferror(stdin
))
334 c
= getcmd(margv
[0]);
335 if (c
== (struct cmd
*)-1) {
336 printf("?Ambiguous command\n");
340 printf("?Invalid command\n");
343 if (c
->c_conn
&& !connected
) {
344 printf ("Not connected.\n");
347 (*c
->c_handler
)(margc
, margv
);
348 if (bell
&& c
->c_bell
)
349 (void) putchar('\007');
350 if (c
->c_handler
!= help
)
353 (void) fflush(stdout
);
354 // (void) signal(SIGINT, intr);
355 // (void) signal(SIGPIPE, lostpeer);
362 extern struct cmd cmdtab
[];
364 struct cmd
*c
, *found
;
365 int nmatches
, longest
;
370 for (c
= cmdtab
; (p
= c
->c_name
); c
++) {
371 for (q
= name
; *q
== *p
++; q
++)
372 if (*q
== 0) /* exact match? */
374 if (!*q
) { /* the name was a prefix */
375 if (q
- name
> longest
) {
379 } else if (q
- name
== longest
)
384 return ((struct cmd
*)-1);
389 * Slice a string up into argc/argv.
400 stringbase
= line
; /* scan from first of buffer */
401 argbase
= argbuf
; /* store from first of buffer */
403 while ((*argp
++ = slurpstring()))
408 * Parse string into argbuf;
409 * implemented with FSM to
410 * handle quoting and strings
416 register char *sb
= stringbase
;
417 register char *ap
= argbase
;
418 char *tmp
= argbase
; /* will return this if token found */
420 if (*sb
== '!' || *sb
== '$') { /* recognize ! as a token for shell */
421 switch (slrflag
) { /* and $ as token for macro invoke */
425 return ((*sb
== '!') ? "!" : "$");
467 goto OUT1
; /* end of token */
470 sb
++; goto S2
; /* slurp next character */
473 sb
++; goto S3
; /* slurp quoted string */
476 *ap
++ = *sb
++; /* add character to token */
511 argbase
= ap
; /* update storage pointer */
512 stringbase
= sb
; /* update scan pointer */
530 #define HELPINDENT (sizeof ("directory"))
534 * Call each command handler with argc == 0 and argv[0] == name.
536 void help(argc
, argv
)
540 extern struct cmd cmdtab
[];
544 register int i
, j
, w
, k
;
545 int columns
, width
= 0, lines
;
548 printf("Commands may be abbreviated. Commands are:\n\n");
549 for (c
= cmdtab
; c
< &cmdtab
[NCMDS
]; c
++) {
550 int len
= strlen(c
->c_name
);
555 width
= (width
+ 8) &~ 7;
556 columns
= 80 / width
;
559 lines
= (NCMDS
+ columns
- 1) / columns
;
560 for (i
= 0; i
< lines
; i
++) {
561 for (j
= 0; j
< columns
; j
++) {
562 c
= cmdtab
+ j
* lines
+ i
;
563 if (c
->c_name
&& (!proxy
|| c
->c_proxy
)) {
564 printf("%s", c
->c_name
);
566 else if (c
->c_name
) {
567 for (k
=0; k
< (int) strlen(c
->c_name
); k
++) {
571 if (c
+ lines
>= &cmdtab
[NCMDS
]) {
575 w
= strlen(c
->c_name
);
578 (void) putchar('\t');
582 (void) fflush(stdout
);
589 if (c
== (struct cmd
*)-1)
590 printf("?Ambiguous help command %s\n", arg
);
591 else if (c
== (struct cmd
*)0)
592 printf("?Invalid help command %s\n", arg
);
594 printf("%-*s\t%s\n", (int)HELPINDENT
,
595 c
->c_name
, c
->c_help
);
597 (void) fflush(stdout
);