File: Open.c

package info (click to toggle)
ncftp2 1%3A2.4.3-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 872 kB
  • ctags: 1,231
  • sloc: ansic: 13,766; makefile: 176; sh: 9
file content (887 lines) | stat: -rw-r--r-- 23,863 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
/* Open.c */

#include "Sys.h"

#ifdef SYSLOG
#	include <syslog.h>
#endif

#include "Util.h"
#include "Open.h"
#include "GetPass.h"
#include "Cmds.h"
#include "RCmd.h"
#include "Bookmark.h"
#include "FTP.h"
#include "Get.h"
#include "Getopt.h"
#include "Macro.h"
#include "Hostwin.h"
#include "Main.h"
#include "Complete.h"

/* This is a preference setting specifying whether we use anonymous logins
 * by default (as opposed to user/passwd logins).
 */
int gAnonOpen = (UOPEN == 0);

/* A structure containing some extra information we've learned about
 * the remote host, so we don't need to find out the hard way each
 * time we do something.
 */
extern Bookmark gRmtInfo;

/* Name of the host we're connected to. */
string gHost;

/* We keep track if we've logged in to the host yet.  It's possible to
 * be connected but not authorized (authenticated, whatever) to do anything.
 */
int gLoggedIn = 0;

/* Need to know if we are trying to connect and login, so if we get
 * hangup during that, we will come back to the open without longjmp'ing
 * out.
 */
int gAttemptingConnection = 0;

/* Need to know if they did a 'open -r' so we don't use the built-in pager
 * for long site-connect messages.  Otherwise the program would stop to
 * wait for the user to hit <space> each time we connected to that site
 * which would sort of defeat the purpose of redialing.
 */
int gRedialModeEnabled = 0;

/* If we connect successfully, we'll want to save this port number
 * when we add a host file entry.
 */
int gPortNumberUsed;

int gSavePasswords = 0;

/* Open.c externs */
extern char *gOptArg;
extern int gOptInd, gIsToTTY;
extern unsigned int gFTPPort;
extern long gEventNumber;
extern char gRemoteCWD[256];
extern char gIPStr[32];
extern int gTransferType, gCurType, gRemoteServerType;
extern int gRmtInfoIsNew, gWantRmtInfoSaved;
extern int gConnected, gHasPASV, gVerbosity;
extern int gDoneApplication, gWinInit, gMode;
extern string gEmailAddress, gAnonPassword, gPager;
extern UserInfo gUserInfo;
extern FILE *gLogFile;
extern LineList gRedir;
extern void GetRemoteCWD(char *cdstub, ResponsePtr cwdrp);


/* This is used primarily for non-anonymous logins.  We'll have to ask
 * the user some questions, like their username, password, etc.
 */
int LoginQuestion(
	char *prompt,
	char *answer,
	size_t siz,
	char *defanswer,
	int noEcho)
{
	string prompt2;
	
	/* Only do this if we have an empty string as the answer. */
	if (*answer == '\0') {
		if (defanswer != NULL)
			sprintf(prompt2, "%s [%s]: ", prompt, defanswer);
		else
			sprintf(prompt2, "%s: ", prompt);
		GetAnswer(prompt2, answer, siz, noEcho);
		if ((*answer == '\0') && (defanswer != NULL))
			Strncpy(answer, defanswer, siz);
	}
	return (*answer == '\0' ? (-1) : 0);
}	/* LoginQuestion */




