home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Best Internet Programs
/
BESTINTERNET.bin
/
internet
/
winftp
/
ws_con.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-01-10
|
41KB
|
1,266 lines
#include "ws_glob.h"
#include "winftp.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
#include <io.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <time.h>
// extern int errno;
extern BOOL bAborted; // timer routine may set this
extern BOOL bDebugLog;
char szDbgLogFile[_MAX_PATH];
//***********************************************************************
//***********************************************************************
int WriteDebugLog (int nCode, int nRetCode, LPSTR lpStr)
{
FILE *fp;
FARPROC lpfnMsgProc;
int nRC;
if (!bDebugLog) return 0;
if (lstrlen (szDbgLogFile)==0)
{
lstrcpy (szDlgPrompt,"Enter log file name:");
lstrcpy (szDlgEdit, "C:\\WNFTPDBG.LOG");
lpfnMsgProc = MakeProcInstance ((FARPROC) WS_InputMsgProc, hInst);
nRC = DialogBox (hInst, (LPSTR) "DLG_INPUT", hWndMain, lpfnMsgProc);
FreeProcInstance (lpfnMsgProc);
if (nRC==IDCANCEL) return 0;
lstrcpy (szDbgLogFile, szDlgEdit);
}
fp = fopen (szDbgLogFile, "at");
switch (nCode)
{
case 0: fprintf (fp, "SEND: %s\n", lpStr); break;
case 1: fprintf (fp, "RESULT: %4d %3d %-16s %s\n", nRetCode, iCode,
(nRetCode==FTP_PRELIM) ? "Prelim" :
(nRetCode==FTP_COMPLETE) ? "Complete" :
(nRetCode==FTP_CONTINUE) ? "Continue" :
(nRetCode==FTP_RETRY) ? "Retry" :
(nRetCode==FTP_ERROR) ? "Error" : "Unknown",
lpStr); break;
}
fclose (fp);
return 0;
}
//***********************************************************************
// Print statistics on the transfer
//***********************************************************************
void PrintTransferStatus (LPSTR lpTyp, LONG lBytes, long nSecs)
{
LONG lSecs = nSecs;
if (lSecs==0) lSecs=1;
DoPrintf ("%sed %ld characters in %ld seconds (%ld bytes/sec)", lpTyp,
lBytes, (long) nSecs, (long) lBytes/lSecs);
}
static UINT nDirNum=0;
//***********************************************************************
// Send in a Command line and return a code indicating type of reply
// send a message on the control socket, read and display the resulting
// message and return the result value
//***********************************************************************
int getreply (SOCKET ctrl_skt,LPSTR cmdstring, BOOL bForce)
{
int iRetCode=0;
iCode=0;
if (strncmp (cmdstring,"PASS ",5)==0) DoAddLine ("PASS xxxxxx");
else DoAddLine (cmdstring);
if (ctrl_skt==INVALID_SOCKET)
{
DoAddLine ("Not connected");
}
else
{
if (bDebugLog) WriteDebugLog (0, iRetCode, cmdstring);
switch (bForce)
{
case TRUE : if (ForcePacket (ctrl_skt,cmdstring)!=-1) iRetCode = ReadDisplayLine (ctrl_skt);
case FALSE: if (SendPacket (ctrl_skt,cmdstring)!=-1) iRetCode = ReadDisplayLine (ctrl_skt);
}
if (bDebugLog) WriteDebugLog (1, iRetCode, cmdstring);
}
return iRetCode; // 0 - 5
}
//***********************************************************************
// Send in a Command line and return a code indicating type of reply
//***********************************************************************
int command (SOCKET ctrl_skt, char *fmt,...)
{
va_list args;
char szBuf[90];
va_start (args, fmt);
vsprintf (szBuf, fmt, args);
va_end (args);
return getreply (ctrl_skt, szBuf, FALSE);
}
//***********************************************************************
// Send in a Command line and return a code indicating type of reply
//***********************************************************************
int ForceCommand (SOCKET ctrl_skt, char *fmt,...)
{
va_list args;
char szBuf[90];
va_start (args, fmt);
vsprintf (szBuf, fmt, args);
va_end (args);
return getreply (ctrl_skt, szBuf, TRUE);
}
//***********************************************************************
// return a string pointer to ON or OFF based on the flag
//***********************************************************************
char *onoff(BOOL flag)
{
if (flag) return("ON"); else return("OFF");
}
//***********************************************************************
// process CWD
//***********************************************************************
int DoCWD(SOCKET ctrl_skt,LPSTR path)
{
char szPath[100];
if (command (ctrl_skt,"CWD %s",path)==FTP_ERROR)
{
if (iCode==500) command (ctrl_skt,"XCWD %s",path);
else
{
lstrcpy (szPath, path);
ConvertTargetDir (szPath, 95);
command (ctrl_skt,"CWD %s", szPath);
}
}
return(iCode/100);
}
//***********************************************************************
// process System Type
//***********************************************************************
int DoSystemCommand (SOCKET ctrl_skt)
{
LPSTR lp;
char szBuf[100];
if (command (ctrl_skt,"SYST")!=FTP_ERROR)
{
lstrcpy (szBuf, szMsgBuf);
strupr (szBuf);
if (strstr (szBuf, "UNIX") != NULL) nHostType = HOST_UNIX;
else if (strstr (szBuf, "ULTRIX") != NULL) nHostType = HOST_UNIX;
else if (strstr (szBuf, "MVS") != NULL) nHostType = HOST_MVS;
else if (strstr (szBuf, "QVT") != NULL) nHostType = HOST_QVT;
else if (strstr (szBuf, "NCSA") != NULL) nHostType = HOST_NCSA;
else if (strstr (szBuf, "CHAMELEON")!=NULL) nHostType = HOST_CHAMELEON;
else if (strstr (szMsgBuf, "VMS") != NULL)
{
lp = strstr (szMsgBuf, "MultiNet");
nHostType = (lp!=NULL)? HOST_VMS_MULTINET : HOST_VMS_UCX;
}
}
return(iCode/100);
}
//***********************************************************************
// proces PWD
//***********************************************************************
int DoPWD(SOCKET ctrl_skt)
{
if (command(ctrl_skt,"PWD")==FTP_ERROR && iCode==500)
{
command(ctrl_skt,"XPWD");
}
return(iCode/100);
}
//***********************************************************************
// process MKD
//***********************************************************************
int DoMKD(SOCKET ctrl_skt,LPSTR pathname)
{
char szPath[100];
if (command (ctrl_skt,"MKD %s",pathname)==FTP_ERROR)
{
if (iCode==500) command (ctrl_skt,"XMKD %s",pathname);
else
{
lstrcpy (szPath, pathname);
ConvertTargetDir (szPath, 95);
command (ctrl_skt,"MKD %s", szPath);
}
}
return(iCode/100);
}
//***********************************************************************
// process RMD
//***********************************************************************
int DoRMD(SOCKET ctrl_skt,LPSTR pathname)
{
char szPath[100];
if (command (ctrl_skt,"RMD %s",pathname)==FTP_ERROR)
{
if (iCode==500) command (ctrl_skt,"XRMD %s",pathname);
else
{
lstrcpy (szPath, pathname);
ConvertTargetDir (szPath, 95);
command (ctrl_skt,"RMD %s", szPath);
}
}
return(iCode/100);
}
//***********************************************************************
// process DELE
//***********************************************************************
int DoDELE(SOCKET ctrl_skt,LPSTR pathname)
{
command(ctrl_skt,"DELE %s",pathname);
return(iCode/100);
}
//***********************************************************************
// Send the Quit Command
//***********************************************************************
int DoDisconnect (SOCKET ctrl_skt)
{
int nRC=-1;
if (ctrl_skt!=INVALID_SOCKET)
{
nRC=command (ctrl_skt, "quit");
shutdown (ctrl_skt, 2);
}
return nRC;
}
//***********************************************************************
// process user command
//***********************************************************************
int DoQUOTE(SOCKET ctrl_skt,LPSTR string)
{
if(strncmp(string,"LIST",4)==0 ||
strncmp(string,"NLST",4)==0)
DoDirList(ctrl_skt,string);
else
command(ctrl_skt,strin