Merge pull request #414 from sesef/datagrid
[mono.git] / mono / wrapper / wrapper.c
1 #include <config.h>
2 #include <limits.h>
3 #include <dirent.h>
4 #include <stdlib.h>
5 #include <errno.h>
6 #include <unistd.h>
7 #include <stdio.h>
8 #include <sys/types.h>
9 #ifdef HAVE_UTIME_H
10 #include <utime.h>
11 #endif
12 #include "wrapper.h"
13
14 extern char **environ;
15
16 gint64
17 mono_wrapper_seek (gpointer fd, gint64 offset, gint32 whence)
18 {
19         off_t code;
20
21         if (offset > INT_MAX || offset < INT_MIN)
22                 return -EINVAL;
23
24         code = lseek ((int)fd, offset, whence);
25         if (code == -1)
26                 return -errno;
27         else
28                 return code;
29 }
30
31 gint32
32 mono_wrapper_read (gpointer fd, void* buf, gint32 count)
33 {
34         int n = read ((int)fd, buf, count);
35
36         if (n == -1)
37                 return -errno;
38         return n;
39 }
40
41 gint32
42 mono_wrapper_write (gpointer fd, void* buf, gint32 count)
43 {
44         int n = write ((int)fd, buf, count);
45
46         if (n == -1)
47                 return -errno;
48         return n;
49 }
50
51 gint32
52 mono_wrapper_fstat (gpointer fd, MonoWrapperStat* buf)
53 {
54         struct stat fs;
55
56         if (fstat ((int)fd, &fs) == -1)
57                 return -errno;
58
59         buf->mst_dev = fs.st_dev;
60         buf->mst_mode = fs.st_mode;
61         buf->mst_nlink = fs.st_nlink;
62         buf->mst_uid = fs.st_uid;
63         buf->mst_gid = fs.st_gid;
64         buf->mst_size = fs.st_size;
65         buf->mst_atime = fs.st_atime;
66         buf->mst_mtime = fs.st_mtime;
67         buf->mst_ctime = fs.st_ctime;
68
69         return 0;
70 }
71
72 gint32
73 mono_wrapper_ftruncate (gpointer fd, gint64 length) 
74 {
75         int code;
76
77         if (length > INT_MAX || length < INT_MIN)
78                 return -1;
79
80         code = ftruncate ((int)fd, length);
81         if (code == -1)
82                 return -errno;
83         return code;
84 }
85
86 gpointer
87 mono_wrapper_open (const char * path, gint32 flags, gint32 mode)
88 {
89         return (gpointer) open (path, flags, mode);
90 }
91
92 gint32
93 mono_wrapper_close (gpointer fd)
94 {
95         return close ((int)fd);
96 }
97
98 gint32
99 mono_wrapper_stat (const char * path, MonoWrapperStat* buf)
100 {
101         struct stat fs;
102
103         if (stat (path, &fs) != 0)
104                 return errno;
105
106         buf->mst_dev = fs.st_dev;
107         buf->mst_mode = fs.st_mode;
108         buf->mst_nlink = fs.st_nlink;
109         buf->mst_uid = fs.st_uid;
110         buf->mst_gid = fs.st_gid;
111         buf->mst_size = fs.st_size;
112         buf->mst_atime = fs.st_atime;
113         buf->mst_mtime = fs.st_mtime;
114         buf->mst_ctime = fs.st_ctime;
115
116         return 0;
117 }
118
119 gint32
120 mono_wrapper_unlink (const char * path)
121 {
122         if (unlink(path) == -1)
123                 return -errno;
124         return 0;
125 }
126
127 gpointer
128 mono_wrapper_opendir (const char * path)
129 {
130         return (gpointer)opendir(path);
131 }
132
133 const char *
134 mono_wrapper_readdir (gpointer dir)
135 {
136         struct dirent* p;
137
138         p = readdir((DIR*)dir);
139
140         return p != NULL ? p->d_name : NULL;
141 }
142
143 gint32
144 mono_wrapper_closedir (gpointer dir)
145 {
146         return closedir((DIR*)dir);
147 }
148
149 gpointer
150 mono_wrapper_getenv (const char * variable)
151 {
152         return (gpointer)getenv(variable);
153 }
154
155 gpointer
156 mono_wrapper_environ ()
157 {
158         return (gpointer)environ;
159 }
160
161 int
162 mono_wrapper_mkdir (const char *path, int mode)
163 {
164 #ifndef HOST_WIN32
165         if (mkdir (path, mode) == -1)
166                 return -errno;
167 #endif
168         return 0;
169 }
170
171 int
172 mono_wrapper_rmdir (const char *path)
173 {
174         if (rmdir (path) == -1)
175                 return -errno;
176         return 0;
177 }
178
179 int
180 mono_wrapper_rename (const char *src, const char *dst)
181 {
182         if (rename (src, dst) == -1)
183                 return -errno;
184         return 0;
185 }
186
187 int
188 mono_wrapper_utime (const char *path, int atime, int mtime)
189 {
190 #ifndef HOST_WIN32
191         struct utimbuf buf;
192
193         buf.actime = atime;
194         buf.modtime = mtime;
195
196         if (utime (path, &buf) == -1)
197                 return -errno;
198 #endif
199         return 0;
200 }
201