int Login(char *u, char *p, char *a)
{
	string u2, p2, a2;
	ResponsePtr rp;
	int result = -1;
	int isAnonLogin = 1;

	STRNCPY(u2, u);
	STRNCPY(p2, p);
	STRNCPY(a2, a);
		
	rp = InitResponse();
	if (LoginQuestion("User", u2, sizeof(u2), "anonymous", 0) < 0)
		goto done;
	RCmd(rp, "USER %s", u2);

	for (;;) {
		/* Here's a mini finite-automaton for the login process.
		 *
		 * Originally, the FTP protocol was designed to be entirely
		 * implementable from a FA.  It could be done, but I don't think
		 * it's something an interactive process could be the most
		 * effective with.
		 */
		switch (rp->code) {
			case 230:	/* 230 User logged in, proceed. */
			case 202:	/* Command not implemented, superfluous at this site. */
				goto okay;

			case 421:	/* 421 Service not available, closing control connection. */
				goto done;
				
			case 331:	/* 331 User name okay, need password. */
				isAnonLogin =	STREQ("anonymous", u2) ||
								STREQ("ftp", u2);
				if (isAnonLogin)
					rp->printMode = kDontPrint;

				ReInitResponse(rp);
				(void) LoginQuestion(
					"Password",
					p2,
					sizeof(p2),
					isAnonLogin ? gAnonPassword : NULL,
					1
				);
				RCmd(rp, "PASS %s", p2);
				break;

         	case 332:	/* 332 Need account for login. */
         	case 532: 	/* 532 Need account for storing files. */
				ReInitResponse(rp);
				(void) LoginQuestion("Account", a2, sizeof(a2), NULL, 1);
				RCmd(rp, "ACCT %s", a2);
				break;

			case 501:	/* Syntax error in parameters or arguments. */
			case 503:	/* Bad sequence of commands. */
			case 530:	/* Not logged in. */
			case 550:	/* Can't set guest privileges. */
				goto done;
				
			default:
				if (rp->msg.first == NULL) {
					Error(kDontPerror, "Lost connection during login.\n");
				} else {
					Error(kDontPerror, "Unexpected response: %s\n",
						rp->msg.first->line
					);
				}
				goto done;
		}
	}
okay:
	if (isAnonLogin) {
		gRmtInfo.user[0] = '\0';
		gRmtInfo.pass[0] = '\0';
		STRNCPY(gRmtInfo.acct, a2);
	} else {
		STRNCPY(gRmtInfo.user, u2);
		if (gSavePasswords) {
			STRNCPY(gRmtInfo.pass, p2);
			STRNCPY(gRmtInfo.acct, a2);
		}
	}

	result = 0;
done:
	DoneWithResponse(rp);
	return result;
}	/* Login */




/* After closing a site, set or restore some things to their original
 * states.
 */
void PostCloseStuff(void)
{
#ifdef SYSLOG
	syslog (LOG_INFO, "%s disconnected from %s.", gUserInfo.userName, gHost);
#endif
	if (gLoggedIn) {
		gRmtInfo.hasPASV = gHasPASV;
		gRmtInfo.port = gPortNumberUsed;
		gRmtInfo.nCalls++;
		time((time_t *) &gRmtInfo.lastCall);
		STRNCPY(gRmtInfo.lastIP, gIPStr);
		if (gEventNumber > 0L) {
			/* Only do these if we were not in batch mode (colon-mode). */
			if (gRmtInfo.noSaveDir == 0)
					STRNCPY(gRmtInfo.dir, gRemoteCWD);
			(void) RunPrefixedMacro("close.", gRmtInfo.bookmarkName);
			(void) RunPrefixedMacro("close.", "any");
		} else {
			/* Only do these if we are running colon mode. */
			(void) RunPrefixedMacro("colon.close.", gRmtInfo.bookmarkName);
			(void) RunPrefixedMacro("colon.close.", "any");
		}
		SaveCurHostBookmark(NULL);
	}
	gLoggedIn = 0;
	gRemoteCWD[0] = gHost[0] = '\0';
}	/* PostCloseStuff */




/* Close the connection to the remote host and cleanup. */
void DoClose(int tryQUIT)
{
	ResponsePtr rp;
	
	if (tryQUIT != 0) {
		rp = InitResponse();
		rp->eofOkay = 1;	/* We are expecting EOF after this cmd. */
		RCmd(rp, "QUIT");
		DoneWithResponse(rp);
	}
	
	CloseControlConnection();
	CloseDataConnection(1);
	PostCloseStuff();
}	/* DoClose */




