xref: /haiku/src/system/libroot/posix/unistd/terminal.c (revision 5af32e752606778be5dd7379f319fe43cb3f6b8c)
1 /*
2  * Copyright 2003-2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <stdio.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <termios.h>
11 
12 #include <syscalls.h>
13 
14 
15 /**	isatty - is the given file descriptor bound to a terminal device?
16  *	a simple call to fetch the terminal control attributes suffices
17  *	(only a valid tty device will succeed)
18  */
19 
20 int
isatty(int fd)21 isatty(int fd)
22 {
23 	struct termios termios;
24 
25 	return _kern_ioctl(fd, TCGETA, &termios, sizeof(struct termios)) == B_OK;
26 		// we don't use tcgetattr() here in order to keep errno unchanged
27 }
28 
29 
30 /** returns the name of the controlling terminal */
31 
32 char *
ctermid(char * s)33 ctermid(char *s)
34 {
35 	static char defaultBuffer[L_ctermid];
36 	char *name = ttyname(STDOUT_FILENO);
37 		// we assume that stdout is our controlling terminal...
38 
39 	if (s == NULL)
40 		s = defaultBuffer;
41 
42 	return strcpy(s, name ? name : "");
43 }
44 
45 
46 int
tcsetpgrp(int fd,pid_t pgrpid)47 tcsetpgrp(int fd, pid_t pgrpid)
48 {
49 	return ioctl(fd, TIOCSPGRP, &pgrpid);
50 }
51 
52 
53 pid_t
tcgetpgrp(int fd)54 tcgetpgrp(int fd)
55 {
56 	pid_t foregroundProcess;
57 	int status = ioctl(fd, TIOCGPGRP, &foregroundProcess);
58 	if (status == 0)
59 		return foregroundProcess;
60 
61 	return -1;
62 }
63