xref: /haiku/src/bin/tty.c (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
1 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
2 //
3 //  Copyright (c) 2001-2003, OpenBeOS
4 //
5 //  This software is part of the OpenBeOS distribution and is covered
6 //  by the OpenBeOS license.
7 //
8 //
9 //  File:        tty.c
10 //  Author:      Daniel Reinhold (danielre@users.sf.net)
11 //  Description: prints the file name of the user's terminal
12 //
13 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
14 
15 #include <OS.h>
16 #include <stdio.h>
17 #include <unistd.h>
18 
19 
20 void
21 usage()
22 {
23 	printf("Usage: tty [-s]\n"
24 	       "Prints the file name for the terminal connected to standard input.\n"
25 	       "  -s   silent mode: no output -- only return an exit status\n");
26 
27 	exit(2);
28 }
29 
30 
31 int
32 main(int argc, char *argv[])
33 {
34 
35 	if (argc > 2)
36 		usage();
37 
38 	else {
39 		bool silent = false;
40 
41 		if (argc == 2) {
42 			if (!strcmp(argv[1], "-s"))
43 				silent = true;
44 			else
45 				usage();
46 		}
47 
48 		if (!silent)
49 			printf("%s\n", ttyname(STDIN_FILENO));
50 	}
51 
52 	return (isatty(STDIN_FILENO) ? 0 : 1);
53 }
54