home *** CD-ROM | disk | FTP | other *** search
/ The Best Internet Programs / BESTINTERNET.bin / internet / winftp / ws_con.c < prev    next >
C/C++ Source or Header  |  1994-01-10  |  41KB  |  1,266 lines

  1. #include "ws_glob.h"
  2. #include "winftp.h"
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include <io.h>
  8. #include <fcntl.h>
  9. #include <sys\stat.h>
  10. #include <time.h>
  11.  
  12. // extern int errno;
  13.  
  14. extern BOOL bAborted;   // timer routine may set this
  15. extern BOOL bDebugLog;  
  16.  
  17. char szDbgLogFile[_MAX_PATH];
  18.  
  19. //***********************************************************************
  20. //***********************************************************************
  21. int WriteDebugLog (int nCode, int nRetCode, LPSTR lpStr)
  22. {
  23.   FILE *fp;
  24.   FARPROC lpfnMsgProc;
  25.   int nRC;
  26.   
  27.   if (!bDebugLog) return 0;
  28.   if (lstrlen (szDbgLogFile)==0)
  29.   {
  30.     lstrcpy (szDlgPrompt,"Enter log file name:");
  31.     lstrcpy (szDlgEdit, "C:\\WNFTPDBG.LOG");
  32.     lpfnMsgProc = MakeProcInstance ((FARPROC) WS_InputMsgProc, hInst);
  33.     nRC = DialogBox (hInst, (LPSTR) "DLG_INPUT", hWndMain, lpfnMsgProc);
  34.     FreeProcInstance (lpfnMsgProc);
  35.     if (nRC==IDCANCEL) return 0;
  36.     lstrcpy (szDbgLogFile, szDlgEdit);
  37.   }
  38.   fp = fopen (szDbgLogFile, "at");
  39.   switch (nCode)
  40.   {
  41.     case 0: fprintf (fp, "SEND: %s\n", lpStr); break;
  42.     case 1: fprintf (fp, "RESULT: %4d  %3d  %-16s  %s\n", nRetCode, iCode,
  43.                            (nRetCode==FTP_PRELIM)   ? "Prelim" :
  44.                            (nRetCode==FTP_COMPLETE) ? "Complete" : 
  45.                            (nRetCode==FTP_CONTINUE) ? "Continue" : 
  46.                            (nRetCode==FTP_RETRY)    ? "Retry" : 
  47.                            (nRetCode==FTP_ERROR)    ? "Error" : "Unknown", 
  48.                            lpStr); break;
  49.   }
  50.   fclose (fp);
  51.   return 0;
  52. }
  53.  
  54. //***********************************************************************
  55. //  Print statistics on the transfer
  56. //***********************************************************************
  57. void PrintTransferStatus (LPSTR lpTyp, LONG lBytes, long nSecs)
  58. {
  59.   LONG lSecs = nSecs;
  60.   
  61.   if (lSecs==0) lSecs=1;
  62.   DoPrintf ("%sed %ld characters in %ld seconds (%ld bytes/sec)", lpTyp,
  63.         lBytes, (long) nSecs, (long) lBytes/lSecs);
  64. }
  65.  
  66. static UINT nDirNum=0;
  67.  
  68. //***********************************************************************
  69. //  Send in a Command line and return a code indicating type of reply
  70. // send a message on the control socket, read and display the resulting
  71. // message and return the result value
  72. //***********************************************************************
  73. int getreply (SOCKET ctrl_skt,LPSTR cmdstring, BOOL bForce)
  74. {
  75.   int iRetCode=0;
  76.  
  77.   iCode=0;
  78.   if (strncmp (cmdstring,"PASS ",5)==0) DoAddLine ("PASS xxxxxx");
  79.   else DoAddLine (cmdstring);
  80.   if (ctrl_skt==INVALID_SOCKET)
  81.   {
  82.     DoAddLine ("Not connected");
  83.   }
  84.   else 
  85.   {
  86.     if (bDebugLog) WriteDebugLog (0, iRetCode, cmdstring);
  87.     switch (bForce)
  88.     {
  89.       case TRUE : if (ForcePacket (ctrl_skt,cmdstring)!=-1) iRetCode = ReadDisplayLine (ctrl_skt);
  90.       case FALSE: if (SendPacket (ctrl_skt,cmdstring)!=-1) iRetCode = ReadDisplayLine (ctrl_skt);
  91.     }
  92.     if (bDebugLog) WriteDebugLog (1, iRetCode, cmdstring);
  93.   }
  94.   return iRetCode;  // 0 - 5
  95. }
  96.  
  97. //***********************************************************************
  98. //  Send in a Command line and return a code indicating type of reply
  99. //***********************************************************************
  100. int command (SOCKET ctrl_skt, char *fmt,...)
  101. {
  102.   va_list args;
  103.   char szBuf[90];
  104.  
  105.   va_start (args, fmt);
  106.   vsprintf (szBuf, fmt, args);
  107.   va_end (args);
  108.   return getreply (ctrl_skt, szBuf, FALSE);
  109. }
  110.  
  111. //***********************************************************************
  112. //  Send in a Command line and return a code indicating type of reply
  113. //***********************************************************************
  114. int ForceCommand (SOCKET ctrl_skt, char *fmt,...)
  115. {
  116.   va_list args;
  117.   char szBuf[90];
  118.  
  119.   va_start (args, fmt);
  120.   vsprintf (szBuf, fmt, args);
  121.   va_end (args);
  122.   return getreply (ctrl_skt, szBuf, TRUE);
  123. }
  124.  
  125. //***********************************************************************
  126. // return a string pointer to ON or OFF based on the flag
  127. //***********************************************************************
  128. char *onoff(BOOL flag)
  129. {
  130.   if (flag) return("ON"); else return("OFF");
  131. }
  132.  
  133. //***********************************************************************
  134. // process CWD
  135. //***********************************************************************
  136. int DoCWD(SOCKET ctrl_skt,LPSTR path)
  137. {
  138.   char szPath[100];
  139.   
  140.   if (command (ctrl_skt,"CWD %s",path)==FTP_ERROR)
  141.   {
  142.     if (iCode==500) command (ctrl_skt,"XCWD %s",path);
  143.     else
  144.     {
  145.       lstrcpy (szPath, path);
  146.       ConvertTargetDir (szPath, 95);
  147.       command (ctrl_skt,"CWD %s", szPath);
  148.     }
  149.   }
  150.   return(iCode/100);
  151. }
  152.  
  153. //***********************************************************************
  154. // process System Type
  155. //***********************************************************************
  156. int DoSystemCommand (SOCKET ctrl_skt)
  157. {
  158.   LPSTR lp;
  159.   char szBuf[100];
  160.   
  161.   if (command (ctrl_skt,"SYST")!=FTP_ERROR)
  162.   {
  163.     lstrcpy (szBuf, szMsgBuf);
  164.     strupr (szBuf);
  165.     if (strstr (szBuf, "UNIX") != NULL)         nHostType = HOST_UNIX;
  166.     else if (strstr (szBuf, "ULTRIX") != NULL)  nHostType = HOST_UNIX;
  167.     else if (strstr (szBuf, "MVS") != NULL)     nHostType = HOST_MVS;
  168.     else if (strstr (szBuf, "QVT") != NULL)     nHostType = HOST_QVT;
  169.     else if (strstr (szBuf, "NCSA") != NULL)    nHostType = HOST_NCSA;
  170.     else if (strstr (szBuf, "CHAMELEON")!=NULL) nHostType = HOST_CHAMELEON;
  171.     else if (strstr (szMsgBuf, "VMS") != NULL)
  172.     {
  173.       lp = strstr (szMsgBuf, "MultiNet");
  174.       nHostType = (lp!=NULL)? HOST_VMS_MULTINET : HOST_VMS_UCX;
  175.     }
  176.   }
  177.   return(iCode/100);
  178. }
  179.  
  180. //***********************************************************************
  181. // proces PWD
  182. //***********************************************************************
  183. int DoPWD(SOCKET ctrl_skt)
  184. {
  185.   if (command(ctrl_skt,"PWD")==FTP_ERROR && iCode==500) 
  186.   {
  187.     command(ctrl_skt,"XPWD");
  188.   }
  189.   return(iCode/100);
  190. }
  191.  
  192. //***********************************************************************
  193. // process MKD
  194. //***********************************************************************
  195. int DoMKD(SOCKET ctrl_skt,LPSTR pathname)
  196. {
  197.   char szPath[100];
  198.   
  199.   if (command (ctrl_skt,"MKD %s",pathname)==FTP_ERROR)
  200.   {
  201.     if (iCode==500) command (ctrl_skt,"XMKD %s",pathname);
  202.     else
  203.     {
  204.       lstrcpy (szPath, pathname);
  205.       ConvertTargetDir (szPath, 95);
  206.       command (ctrl_skt,"MKD %s", szPath);
  207.     }
  208.   }
  209.   return(iCode/100);
  210. }
  211.  
  212. //***********************************************************************
  213. // process RMD
  214. //***********************************************************************
  215. int DoRMD(SOCKET ctrl_skt,LPSTR pathname)
  216. {
  217.   char szPath[100];
  218.   
  219.   if (command (ctrl_skt,"RMD %s",pathname)==FTP_ERROR)
  220.   {
  221.     if (iCode==500) command (ctrl_skt,"XRMD %s",pathname);
  222.     else
  223.     {
  224.       lstrcpy (szPath, pathname);
  225.       ConvertTargetDir (szPath, 95);
  226.       command (ctrl_skt,"RMD %s", szPath);
  227.     }
  228.   }
  229.   return(iCode/100);
  230. }
  231.  
  232. //***********************************************************************
  233. // process DELE
  234. //***********************************************************************
  235. int DoDELE(SOCKET ctrl_skt,LPSTR pathname)
  236. {
  237.   command(ctrl_skt,"DELE %s",pathname);
  238.   return(iCode/100);
  239. }
  240.  
  241. //***********************************************************************
  242. //  Send the Quit Command
  243. //***********************************************************************
  244. int DoDisconnect (SOCKET ctrl_skt)
  245. {
  246.   int nRC=-1;
  247.  
  248.   if (ctrl_skt!=INVALID_SOCKET)
  249.   {
  250.     nRC=command (ctrl_skt, "quit");
  251.     shutdown (ctrl_skt, 2);
  252.   }
  253.   return nRC;
  254. }
  255.  
  256. //***********************************************************************
  257. // process user command
  258. //***********************************************************************
  259. int DoQUOTE(SOCKET ctrl_skt,LPSTR string)
  260. {
  261.   if(strncmp(string,"LIST",4)==0 ||
  262.      strncmp(string,"NLST",4)==0)
  263.     DoDirList(ctrl_skt,string);
  264.   else
  265.     command(ctrl_skt,strin