~sircmpwn/ctools

ref: bb5007143abfc22d947c422bfade941e2a741c8c ctools/src/tty.c -rw-r--r-- 392 bytes
bb500714Drew DeVault link: add conformance test 2 years ago
                                                                                
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
#include <errno.h>
#include <stdio.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
	if (getopt(argc, argv, "") != -1 || optind != argc) {
		fprintf(stderr, "usage: tty\n");
		return 2;
	}

	char *name = ttyname(STDIN_FILENO);
	if (!name) {
		if (errno == ENOTTY) {
			printf("not a tty\n");
			return 1;
		}
		perror("ttyname");
		return 2;
	}

	printf("%s\n", name);
	return 0;
}