/
wrapper.c
201 lines (165 loc) · 3.12 KB
/
wrapper.c
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <config.h>
#include <limits.h>
#include <dirent.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#ifdef HAVE_UTIME_H
#include <utime.h>
#endif
#include "wrapper.h"
extern char **environ;
gint64
mono_wrapper_seek (gpointer fd, gint64 offset, gint32 whence)
{
off_t code;
if (offset > INT_MAX || offset < INT_MIN)
return -EINVAL;
code = lseek ((int)fd, offset, whence);
if (code == -1)
return -errno;
else
return code;
}
gint32
mono_wrapper_read (gpointer fd, void* buf, gint32 count)
{
int n = read ((int)fd, buf, count);
if (n == -1)
return -errno;
return n;
}
gint32
mono_wrapper_write (gpointer fd, void* buf, gint32 count)
{
int n = write ((int)fd, buf, count);
if (n == -1)
return -errno;
return n;
}
gint32
mono_wrapper_fstat (gpointer fd, MonoWrapperStat* buf)
{
struct stat fs;
if (fstat ((int)fd, &fs) == -1)
return -errno;
buf->mst_dev = fs.st_dev;
buf->mst_mode = fs.st_mode;
buf->mst_nlink = fs.st_nlink;
buf->mst_uid = fs.st_uid;
buf->mst_gid = fs.st_gid;
buf->mst_size = fs.st_size;
buf->mst_atime = fs.st_atime;
buf->mst_mtime = fs.st_mtime;
buf->mst_ctime = fs.st_ctime;
return 0;
}
gint32
mono_wrapper_ftruncate (gpointer fd, gint64 length)
{
int code;
if (length > INT_MAX || length < INT_MIN)
return -1;
code = ftruncate ((int)fd, length);
if (code == -1)
return -errno;
return code;
}
gpointer
mono_wrapper_open (const char * path, gint32 flags, gint32 mode)
{
return (gpointer) open (path, flags, mode);
}
gint32
mono_wrapper_close (gpointer fd)
{
return close ((int)fd);
}
gint32
mono_wrapper_stat (const char * path, MonoWrapperStat* buf)
{
struct stat fs;
if (stat (path, &fs) != 0)
return errno;
buf->mst_dev = fs.st_dev;
buf->mst_mode = fs.st_mode;
buf->mst_nlink = fs.st_nlink;
buf->mst_uid = fs.st_uid;
buf->mst_gid = fs.st_gid;
buf->mst_size = fs.st_size;
buf->mst_atime = fs.st_atime;
buf->mst_mtime = fs.st_mtime;
buf->mst_ctime = fs.st_ctime;
return 0;
}
gint32
mono_wrapper_unlink (const char * path)
{
if (unlink(path) == -1)
return -errno;
return 0;
}
gpointer
mono_wrapper_opendir (const char * path)
{
return (gpointer)opendir(path);
}
const char *
mono_wrapper_readdir (gpointer dir)
{
struct dirent* p;
p = readdir((DIR*)dir);
return p != NULL ? p->d_name : NULL;
}
gint32
mono_wrapper_closedir (gpointer dir)
{
return closedir((DIR*)dir);
}
gpointer
mono_wrapper_getenv (const char * variable)
{
return (gpointer)getenv(variable);
}
gpointer
mono_wrapper_environ ()
{
return (gpointer)environ;
}
int
mono_wrapper_mkdir (const char *path, int mode)
{
#ifndef PLATFORM_WIN32
if (mkdir (path, mode) == -1)
return -errno;
#endif
return 0;
}
int
mono_wrapper_rmdir (const char *path)
{
if (rmdir (path) == -1)
return -errno;
return 0;
}
int
mono_wrapper_rename (const char *src, const char *dst)
{
if (rename (src, dst) == -1)
return -errno;
return 0;
}
int
mono_wrapper_utime (const char *path, int atime, int mtime)
{
#ifndef PLATFORM_WIN32
struct utimbuf buf;
buf.actime = atime;
buf.modtime = mtime;
if (utime (path, &buf) == -1)
return -errno;
#endif
return 0;
}