ReactOS  0.4.14-dev-556-g4c5b21f
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  __inline void DisposeWinsock(int aUNUSED) { if (wsaInit > 0) WSACleanup(); wsaInit--; }
28 #else
29 # define DisposeWinsock(a)
30 #endif
31 
35 
41 int gURLMode = 0;
42 extern int gUnprocessedJobs;
43 char gLocalCWD[512], gPrevLocalCWD[512];
44 
45 extern char *gOptArg;
46 extern int gOptInd;
47 extern char gRemoteCWD[512], gPrevRemoteCWD[512];
48 extern Bookmark gBm;
49 extern int gLoadedBm;
50 extern int gFirstTimeUser;
51 extern int gFirewallType;
52 extern char gAutoAscii[];
53 extern char gFirewallHost[64];
54 extern char gFirewallUser[32];
55 extern char gFirewallPass[32];
56 extern char gFirewallExceptionList[];
57 extern char gCopyright[], gVersion[];
58 extern unsigned int gFirewallPort;
60 extern int gDataPortMode, gRedialDelay;
61 extern int gDebug;
63 extern int gSOBufsize;
65 
66 static void
67 Usage(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 
96 static 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  */
108 void
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 
119  gConn.debugLog = NULL;
120  gConn.errLog = stderr;
121  SetDebug(gDebug);
122  UseTrace();
123  (void) STRNCPY(gConn.user, "anonymous");
124  gConn.host[0] = '\0';
125  gConn.progress = NULL;
128  gConn.leavePass = 1; /* Don't let the lib zap it. */
133  gConn.maxDials = (-1); /* Dial forever, until they hit ^C. */
134  gUnprocessedJobs = 0;
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  */
152 void
154 {
155  if (gConn.connected != 0) {
156  if (gConn.loggedIn != 0) {
158  }
160  }
161  gConn.ctrlTimeout = 3;
162  (void) FTPCloseHost(&gConn);
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  */
171 static void
172 SetStartupURL(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 
232 static void
233 OpenURL(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  */
266 static void
267 PreInit(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();
310  InitCommandList();
311  InitLs();
312  TruncBatchLog();
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  */
324 static void
325 PostInit(void)
326 {
327  PostInitPrefs();
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. */
357 static void
358 Plug(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. */
373 static void
375 {
376  SetXtermTitle("RESTORE");
377  CloseHost();
378  DisposeReadline();
379  CloseTrace();
380  SavePrefs();
381  EndLog();
382  Plug();
383  DisposeWinsock(0);
384 } /* PostShell */
385 
386 
387 
388 
389 int
390 main(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':
433  gConn.port = atoi(gOptArg);
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)
454  gConn.redialDelay = n;
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 */
int gIsTTYr
Definition: main.c:33
#define DisposeWinsock(a)
Definition: main.c:29
static int argc
Definition: ServiceArgs.c:12
unsigned int ctrlTimeout
Definition: ncftp.h:143
void CloseTrace(void)
Definition: trace.c:159
unsigned int port
Definition: ncftp.h:140
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
const char * asciiFilenameExtensions
Definition: ncftp.h:246
int gOptInd
Definition: getopt.c:14
void SetDebug(int i)
Definition: trace.c:88
int gUnprocessedJobs
Definition: spool.c:19
void FTPPerror(const FTPCIPtr cip, const int err, const int eerr, const char *const s1, const char *const s2)
Definition: errno.c:134
#define MAKEWORD(a, b)
Definition: typedefs.h:247
int gCtrlTimeout
Definition: pref.c:24
char pass[64]
Definition: ncftp.h:138
int FTPInitLibrary(const FTPLIPtr lip)
Definition: open.c:1110
int gRedialDelay
Definition: pref.c:44
void AbbrevStr(char *dst, const char *src, size_t max, int mode)
Definition: util.c:451
INT WSAAPI WSACleanup(VOID)
Definition: startup.c:60
DWORD WINAPI GetTickCount(VOID)
Definition: time.c:455
void __cdecl srand(_In_ unsigned int _Seed)
char ourHostName[64]
Definition: ncftp.h:118
char user[64]
Definition: ncftp.h:137
void InitLs(void)
Definition: ls.c:68
GLdouble n
Definition: glext.h:7729
char name[64]
Definition: bookmark.h:11
LinePtr next
Definition: ncftp.h:80
#define STRNCPY(dst, src, n)
Definition: rdesktop.h:168
void GetScreenColumns(void)
Definition: readln.c:55
int gLoadedBm
Definition: bookmark.c:25
int gFirewallType
Definition: preffw.c:17
FTPProgressMeterProc progress
Definition: ncftp.h:198
#define kFirewallNotInUse
Definition: ncftp.h:388
char gLocalCWD[512]
Definition: main.c:43
void SavePrefs(void)
Definition: pref.c:695
int hasMDTM
Definition: bookmark.h:21
#define argv
Definition: mplay32.c:18
int hasPASV
Definition: bookmark.h:22
int hasSIZE
Definition: bookmark.h:20
FILE * stdout
#define ISTREQ(a, b)
Definition: util.h:33
size_t dataSocketSBufSize
Definition: ncftp.h:231
void DisposeReadline(void)
Definition: readln.c:707
#define kErrPWDFailed
Definition: ncftp_errno.h:41
FTPConnectionInfo gConn
Definition: main.c:37
void InitReadline(void)
Definition: readln.c:659
int gDoNotDisplayAds
Definition: pref.c:64
int gTransferType
Definition: main.c:40
int gDebug
Definition: trace.c:19
char host[64]
Definition: ncftp.h:136
unsigned int gFirewallPort
Definition: preffw.c:22
unsigned int port
Definition: bookmark.h:18
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
char gPrevRemoteCWD[512]
Definition: cmds.c:38
int gConnTimeout
Definition: pref.c:24
int gFirstTimeUser
void GetoptReset(void)
Definition: getopt.c:21
FILE * errLog
Definition: ncftp.h:146
char firewallHost[64]
Definition: ncftp.h:217
_Check_return_ _CRTIMP int __cdecl isatty(_In_ int _FileHandle)
void SetXtermTitle(const char *const fmt,...)
Definition: readln.c:718
#define kMalformedURL
Definition: ncftp.h:385
char gPrevLocalCWD[512]
Definition: main.c:43
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
smooth NULL
Definition: ftsmooth.c:416
int gSOBufsize
Definition: pref.c:50
char pass[64]
Definition: bookmark.h:13
int firewallType
Definition: ncftp.h:221
int gURLMode
Definition: main.c:41
#define OpenTrace
Definition: evntrace.h:768
void LoadFirewallPrefs(int)
Definition: preffw.c:268
void InitConnectionInfo(void)
Definition: main.c:109
#define kDefaultFTPBufSize
Definition: ncftp.h:314
void SaveUnsavedBookmark(void)
Definition: cmds.c:270
char firewallPass[64]
Definition: ncftp.h:219
void WriteDefaultFirewallPrefs(FILE *)
Definition: preffw.c:39
FTPLibraryInfo gLib
Definition: main.c:36
char user[64]
Definition: bookmark.h:12
void InitLog(void)
Definition: log.c:20
#define LC_ALL
Definition: locale.h:25
int GetBookmark(const char *const bmabbr, Bookmark *bmp)
Definition: bookmark.c:387
_CRTIMP int __cdecl getpid(void)
void(* FTPProgressMeterProc)(const FTPCIPtr, int)
Definition: ncftp.h:125
FTPProgressMeterProc gProgressMeter
char gVersion[]
Definition: init.c:22
char acct[64]
Definition: ncftp.h:139
int gIsTTY
Definition: main.c:33
void TruncBatchLog(void)
Definition: spool.c:30
void CheckForNewV3User(void)
Definition: pref.c:738
static void OpenURL(void)
Definition: main.c:233
static const WCHAR url[]
Definition: encode.c:1432
#define kNotURL
Definition: ncftp.h:384
const GLubyte * c
Definition: glext.h:8905
int gDataPortMode
Definition: preffw.c:26
static void SetStartupURL(const char *const urlgiven)
Definition: main.c:172
#define kTypeBinary
Definition: ncftp.h:354
void InitCommandList(void)
Definition: shell.c:90
int gScreenColumns
Definition: main.c:34
int gTransferTypeInitialized
Definition: main.c:39
void(* FTPSigProc)(int)
Definition: ncftp.h:76
Bookmark gBm
Definition: bookmark.c:24
static void PostInit(void)
Definition: main.c:325
const char * FTPStrError(int e)
Definition: errno.c:114
Definition: ncftp.h:79
char firewallUser[64]
Definition: ncftp.h:218
int main(int argc, const char *argv[])
Definition: main.c:122
int FTPCloseHost(const FTPCIPtr cip)
Definition: open.c:523
Definition: ncftp.h:84
unsigned int xferTimeout
Definition: ncftp.h:141
char * gOptArg
Definition: getopt.c:16
void PrintStartupBanner(void)
Definition: readln.c:759
char gFirewallHost[64]
Definition: preffw.c:18
void RunBatchIfNeeded(const FTPCIPtr cip)
Definition: spool.c:482
int DecodeDirectoryURL(const FTPCIPtr cip, char *url, LineListPtr cdlist, char *fn, size_t fnsize)
Definition: util.c:1086
GLdouble s
Definition: gl.h:2039
void CommandShell(void)
Definition: shell.c:499
#define Trace(x)
Definition: zutil.h:197
LinePtr first
Definition: ncftp.h:85
static void Plug(void)
Definition: main.c:358
FILE * debugLog
Definition: ncftp.h:145
void EndLog(void)
Definition: log.c:65
size_t dataSocketRBufSize
Definition: ncftp.h:230
char gFirewallPass[32]
Definition: preffw.c:20
int FTPChdir(const FTPCIPtr cip, const char *const cdCwd)
Definition: cmds.c:11
int Getopt(int nargc, const char **const nargv, const char *const ostr)
Definition: getopt.c:42
void LoadPrefs(void)
Definition: pref.c:555
#define NcSignal
Definition: ncftp.h:604
void SetBookmarkDefaults(BookmarkPtr bmp)
Definition: bookmark.c:74
void CloseHost(void)
Definition: main.c:153
void Usage(WCHAR *name)
Definition: main.c:4
int gStartupUrlParameterGiven
Definition: main.c:32
char gLibNcFTPVersion[64]
Definition: ftp.c:11
static void PreInit(void)
Definition: main.c:267
void InitTermcap(void)
Definition: readln.c:148
void InitLineList(LineListPtr list)
Definition: linelist.c:54
int FTPGetCWD(const FTPCIPtr cip, char *const newCwd, const size_t newCwdSize)
Definition: cmds.c:254
int hasUTIME
Definition: bookmark.h:27
unsigned int firewallPort
Definition: ncftp.h:220
char * strchr(const char *String, int ch)
Definition: utclib.c:501
static void PostShell(void)
Definition: main.c:374
_Check_return_ int __cdecl atoi(_In_z_ const char *_Str)
char gFirewallUser[32]
Definition: preffw.c:19
void UseTrace(void)
Definition: trace.c:97
char gFirewallExceptionList[]
Definition: preffw.c:21
#define c
Definition: ke_i.h:80
#define kNoErr
Definition: ncftp_errno.h:9
FILE * stderr
void InitUserInfo(void)
Definition: util.c:630
#define setlocale(n, s)
Definition: locale.h:46
char acct[64]
Definition: bookmark.h:14
int DoOpen(void)
Definition: cmds.c:2168
LineList gStartupURLCdList
Definition: main.c:38
char gAutoAscii[]
Definition: pref.c:56
void exit(int exitcode)
Definition: _exit.c:33
int MayUseFirewall(const char *const hn, int firewallType, const char *const firewallExceptionList)
Definition: util.c:695
#define SIG_IGN
Definition: signal.h:48
GLuint64EXT * result
Definition: glext.h:11304
unsigned int connTimeout
Definition: ncftp.h:142
char * line
Definition: ncftp.h:81
#define kErrCWDFailed
Definition: ncftp_errno.h:42
void InitPrefs(void)
Definition: pref.c:616
int FTPInitConnectionInfo(const FTPLIPtr lip, const FTPCIPtr cip, size_t bufsize)
Definition: open.c:984
void PostInitPrefs(void)
Definition: pref.c:680
int dataPortMode
Definition: ncftp.h:152
char gCopyright[]
Definition: version.c:17
static void DumpFirewallPrefsTemplate(void)
Definition: main.c:97
char * FTPGetLocalCWD(char *buf, size_t size)
Definition: util.c:29
#define printf
Definition: config.h:203
int gNumProgramRuns
Definition: preffw.c:14
char gRemoteCWD[512]
Definition: cmds.c:33
int gXferTimeout
Definition: pref.c:24