bool File::IsDevice() { /*if (hFile==BAD_HANDLE) return(false);*/ #if defined(_XBOX) || defined(TARGET_POSIX) || defined(_XBMC) return false; //#ifdef _WIN_32 #elif defined(_WIN_32) uint Type=GetFileType(hFile); return(Type==FILE_TYPE_CHAR || Type==FILE_TYPE_PIPE); #else return(isatty(fileno(hFile))); #endif }
oexINT CImage::Encode( oexPBYTE *x_buf, oexINT *x_pnSize, oexCSTR x_pType ) {_STT(); #if !defined( OEX_ENABLE_XIMAGE ) return 0; #else // Lose old memory ReleaseEncodeMemory(); // Get image object if ( !oexCHECK_PTR( m_pimg ) ) return 0; CCxCustomImg *pimg = (CCxCustomImg*)m_pimg; // Copy filename if ( oexCHECK_PTR( x_pType ) ) m_filename = x_pType; // Get the file type DWORD type = ( oexCHECK_PTR( x_pType ) && *x_pType ) ? GetFileType( x_pType ) : -1; if ( (DWORD)-1 == type ) return 0; // Encode the image BYTE *pMem = 0; long lSize = 0; if ( !pimg->Encode( pMem, lSize, type ) || !oexCHECK_PTR( pMem ) || 0 >= lSize ) return 0; // Save pointers m_pMem = pMem; m_uMemSize = (oexUINT)lSize; // +++ This would be nice, it would save a copy, but to make it work // we must ensure that xImage uses oex_malloc() to allocate // the memory. // m_mem.setBuffer( (CBin::t_byte*)pMem, lSize ); // Save data if ( oexCHECK_PTR( x_buf ) ) *x_buf = (oexPBYTE)m_pMem; if ( oexCHECK_PTR( x_pnSize ) ) *x_pnSize = m_uMemSize; return lSize; #endif }
/* * Check whether a given standard handle needs to be redirected. * * If you run a Windows-subsystem program from cmd.exe on Windows XP, * and you haven't redirected the handle in question, GetStdHandle() * succeeds (so it doesn't return INVALID_HANDLE_VALUE or NULL), but * GetFile_type fails on the results with ERROR_INVALID_HANDLE. * In that case, redirection to a console is necessary. * * If you run it from the shell prompt in "mintty" in at least some * versions of Cygwin on Windows XP, and you haven't redirected the * handle in question, GetStdHandle() succeeds and returns a handle * that's a pipe or socket; it appears mintty reads from it and outputs * what it reads to the console. */ static gboolean needs_redirection(int std_handle) { HANDLE fd; DWORD handle_type; DWORD error; fd = GetStdHandle(std_handle); if (fd == NULL) { /* * No standard handle. According to Microsoft's * documentation for GetStdHandle(), one reason for * this would be that the process is "a service on * an interactive desktop"; I'm not sure whether * such a process should be popping up a console. * * However, it also appears to be the case for * the standard input and standard error, but * *not* the standard output, for something run * with a double-click in Windows Explorer, * sow we'll say it needs redirection. */ return TRUE; } if (fd == INVALID_HANDLE_VALUE) { /* * OK, I'm not when this would happen; return * "no redirection" for now. */ return FALSE; } handle_type = GetFileType(fd); if (handle_type == FILE_TYPE_UNKNOWN) { error = GetLastError(); if (error == ERROR_INVALID_HANDLE) { /* * OK, this appears to be the case where we're * running something in a mode that needs a * console. */ return TRUE; } } /* * Assume no redirection is needed for all other cases. */ return FALSE; }
/** * Read header from the input file. * @param [in] InputFile - Input file to read from */ void THDF5_FileHeader::ReadHeaderFromInputFile(THDF5_File & InputFile){ // read file type HDF5_FileHeaderValues[hdf5_fhi_file_type] = InputFile.ReadStringAttribute("/",HDF5_FileHeaderNames[hdf5_fhi_file_type].c_str()); if (GetFileType() == hdf5_ft_input) { HDF5_FileHeaderValues[hdf5_fhi_created_by] = InputFile.ReadStringAttribute("/",HDF5_FileHeaderNames[hdf5_fhi_created_by].c_str()); HDF5_FileHeaderValues[hdf5_fhi_creation_date] = InputFile.ReadStringAttribute("/",HDF5_FileHeaderNames[hdf5_fhi_creation_date].c_str()); HDF5_FileHeaderValues[hdf5_fhi_file_description] = InputFile.ReadStringAttribute("/",HDF5_FileHeaderNames[hdf5_fhi_file_description].c_str()); HDF5_FileHeaderValues[hdf5_fhi_major_version] = InputFile.ReadStringAttribute("/",HDF5_FileHeaderNames[hdf5_fhi_major_version].c_str()); HDF5_FileHeaderValues[hdf5_fhi_minor_version] = InputFile.ReadStringAttribute("/",HDF5_FileHeaderNames[hdf5_fhi_minor_version].c_str()); } }// end of ReadHeaderFromInputFile
void init_stdio(void) { int i; __gOpenFiles[0].handle = GetStdHandle(STD_INPUT_HANDLE); __gOpenFiles[1].handle = GetStdHandle(STD_OUTPUT_HANDLE); __gOpenFiles[2].handle = GetStdHandle(STD_ERROR_HANDLE); __gOpenFiles[0].flags = (GetFileType(ULongToPtr(STD_INPUT_HANDLE))== FILE_TYPE_CHAR)? FCONSOLE:0; __gOpenFiles[1].flags = (GetFileType(ULongToPtr(STD_OUTPUT_HANDLE))== FILE_TYPE_CHAR)? FCONSOLE:0; __gOpenFiles[2].flags = (GetFileType(ULongToPtr(STD_ERROR_HANDLE))== FILE_TYPE_CHAR)? FCONSOLE:0; for(i=3; i<__MAX_OPEN_FILES; i++) { __gOpenFiles[i].handle = INVHL; __gOpenFilesCopy[i].handle = INVHL; __gOpenFiles[i].flags = 0; } my_stdin = &__gOpenFiles[0]; my_stdout = &__gOpenFiles[1]; my_stderr = &__gOpenFiles[2]; }
//===================>>> vedTextEditor::TextMouseDown <<<==================== void vedTextEditor::TextMouseDown(int row, int col, int button) { static int clicks = 0; int btn = (GetFileType() == gccError || GetFileType() == bccError) ? 1 : button; long oldLine = GetCurLine(); // remember current position int oldCol = getColPos(); vTextEditor::TextMouseDown(row, col, btn); // translate to left if (button == 1 && oldLine == GetCurLine() && oldCol == getColPos()) // double click... { ++clicks; if (clicks > 3) clicks = 1; setSelection(clicks); } else { clicks = 0; } }
static void lseek_work_cb(uv_work_t *req) { struct req * r = req->data; const int fd = (int)r->buf.len; int64_t offset = voids_to_int64_t(&r->c); #ifdef _WIN32 const DWORD whence = r->offset; HANDLE handle = (HANDLE)(0 + _get_osfhandle(fd)); if ( handle == INVALID_HANDLE_VALUE ){ r->offset = UV_EBADF; offset = -1; } else if ( GetFileType(handle) != FILE_TYPE_DISK ){ /* from: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365542(v=vs.85).aspx "You cannot use the SetFilePointerEx function with a handle to a nonseeking device such as a pipe or a communications device. To determine the file type for hFile, use the GetFileType function." But it doesn't return an error for whatever reason. So I check it manually */ r->offset = UV_ESPIPE; offset = -1; } else { LARGE_INTEGER distance_to_move; LARGE_INTEGER new_position; distance_to_move.QuadPart = offset; if ( SetFilePointerEx(handle,distance_to_move,&new_position,whence) ){ offset = new_position.QuadPart; } else { DWORD er = GetLastError(); r->offset = uwt_translate_sys_error(er); offset = -1; } } #else const int whence = r->offset; errno = 0; offset = lseek(fd,offset,whence); r->offset = -errno; #endif int64_t_to_voids(offset,&r->c); }
int rktio_system_fd_is_terminal(rktio_t *rktio, intptr_t fd) { #ifdef RKTIO_SYSTEM_UNIX return isatty(fd); #endif #ifdef RKTIO_SYSTEM_WINDOWS if (GetFileType((HANDLE)fd) == FILE_TYPE_CHAR) { DWORD mode; if (GetConsoleMode((HANDLE)fd, &mode)) return 1; else return 0; } else return 0; #endif }
void print_utf8_string(const char *string) { #ifdef G_OS_WIN32 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); if (GetFileType(h) != FILE_TYPE_CHAR) fputs(string, stdout); else { gunichar2 *utf16 = g_utf8_to_utf16(string, -1, NULL, NULL, NULL); fflush(stdout); WriteConsoleW(h, utf16, wcslen(utf16), NULL, NULL); g_free(utf16); } #else g_print("%s", string); #endif }
/* \internal */ bool QFSFileEnginePrivate::nativeIsSequential() const { #if !defined(Q_OS_WINCE) && !defined(Q_OS_WINRT) HANDLE handle = fileHandle; if (fh || fd != -1) handle = (HANDLE)_get_osfhandle(fh ? QT_FILENO(fh) : fd); if (handle == INVALID_HANDLE_VALUE) return false; DWORD fileType = GetFileType(handle); return (fileType == FILE_TYPE_CHAR) || (fileType == FILE_TYPE_PIPE); #else return false; #endif }
char * getpass (const char * prompt) { static char input[256]; HANDLE in; HANDLE err; DWORD count; in = GetStdHandle (STD_INPUT_HANDLE); err = GetStdHandle (STD_ERROR_HANDLE); if (in == INVALID_HANDLE_VALUE || err == INVALID_HANDLE_VALUE) return NULL; if (WriteFile (err, prompt, strlen (prompt), &count, NULL)) { int istty = (GetFileType (in) == FILE_TYPE_CHAR); DWORD old_flags; int rc; if (istty) { if (GetConsoleMode (in, &old_flags)) SetConsoleMode (in, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT); else istty = 0; } rc = ReadFile (in, input, sizeof (input), &count, NULL); if (count >= 2 && input[count - 2] == '\r') input[count - 2] = '\0'; else { char buf[256]; while (ReadFile (in, buf, sizeof (buf), &count, NULL) > 0) if (count >= 2 && buf[count - 2] == '\r') break; } WriteFile (err, "\r\n", 2, &count, NULL); if (istty) SetConsoleMode (in, old_flags); if (rc) return input; } return NULL; }
static value do_fstat(value handle, int use_64) { int ret; struct _stat64 buf; __int64 st_ino; HANDLE h; DWORD ft; st_ino = 0; memset(&buf, 0, sizeof buf); buf.st_nlink = 1; h = Handle_val(handle); ft = GetFileType(h) & ~FILE_TYPE_REMOTE; switch(ft) { case FILE_TYPE_DISK: if (!safe_do_stat(0, use_64, NULL, Handle_val(handle), &st_ino, &buf)) { uerror("fstat", Nothing); } break; case FILE_TYPE_CHAR: buf.st_mode = S_IFCHR; break; case FILE_TYPE_PIPE: { DWORD n_avail; if (Descr_kind_val(handle) == KIND_SOCKET) { buf.st_mode = S_IFSOCK; } else { buf.st_mode = S_IFIFO; } if (PeekNamedPipe(h, NULL, 0, NULL, &n_avail, NULL)) { buf.st_size = n_avail; } } break; case FILE_TYPE_UNKNOWN: unix_error(EBADF, "fstat", Nothing); default: win32_maperr(GetLastError()); uerror("fstat", Nothing); } return stat_aux(use_64, st_ino, &buf); }
config_struct* getConfigObject(LPCWSTR name,config_struct* obj = 0){ static ObjectList<config_struct>* dbList = 0; LocalCriticalSection lcs(_configCs); if(dbList==0){ dbList = new (mallocGlobalStaticMem(sizeof(ObjectList<config_struct>))) ObjectList<config_struct>; } if(obj!=NULL){ obj->icount--; if(obj->icount==0){ dbList->Delete(obj); } return 0; } String fn,title; if(WcsLength(name)==0){ fn = App::GetAppDirectory(); title = App::GetAppName(); FPLinkPath(fn,title); FPLinkExt(fn,L"json"); }else{ fn = name; } config_struct* rs = 0; for(uint i=0;i<dbList->Count();i++){ rs = &(*dbList)[i]; if(FPIsSame(fn,rs->FileName)){ rs->icount++; return rs; } } rs = new config_struct; rs->icount = 1; if(1!=GetFileType(fn)){ File f; f.Create(fn); } if(!rs->Tree.LoadFromFile(fn)){ CONASSERT(L"config file is not a valid json format"); } rs->FileName = fn; dbList->AddIn(rs); return rs; }
void brhist_jump_back(void) { #if defined(_WIN32) && !defined(__CYGWIN__) if (GetFileType(Console_IO.Console_Handle) != FILE_TYPE_PIPE) { COORD Pos; CONSOLE_SCREEN_BUFFER_INFO CSBI; GetConsoleScreenBufferInfo(Console_IO.Console_Handle, &CSBI); Pos.Y = CSBI.dwCursorPosition.Y - brhist.hist_printed_lines; Pos.X = 0; SetConsoleCursorPosition(Console_IO.Console_Handle, Pos); } #else while (brhist.hist_printed_lines-- > 0) fputs(Console_IO.str_up, Console_IO.Console_fp); #endif }
/* move up cursor */ void cursor_up(int rows) { HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); if (GetFileType(hStdOut) == FILE_TYPE_CHAR) { CONSOLE_SCREEN_BUFFER_INFO console_info; if (GetConsoleScreenBufferInfo(hStdOut, &console_info)) { console_info.dwCursorPosition.X = 0; console_info.dwCursorPosition.Y = (short)(console_info.dwCursorPosition.Y - rows); SetConsoleCursorPosition( hStdOut, console_info.dwCursorPosition); } } }
/** 读入文件 @param filename 被读入的文件名 */ bool Open(char *filename) { int nSrcLen = strlen(filename); if ( MAX_PATH <= nSrcLen) strcpy_s(g_FileName, nSrcLen, filename); else return false;//输入过长 Clear();//先清理了 g_DataLen = GetFileLength(g_FileName); //有内容存在 if( g_DataLen > 0 ) { g_pData = _MALLOC(char,g_DataLen + 1);//多个"\0" FILE *fp = fopen(g_FileName, "rb"); fread(g_pData, g_DataLen, 1, fp); //读数据 fclose(fp); g_FileType= GetFileType();//判断文件类型是dos还是unix } else {
/* erase to the end of current line */ void erase_eol() { HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); if (GetFileType(hStdOut) == FILE_TYPE_CHAR) { CONSOLE_SCREEN_BUFFER_INFO console_info; if (GetConsoleScreenBufferInfo(hStdOut, &console_info)) { DWORD result; FillConsoleOutputCharacter( hStdOut, ' ', console_info.dwSize.X - console_info.dwCursorPosition.X, console_info.dwCursorPosition, &result); } } }
static int is_console(int fd) { CONSOLE_SCREEN_BUFFER_INFO sbi; DWORD mode; HANDLE hcon; static int initialized = 0; /* get OS handle of the file descriptor */ hcon = (HANDLE) _get_osfhandle(fd); if (hcon == INVALID_HANDLE_VALUE) return 0; /* check if its a device (i.e. console, printer, serial port) */ if (GetFileType(hcon) != FILE_TYPE_CHAR) return 0; /* check if its a handle to a console output screen buffer */ if (!fd) { if (!GetConsoleMode(hcon, &mode)) return 0; /* * This code path is only reached if there is no console * attached to stdout/stderr, i.e. we will not need to output * any text to any console, therefore we might just as well * use black as foreground color. */ sbi.wAttributes = 0; } else if (!GetConsoleScreenBufferInfo(hcon, &sbi)) return 0; if (fd >= 0 && fd <= 2) fd_is_interactive[fd] |= FD_CONSOLE; /* initialize attributes */ if (!initialized) { console = hcon; attr = plain_attr = sbi.wAttributes; negative = 0; initialized = 1; } return 1; }
int _tmain(int argc, _TCHAR* argv[]) { DWORD aid = 0; video_info_t VideoInfo; audio_info_t AudioInfo; HMODULE hDLL; if (argc < 2) return 0; if (argc > 2) aid = _wtoi(argv[2]); if (NULL == (hDLL = LoadLibraryA("dshownative.dll"))) return 0; InitDShowGraphFromFile = (TInitDShowGraphFromFile)GetProcAddress(hDLL,"InitDShowGraphFromFileW"); StartGraph = (TGraphOperate)GetProcAddress(hDLL,"StartGraph"); StopGraph = (TGraphOperate)GetProcAddress(hDLL,"StopGraph"); DestroyGraph = (TGraphOperate)GetProcAddress(hDLL,"DestroyGraph"); if (!(g_pdgi = InitDShowGraphFromFile(argv[1],MEDIASUBTYPE_YV12,0,0,VCallBackProc,CallBackProc,&VideoInfo,&AudioInfo))) return 0; #ifndef _DEBUG waveheader.channels = AudioInfo.nChannels; waveheader.sample_rate = AudioInfo.nSamplesPerSec; waveheader.bits_per_sample = AudioInfo.wBitsPerSample; waveheader.block_align = waveheader.channels * waveheader.bits_per_sample / 8; waveheader.avg_bytes_sec = waveheader.block_align * waveheader.sample_rate; waveheader.format_tag = 0x3; StdOut = GetStdHandle(STD_OUTPUT_HANDLE); WriteFile(StdOut,&waveheader,sizeof(TWaveHeader),&dwWritten,NULL); #endif SetConsoleCtrlHandler(HandlerRoutine, TRUE); hEvent = CreateEventA(NULL,FALSE,FALSE,NULL); StartGraph(g_pdgi); WaitForSingleObject(hEvent,INFINITE); StopGraph(g_pdgi);DestroyGraph(g_pdgi); if (GetFileType(StdOut) == FILE_TYPE_DISK) { waveheader.data_size = GetFileSize(StdOut,NULL) - sizeof(TWaveHeader); waveheader.size = waveheader.data_size + sizeof(TWaveHeader) - 8; SetFilePointer(StdOut,0,NULL,FILE_BEGIN); WriteFile(StdOut,&waveheader,sizeof(TWaveHeader),&dwWritten,NULL); } CloseHandle(StdOut); CoUninitialize(); return 1; }
oexINT CImage::Encode( oexPBYTE x_pBuf, oexINT x_nSize, oexCSTR x_pType ) {_STT(); #if !defined( OEX_ENABLE_XIMAGE ) return 0; #else // Sanity checks if ( oexCHECK_PTR( x_pBuf ) || !x_nSize ) return 0; // Get image object if ( !oexCHECK_PTR( m_pimg ) ) return 0; CCxCustomImg *pimg = (CCxCustomImg*)m_pimg; // Use filename if not supplied if ( !oexCHECK_PTR( x_pType ) ) x_pType = m_filename.Ptr(); // Get the file type DWORD type = GetFileType( x_pType ); if ( (DWORD)-1 == type ) return 0; // Wrap the memory pointer s_CxMemFile mf( x_pBuf, x_nSize ); // Encode the image if ( !pimg->Encode( &mf, type ) ) { mf.Detach(); return 0; } // Get the number of bytes encoded oexINT nEncoded = mf.Size(); // Detach from the buffer mf.Detach(); return nEncoded; #endif }
oexBOOL CImage::Load( oexCSTR x_pFile, oexCSTR x_pType ) {_STT(); #if !defined( OEX_ENABLE_XIMAGE ) return oexFALSE; #else if ( !oexCHECK_PTR( x_pFile ) ) return oexFALSE; // Get image object if ( !oexCHECK_PTR( m_pimg ) ) return oexFALSE; CCxCustomImg *pimg = (CCxCustomImg*)m_pimg; // Copy filename m_filename = x_pFile; // Get the file type DWORD type = GetFileType( ( oexCHECK_PTR( x_pType ) && *x_pType ) ? x_pType : x_pFile ); if ( (DWORD)-1 == type ) return oexFALSE; #if defined( OEX_WINCE ) // Load the file oexBOOL bRet = pimg->Load( x_pFile, type ); #else // Load the file oexBOOL bRet = pimg->Load( oexStrToMbPtr( x_pFile ), type ); #endif GetRect(); return bRet; #endif }
void exit(int status) #endif { if (!__aborting) { __begin_critical_region(atexit_funcs_access); while (atexit_curr_func > 0) (*atexit_funcs[--atexit_curr_func])(); #if (__dest_os == __win32_os) && (STOP_PROGRAM_BEFORE_EXIT==1) if(GetFileType(GetStdHandle(STD_OUTPUT_HANDLE))==FILE_TYPE_CHAR) { //printf("\n \n Press Enter to continue \n"); //fflush(stdin); //getc(stdin); } #endif __end_critical_region(atexit_funcs_access); /* 970218 bkoz need to move destroy global chain above __stdio_exit as some static objects may have destructors that flush streams */ #if !__INTEL__ #if __POWERPC__ || __CFM68K__ || (__MC68K__ && __A5__) || (__dest_os == __be_os) __destroy_global_chain(); #endif #endif if (__stdio_exit) { (*__stdio_exit)(); __stdio_exit = 0; } } __exit(status); }
BOOL WINAPI FRealFile(LPCSTR nm,FAR_FIND_DATA* fd) { HANDLE f; BOOL rc; f = CreateFile(nm, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); rc = f && f != INVALID_HANDLE_VALUE && GetFileType(f) == FILE_TYPE_DISK; if(rc && fd) { strcpy(fd->cFileName, nm); fd->dwFileAttributes = GetFileAttributes(nm); fd->nFileSizeLow = GetFileSize(f, &fd->nFileSizeHigh); GetFileTime(f, &fd->ftCreationTime, &fd->ftLastAccessTime, &fd->ftLastWriteTime); } CloseHandle(f); return rc; }
void CMain::Go() { // Do whatever the command line parameters say FileName = cmd.InputFile; // Get input file name from command line // Ignore nonexisting filename when building library int IgnoreError = (cmd.FileOptions & CMDL_FILE_IN_IF_EXISTS) && !cmd.OutputFile; Read(IgnoreError); // Read input file GetFileType(); // Determine file type cmd.InputType = FileType; // Save input file type in cmd for access from other modules if (err.Number()) return; // Return if error CheckOutputFileName(); // Construct output file name with default extension if (err.Number()) return; if ((FileType & (FILETYPE_LIBRARY | FILETYPE_OMFLIBRARY)) || (cmd.LibraryOptions & CMDL_LIBRARY_ADDMEMBER)) { // Input file is a library or we are building a library CLibrary lib; // Library handler object *this >> lib; // Transfer my file buffer to lib lib.Go(); // Do conversion or dump *this << lib; // Get file buffer back }
void _free_index(int _index) { cancel_io(_index); if ( (poll_fd[_index].handle != INVALID_HANDLE_VALUE) && (poll_fd[_index].handle != 0) && (GetFileType(poll_fd[_index].handle) == FILE_TYPE_UNKNOWN) ) { _close(poll_fd[_index].fd); } if (!CancelIoEx_Available) { if (_poll_fd[_index].original_handle != INVALID_HANDLE_VALUE) { CloseHandle(poll_fd[_index].handle); } _poll_fd[_index].original_handle = INVALID_HANDLE_VALUE; _poll_fd[_index].thread_id = 0; } free_overlapped(poll_fd[_index].overlapped); poll_fd[_index] = INVALID_WINFD; }
void _free_index(int _index) { // Cancel any async IO (Don't care about the validity of our handles for this) cancel_io(_index); // close fake handle for devices if ( (poll_fd[_index].handle != INVALID_HANDLE_VALUE) && (poll_fd[_index].handle != 0) && (GetFileType(poll_fd[_index].handle) == FILE_TYPE_UNKNOWN) ) { _close(poll_fd[_index].fd); } // close the duplicate handle (if we have an actual duplicate) if (!CancelIoEx_Available) { if (_poll_fd[_index].original_handle != INVALID_HANDLE_VALUE) { CloseHandle(poll_fd[_index].handle); } _poll_fd[_index].original_handle = INVALID_HANDLE_VALUE; _poll_fd[_index].thread_id = 0; } free_overlapped(poll_fd[_index].overlapped); poll_fd[_index] = INVALID_WINFD; }
/* Guess handle type */ static SELECTHANDLETYPE get_handle_type(value fd) { DWORD mode; SELECTHANDLETYPE res; CAMLparam1(fd); mode = 0; res = SELECT_HANDLE_NONE; if (Descr_kind_val(fd) == KIND_SOCKET) { res = SELECT_HANDLE_SOCKET; } else { switch(GetFileType(Handle_val(fd))) { case FILE_TYPE_DISK: res = SELECT_HANDLE_DISK; break; case FILE_TYPE_CHAR: /* character file or a console */ if (GetConsoleMode(Handle_val(fd), &mode) != 0) { res = SELECT_HANDLE_CONSOLE; } else { res = SELECT_HANDLE_NONE; }; break; case FILE_TYPE_PIPE: /* a named or an anonymous pipe (socket already handled) */ res = SELECT_HANDLE_PIPE; break; }; }; CAMLreturnT(SELECTHANDLETYPE, res); }
static int _zip_stat_win32(HANDLE h, zip_stat_t *st, _zip_source_win32_read_file_t *ctx) { FILETIME mtimeft; time_t mtime; LARGE_INTEGER size; int regularp; if (!GetFileTime(h, NULL, NULL, &mtimeft)) { zip_error_set(&ctx->error, ZIP_ER_READ, _zip_win32_error_to_errno(GetLastError())); return -1; } if (_zip_filetime_to_time_t(mtimeft, &mtime) < 0) { zip_error_set(&ctx->error, ZIP_ER_READ, ERANGE); return -1; } regularp = 0; if (GetFileType(h) == FILE_TYPE_DISK) { regularp = 1; } if (!GetFileSizeEx(h, &size)) { zip_error_set(&ctx->error, ZIP_ER_READ, _zip_win32_error_to_errno(GetLastError())); return -1; } zip_stat_init(st); st->mtime = mtime; st->valid |= ZIP_STAT_MTIME; if (ctx->end != 0) { st->size = ctx->end - ctx->start; st->valid |= ZIP_STAT_SIZE; } else if (regularp) { st->size = (zip_uint64_t)size.QuadPart; st->valid |= ZIP_STAT_SIZE; } return 0; }
static ssize_t fd_write(hFILE *fpv, const void *buffer, size_t nbytes) { hFILE_fd *fp = (hFILE_fd *) fpv; ssize_t n; do { n = write(fp->fd, buffer, nbytes); } while (n < 0 && errno == EINTR); #ifdef _WIN32 // On windows we have no SIGPIPE. Instead write returns // EINVAL. We check for this and our fd being a pipe. // If so, we raise SIGTERM instead of SIGPIPE. It's not // ideal, but I think the only alternative is extra checking // in every single piece of code. if (n < 0 && errno == EINVAL && GetLastError() == ERROR_NO_DATA && GetFileType((HANDLE)_get_osfhandle(fp->fd)) == FILE_TYPE_PIPE) { raise(SIGTERM); } #endif return n; }
bool SetupWin32ConsoleIO() { #if defined( _WIN32 ) // Only useful on Windows platforms bool newConsole( false ); if ( GetFileType( GetStdHandle( STD_OUTPUT_HANDLE ) ) == FILE_TYPE_UNKNOWN ) { HINSTANCE hInst = ::LoadLibrary( "kernel32.dll" ); typedef BOOL ( WINAPI * pAttachConsole_t )( DWORD ); pAttachConsole_t pAttachConsole( ( BOOL ( _stdcall * )( DWORD ) )GetProcAddress( hInst, "AttachConsole" ) ); if ( !( pAttachConsole && (*pAttachConsole)( ( DWORD ) - 1 ) ) ) { newConsole = true; AllocConsole(); } *stdout = *_fdopen( _open_osfhandle( reinterpret_cast< long >( GetStdHandle( STD_OUTPUT_HANDLE ) ), _O_TEXT ), "w" ); setvbuf( stdout, NULL, _IONBF, 0 ); *stdin = *_fdopen( _open_osfhandle( reinterpret_cast< long >( GetStdHandle( STD_INPUT_HANDLE ) ), _O_TEXT ), "r" ); setvbuf( stdin, NULL, _IONBF, 0 ); *stderr = *_fdopen( _open_osfhandle( reinterpret_cast< long >( GetStdHandle( STD_ERROR_HANDLE ) ), _O_TEXT ), "w" ); setvbuf( stdout, NULL, _IONBF, 0 ); std::ios_base::sync_with_stdio(); } return newConsole; #else // defined( _WIN32 ) return false; #endif // defined( _WIN32 ) }