/
isatty.c
59 lines (52 loc) · 1.64 KB
/
isatty.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/***
*isatty.c - check if file handle refers to a device
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines _isatty() - check if file handle refers to a device
*
*******************************************************************************/
#include <cruntime.h>
#include <msdos.h>
#include <internal.h>
#include <io.h>
/***
*int _isatty(handle) - check if handle is a device
*
*Purpose:
* Checks if the given handle is associated with a character device
* (terminal, console, printer, serial port)
*
*Entry:
* int handle - handle of file to be tested
*
*Exit:
* returns non-0 if handle refers to character device,
* returns 0 otherwise
*
*Exceptions:
*
*******************************************************************************/
int __cdecl _isatty (
int fh
)
{
#if defined (_DEBUG) && !defined (_SYSCRT)
/* make sure we ask debugger only once and cache the answer */
static int knownHandle = -1;
#endif /* defined (_DEBUG) && !defined (_SYSCRT) */
/* see if file handle is valid, otherwise return FALSE */
_CHECK_FH_RETURN(fh, EBADF, 0);
_VALIDATE_RETURN((fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF, 0);
#if defined (_DEBUG) && !defined (_SYSCRT)
if (knownHandle == -1) {
knownHandle = DebuggerKnownHandle();
}
if (knownHandle) {
return TRUE;
}
#endif /* defined (_DEBUG) && !defined (_SYSCRT) */
/* check file handle database to see if device bit set */
return (int)(_osfile(fh) & FDEV);
}