int CloseCmd(void)
{
	DoClose(gConnected);
	if (gWinInit == 0)
			SetScreenInfo();	/* Need for line mode. */
	return kNoErr;
}	/* CloseCmd */




/* Given a pointer to an OpenOptions (structure containing all variables
 * that can be set from the command line), this routine makes sure all
 * the variables have valid values by setting them to their defaults.
 */
 
void InitOpenOptions(OpenOptions *openopt)
{
	/* How do you want to open a site if neither -a or -u are given?
	 * gAnonOpen is true (default to anonymous login), unless
	 * Config.h was edited to set UOPEN to 0 instead.
	 */
	openopt->openmode = gAnonOpen ? kOpenImplicitAnon : kOpenImplicitUser;

	/* Normally you don't want to ignore the entry in your netrc. */
	openopt->ignoreRC = 0;

	/* Set the default delay if the user specifies redial mode without
	 * specifying the redial delay.
	 */
	openopt->redialDelay = kRedialDelay;

	/* Normally, you only want to try once. If you specify redial mode,
	 * this is changed.
	 */
	openopt->maxDials = 1;
	
	/* You don't want to dump the file to stdout by default. */
	openopt->ftpcat = kNoFTPCat;

	/* We'll set this later, but we'll use 0 to mean that the port
	 * is explicitly not set by the user.
	 */
	openopt->port = kPortUnset;

	/* We are not in colon-mode (yet). */
	openopt->colonModePath[0] = 0;

	/* Set the hostname to a null string, since there is no default host. */
	openopt->hostname[0] = 0;
	
	/* Set the opening directory path to a null string. */
	openopt->cdpath[0] = 0;

	openopt->interactiveColonMode = 0;

	/* Since we're opening with a possible colon-mode item, we have to
	 * track options for the GetCmd too.
	 */
	InitGetOptions(&openopt->gopt);
}	/* InitOpenOptions */





/* This is responsible for parsing the command line and setting variables
 * in the OpenOptions structure according to the user's flags.
 */

