File: wasmfs_open_append.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 (85 lines) | stat: -rw-r--r-- 1,639 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
/*
 * Copyright 2022 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 <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

const char* file = "/foo.txt";

off_t getPos(int fd) {
  errno = 0;
  off_t pos = lseek(fd, 0, SEEK_CUR);
  assert(pos != (off_t)-1);
  assert(errno == 0);
  return pos;
}

void setPos(int fd, off_t pos) {
  errno = 0;
  pos = lseek(fd, pos, SEEK_SET);
  assert(pos != (off_t)-1);
  assert(errno == 0);
}

int main() {
  int fd = open(file, O_RDWR | O_CREAT | O_EXCL | O_APPEND, 0777);
  assert(fd > 0);

  off_t pos = getPos(fd);
  assert(pos == 0);

  ssize_t nwritten = write(fd, "hello", 5);
  assert(nwritten == 5);

  pos = getPos(fd);
  assert(pos == 5);

  setPos(fd, 0);
  pos = getPos(fd);
  assert(pos == 0);

  nwritten = write(fd, "", 0);
  assert(nwritten == 0);

  pos = getPos(fd);
  assert(pos == 5);

  setPos(fd, 0);
  pos = getPos(fd);
  assert(pos == 0);

  nwritten = write(fd, ", world!", 8);
  assert(nwritten == 8);

  pos = getPos(fd);
  assert(pos == 13);

  setPos(fd, 42);
  pos = getPos(fd);
  assert(pos == 42);

  nwritten = write(fd, "!!", 2);
  assert(nwritten == 2);

  pos = getPos(fd);
  assert(pos == 15);

  setPos(fd, 0);

  char buf[100] = {};
  ssize_t nread = read(fd, buf, 100);
  assert(nread == 15);
  assert(strcmp(buf, "hello, world!!!") == 0);

  printf("ok\n");
}