unsigned BatchChdir( char *new_dir ) { buff[0] = LNK_CWD; strcpy( &buff[1], new_dir ); BatservWrite( buff, strlen( buff ) + 1 ); BatservRead( buff, sizeof( buff ) ); return( *(unsigned long *)&buff[1] ); }
unsigned BatchCollect( void *ptr, unsigned max, unsigned long *status ) { int len; char *buff = ptr; buff[0] = LNK_QUERY; *(unsigned long *)&buff[1] = max; BatservWrite( buff, 5 ); len = BatservRead( buff, max ) - 1; if( len <= 0 ) return( 0 ); if( *buff == LNK_STATUS ) { *status = *(unsigned long *)&buff[1]; return( -1 ); } memmove( buff, &buff[1], len ); return( len ); }
static void ProcessConnection( void ) { char buff[MAX_TRANS]; DWORD bytes_read; char *dir; DWORD rc; DWORD status; unsigned max; for( ;; ) { bytes_read = BatservRead( buff, sizeof( buff ) ); if( bytes_read == 0 ) break; buff[bytes_read] = '\0'; switch( buff[0] ) { case LNK_CWD: rc = 0; dir = &buff[1]; if( !SetCurrentDirectory( dir ) ) { rc = GetLastError(); } SendStatus( rc ); break; case LNK_RUN: RunCmd( &buff[1] ); break; case LNK_QUERY: max = *(unsigned long *)&buff[1]; if( max > sizeof( buff ) ) max = sizeof( buff ); --max; if( PeekNamedPipe( RedirRead, buff, 0, NULL, &bytes_read, NULL ) && bytes_read != 0 ) { if( bytes_read < max ) max = bytes_read; ReadFile( RedirRead, &buff[1], max, &bytes_read, NULL ); buff[0] = LNK_OUTPUT; BatservWrite( buff, bytes_read + 1 ); } else { if( WaitForSingleObject( ProcHdl, 0 ) == WAIT_TIMEOUT ) { /* let someone else run */ Sleep( 1 ); buff[0] = LNK_NOP; BatservWrite( buff, 1 ); } else { GetExitCodeProcess( ProcHdl, &status ); CloseHandle( RedirRead ); SendStatus( status ); ProcId = 0; CloseHandle( ProcHdl ); } } break; case LNK_CANCEL: // GenerateConsoleCtrlEvent( CTRL_BREAK_EVENT, ProcId ); GenerateConsoleCtrlEvent( CTRL_BREAK_EVENT, 0 ); break; case LNK_ABORT: TerminateProcess( ProcHdl, 0 ); break; case LNK_DONE: Say(( "LNK_DONE\n" )); return; case LNK_SHUTDOWN: Say(( "LNK_SHUTDOWN\n" )); CloseHandle( SemReadUp ); CloseHandle( SemReadDone ); CloseHandle( SemWritten ); CloseHandle( MemHdl ); UnmapViewOfFile( SharedMem ); exit( 0 ); break; } } }