2004-12-27 Ben Maurer <bmaurer@ximian.com>
[mono-project.git] / support / sys-stat.c
blob49405e51e1d5c2861fa21607aa798bb3109a089b
1 /*
2 * <sys/stat.h> wrapper functions.
4 * Authors:
5 * Jonathan Pryor (jonpryor@vt.edu)
7 * Copyright (C) 2004 Jonathan Pryor
8 */
10 #ifndef _GNU_SOURCE
11 #define _GNU_SOURCE
12 #endif /* ndef _GNU_SOURCE */
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <errno.h>
20 #include "map.h"
21 #include "mph.h"
23 G_BEGIN_DECLS
25 struct Mono_Posix_Syscall_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 */
38 /* st_atime, st_mtime, and st_ctime are macros (!), so use a slightly
39 * different name to appease CPP */
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 */
46 static int
47 copy_stat (struct Mono_Posix_Syscall_Stat *to, struct stat *from)
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;
66 gint32
67 Mono_Posix_Syscall_stat (const char *file_name, struct Mono_Posix_Syscall_Stat *buf)
69 int r;
70 struct stat _buf;
72 if (buf == NULL) {
73 errno = EFAULT;
74 return -1;
76 r = stat (file_name, &_buf);
77 if (r != -1 && copy_stat (buf, &_buf) == -1)
78 r = -1;
79 return r;
82 gint32
83 Mono_Posix_Syscall_fstat (int filedes, struct Mono_Posix_Syscall_Stat *buf)
85 int r;
86 struct stat _buf;
88 if (buf == NULL) {
89 errno = EFAULT;
90 return -1;
92 r = fstat (filedes, &_buf);
93 if (r != -1 && copy_stat (buf, &_buf) == -1)
94 r = -1;
95 return r;
98 gint32
99 Mono_Posix_Syscall_lstat (const char *file_name, struct Mono_Posix_Syscall_Stat *buf)
101 int r;
102 struct stat _buf;
104 if (buf == NULL) {
105 errno = EFAULT;
106 return -1;
108 r = lstat (file_name, &_buf);
109 if (r != -1 && copy_stat (buf, &_buf) == -1)
110 r = -1;
111 return r;
114 gint32
115 Mono_Posix_Syscall_mknod (const char *pathname, guint32 mode, mph_dev_t dev)
117 if (Mono_Posix_FromFilePermissions (mode, &mode) == -1)
118 return -1;
119 return mknod (pathname, mode, dev);
122 G_END_DECLS
125 * vim: noexpandtab