int GetOpenOptions(int argc, char **argv, OpenOptions *openopt, int fromMain)
{
	int					opt, www;
	char				*cp, *hostp, *cpath;

	/* First setup the openopt variables. */
	InitOpenOptions(openopt);

	/* Tell Getopt() that we want to start over with a new command. */
	GetoptReset();
	while ((opt = Getopt(argc, argv, "aiup:rd:g:cmCfGRn:")) >= 0) {
		switch (opt) {		
			case 'a':
				/* User wants to open anonymously. */
				openopt->openmode = kOpenExplicitAnon;
				break;
				
			case 'u':
				/* User wants to open with a login and password. */
				openopt->openmode = kOpenExplicitUser;
				break;

			case 'i':
				/* User wants to ignore the entry in the netrc. */
				openopt->ignoreRC = 1;
				break;

			case 'p':
				/* User supplied a port number different from the default
				 * ftp port.
				 */
				openopt->port = atoi(gOptArg);
				if (openopt->port <= 0) {
					/* Probably never happen, but just in case. */
					(void) PrintF("%s: bad port number (%s).\n", argv[0],
						gOptArg);
					goto usage;
				}
				break;
				
			case 'd':
				/* User supplied a delay (in seconds) that differs from
				 * the default.
				 */
				openopt->redialDelay = atoi(gOptArg);
				break;
				
			case 'g':
				/* User supplied an upper-bound on the number of redials
				 * to try.
				 */
				openopt->maxDials = atoi(gOptArg);
				break;

			case 'r':
				openopt->maxDials = -1;
				break;

			case 'm':
				/* ftpcat mode is only available from your shell command-line,
				 * not from the ncftp shell.  Do that yourself with 'more zz'.
				 */
				if (gEventNumber == 0L) {
					/* If gEventNumber is zero, then we were called directly
					 * from main(), and before the ftp shell has started.
					 */
					openopt->ftpcat = kFTPMore;
					/* ftpcat mode is really ftpmore mode. */
					break;
				} else {
					PrintF(
"You can only use this form of colon-mode (-m) from your shell command line.\n\
Try 'ncftp -m wuarchive.wustl.edu:/README'\n");
				}
				goto usage;

			case 'c':
				/* ftpcat mode is only available from your shell command-line,
				 * not from the ncftp shell.  Do that yourself with 'get zz -'.
				 */
				if (gEventNumber == 0L) {
					/* If gEventNumber is zero, then we were called directly
					 * from main(), and before the ftp shell has started.
					 */
					openopt->ftpcat = kFTPCat;
					break;
				} else {
					PrintF(
"You can only use ftpcat/colon-mode from your shell command line.\n\
Try 'ncftp -c wuarchive.wustl.edu:/README > file.'\n");
				}
				goto usage;

			/* These are options that can be passed to get. */
			case 'C':
			case 'f':
			case 'G':
			case 'R':
			/* case 'z':  Note that we can't handle the rename here. */
			case 'n':
				if (fromMain) {
					(void) SetGetOption(&openopt->gopt, opt, gOptArg);
					break;
				}
				goto usage;

			default:
				if (fromMain)
					break;
					
			usage:
				return kUsageErr;
		}
	}

	if (argv[gOptInd] == NULL) {
		if (!fromMain) {
			if (openopt->hostname[0] == 0)
				goto usage;
		}
	} else {
		/* The user gave us a host to open.
		 *
		 * First, check to see if they gave us a colon-mode path
		 * along with the hostname.  We also understand a WWW path,
		 * like "ftp://bang.nta.no/pub/fm2html.v.0.8.4.tar.Z".
		 */
		hostp = argv[gOptInd];
		cpath = NULL;
		if ((cp = strchr(hostp, ':')) != NULL) {
			if (fromMain) {
				*cp++ = '\0';
				cpath = cp;
				www = 0;	/* Is 0 or 1, depending on the type of path. */
				if ((*cp == '/') && (cp[1] == '/')) {
					/* First make sure the path was intended to be used
					 * with ftp and not one of the other URLs.
					 */
					if (!ISTREQ(argv[gOptInd], "ftp")) {
						PrintF(
							"Bad URL '%s' -- WWW paths must be prefixed by 'ftp://'.\n",
							argv[gOptInd]
						);
						goto usage;
					}
	
					cp += 2;
					hostp = cp;
					cpath = NULL;	/* It could have been ftp://hostname only. */
	
					if ((cp = strchr(hostp, '/')) != NULL) {
						*cp++ = '\0';
						cpath = cp;
					}
					www = 1;
				}
				if (cpath != NULL) {
					(void) STRNCPY(openopt->colonModePath, www ? "/" : "");
					(void) STRNCAT(openopt->colonModePath, cpath);
					cp = openopt->colonModePath +
						strlen(openopt->colonModePath) - 1;
					if (*cp == '/') {
						/* Colon-mode path ended in a slash, so you said it
						 * was a directory.  That means we should start from
						 * this directory when we open this site.
						 */
						*cp = '\0';
						openopt->interactiveColonMode = 1;
					}
					DebugMsg("Colon-Mode Path = '%s'\n", openopt->colonModePath);
				}

			} else {
				/* I may lift this restriction later. */
				EPrintF("You can't use colon mode in the command shell.\n");
				return kUsageErr;
			}
		}
		(void) STRNCPY(openopt->hostname, hostp);

		if ((openopt->colonModePath[0] == '\0') &&
			(openopt->ftpcat != kNoFTPCat))
		{
			/* User specified ftpcat mode, but didn't supply
			 * the host:file.
			 */
			EPrintF("You didn't use colon mode correctly.\n\
If you use -c or -m, you need to do something like this:\n\
ncftp -c wuarchive.wustl.edu:/pub/README (to cat this file to stdout).\n");
			return kUsageErr;
		}

		GetBookmark(openopt->hostname, sizeof(openopt->hostname));
		if (openopt->port == kPortUnset) {
			/* No port specified, so use same port as last time. */
			if (gRmtInfo.port == kPortUnset)
				openopt->port = gFTPPort;
			else
				openopt->port = gRmtInfo.port;
		}
	}
	return kNoErr;
}	/* GetOpenOptions */




