xref: /plan9-contrib/sys/src/ape/lib/ap/plan9/isatty.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 #include "lib.h"
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include "sys9.h"
6 #include "dir.h"
7 
8 int
9 _isatty(int fd)
10 {
11 	int t;
12 	Dir d1, d2;
13 	char cd[DIRLEN];
14 	char buf[5+NAMELEN];
15 
16 	if(_FSTAT(fd, cd) < 0)
17 		return 0;
18 	convM2D(cd, &d1);
19 	if(strncmp(d1.name, "ptty", 4)==0)
20 		return 1;
21 	if(_STAT("/dev/cons", cd) < 0)
22 		return 0;
23 	convM2D(cd, &d2);
24 
25 	/*
26 	 * If we came in through con, /dev/cons is probably #d/0, which
27 	 * won't match stdin.  Opening #d/0 and fstating it gives the
28 	 * values of the underlying channel
29 	 */
30 
31 	if(d2.type == 'd'){
32 		strcpy(buf, "#d/");
33 		strcpy(buf+3, d2.name);
34 		if((t = _OPEN(buf, 0)) < 0)
35 			return 0;
36 		if(_FSTAT(t, cd) < 0){
37 			_CLOSE(t);
38 			return 0;
39 		}
40 		_CLOSE(t);
41 		convM2D(cd, &d2);
42 	}
43 	return (d1.type == d2.type) && (d1.dev == d2.dev);
44 }
45 
46 /* The FD_ISTTY flag is set via _isatty in _fdsetup or open */
47 int
48 isatty(fd)
49 {
50 	if(_fdinfo[fd].flags&FD_ISTTY)
51 		return 1;
52 	else
53 		return 0;
54 }
55