aboutsummaryrefslogtreecommitdiff
path: root/tests/termios/test_tcgetattr.c
blob: 2b3780ee5292915a08fc8c6a01691615765226e9 (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
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

static void create_file(const char *path, const char *buffer, int mode) {
  int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
  assert(fd >= 0);

  int err = write(fd, buffer, sizeof(char) * strlen(buffer));
  assert(err ==  (sizeof(char) * strlen(buffer)));

  close(fd);
}

void setup() {
	create_file("test.txt", "abcdefg", 0666);
}

void cleanup() {
	unlink("test.txt");
}

void test() {
	struct termios tc;
	int ret;
	int fd;

	fd = open("test.txt", O_RDONLY);

	ret = tcgetattr(fd, &tc);
	assert(ret == -1);
	assert(errno = ENOTTY);

	ret = tcgetattr(STDIN_FILENO, &tc);
	assert(!ret);

	ret = tcsetattr(fd, 0, &tc);
	assert(ret == -1);
	assert(errno = ENOTTY);

	ret = tcsetattr(STDIN_FILENO, 0, &tc);
	assert(!ret);

	close(fd);

	puts("success");
}

int main() {
  atexit(cleanup);
  signal(SIGABRT, cleanup);
  setup();
  test();
  return EXIT_SUCCESS;
}