📄 main.cpp
字号:
///////////////////////////////////////////////////////////////////////////////
//
// File:
// main.cpp
//
// Purpose:
// This file provdes the main entry point for the project, and all the
// global scope support routines.
//
// Notes:
// This file expects to be linked without the C-Runtime. If compiling,
// please force the entry point symbol to be "main", and do not link in
// the default librarys.
// This means that no c-runtime functions can be used anywhere in the
// project. I expect this will also exclude any MFC based additions.
//
///////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <winsock.h>
#include <process.h>
#include "telnet.h"
///////////////////////////////////////////////////////////////////////////////
//
// Our simple replacement for the c-runtime includes getting the stdin,
// stdout & stderr handles, and providing new and delete operators, that work
// with the win32 heap functions.
//
//
// standard handles needed for CRT emulation
//
HANDLE hHeap;
HANDLE stdin;
HANDLE stdout;
HANDLE stderr;
//
// new will use the win32 heap functions.
//
void* operator new(unsigned int nSize)
{
return HeapAlloc(hHeap,0,nSize);
}
//
// delete operator provides all memory de-allocation.
// HeapFree doesn't accept NULL.
//
void operator delete(void* pMem)
{
if(pMem) HeapFree(hHeap,0,pMem);
}
//DWORD NumberOfBytes;
//#define print(s) WriteFile(stdout, s, lstrlen(s), &NumberOfBytes,NULL)
//#define input(s) ReadFile(stdin, s, 255, &NumberOfBytes, NULL)
///////////////////////////////////////////////////////////////////////////////
void err(char const* s,...)
{
char buf[1024];
wvsprintf(buf,s,(char*)(s+sizeof(int)));
DWORD nout;
WriteFile(stderr,"Error: ",7,&nout,NULL);
WriteFile(stderr,buf,lstrlen(buf),&nout,NULL);
WriteFile(stderr,"\r\n\r\n",4,&nout,NULL);
#ifdef _DEBUG
OutputDebugString(buf);
OutputDebugString("\n");
#endif
ExitProcess(0);
}
////////////////////////////////////////////
void main()
{
///////////////////////////////////////
// CRT emulation init
// Get the IO handles
stdin = GetStdHandle(STD_INPUT_HANDLE);
stdout = GetStdHandle(STD_OUTPUT_HANDLE);
stderr = GetStdHandle(STD_ERROR_HANDLE);
// Get the heap
hHeap = GetProcessHeap();
// Explicitly force the console into a good mode (actually all we are doing is turning
// mouse input off.
SetConsoleMode(stdin,ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT);
///////////////////////////////////////
// Init winsock
WSADATA wd;
int errn;
if(errn = WSAStartup(0x0101,&wd))
err(sockmsg(errn));
LPTSTR pszCommandLine = GetCommandLine();
char name[256];
int port = IPPORT_TELNET;
// Strip out the exe name
while( *pszCommandLine && (*pszCommandLine != ' '))
pszCommandLine++;
// And any spaces after it.
while( *pszCommandLine && (*pszCommandLine == ' '))
pszCommandLine++;
// No parameter given: Abort with error
if(!*pszCommandLine)
err("syntax: telnet <hostname> [<port>]");
//Get the remote host name
char* tmp = name;
while(*pszCommandLine && (*pszCommandLine != ' '))
*tmp++ = *pszCommandLine++;
*tmp = 0;
// Strip excess spaces. (again)
while( *pszCommandLine && (*pszCommandLine == ' '))
pszCommandLine++;
//Get the port number if present.
if(*pszCommandLine)
if(*pszCommandLine >= '0' && *pszCommandLine < '9')
{
port = 0;
while(*pszCommandLine && (*pszCommandLine != ' '))
port = port*10 + *pszCommandLine++ -48;
}
else
{
_tag_well_known_port_numbers* wkpn = well_known_port_numbers;
while(wkpn->port && lstrcmpi(wkpn->keyword,pszCommandLine))wkpn++;
port = wkpn->port;
}
if(!port)
err("Invalid port specified");
// Give ourselves a neato title.
char title[256];
wsprintf(title,"connecting to %s:%i",name,port);
SetConsoleTitle(title);
// guess what this does.
telnet(name,port);
//Bye bye...
WSACleanup();
//Exit process terminates any waiting threads.
// (Its the CRT that makes a process close when the main thread exits.
// The WinAPI will leave the process as is for as long as it has a
// thread any thread.
ExitProcess(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -