347b3a920803fb5fb51eea2b2d698e6c1c2b367f
[mono.git] / mono / wrapper / wrapper.c
1 #include <config.h>
2 #include <limits.h>
3 #include <dirent.h>\r
4 #include <stdlib.h>\r
5
6 #include "wrapper.h"
7
8 gint64
9 mono_wrapper_seek (int fd, gint64 offset, gint32 whence)
10 {
11         if (offset > INT_MAX || offset < INT_MIN)
12                 return -1;
13
14         return lseek (fd, offset, whence);
15 }
16
17 gint32
18 mono_wrapper_read (int fd, void* buf, gint32 count)
19 {
20         return read (fd, buf, count);
21 }
22
23 gint32
24 mono_wrapper_write (int fd, void* buf, gint32 count)
25 {
26         return write (fd, buf, count);
27 }
28
29 gint32
30 mono_wrapper_fstat (int fd, MonoWrapperStat* buf)
31 {
32         struct stat fs;
33
34         if (fstat (fd, &fs) != 0)
35                 return -1;
36
37         buf->st_dev = fs.st_dev;
38         buf->st_mode = fs.st_mode;
39         buf->st_nlink = fs.st_nlink;
40         buf->st_uid = fs.st_uid;
41         buf->st_gid = fs.st_gid;
42         buf->st_size = fs.st_size;
43         buf->st_atime = fs.st_atime;
44         buf->st_mtime = fs.st_ctime;
45         buf->st_ctime = fs.st_ctime;
46
47         return 0;
48 }
49
50 gint32
51 mono_wrapper_ftruncate (int fd, gint64 length) 
52 {
53         if (length > INT_MAX || length < INT_MIN)
54                 return -1;
55
56         return ftruncate (fd, length);
57 }
58
59 int
60 mono_wrapper_open (const char * path, gint32 flags, gint32 mode)
61 {
62         return open (path, flags, mode);
63 }
64
65 gint32
66 mono_wrapper_close (int fd)
67 {
68         return close (fd);
69 }
70
71 gint32
72 mono_wrapper_stat (const char * path, MonoWrapperStat* buf)
73 {
74         struct stat fs;
75
76         if (stat (path, &fs) != 0)
77                 return errno;
78
79         buf->st_dev = fs.st_dev;
80         buf->st_mode = fs.st_mode;
81         buf->st_nlink = fs.st_nlink;
82         buf->st_uid = fs.st_uid;
83         buf->st_gid = fs.st_gid;
84         buf->st_size = fs.st_size;
85         buf->st_atime = fs.st_atime;
86         buf->st_mtime = fs.st_ctime;
87         buf->st_ctime = fs.st_ctime;
88
89         return 0;
90 }
91
92 gint32
93 mono_wrapper_unlink (const char * path)
94 {
95         return unlink(path);
96 }
97
98 int
99 mono_wrapper_opendir (const char * path)\r
100 {\r
101         return (int)opendir(path);\r
102 }
103
104 const char *
105 mono_wrapper_readdir (int dir)\r
106 {\r
107         struct dirent* p = readdir((DIR*)dir);\r
108         return p != NULL ? p->d_name : NULL;\r
109 }
110
111 gint32
112 mono_wrapper_closedir (int dir)\r
113 {\r
114         return closedir((DIR*)dir);\r
115 }
116
117 int
118 mono_wrapper_getenv (const char * variable)\r
119 {\r
120         return (int)getenv(variable);\r
121 }
122
123 int
124 mono_wrapper_environ ()\r
125 {\r
126         return (int)environ;\r
127 }\r