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

int main() {
  int err;
  int stdin, null, dev;
  char buffer[256];
  char* result;

  stdin = open("/dev/stdin", O_RDONLY);
  null = open("/dev/null", O_RDONLY);
  dev = open("/dev", O_RDONLY);

  result = ctermid(buffer);
  assert(!strcmp(result, "/dev/tty"));

  // strstr instead of strcmp as native code may
  // be using a virtual console (e.g. /dev/tty02)
  err = ttyname_r(stdin, buffer, 256);
  assert(!err);
  assert(strstr(buffer, "/dev/tty"));

  err = ttyname_r(stdin, buffer, 2);
  assert(err == ERANGE);

  result = ttyname(stdin);
  assert(strstr(result, "/dev/tty"));

  result = ttyname(null);
  assert(!result);

  result = ttyname(dev);
  assert(!result);

  puts("success");

  return EXIT_SUCCESS;
}