void CheckRemoteSystemType(int force_binary)
{
	string remoteSys;

	if (gRmtInfoIsNew) {
		/* If first time here, check somethings while we can. */
		if (DoSystem(remoteSys, sizeof(remoteSys)) == 0)
			gRmtInfo.isUnix = !strncmp(remoteSys, "UNIX", (size_t) 4);
	
		/* Set to binary mode if any of the following are true:
		 * (a) The user is using colon-mode (force_binary; below);
		 * (b) The reply-string from SYST said it was UNIX with 8-bit chars.
		 */
		if (!strncmp(remoteSys, "UNIX Type: L8", (size_t) 13))
		{
			gRmtInfo.xferType = 'I';
		}
	
		/* Print a warning for that (extremely) rare Tenex machine. */
		if (!strncmp(remoteSys, "TOPS20", (size_t) 6)) {
			gRmtInfo.xferType = 'T';
			(void) PrintF("Using tenex mode to transfer files.\n");
		}

		if (gRemoteServerType == kNcFTPd) {
			/* This server correctly implements
			 * block transfer mode, so turn it
			 * on by default.
			 *
			 * Unfortunately, most servers don't
			 * support it, and those that do
			 * (AIX ftpd) don't do it correctly.
			 */
			gRmtInfo.xferMode = 'B';
		}

	}
	gTransferType = gRmtInfo.xferType;
	
	/* Use binary mode if this site was last set using ascii mode,
	 * and we are using colon-mode.
	 */
	if ((force_binary) && (gTransferType == 'A'))
		gTransferType = 'I';

	if (SetMode(gRmtInfo.xferMode) < 0) {
		gRmtInfo.xferMode = gMode;
	}
}	/* CheckRemoteSystemType */



/* This is called if the user opened the host with a file appended to
 * the host's name, like "wuarchive.wustl.edu:/pub/readme," or
 * "wuarchive.wustl.edu:/pub."  In the former case, we open wuarchive,
 * and fetch "readme."  In the latter case, we open wuarchive, then set
 * the current remote directory to "/pub."  If we are fetching a file,
 * we can do some other tricks if "ftpcat mode" is enabled.  This mode
 * must be selected from your shell's command line, and this allows you
 * to use the program as a one-liner to pipe a remote file into something,
 * like "ncftp -c wu:/pub/README | wc."  If the user uses ftpcat mode,
 * the program immediately quits instead of going into its own command
 * shell.
 */
void ColonMode(OpenOptions *openopt)
{
	int result;

	/* Check for FTP-cat mode, so we call the appropriate
	 * fetching routine.
	 */
	if (openopt->ftpcat == kFTPCat) {
		InitGetOutputMode(&openopt->gopt, kDumpToStdout);
		openopt->gopt.rName = openopt->colonModePath;
		openopt->gopt.doReports = 0;
		result = DoGet(&openopt->gopt);
	} else if (openopt->ftpcat == kFTPMore) {
		result = DoPage(openopt->colonModePath);
	} else {
		/* Regular colon-mode, where we fetch the file, putting the
		 * copy in the current local directory.
		 */
		InitGetOutputMode(&openopt->gopt, kSaveToDisk);
		openopt->gopt.doReports = 0;
		openopt->gopt.rName = openopt->colonModePath;
		result = DoGetWithGlobbingAndRecursion(&openopt->gopt);
	}
	DoQuit(result == 0 ? result : kExitColonModeFail);
	/*NOTREACHED*/
}	/* ColonMode */




/* Now that we're logged in, do some other stuff prior to letting the
 * user type away at the shell.
 */
