ReactOS 0.4.16-dev-340-g0540c21
main.c
Go to the documentation of this file.
1/* main.c
2 *
3 * Copyright (c) 1992-2001 by Mike Gleason.
4 * All rights reserved.
5 *
6 */
7
8#include "syshdrs.h"
9#include "ls.h"
10#include "bookmark.h"
11#include "shell.h"
12#include "cmds.h"
13#include "main.h"
14#include "getopt.h"
15#include "progress.h"
16#include "pref.h"
17#include "readln.h"
18#include "trace.h"
19#include "log.h"
20#include "spool.h"
21#include "util.h"
22
23#if defined(WIN32) || defined(_WINDOWS)
24 WSADATA wsaData;
25 int wsaInit = 0;
26
27 static __inline void DisposeWinsock(int aUNUSED) { if (wsaInit > 0) WSACleanup(); wsaInit--; }
28#else
29# define DisposeWinsock(a)
30#endif
31
35
41int gURLMode = 0;
42extern int gUnprocessedJobs;
43char gLocalCWD[512], gPrevLocalCWD[512];
44
45extern char *gOptArg;
46extern int gOptInd;
47extern char gRemoteCWD[512], gPrevRemoteCWD[512];
48extern Bookmark gBm;
49extern int gLoadedBm;
50extern int gFirstTimeUser;
51extern int gFirewallType;
52extern char gAutoAscii[];
53extern char gFirewallHost[64];
54extern char gFirewallUser[32];
55extern char gFirewallPass[32];
56extern char gFirewallExceptionList[];
57extern char gCopyright[], gVersion[];
58extern unsigned int gFirewallPort;
61extern int gDebug;
63extern int gSOBufsize;
65
66static void
67Usage(void)
68{
69 FILE *fp;
70#ifdef UNAME
71 char s[80];
72#endif
73
74 fp = stderr;
75 (void) fprintf(fp, "\nUsage: ncftp [flags] [<host> | <directory URL to browse>]\n");
76 (void) fprintf(fp, "\nFlags:\n\
77 -u XX Use username XX instead of anonymous.\n\
78 -p XX Use password XX with the username.\n\
79 -P XX Use port number XX instead of the default FTP service port (21).\n\
80 -j XX Use account XX with the username (rarely needed).\n\
81 -F Dump a sample $HOME/.ncftp/firewall prefs file to stdout and exit.\n");
82
83 (void) fprintf(fp, "\nProgram version: %s\nLibrary version: %s\n", gVersion + 5, gLibNcFTPVersion + 5);
84#ifdef UNAME
85 AbbrevStr(s, UNAME, 60, 1);
86 (void) fprintf(fp, "System: %s\n", s);
87#endif
88 (void) fprintf(fp, "\nThis is a freeware program by Mike Gleason (ncftp@ncftp.com).\n");
89 (void) fprintf(fp, "Use ncftpget and ncftpput for command-line FTP.\n\n");
90 exit(2);
91} /* Usage */
92
93
94
95
96static void
98{
100} /* DumpFirewallPrefsTemplate */
101
102
103
104
105/* This resets our state information whenever we are ready to open a new
106 * host.
107 */
108void
110{
111 int result;
112
114 if (result < 0) {
115 (void) fprintf(stderr, "ncftp: init connection info error %d (%s).\n", result, FTPStrError(result));
116 exit(1);
117 }
118
122 UseTrace();
123 (void) STRNCPY(gConn.user, "anonymous");
124 gConn.host[0] = '\0';
128 gConn.leavePass = 1; /* Don't let the lib zap it. */
133 gConn.maxDials = (-1); /* Dial forever, until they hit ^C. */
135 gPrevRemoteCWD[0] = '\0';
137 if (gRedialDelay >= 10)
139 if ((gAutoAscii[0] == '\0') || (ISTREQ(gAutoAscii, "no")) || (ISTREQ(gAutoAscii, "off")) || (ISTREQ(gAutoAscii, "false"))) {
141 } else {
143 }
144} /* InitConnectionInfo */
145
146
147
148
149/* This lets us do things with our state information just before the
150 * host is closed.
151 */
152void
154{
155 if (gConn.connected != 0) {
156 if (gConn.loggedIn != 0) {
158 }
160 }
161 gConn.ctrlTimeout = 3;
163} /* CloseHost */
164
165
166
167
168/* If the user specified a URL on the command-line, this initializes
169 * our state information based upon it.
170 */
171static void
172SetStartupURL(const char *const urlgiven)
173{
174 int rc;
175 char url[256];
176 char urlfile[128];
177
178 gLoadedBm = 0;
179 (void) STRNCPY(url, urlgiven);
180
181 rc = DecodeDirectoryURL(&gConn, url, &gStartupURLCdList, urlfile, sizeof(urlfile));
182 if (rc == kMalformedURL) {
183 (void) fprintf(stderr, "Malformed URL: %s\n", url);
184 exit(1);
185 } else if (rc == kNotURL) {
186 /* This is what should happen most of the time. */
187 (void) STRNCPY(gConn.host, urlgiven);
188 gURLMode = 2;
189 if (GetBookmark(gConn.host, &gBm) >= 0) {
190 gLoadedBm = 1;
199 gConn.port = gBm.port;
200 } else {
202 }
203
210 }
211 } else {
212 /* URL okay */
213 if (urlfile[0] != '\0') {
214 /* It was obviously not a directory */
215 (void) fprintf(stderr, "Use ncftpget or ncftpput to handle file URLs.\n");
216 exit(1);
217 }
218 gURLMode = 1;
225 }
226 }
227} /* SetStartupURL */
228
229
230
231
232static void
234{
235 LinePtr lp;
236 int result;
237
238 if (gURLMode == 1) {
240 if (DoOpen() >= 0) {
241 for (lp = gStartupURLCdList.first; lp != NULL; lp = lp->next) {
242 result = FTPChdir(&gConn, lp->line);
243 if (result != kNoErr) {
244 FTPPerror(&gConn, result, kErrCWDFailed, "Could not chdir to", lp->line);
245 break;
246 }
247 }
249 if (result != kNoErr) {
251 } else {
252 (void) printf("Current remote directory is %s.\n", gRemoteCWD);
253 }
254 }
255 } else if (gURLMode == 2) {
256 (void) DoOpen();
257 }
258} /* OpenURL */
259
260
261
262
263/* These things are done first, before we even parse the command-line
264 * options.
265 */
266static void
268{
269 int result;
270
271#if defined(WIN32) || defined(_WINDOWS)
272 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
273 fprintf(stderr, "could not initialize winsock\n");
274 exit(1);
275 }
276 wsaInit++;
277#endif
278
279#ifdef HAVE_SETLOCALE
280 setlocale(LC_ALL, "");
281#endif
282#if defined(WIN32) || defined(_WINDOWS)
283 gIsTTY = 1;
284 gIsTTYr = 1;
285#else
286 gIsTTY = ((isatty(2) != 0) && (getppid() > 1)) ? 1 : 0;
287 gIsTTYr = ((isatty(0) != 0) && (getppid() > 1)) ? 1 : 0;
288#endif
289#ifdef SIGPOLL
290 (void) NcSignal(SIGPOLL, (FTPSigProc) SIG_IGN);
291#endif
292 InitUserInfo();
294 if (result < 0) {
295 (void) fprintf(stderr, "ncftp: init library error %d (%s).\n", result, FTPStrError(result));
296 exit(1);
297 }
298#if defined(WIN32) || defined(_WINDOWS)
299 srand((unsigned int) (GetTickCount() & 0x7FFF));
300#else
301 srand((unsigned int) getpid());
302#endif
305 InitLog();
306 InitPrefs();
308 LoadPrefs();
311 InitLs();
313 GetoptReset();
315} /* PreInit */
316
317
318
319
320
321/* These things are done at startup, but after we parse the command-line
322 * options.
323 */
324static void
326{
328 OpenTrace();
329 InitTermcap();
330 InitReadline();
332 gPrevLocalCWD[0] = '\0';
334 if (gNumProgramRuns <= 1)
335 (void) printf("\n%s\n", gCopyright + 5);
336
337 Trace(0, "Fw: %s Type: %d User: %s Pass: %s Port: %u\n",
341 (gFirewallPass[0] == '\0') ? "(none)" : "********",
343 );
344 Trace(0, "FwExceptions: %s\n", gFirewallExceptionList);
345 if (strchr(gLib.ourHostName, '.') == NULL) {
346 Trace(0, "NOTE: Your domain name could not be detected.\n");
348 Trace(0, " Make sure you manually add your domain name to firewall-exception-list.\n");
349 }
350 }
351} /* PostInit */
352
353
354
355
356/* Try to get the user to evaluate my commercial offerings. */
357static void
358Plug(void)
359{
360#if defined(WIN32) || defined(_WINDOWS)
361 /* NcFTPd hasn't been ported to Windows. */
362#else
363 if ((gDoNotDisplayAds == 0) && ((gNumProgramRuns % 7) == 2)) {
364 (void) printf("\n\n\n\tThank you for using NcFTP Client.\n\tAsk your system administrator to try NcFTPd Server!\n\thttp://www.ncftpd.com\n\n\n\n");
365 }
366#endif
367} /* Plug */
368
369
370
371
372/* These things are just before the program exits. */
373static void
375{
376 SetXtermTitle("RESTORE");
377 CloseHost();
379 CloseTrace();
380 SavePrefs();
381 EndLog();
382 Plug();
384} /* PostShell */
385
386
387
388
389int
390main(int argc, const char **const argv)
391{
392 int c;
393 int n;
394
395 PreInit();
396 while ((c = Getopt(argc, argv, "P:u:p:J:rd:g:FVLD")) > 0) switch(c) {
397 case 'P':
398 case 'u':
399 case 'p':
400 case 'J':
402 break;
403 case 'r':
404 case 'g':
405 case 'd':
406 case 'V':
407 case 'L':
408 case 'D':
409 case 'F':
410 break;
411 default:
412 Usage();
413 }
414
415 if (gOptInd < argc) {
418 } else if (gStartupUrlParameterGiven != 0) {
419 /* One of the flags they specified
420 * requires a URL or hostname to
421 * open.
422 */
423 Usage();
424 }
425
426 GetoptReset();
427 /* Allow command-line parameters to override
428 * bookmark settings.
429 */
430
431 while ((c = Getopt(argc, argv, "P:u:p:j:J:rd:g:FVLD")) > 0) switch(c) {
432 case 'P':
434 break;
435 case 'u':
437 break;
438 case 'p':
439 (void) STRNCPY(gConn.pass, gOptArg); /* Don't recommend doing this! */
440 break;
441 case 'J':
442 case 'j':
444 break;
445 case 'r':
446 /* redial is always on */
447 break;
448 case 'g':
450 break;
451 case 'd':
452 n = atoi(gOptArg);
453 if (n >= 10)
455 break;
456 case 'F':
458 exit(0);
459 /*NOTREACHED*/
460 break;
461 case 'V':
462 /*FALLTHROUGH*/
463 case 'L':
464 /*FALLTHROUGH*/
465 case 'D':
466 /* silently ignore these legacy options */
467 break;
468 default:
469 Usage();
470 }
471
472 PostInit();
473 OpenURL();
474 CommandShell();
475 PostShell();
476 exit(0);
477} /* main */
static int argc
Definition: ServiceArgs.c:12
char * strchr(const char *String, int ch)
Definition: utclib.c:501
#define isatty
Definition: acwin.h:103
#define STRNCPY(dst, src, n)
Definition: rdesktop.h:168
void SetBookmarkDefaults(BookmarkPtr bmp)
Definition: bookmark.c:74
int GetBookmark(const char *const bmabbr, Bookmark *bmp)
Definition: bookmark.c:387
#define SIG_IGN
Definition: signal.h:48
#define NULL
Definition: types.h:112
#define Trace(x)
Definition: inflate.c:42
DWORD WINAPI GetTickCount(VOID)
Definition: time.c:455
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
int main()
Definition: test.c:6
#define OpenTrace
Definition: evntrace.h:768
#define printf
Definition: freeldr.h:97
GLdouble s
Definition: gl.h:2039
GLdouble n
Definition: glext.h:7729
const GLubyte * c
Definition: glext.h:8905
GLuint64EXT * result
Definition: glext.h:11304
_Must_inspect_result_ _In_ USAGE _In_ USHORT _In_ USAGE Usage
Definition: hidpi.h:384
#define LC_ALL
Definition: locale.h:17
#define stdout
Definition: stdio.h:99
#define stderr
Definition: stdio.h:100
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
void __cdecl srand(_In_ unsigned int _Seed)
_Check_return_ int __cdecl atoi(_In_z_ const char *_Str)
#define c
Definition: ke_i.h:80
void InitLineList(LineListPtr list)
Definition: linelist.c:54
void InitLs(void)
Definition: ls.c:68
int FTPGetCWD(const FTPCIPtr cip, char *const newCwd, const size_t newCwdSize)
Definition: cmds.c:254
int FTPChdir(const FTPCIPtr cip, const char *const cdCwd)
Definition: cmds.c:11
void FTPPerror(const FTPCIPtr cip, const int err, const int eerr, const char *const s1, const char *const s2)
Definition: errno.c:134
const char * FTPStrError(int e)
Definition: errno.c:114
char gLibNcFTPVersion[64]
Definition: ftp.c:11
#define ISTREQ(a, b)
Definition: util.h:33
void SaveUnsavedBookmark(void)
Definition: cmds.c:270
int DoOpen(void)
Definition: cmds.c:2168
void GetoptReset(void)
Definition: getopt.c:21
int Getopt(int nargc, const char **const nargv, const char *const ostr)
Definition: getopt.c:42
void InitLog(void)
Definition: log.c:20
void EndLog(void)
Definition: log.c:65
int gSOBufsize
Definition: pref.c:50
int gTransferTypeInitialized
Definition: main.c:39
FTPConnectionInfo gConn
Definition: main.c:37
unsigned int gFirewallPort
Definition: preffw.c:22
void InitConnectionInfo(void)
Definition: main.c:109
int gUnprocessedJobs
Definition: spool.c:19
char gPrevLocalCWD[512]
Definition: main.c:43
int gScreenColumns
Definition: main.c:34
char gFirewallHost[64]
Definition: preffw.c:18
static void OpenURL(void)
Definition: main.c:233
int gOptInd
Definition: getopt.c:14
static void PreInit(void)
Definition: main.c:267
int gTransferType
Definition: main.c:40
int gURLMode
Definition: main.c:41
int gConnTimeout
Definition: pref.c:24
LineList gStartupURLCdList
Definition: main.c:38
char * gOptArg
Definition: getopt.c:16
FTPLibraryInfo gLib
Definition: main.c:36
char gFirewallExceptionList[]
Definition: preffw.c:21
int gIsTTYr
Definition: main.c:33
void CloseHost(void)
Definition: main.c:153
FTPProgressMeterProc gProgressMeter
#define DisposeWinsock(a)
Definition: main.c:29
int gNumProgramRuns
Definition: preffw.c:14
char gFirewallUser[32]
Definition: preffw.c:19
int gFirstTimeUser
int gIsTTY
Definition: main.c:33
int gDataPortMode
Definition: preffw.c:26
static void DumpFirewallPrefsTemplate(void)
Definition: main.c:97
char gLocalCWD[512]
Definition: main.c:43
char gRemoteCWD[512]
Definition: cmds.c:33
char gVersion[]
Definition: main.c:57
int gDebug
Definition: ls.c:32
static void SetStartupURL(const char *const urlgiven)
Definition: main.c:172
static void Plug(void)
Definition: main.c:358
int gXferTimeout
Definition: main.c:59
char gAutoAscii[]
Definition: pref.c:56
static void PostInit(void)
Definition: main.c:325
char gFirewallPass[32]
Definition: preffw.c:20
int gRedialDelay
Definition: main.c:60
char gPrevRemoteCWD[512]
Definition: main.c:47
int gDoNotDisplayAds
Definition: main.c:62
int gFirewallType
Definition: preffw.c:17
static void PostShell(void)
Definition: main.c:374
int gStartupUrlParameterGiven
Definition: main.c:32
char gCopyright[]
Definition: version.c:17
int gLoadedBm
Definition: bookmark.c:25
int gCtrlTimeout
Definition: main.c:59
Bookmark gBm
Definition: bookmark.c:24
void InitCommandList(void)
Definition: shell.c:90
void CommandShell(void)
Definition: shell.c:499
void SetDebug(int i)
Definition: trace.c:88
void UseTrace(void)
Definition: trace.c:97
void CloseTrace(void)
Definition: trace.c:159
int MayUseFirewall(const char *const hn, int firewallType, const char *const firewallExceptionList)
Definition: util.c:695
int DecodeDirectoryURL(const FTPCIPtr cip, char *url, LineListPtr cdlist, char *fn, size_t fnsize)
Definition: util.c:1086
void AbbrevStr(char *dst, const char *src, size_t max, int mode)
Definition: util.c:451
void InitUserInfo(void)
Definition: util.c:630
static const WCHAR url[]
Definition: encode.c:1432
#define argv
Definition: mplay32.c:18
#define kDefaultFTPBufSize
Definition: ncftp.h:314
#define kNotURL
Definition: ncftp.h:384
#define kFirewallNotInUse
Definition: ncftp.h:388
void(* FTPSigProc)(int)
Definition: ncftp.h:76
char * FTPGetLocalCWD(char *buf, size_t size)
Definition: util.c:29
int FTPCloseHost(const FTPCIPtr cip)
Definition: open.c:523
int FTPInitConnectionInfo(const FTPLIPtr lip, const FTPCIPtr cip, size_t bufsize)
Definition: open.c:984
#define kMalformedURL
Definition: ncftp.h:385
#define NcSignal
Definition: ncftp.h:604
#define kTypeBinary
Definition: ncftp.h:354
int FTPInitLibrary(const FTPLIPtr lip)
Definition: open.c:1110
void(* FTPProgressMeterProc)(const FTPCIPtr, int)
Definition: ncftp.h:125
#define kNoErr
Definition: ncftp_errno.h:9
#define kErrPWDFailed
Definition: ncftp_errno.h:41
#define kErrCWDFailed
Definition: ncftp_errno.h:42
void LoadPrefs(void)
Definition: pref.c:555
void CheckForNewV3User(void)
Definition: pref.c:738
void SavePrefs(void)
Definition: pref.c:695
void PostInitPrefs(void)
Definition: pref.c:680
void InitPrefs(void)
Definition: pref.c:616
void LoadFirewallPrefs(int)
Definition: preffw.c:268
void WriteDefaultFirewallPrefs(FILE *)
Definition: preffw.c:39
void InitReadline(void)
Definition: readln.c:659
void DisposeReadline(void)
Definition: readln.c:707
void SetXtermTitle(const char *const fmt,...)
Definition: readln.c:718
void PrintStartupBanner(void)
Definition: readln.c:759
void GetScreenColumns(void)
Definition: readln.c:55
void InitTermcap(void)
Definition: readln.c:148
#define exit(n)
Definition: config.h:202
void TruncBatchLog(void)
Definition: spool.c:30
void RunBatchIfNeeded(const FTPCIPtr cip)
Definition: spool.c:482
int hasSIZE
Definition: bookmark.h:20
int hasPASV
Definition: bookmark.h:22
int hasUTIME
Definition: bookmark.h:27
char name[64]
Definition: bookmark.h:11
char acct[64]
Definition: bookmark.h:14
int hasMDTM
Definition: bookmark.h:21
unsigned int port
Definition: bookmark.h:18
char user[64]
Definition: bookmark.h:12
char pass[64]
Definition: bookmark.h:13
int firewallType
Definition: ncftp.h:221
unsigned int firewallPort
Definition: ncftp.h:220
char host[64]
Definition: ncftp.h:136
unsigned int xferTimeout
Definition: ncftp.h:141
char firewallPass[64]
Definition: ncftp.h:219
unsigned int connTimeout
Definition: ncftp.h:142
char firewallHost[64]
Definition: ncftp.h:217
size_t dataSocketRBufSize
Definition: ncftp.h:230
int dataPortMode
Definition: ncftp.h:152
FTPProgressMeterProc progress
Definition: ncftp.h:198
const char * asciiFilenameExtensions
Definition: ncftp.h:246
FILE * debugLog
Definition: ncftp.h:145
FILE * errLog
Definition: ncftp.h:146
size_t dataSocketSBufSize
Definition: ncftp.h:231
unsigned int port
Definition: ncftp.h:140
char pass[64]
Definition: ncftp.h:138
char firewallUser[64]
Definition: ncftp.h:218
unsigned int ctrlTimeout
Definition: ncftp.h:143
char acct[64]
Definition: ncftp.h:139
char user[64]
Definition: ncftp.h:137
char ourHostName[64]
Definition: ncftp.h:118
Definition: ncftp.h:84
LinePtr first
Definition: ncftp.h:85
Definition: ncftp.h:79
char * line
Definition: ncftp.h:81
LinePtr next
Definition: ncftp.h:80
#define setlocale(n, s)
Definition: locale.h:46
#define MAKEWORD(a, b)
Definition: typedefs.h:248
int PASCAL FAR WSACleanup(void)
Definition: startup.c:60
#define getpid
Definition: wintirpc.h:52