aboutsummaryrefslogtreecommitdiff
path: root/tests/fcntl-open/src.c
blob: 52d2e7e43192592f225d92b207c37a44004352b9 (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
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

int main() {
  struct stat s;
  int modes[] = {O_RDONLY, O_WRONLY, O_RDWR};
  char nonexistent_name[] = "/noexist-##";

  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 16; j++) {
      int flags = modes[i];
      if (j & 0x1) flags |= O_CREAT;
      if (j & 0x2) flags |= O_EXCL;
      if (j & 0x4) flags |= O_TRUNC;
      if (j & 0x8) flags |= O_APPEND;

      printf("EXISTING FILE %d,%d\n", i, j);
      printf("success: %d\n", open("/test-file", flags, 0777) != -1);
      printf("errno: %d\n", errno);
      stat("/test-file", &s);
      printf("st_mode: 0%o\n", s.st_mode & 037777777000);
      memset(&s, 0, sizeof s);
      printf("\n");
      errno = 0;

      printf("EXISTING FOLDER %d,%d\n", i, j);
      printf("success: %d\n", open("/test-folder", flags, 0777) != -1);
      printf("errno: %d\n", errno);
      stat("/test-folder", &s);
      printf("st_mode: 0%o\n", s.st_mode & 037777777000);
      memset(&s, 0, sizeof s);
      printf("\n");
      errno = 0;

      nonexistent_name[9] = 'a' + i;
      nonexistent_name[10] = 'a' + j;
      printf("NON-EXISTING %d,%d\n", i, j);
      printf("success: %d\n", open(nonexistent_name, flags, 0777) != -1);
      printf("errno: %d\n", errno);
      stat(nonexistent_name, &s);
      printf("st_mode: 0%o\n", s.st_mode & 037777777000);
      memset(&s, 0, sizeof s);
      printf("\n");
      errno = 0;
    }
  }

  printf("CREAT\n");
  printf("success: %d\n", creat("/creat-me", 0777) != -1);
  printf("errno: %d\n", errno);

  return 0;
}