* Makefile (MCS) [PROFILE=default]: Force testing of 'mcs'.
[mono.git] / mono / wrapper / wrapper.c
index 77ec2f50cfcf9a7941f562f2843cd3b87667e321..6ad29668e0407ebf14003cec9e46f62bd709e9ba 100644 (file)
@@ -2,7 +2,13 @@
 #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;
@@ -10,22 +16,36 @@ extern char **environ;
 gint64
 mono_wrapper_seek (gpointer fd, gint64 offset, gint32 whence)
 {
+       off_t code;
+
        if (offset > INT_MAX || offset < INT_MIN)
-               return -1;
+               return -EINVAL;
 
-       return lseek ((int)fd, offset, whence);
+       code = lseek ((int)fd, offset, whence);
+       if (code == -1)
+               return -errno;
+       else
+               return code;
 }
 
 gint32
 mono_wrapper_read (gpointer fd, void* buf, gint32 count)
 {
-       return read ((int)fd, buf, 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)
 {
-       return write ((int)fd, buf, count);
+       int n = write ((int)fd, buf, count);
+
+       if (n == -1)
+               return -errno;
+       return n;
 }
 
 gint32
@@ -33,18 +53,18 @@ mono_wrapper_fstat (gpointer fd, MonoWrapperStat* buf)
 {
        struct stat fs;
 
-       if (fstat ((int)fd, &fs) != 0)
-               return -1;
+       if (fstat ((int)fd, &fs) == -1)
+               return -errno;
 
-       buf->st_dev = fs.st_dev;
-       buf->st_mode = fs.st_mode;
-       buf->st_nlink = fs.st_nlink;
-       buf->st_uid = fs.st_uid;
-       buf->st_gid = fs.st_gid;
-       buf->st_size = fs.st_size;
-       buf->st_atime = fs.st_atime;
-       buf->st_mtime = fs.st_ctime;
-       buf->st_ctime = fs.st_ctime;
+       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;
 }
@@ -52,10 +72,15 @@ mono_wrapper_fstat (gpointer fd, MonoWrapperStat* buf)
 gint32
 mono_wrapper_ftruncate (gpointer fd, gint64 length) 
 {
+       int code;
+
        if (length > INT_MAX || length < INT_MIN)
                return -1;
 
-       return ftruncate ((int)fd, length);
+       code = ftruncate ((int)fd, length);
+       if (code == -1)
+               return -errno;
+       return code;
 }
 
 gpointer
@@ -78,15 +103,15 @@ mono_wrapper_stat (const char * path, MonoWrapperStat* buf)
        if (stat (path, &fs) != 0)
                return errno;
 
-       buf->st_dev = fs.st_dev;
-       buf->st_mode = fs.st_mode;
-       buf->st_nlink = fs.st_nlink;
-       buf->st_uid = fs.st_uid;
-       buf->st_gid = fs.st_gid;
-       buf->st_size = fs.st_size;
-       buf->st_atime = fs.st_atime;
-       buf->st_mtime = fs.st_ctime;
-       buf->st_ctime = fs.st_ctime;
+       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;
 }
@@ -94,7 +119,9 @@ mono_wrapper_stat (const char * path, MonoWrapperStat* buf)
 gint32
 mono_wrapper_unlink (const char * path)
 {
-       return unlink(path);
+       if (unlink(path) == -1)
+               return -errno;
+       return 0;
 }
 
 gpointer
@@ -106,7 +133,10 @@ mono_wrapper_opendir (const char * path)
 const char *
 mono_wrapper_readdir (gpointer dir)
 {
-       struct dirent* p = readdir((DIR*)dir);
+       struct dirent* p;
+
+       p = readdir((DIR*)dir);
+
        return p != NULL ? p->d_name : NULL;
 }
 
@@ -131,19 +161,41 @@ mono_wrapper_environ ()
 int
 mono_wrapper_mkdir (const char *path, int mode)
 {
-       return mkdir (path, mode);
+#ifndef PLATFORM_WIN32
+       if (mkdir (path, mode) == -1)
+               return -errno;
+#endif
+       return 0;
 }
 
 int
 mono_wrapper_rmdir (const char *path)
 {
-       return rmdir (path);
+       if (rmdir (path) == -1)
+               return -errno;
+       return 0;
 }
 
 int
 mono_wrapper_rename (const char *src, const char *dst)
 {
-       return rename (src, 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;
+}