New test.
[mono.git] / support / sys-stat.c
1 /*
2  * <sys/stat.h> wrapper functions.
3  *
4  * Authors:
5  *   Jonathan Pryor (jonpryor@vt.edu)
6  *
7  * Copyright (C) 2004 Jonathan Pryor
8  */
9
10 #ifndef _GNU_SOURCE
11 #define _GNU_SOURCE
12 #endif /* ndef _GNU_SOURCE */
13
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <errno.h>
19
20 #include "map.h"
21 #include "mph.h"
22
23 G_BEGIN_DECLS
24
25 struct Mono_Posix_Stat {
26         /* dev_t */     mph_dev_t     st_dev;     /* device */
27         /* ino_t */     mph_ino_t     st_ino;     /* inode */
28         /* mode_t */    guint32       st_mode;    /* protection */
29                         guint32       _padding_;  /* structure padding */
30         /* nlink_t */   mph_nlink_t   st_nlink;   /* number of hard links */
31         /* uid_t */     mph_uid_t     st_uid;     /* user ID of owner */
32         /* gid_t */     mph_gid_t     st_gid;     /* group ID of owner */
33         /* dev_t */     mph_dev_t     st_rdev;    /* device type (if inode device) */
34         /* off_t */     mph_off_t     st_size;    /* total size, in bytes */
35         /* blksize_t */ mph_blksize_t st_blksize; /* blocksize for filesystem I/O */
36         /* blkcnt_t */  mph_blkcnt_t  st_blocks;  /* number of blocks allocated */
37
38         /* st_atime, st_mtime, and st_ctime are macros (!), so use a slightly
39          * different name to appease CPP */
40
41         /* time_t */    mph_time_t    st_atime_;  /* time of last access */
42         /* time_t */    mph_time_t    st_mtime_;  /* time of last modification */
43         /* time_t */    mph_time_t    st_ctime_;  /* time of last status change */
44 };
45
46 static int
47 copy_stat (struct Mono_Posix_Stat *to, struct stat *from)
48 {
49         if (Mono_Posix_ToFilePermissions (from->st_mode, &to->st_mode) == -1)
50                 return -1;
51         to->st_dev      = from->st_dev;
52         to->st_ino      = from->st_ino;
53         to->st_nlink    = from->st_nlink;
54         to->st_uid      = from->st_uid;
55         to->st_gid      = from->st_gid;
56         to->st_rdev     = from->st_rdev;
57         to->st_size     = from->st_size;
58         to->st_blksize  = from->st_blksize;
59         to->st_blocks   = from->st_blocks;
60         to->st_atime_   = from->st_atime;
61         to->st_mtime_   = from->st_mtime;
62         to->st_ctime_   = from->st_ctime;
63         return 0;
64 }
65
66 gint32
67 Mono_Posix_Syscall_stat (const char *file_name, struct Mono_Posix_Stat *buf)
68 {
69         int r;
70         struct stat _buf;
71
72         if (buf == NULL) {
73                 errno = EFAULT;
74                 return -1;
75         }
76         r = stat (file_name, &_buf);
77         if (r != -1 && copy_stat (buf, &_buf) == -1)
78                 r = -1;
79         return r;
80 }
81
82 gint32
83 Mono_Posix_Syscall_fstat (int filedes, struct Mono_Posix_Stat *buf)
84 {
85         int r;
86         struct stat _buf;
87
88         if (buf == NULL) {
89                 errno = EFAULT;
90                 return -1;
91         }
92         r = fstat (filedes, &_buf);
93         if (r != -1 && copy_stat (buf, &_buf) == -1)
94                 r = -1;
95         return r;
96 }
97
98 gint32
99 Mono_Posix_Syscall_lstat (const char *file_name, struct Mono_Posix_Stat *buf)
100 {
101         int r;
102         struct stat _buf;
103
104         if (buf == NULL) {
105                 errno = EFAULT;
106                 return -1;
107         }
108         r = lstat (file_name, &_buf);
109         if (r != -1 && copy_stat (buf, &_buf) == -1)
110                 r = -1;
111         return r;
112 }
113
114 gint32
115 Mono_Posix_Syscall_mknod (const char *pathname, guint32 mode, mph_dev_t dev)
116 {
117         if (Mono_Posix_FromFilePermissions (mode, &mode) == -1)
118                 return -1;
119         return mknod (pathname, mode, dev);
120 }
121
122 G_END_DECLS
123
124 /*
125  * vim: noexpandtab
126  */