link-grammar-5.8.0/link-parser/parser-utilities.c
/**
 * isatty() compatibility for running under Cygwin when compiling
 * using the Windows native C library.
 * WIN32 isatty() gives a wrong result (for Cygwin) on Cygwin's
 * pseudo-ttys, because they are implemented as Windows pipes.
 * Here is compatibility layer, originally sent to the Cygwin
 * discussion group by Corinna Vinschen, Cygwin Project Co-Leader.
 * See: https://www.cygwin.com/ml/cygwin/2012-11/msg00214.html
 */
isatty() compatibility for running under Cygwin when compiling
using the Windows native C library.
WIN32 isatty() gives a wrong result (for Cygwin) on Cygwin's
pseudo-ttys, because they are implemented as Windows pipes.
Here is compatibility layer, originally sent to the Cygwin
discussion group by Corinna Vinschen, Cygwin Project Co-Leader.
isatty () compatibility for running under Cygwin when compiling using the Windows native C library . WIN32 isatty () gives a wrong result ( for Cygwin ) on Cygwin ' s pseudo - ttys because they are implemented as Windows pipes . Here is compatibility layer originally sent to the Cygwin discussion group by Corinna Vinschen Cygwin Project Co - Leader .
int lg_isatty(int fd)
{
	HANDLE fh;
	long buf[66];  
	PFILE_NAME_INFO pfni = (PFILE_NAME_INFO)buf;
	PWCHAR cp;
	fh = (HANDLE)_get_osfhandle(fd);
	if (!fh || (INVALID_HANDLE_VALUE == fh))
	{
		errno = EBADF;
		return 0;
	}
#if 0
	if (_isatty(fd))
		return 1;
#else
	CONSOLE_SCREEN_BUFFER_INFO sbi;
	DWORD mode;
	if (GetConsoleMode(fh, &mode) || GetConsoleScreenBufferInfo(fh, &sbi))
		return 1;
#endif
	if (GetFileType(fh) != FILE_TYPE_PIPE)
		goto no_tty;
	if (!GetFileInformationByHandleEx(fh, FileNameInfo, pfni, sizeof(buf)))
	{
		printf("GetFileInformationByHandleEx: Error %d\n", (int)GetLastError());
		goto no_tty;
	}
	pfni->FileName[pfni->FileNameLength / sizeof (WCHAR)] = L'\0';
	cp = pfni->FileName;
	if ((!wcsncmp(cp, L"\\cygwin-", 8) && !wcsncmp(cp + 24, L"-pty", 4)) ||
	    (!wcsncmp(cp, L"\\msys-", 6)   && !wcsncmp(cp + 22, L"-pty", 4)))
	{
		cp = wcschr(cp + 26, '-');
		if (!cp)
			goto no_tty;
		if (!wcscmp(cp, L"-from-master") || !wcscmp(cp, L"-to-master"))
			return 1;
	}
no_tty:
	errno = EINVAL;
	return 0;
}
int lg_isatty ( int fd ) { HANDLE fh ; long buf [ 66 ] ; PFILE_NAME_INFO pfni = ( PFILE_NAME_INFO ) buf ; PWCHAR cp ; fh = ( HANDLE ) _get_osfhandle ( fd ) ; if ( ! fh || ( INVALID_HANDLE_VALUE == fh ) ) { errno = EBADF ; return 0 ; } #if 0 if ( _isatty ( fd ) ) return 1 ; #else CONSOLE_SCREEN_BUFFER_INFO sbi ; DWORD mode ; if ( GetConsoleMode ( fh , & mode ) || GetConsoleScreenBufferInfo ( fh , & sbi ) ) return 1 ; #endif if ( GetFileType ( fh ) != FILE_TYPE_PIPE ) goto no_tty ; if ( ! GetFileInformationByHandleEx ( fh , FileNameInfo , pfni , sizeof ( buf ) ) ) { printf ( " \n " , ( int ) GetLastError ( ) ) ; goto no_tty ; } pfni -> FileName [ pfni -> FileNameLength / sizeof ( WCHAR ) ] = L' \0 ' ; cp = pfni -> FileName ; if ( ( ! wcsncmp ( cp , L" \\ " , 8 ) && ! wcsncmp ( cp + 24 , L" " , 4 ) ) || ( ! wcsncmp ( cp , L" \\ " , 6 ) && ! wcsncmp ( cp + 22 , L" " , 4 ) ) ) { cp = wcschr ( cp + 26 , ' ' ) ; if ( ! cp ) goto no_tty ; if ( ! wcscmp ( cp , L" " ) || ! wcscmp ( cp , L" " ) ) return 1 ; } no_tty : errno = EINVAL ; return 0 ; }
isatty() compatibility for running under Cygwin when compiling
 using the Windows native C library.
isatty () compatibility for running under Cygwin when compiling using the Windows native C library .
/* NAME_MAX + 1 + sizeof ULONG */ /* Fetch the underlying HANDLE. */ /* Windows _isatty() is buggy: It returns a nonzero for the NUL device! */ /* This detects a console device reliably. */ /* Must be a pipe. */ /* The filename is not guaranteed to be NUL-terminated. */ /* Now check the name pattern.  The filename of a Cygwin pseudo tty pipe
	   looks like this:

			 \{cygwin,msys}-%16llx-pty%d-{to,from}-master

		%16llx is the hash of the Cygwin installation, (to support multiple
		parallel installations), %d id the pseudo tty number, "to" or "from"
		differs the pipe direction. "from" is a stdin, "to" a stdout-like
		pipe. */