Blame view

RIOT/cpu/native/vfs/native_vfs.c 2.65 KB
a752c7ab   elopes   add first test an...
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  /*
   * Copyright (C) 2016 Kaspar Schleiser <kaspar@schleiser.de>
   *
   * This file is subject to the terms and conditions of the GNU Lesser General
   * Public License v2.1. See the file LICENSE in the top level directory for
   * more details.
   */
  
  /**
   * @ingroup     native_cpu
   * @{
   *
   * @file
   * @brief       VFS wrappers for POSIX file I/O functions
   *
   * @author      Kaspar Schleiser <kaspar@schleiser.de>
   */
  
  #include <errno.h>
  #include <fcntl.h>
  #include <stdarg.h>
  #include <sys/stat.h>
  #include <unistd.h>
  
  #include "vfs.h"
  
  int open(const char *name, int flags, ...)
  {
      unsigned mode = 0;
  
      if ((flags & O_CREAT)) {
          va_list ap;
          va_start(ap, flags);
          mode = va_arg(ap, unsigned);
          va_end(ap);
      }
  
      int fd = vfs_open(name, flags, mode);
      if (fd < 0) {
          /* vfs returns negative error codes */
          errno = -fd;
          return -1;
      }
      return fd;
  }
  
  ssize_t read(int fd, void *dest, size_t count)
  {
      int res = vfs_read(fd, dest, count);
  
      if (res < 0) {
          /* vfs returns negative error codes */
          errno = -res;
          return -1;
      }
      return res;
  }
  
  ssize_t write(int fd, const void *src, size_t count)
  {
      int res = vfs_write(fd, src, count);
  
      if (res < 0) {
          /* vfs returns negative error codes */
          errno = -res;
          return -1;
      }
      return res;
  }
  
  int close(int fd)
  {
      int res = vfs_close(fd);
  
      if (res < 0) {
          /* vfs returns negative error codes */
          errno = -res;
          return -1;
      }
      return res;
  }
  
  int fcntl(int fd, int cmd, ...)
  {
      unsigned long arg;
      va_list ap;
      va_start(ap, cmd);
      arg = va_arg(ap, unsigned long);
      va_end(ap);
  
      int res = vfs_fcntl(fd, cmd, arg);
  
      if (res < 0) {
          /* vfs returns negative error codes */
          errno = -res;
          return -1;
      }
      return res;
  }
  
  
  off_t lseek(int fd, off_t off, int whence)
  {
      int res = vfs_lseek(fd, off, whence);
  
      if (res < 0) {
          /* vfs returns negative error codes */
          errno = -res;
          return -1;
      }
      return res;
  }
  
  int fstat(int fd, struct stat *buf)
  {
      int res = vfs_fstat(fd, buf);
  
      if (res < 0) {
          /* vfs returns negative error codes */
          errno = -res;
          return -1;
      }
      return 0;
  }
  
  int stat(const char *name, struct stat *st)
  {
      int res = vfs_stat(name, st);
  
      if (res < 0) {
          /* vfs returns negative error codes */
          errno = -res;
          return -1;
      }
      return 0;
  }
  
  int unlink(const char *path)
  {
      int res = vfs_unlink(path);
  
      if (res < 0) {
          /* vfs returns negative error codes */
          errno = -res;
          return -1;
      }
      return 0;
  }
  
  /** @} */