void PostLoginStuff(OpenOptions *openopt)
{
	time_t now;
	int wasColonMode;
	
	gLoggedIn = 1;

	/* Clear out the old redir buffer, since we are on a new site. */
	DisposeLineListContents(&gRedir);

	/* Since we're logged in okay, we know what we've collected so far
	 * should be saved for next time.
	 */
	gWantRmtInfoSaved = 1;
	
	/* The FTP module keeps its own note of whether the site has PASV
	 * or not, and has already initialized it to true.  If the gRmtInfo
	 * was fetched from our host file, we can tell FTP.c for sure if
	 * PASV should even be attempted.  If this was a new gRmtInfo, we
	 * will just be a little redundant, since new entries also assume
	 * PASV is supported.
	 */
	gHasPASV = gRmtInfo.hasPASV;
	
	/* Since we connected okay, save this port number for later. */
	gPortNumberUsed = openopt->port;

	/* When a new site is opened, ASCII type is assumed (by protocol),
	 * as well as stream transfer mode.
	 */
	gCurType = 'A';
	gMode = 'S';

	STRNCPY(gHost, openopt->hostname);
#ifdef SYSLOG
	syslog (LOG_INFO, "%s logged into %s.", gUserInfo.userName, gHost);
#endif
	if (gLogFile != NULL) {
		(void) time(&now);
		fprintf(gLogFile, "%s at %s", gHost, ctime(&now));
	}

	wasColonMode = openopt->colonModePath[0] != (char)0;

	/* We need to check for unix and see if we should set binary
	 * mode automatically.
	 */
	CheckRemoteSystemType(wasColonMode);

	if (wasColonMode) {
		if (openopt->interactiveColonMode) {
			/* Interactive colon mode simply means that the thing they
			 * gave us was a directory, and we should just cd to that
			 * directory when we start up.
			 */
			(void) DoChdir(openopt->colonModePath);
		} else {
			/* Regular colon-mode is fetching a file by specifying the
			 * pathname of the file on the shell command line.
			 */
			(void) GetRemoteCWD(kDidNotChdir, NULL);
			ColonMode(openopt);		/* Does not return... */
		}
	} else if (gRmtInfo.dir[0]) {
		/* If we didn't have a colon-mode path, we try setting
		 * the current remote directory to cdpath.  The .dir field is
		 * (usually) the last directory we were in the previous
		 * time we called this site.
		 */
		(void) DoChdir(gRmtInfo.dir);
	} else {
		/* Freshen 'cwd' variable for the prompt. */
		(void) GetRemoteCWD(kDidNotChdir, NULL);
	}

	if (wasColonMode) {
		/* Run separate sets of macros for colon-mode opens and regular,
		 * interactive opens.
		 */
		(void) RunPrefixedMacro("colon.open.", "any");
		(void) RunPrefixedMacro("colon.open.", gRmtInfo.bookmarkName);
	} else {
		(void) RunPrefixedMacro("open.", "any");
		(void) RunPrefixedMacro("open.", gRmtInfo.bookmarkName);
		if (gWinInit == 0)
				SetScreenInfo();	/* Need for line mode. */
		ClearDirCache();
	}
}	/* PostLoginStuff */





/* Given a properly set up OpenOptions, we try connecting to the site,
 * redialing if necessary, and do some initialization steps so the user
 * can send commands.
 */
