aboutsummaryrefslogtreecommitdiff
path: root/da.c
blob: 6f1b5114708cb7f7d518f21b46e20ef3cb519987 (plain)
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
/*
 *  da prints the device attributes of the current terminal
 *  to standard output as a string of comma-separated integers.
 *
 *  If run inside GNU screen, it will print the device attributes
 *  of GNU screen itself and not those of the real terminal.
 */

#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>

int
main()
{
	char c;
	int r, ttyfd;
	struct termios term, restore;

        if((ttyfd = open("/dev/tty", O_RDWR))==-1)
                err(1, "open");

        tcgetattr(ttyfd, &restore);
        tcgetattr(ttyfd, &term);
        term.c_lflag &= ~(ICANON|ECHO);
        tcsetattr(ttyfd, TCSANOW, &term);

	write(ttyfd, "\033[>c", 4);
	while(r = read(ttyfd, &c, 1)){
		if(r==-1){
			warn("read");
			goto end;
		}
		if(c=='c') break;
		printf("%d,", c);
	}
	printf("99\n");

end:
        tcsetattr(ttyfd, TCSANOW, &restore);
}