Project

General

Profile

Bug #3252 ยป tcerror.c

test code - tonyc, 10/25/2020 09:35 PM

 
1
#include <termios.h>
2
#include <stdio.h>
3
#include <fcntl.h>
4
#include <stdlib.h>
5
#include <errno.h>
6

    
7
int main(void) {
8
  struct termios ti;
9
  int nullfd = open("/dev/null", O_RDWR);
10
  int termfd = fileno(stdin); /* stdin: intended to be run in a terminal */
11
  int err;
12

    
13
  if (nullfd < 0) {
14
    perror("NA: null open failed");
15
    exit(1);
16
  }
17

    
18
  errno = 0;
19
  if (tcgetattr(nullfd, &ti) < 0) {
20
    err = errno;
21
    printf("OK: tcgetattr on /dev/null failed as expected\n");
22
    if (err != ENOTTY) {
23
      perror("FAIL: tcgetattr on /dev/null set errno incorrectly");
24
    }
25
  }
26
  else {
27
    printf("FAIL: tcgetattr on /dev/null succeeded\n");
28
  }
29

    
30
  errno = 0;
31
  if (tcgetattr(termfd, &ti) < 0) {
32
    perror("NA: tcgetattr failed");
33
    exit(1);
34
  }
35

    
36
  errno = 0;
37
  if (tcsetattr(nullfd, TCSADRAIN, &ti) == 0) {
38
    perror("FAIL: tcsetattr succeeded unexpectedly");
39
    exit(1);
40
  }
41
  err = errno;
42
  printf("OK: tcsetattr failed as expected\n");
43
  if (err != ENOTTY) {
44
    perror("FAIL: tcsetattr failure set errno incorrectly");
45
    exit(1);
46
  }
47

    
48
  return 0;
49
}
    (1-1/1)