int Open(OpenOptions *openopt)
{
	int					hErr;
	int					dials;
	char				*user, *pass, *r_acct;	
	int					loginResult;
	int					openMode;

	gRedialModeEnabled = (openopt->maxDials != 1);
	if (ISEXPLICITOPEN(openopt->openmode)) {
		/* User specified an open mode explictly, like open -u,
		 * so do what the user said in spite of what we may have had
		 * in the gRmtInfo.
		 */
		openMode = (ISANONOPEN(openopt->openmode)) ? 'a' : 'u';
	} else {
		if (gRmtInfoIsNew == 1) {
			/* First time opening this site.  Open it anonymously
			 * unless you said not to with a -option.
			 */
			openMode = (ISANONOPEN(openopt->openmode)) ? 'a' : 'u';
		} else {
			/* We've been here before, so use what we had last time. */
			openMode = 'r';
		}
	}

	if ((openMode == 'r') && (gRmtInfo.user[0] == '\0'))
		openMode = 'a';
	if (openMode == 'a') {
		user = "anonymous";
		pass = strchr(gRmtInfo.pass, '@') ? gRmtInfo.pass : gAnonPassword;
		r_acct = "";
	} else if (openMode == 'u') {
		user = "";
		pass = "";
		r_acct = "";
	} else {
		user = gRmtInfo.user;
		pass = gRmtInfo.pass;
		r_acct = gRmtInfo.acct;
	}

	if ((openopt->colonModePath[0]) && (!openopt->interactiveColonMode)) {
		/* If we're running from a shell script (or whatever)
		 * don't dump any output.  If the user is doing this from
		 * the shell, we will at least print the error messages.
		 * We also don't want to "pollute" ftpcat mode, but since
		 * error messages are printed to stderr, and we weren't
		 * going to print anything but error messages anyway,
		 * we're okay by using kErrorsOnly.
		 */
		if (gIsToTTY != 0)
			(void) SetVerbose(kErrorsOnly);
		else
			(void) SetVerbose(kQuiet);
	} else {
		/* If we haven't already setup our interactive shell, which
		 * would happen if you gave a host on the command line, then
		 * we need to do that now because we want the remote host's
		 * startup information to be displayed.
		 */
		Startup();
	}

	
	PrintF("Trying to connect to %s...\n", openopt->hostname);
	for (
			dials = 0;
			openopt->maxDials < 0 || dials < openopt->maxDials;
			dials++)
	{
		if (dials > 0) {
			/* If this is the second dial or higher, sleep a bit. */
			PrintF("Sleeping %u seconds...  ",
				(unsigned) openopt->redialDelay);
			FlushListWindow();
			(void) sleep((unsigned) openopt->redialDelay);
			PrintF("Retry Number: %d\n", dials + 1);
		}
		FlushListWindow();

		SetBar(NULL, "CONNECTING", NULL, 1, 1);
		gAttemptingConnection = 1;
		hErr = OpenControlConnection(openopt->hostname, openopt->port);
		if (hErr == kConnectErrFatal) {
			/* Irrecoverable error, so don't bother redialing.
			 * The error message should have already been printed
			 * from OpenControlConnection().
			 */
			DebugMsg("Cannot recover from open error %d.\n", hErr);
			break;
		} else if (hErr == kConnectNoErr) {
			/* We were hooked up successfully. */
			
			gRemoteCWD[0] = '\0';
	
			/* This updates the status bar (for visual mode). */
			SetBar(NULL, "LOGGING IN", NULL, 1, 1);
			SetPostHangupOnServerProc(PostCloseStuff);
			loginResult = Login(user, pass, r_acct);
			
			if (loginResult == 0) {
				PostLoginStuff(openopt);
				if (openopt->maxDials != 1) {
					/* If they selected redial mode, beep at the user
					 * to get their attention.
					 */
					Beep(1);
				}
				gAttemptingConnection = 0;	/* We are connected. */
				gRedialModeEnabled = 0;		/* Not dialing any more. */
				return(kNoErr);	/* Login okay, so done. */
			}
			/* Otherwise, an error during login occurred, so try again. */
		} else /* (hErr == kConnectErrReTryable), so redial. */ {
			/* Display error now. */
			FlushListWindow();
		}
		
		DoClose(gConnected);
		gAttemptingConnection = 0;
	}

	/* Display error now. */
	FlushListWindow();

	if ((openopt->colonModePath[0]) && (!openopt->interactiveColonMode)) {
		/* If we get here, we we're colon-moding and got a non-redialable
		 * error or we ran out of attempts.
		 */
		DoQuit(kExitColonModeFail);
	}
	return (kCmdErr);
}	/* Open */




int OpenCmd(int argc, char **argv)
{
	OpenOptions			openopt;
	int result;

	/* If there is already a site open, close that one so we can
	 * open a new one.
	 */
	DoClose(gConnected);

	if (argc < 2) {
		result = HostWindow();
	} else {
		if ((result = GetOpenOptions(argc, argv, &openopt, 0)) == kNoErr)
			result = Open(&openopt);
	}
	return result;
}	/* OpenCmd */

/* Open.c */