#define _PORTABLE_32BIT_CONTEXT #include #include #include #include #include #include #include #include extern int optind; void merror(char *msg, int quit); void usage(char *msg); extern char getopt( int argc, char *argv[], char *format ); void usage(char *msg) { fprintf(stderr, "usage: %s [-b] [-s skip] drive\n\n", msg); fprintf(stderr, "Try %s \\\\.\\PHYSICALDRIVE2\n", msg); fprintf(stderr, "Try %s \\\\.\\LPT1\n", msg); exit(1); } void merror(char *msg, int quit) { char * lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL ); /* Display the string. */ // MessageBox( NULL, lpMsgBuf, // msg, MB_OK|MB_ICONINFORMATION ); fprintf( stderr, "%s: %s", msg, lpMsgBuf ); LocalFree( lpMsgBuf ); if (quit) exit(1); } int main( int argc, char **argv ) { extern char *optarg; extern int optind,opterr; HANDLE hf; char buffer[512], *b; LPVOID lpBuffer=buffer; DWORD i,n, m; int option; int binary=0; BOOL err; while(( option = getopt(argc, argv, "bh"))!=EOF ) { switch(option) { case 'b': binary=1; break; default: usage(argv[0]); break; } } if ( argv[optind] ) { #if 1 hf = CreateFile( (LPCTSTR) argv[optind], GENERIC_WRITE, 0, /* Not shared */ NULL, /* Not inherited by child processes */ OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL ); #else hf = CreateNamedPipe( (LPCTSTR) argv[optind], PIPE_ACCESS_OUTBOUND, 0, /* Not shared */ PIPE_TYPE_BYTE, /* Not inherited by child processes */ PIPE_UNLIMITED_INSTANCES, 512, 512, 2000, NULL ); #endif if ( hf == INVALID_HANDLE_VALUE ) { fprintf(stderr,"%s\n", argv[optind]); merror("CreateFile", 1); } else { if ( binary ) { /* * braindamage all over */ if( -1 == _setmode( _fileno( stdin ), _O_BINARY )) merror("Can't set stdin mode to binary", 1); } while ( !feof(stdin)) { if (0<(n=fread( buffer, 1, sizeof(buffer), stdin ))) WriteFile( hf, lpBuffer, (DWORD)n, (LPDWORD)&m, NULL ); } CloseHandle(hf); } } else usage(argv[0]); return 0; }