add some (mostly non-working) data binding tests
[mono-project.git] / support / fcntl.c
blob7914e41dd5777c0719738d4a33d28c964bb2353c
1 /*
2 * <fcntl.h> wrapper functions.
4 * Authors:
5 * Jonathan Pryor (jonpryor@vt.edu)
7 * Copyright (C) 2004, 2006 Jonathan Pryor
8 */
10 #ifndef _GNU_SOURCE
11 #define _GNU_SOURCE
12 #endif
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 gint32
26 Mono_Posix_Syscall_fcntl (gint32 fd, gint32 cmd)
28 if (Mono_Posix_FromFcntlCommand (cmd, &cmd) == -1)
29 return -1;
30 return fcntl (fd, cmd);
33 gint32
34 Mono_Posix_Syscall_fcntl_arg (gint32 fd, gint32 cmd, gint64 arg)
36 long _arg;
37 gint32 _cmd;
39 mph_return_if_long_overflow (arg);
41 #ifdef F_NOTIFY
42 if (cmd == F_NOTIFY) {
43 int _argi;
44 if (Mono_Posix_FromDirectoryNotifyFlags (arg, &_argi) == -1) {
45 return -1;
47 _arg = _argi;
49 else
50 #endif
51 _arg = (long) arg;
53 if (Mono_Posix_FromFcntlCommand (cmd, &_cmd) == -1)
54 return -1;
55 return fcntl (fd, cmd, _arg);
58 gint32
59 Mono_Posix_Syscall_fcntl_lock (gint32 fd, gint32 cmd, struct Mono_Posix_Flock *lock)
61 struct flock _lock;
62 int r;
64 if (lock == NULL) {
65 errno = EFAULT;
66 return -1;
69 mph_return_if_off_t_overflow (lock->l_start);
70 mph_return_if_off_t_overflow (lock->l_len);
72 if (Mono_Posix_FromLockType (lock->l_type, &lock->l_type) == -1)
73 return -1;
74 _lock.l_type = lock->l_type;
75 _lock.l_whence = lock->l_whence;
76 _lock.l_start = lock->l_start;
77 _lock.l_len = lock->l_len;
78 _lock.l_pid = lock->l_pid;
80 r = fcntl (fd, cmd, &_lock);
82 if (Mono_Posix_ToLockType (_lock.l_type, &_lock.l_type) == -1)
83 r = -1;
84 lock->l_type = _lock.l_type;
85 lock->l_whence = _lock.l_whence;
86 lock->l_start = _lock.l_start;
87 lock->l_len = _lock.l_len;
88 lock->l_pid = _lock.l_pid;
90 return r;
93 gint32
94 Mono_Posix_Syscall_open (const char *pathname, gint32 flags)
96 if (Mono_Posix_FromOpenFlags (flags, &flags) == -1)
97 return -1;
99 return open (pathname, flags);
102 gint32
103 Mono_Posix_Syscall_open_mode (const char *pathname, gint32 flags, guint32 mode)
105 if (Mono_Posix_FromOpenFlags (flags, &flags) == -1)
106 return -1;
107 if (Mono_Posix_FromFilePermissions (mode, &mode) == -1)
108 return -1;
110 return open (pathname, flags, mode);
113 gint32
114 Mono_Posix_Syscall_creat (const char *pathname, guint32 mode)
116 if (Mono_Posix_FromFilePermissions (mode, &mode) == -1)
117 return -1;
119 return creat (pathname, mode);
122 #ifdef HAVE_POSIX_FADVISE
123 gint32
124 Mono_Posix_Syscall_posix_fadvise (gint32 fd, mph_off_t offset, mph_off_t len,
125 gint32 advice)
127 mph_return_if_off_t_overflow (offset);
128 mph_return_if_off_t_overflow (len);
130 if (Mono_Posix_FromPosixFadviseAdvice (advice, &advice) == -1)
131 return -1;
133 return posix_fadvise (fd, (off_t) offset, (off_t) len, advice);
135 #endif /* ndef HAVE_POSIX_FADVISE */
137 #ifdef HAVE_POSIX_FALLOCATE
138 gint32
139 Mono_Posix_Syscall_posix_fallocate (gint32 fd, mph_off_t offset, mph_size_t len)
141 mph_return_if_off_t_overflow (offset);
142 mph_return_if_size_t_overflow (len);
144 return posix_fallocate (fd, (off_t) offset, (size_t) len);
146 #endif /* ndef HAVE_POSIX_FALLOCATE */
148 G_END_DECLS
151 * vim: noexpandtab