GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bin/ksh/tty.c Lines: 15 26 57.7 %
Date: 2016-12-06 Branches: 6 14 42.9 %

Line Branch Exec Source
1
/*	$OpenBSD: tty.c,v 1.16 2015/12/14 13:59:42 tb Exp $	*/
2
3
#include <errno.h>
4
#include <fcntl.h>
5
#include <string.h>
6
#include <unistd.h>
7
8
#include "sh.h"
9
#include "tty.h"
10
11
int		tty_fd = -1;	/* dup'd tty file descriptor */
12
int		tty_devtty;	/* true if tty_fd is from /dev/tty */
13
struct termios	tty_state;	/* saved tty state */
14
15
void
16
tty_close(void)
17
14234
{
18
14234
	if (tty_fd >= 0) {
19
28
		close(tty_fd);
20
28
		tty_fd = -1;
21
	}
22
14234
}
23
24
/* Initialize tty_fd.  Used for saving/resetting tty modes upon
25
 * foreground job completion and for setting up tty process group.
26
 */
27
void
28
tty_init(int init_ttystate)
29
63
{
30
63
	int	do_close = 1;
31
	int	tfd;
32
33
63
	tty_close();
34
63
	tty_devtty = 1;
35
36
63
	tfd = open("/dev/tty", O_RDWR, 0);
37
63
	if (tfd < 0) {
38
		tty_devtty = 0;
39
		warningf(false, "No controlling tty (open /dev/tty: %s)",
40
		    strerror(errno));
41
42
		do_close = 0;
43
		if (isatty(0))
44
			tfd = 0;
45
		else if (isatty(2))
46
			tfd = 2;
47
		else {
48
			warningf(false, "Can't find tty file descriptor");
49
			return;
50
		}
51
	}
52
63
	if ((tty_fd = fcntl(tfd, F_DUPFD_CLOEXEC, FDBASE)) < 0) {
53
		warningf(false, "j_ttyinit: dup of tty fd failed: %s",
54
		    strerror(errno));
55
63
	} else if (init_ttystate)
56
		tcgetattr(tty_fd, &tty_state);
57
63
	if (do_close)
58
63
		close(tfd);
59
}