headers/bsd: Add sys/queue.h.
[haiku.git] / src / system / libroot / posix / unistd / terminal.c
blob56a175054b07b01b5b3c1b8b2e2fc450fc67f4b6
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 */
7 #include <stdio.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <termios.h>
12 #include <syscalls.h>
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)
20 int
21 isatty(int fd)
23 struct termios termios;
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
30 /** returns the name of the controlling terminal */
32 char *
33 ctermid(char *s)
35 static char defaultBuffer[L_ctermid];
36 char *name = ttyname(STDOUT_FILENO);
37 // we assume that stdout is our controlling terminal...
39 if (s == NULL)
40 s = defaultBuffer;
42 return strcpy(s, name ? name : "");
46 int
47 tcsetpgrp(int fd, pid_t pgrpid)
49 return ioctl(fd, TIOCSPGRP, &pgrpid);
53 pid_t
54 tcgetpgrp(int fd)
56 pid_t foregroundProcess;
57 int status = ioctl(fd, TIOCGPGRP, &foregroundProcess);
58 if (status == 0)
59 return foregroundProcess;
61 return -1;