#include "EvryThng.h"
VOID ReportError(LPCTSTR UserMessage, DWORD ExitCode, BOOL PrintErrorMsg)
/* . */
{
DWORD eMsgLen, LastErr = GetLastError();
LPTSTR lpvSysMsg;
HANDLE hStdErr = GetStdHandle(STD_ERROR_HANDLE);
PrintMsg(hStdErr, UserMessage);
if (PrintErrorMsg) {
eMsgLen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, LastErr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpvSysMsg, 0, NULL);
PrintStrings (hStdErr, _T("\n"), lpvSysMsg, _T("\n"), NULL);
/* , . */
HeapFree(GetProcessHeap(), 0, lpvSysMsg); /* . . 5. */
}
if (ExitCode > 0) ExitProcess (ExitCode);
else return;
}
:
2.3 /, , . UNIX cat, ( , ) .
2.3 . , , Web-. Options ( ), . , , Web-, , argv, . Options getopt, UNIX.
2.3. cat:
/* 2. cat. */
/* cat [] [] s, , . */
#include "EvryThng.h"
#define BUF_SIZE 0x200
static VOID CatFile(HANDLE, HANDLE);
int _tmain(int argc, LPTSTR argv[]) {
HANDLE hInFile, hStdIn = GetStdHandle(STD_INPUT_HANDLE);
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
BOOL DashS;
int iArg, iFirstFile;
/* DashS "-s" . */
/* iFirstFile argv[]. */
iFirstFile = Options(argc, argv, _T("s"), &DashS, NULL);
if (iFirstFile == argc) { /* .*/
/* . */
CatFile(hStdIn, hStdOut);
return 0;
}
/* . */
|
|
for (iArg = iFirstFile; iArg < argc; iArg++) {
hInFile = CreateFile(argv [iArg], GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hInFile == INVALID_HANDLE_VALUE &&!DashS) ReportError (_T("Cat: "), 1, TRUE);
CatFile (hInFile, hStdOut);
CloseHandle (hInFile);
}
return 0;
}
/* , :
/* . */
static VOID CatFile(HANDLE hInFile, HANDLE hOutFile) {
DWORD nIn, nOut;
BYTE Buffer [BUF_SIZE];
while (ReadFile(hInFile, Buffer, BUF_SIZE, &nIn, NULL) && (nIn!= 0) && WriteFile(hOutFile, Buffer, nIn, &nOut, NULL));
return;
}
: ASCII Unicode
2.4 1.3, CopyFile. , Unicode , ASCII, . , ; CopyFile , ASCII Unicode.
. , . , , .
_taccess, . access, UNIX, . <io.h>. , _taccess , . 0 , 2 , 4 , 6 , ( , Windows, GENERIC_READ). CreateFile .
2.4. atou:
/* 2. atou ASCII Unicode. */
#include "EvryThng.h"
BOOL Asc2Un(LPCTSTR, LPCTSTR, BOOL);
int _tmain(int argc, LPTSTR argv[]) {
DWORD LocFileIn, LocFileOut;
BOOL DashI = FALSE;
TCHAR YNResp[3] = _T("y");
/* . */
LocFileIn = Options(argc, argv, _T("i"), &DashI, NULL);
LocFileOut = LocFileIn + 1;
if (DashI) { /* ? */
|
|
/* access, . */
if (_taccess(argv[LocFileOut], 0) == 0) {
_tprintf(_T(" ? [y/n]"));
_tscanf(_T ("%s"), &YNResp);
if (lstrcmp(CharLower(YNResp), YES)!= 0) ReportError(_T(" "), 4, FALSE);
}
}
/* CopyFile. */
Asc2Un(argv[LocFileIn], argv [LocFileOut], FALSE);
return 0;
}
2.5 2.4 Asc2Un, .