summaryrefslogtreecommitdiff
path: root/regress/lib/libpthread/stdfiles/stdfiles.c
blob: 456032ffe4304f54dc9ded7d7426b3f38270585d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* $snafu: stdfiles.c,v 1.3 2003/02/03 21:22:26 marc Exp $ */
/* PUBLIC DOMAIN Oct 2002 <marc@snafu.org> */

#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

/*
 * test what happens to blocking/non-blocking mode on stdout/stderr when
 * it is changed on stdin.   A comment in the pthreads code implies that
 * all three files are linked.   Check it out.
 */

int
main(int argc, char *argv[])
{
	int dup_stdout;
	int stdin_flags;
	int stdout_flags;
	int dup_flags;
	int stderr_flags;
	int new_flags;
	int new_fd;

	/* dup stdout for the fun of it. */
	dup_stdout = dup(STDOUT_FILENO);

	/* read std in/out/err flags */
	stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
	assert(stdin_flags != -1);
	stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0);
	assert(stdout_flags != -1);
	dup_flags = fcntl(dup_stdout, F_GETFL, 0);
	assert(dup_flags != -1);
	stderr_flags = fcntl(STDERR_FILENO, F_GETFL, 0);
	assert(stderr_flags != -1);
	printf("starting flags: in = %x, out = %x, dup = %x, err = %x\n",
	       stdin_flags, stdout_flags, dup_flags, stderr_flags);

	/* set stdin to non-blocking mode and see if stdout/stderr change */
	new_flags = stdin_flags | O_NONBLOCK;
	printf("forcing stdin to O_NONBLOCK (flags %x)\n", new_flags);
	assert(fcntl(STDIN_FILENO, F_SETFL, new_flags) != -1);

	new_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
	assert(new_flags != -1);
	if (new_flags != stdin_flags) {
		printf("stdin flags changed %x -> %x\n", stdin_flags,
		       new_flags);
		stdin_flags = new_flags;
	}

	new_flags = fcntl(STDOUT_FILENO, F_GETFL, 0);
	assert(new_flags != -1);
	if (new_flags != stdout_flags) {
		printf("stdout flags changed %x -> %x\n", stdout_flags,
		       new_flags);
		stdout_flags = new_flags;
	}

	new_flags = fcntl(dup_stdout, F_GETFL, 0);
	assert(new_flags != -1);
	if (new_flags != dup_flags) {
		printf("dup_stdout flags changed %x -> %x\n", dup_flags,
		       new_flags);
		dup_flags = new_flags;
	}

	new_flags = fcntl(STDERR_FILENO, F_GETFL, 0);
	assert(new_flags != -1);
	if (new_flags != stderr_flags) {
		printf("stderr flags changed %x -> %x\n", stderr_flags,
		       new_flags);
		stderr_flags = new_flags;
	}

	/*
	 * Close stderr and open /dev/tty.   See what it's flags
	 * are.   Set the file to non blocking.
	 */
	printf("close stderr and open /dev/tty\n");
	assert(close(STDERR_FILENO) != -1);
	new_fd = open("/dev/tty", O_RDWR|O_CREAT, 0666);
	assert(new_fd == STDERR_FILENO);
	new_flags = fcntl(STDERR_FILENO, F_GETFL, 0);
	assert(new_flags != -1);
	printf("/dev/tty [STDERR_FILENO] flags are %x\n", new_flags);
	stderr_flags = new_flags | O_NONBLOCK;
	printf("forcing /dev/tty to O_NONBLOCK (flags %x)\n", stderr_flags);
	assert(fcntl(STDERR_FILENO, F_SETFL, stdin_flags) != -1);

	/* now turn off non blocking mode on stdin */
	stdin_flags &= ~O_NONBLOCK;
	printf("turning off O_NONBLOCK on stdin (flags %x)\n", stdin_flags);
	assert(fcntl(STDIN_FILENO, F_SETFL, stdin_flags) != -1);

	new_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
	assert(new_flags != -1);
	assert(new_flags == stdin_flags);

	new_flags = fcntl(STDOUT_FILENO, F_GETFL, 0);
	assert(new_flags != -1);
	if (new_flags != stdout_flags) {
		printf("stdout flags changed %x -> %x\n", stdout_flags,
		       new_flags);
		stdout_flags = new_flags;
	}

	new_flags = fcntl(dup_stdout, F_GETFL, 0);
	assert(new_flags != -1);
	if (new_flags != dup_flags) {
		printf("dup_stdout flags changed %x -> %x\n", dup_flags,
		       new_flags);
		dup_flags = new_flags;
	}

	new_flags = fcntl(STDERR_FILENO, F_GETFL, 0);
	assert(new_flags != -1);
	if (new_flags != stderr_flags) {
		printf("stderr flags changed %x -> %x\n", stderr_flags,
		       new_flags);
		stderr_flags = new_flags;
	}

	return 0;
}