xref: /haiku/src/bin/tty.c (revision 83b1a68c52ba3e0e8796282759f694b7fdddf06d)
1 /*
2  * Copyright 2001-2013 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *				Daniel Reinhold, danielre@users.sf.net
7  */
8 
9 
10 #include <stdio.h>
11 #include <unistd.h>
12 
13 #include <OS.h>
14 
15 
16 void
17 usage()
18 {
19 	printf("Usage: tty [-s]\n"
20 	       "Prints the file name for the terminal connected to standard input.\n"
21 	       "  -s   silent mode: no output -- only return an exit status\n");
22 
23 	exit(2);
24 }
25 
26 
27 int
28 main(int argc, char *argv[])
29 {
30 
31 	if (argc > 2)
32 		usage();
33 
34 	else {
35 		bool silent = false;
36 
37 		if (argc == 2) {
38 			if (!strcmp(argv[1], "-s"))
39 				silent = true;
40 			else
41 				usage();
42 		}
43 
44 		if (!silent)
45 			printf("%s\n", ttyname(STDIN_FILENO));
46 	}
47 
48 	return (isatty(STDIN_FILENO) ? 0 : 1);
49 }
50