File: wasmfs_chdir.c

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 121,872 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,365; asm: 2,415; lisp: 1,869
file content (101 lines) | stat: -rw-r--r-- 2,736 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright 2021 The Emscripten Authors.  All rights reserved.
 * Emscripten is available under two separate licenses, the MIT license and the
 * University of Illinois/NCSA Open Source License.  Both these licenses can be
 * found in the LICENSE file.
 */

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

int main() {
  // Set up test directories.
  assert(mkdir("working", 0777) != -1);
  assert(mkdir("/working/test", 0777) != -1);

  // Try to print the root directory.
  char cwd[100];
  char* ret;
  ret = getcwd(cwd, sizeof(cwd));
  assert(ret == cwd);
  printf("Current working dir: %s\n", cwd);

  // Try to switch to /working.
  chdir("working");
  ret = getcwd(cwd, sizeof(cwd));
  assert(ret == cwd);
  printf("Current working dir: %s\n", cwd);

  // Try to switch back to the root directory.
  chdir("/");
  ret = getcwd(cwd, sizeof(cwd));
  assert(ret == cwd);
  printf("Current working dir: %s\n", cwd);

  // Try to switch to a subdirectory of working.
  chdir("/working/test");
  ret = getcwd(cwd, sizeof(cwd));
  assert(ret == cwd);
  printf("Current working dir: %s\n", cwd);

  // Try to switch to a non-existent relative path from subdirectory.
  // Changing to working in /working/test should fail.
  chdir("working");
  printf("Errno: %s\n", strerror(errno));
  assert(errno == ENOENT);
  ret = getcwd(cwd, sizeof(cwd));
  assert(ret == cwd);
  printf("Current working dir is still: %s\n", cwd);

  // Try to switch back to absolute path /working.
  errno = 0;
  chdir("/working");
  printf("Errno: %s\n", strerror(errno));
  assert(errno == 0);
  ret = getcwd(cwd, sizeof(cwd));
  assert(ret == cwd);
  printf("Current working dir is now: %s\n", cwd);

  // Try to switch to a non-existent absolute path.
  errno = 0;
  chdir("/foobar");
  printf("Errno: %s\n", strerror(errno));
  assert(errno == ENOENT);
  ret = getcwd(cwd, sizeof(cwd));
  assert(ret == cwd);
  printf("Current working dir is still: %s\n", cwd);

  // Try to switch to /dev.
  chdir("/dev");
  ret = getcwd(cwd, sizeof(cwd));
  assert(ret == cwd);
  printf("Current working dir: %s\n", cwd);

  // Try to change cwd to a file.
  chdir("/dev/stdout");
  printf("Errno: %s\n", strerror(errno));
  assert(errno == ENOTDIR);
  ret = getcwd(cwd, sizeof(cwd));
  assert(ret == cwd);
  printf("Current working dir is still: %s\n", cwd);

  // Try to pass a size of 0.
  errno = 0;
  getcwd(cwd, 0);
  printf("Errno: %s\n", strerror(errno));
  assert(errno == EINVAL);

  // Try to write to a buffer of size 1.
  errno = 0;
  char smallBuffer[1];
  getcwd(smallBuffer, 1);
  assert(errno == ERANGE);
  return 0;
}