unistd.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * <unistd.h> wrapper functions.
  3. *
  4. * Authors:
  5. * Jonathan Pryor ([email protected])
  6. *
  7. * Copyright (C) 2004-2006 Jonathan Pryor
  8. */
  9. #ifndef _GNU_SOURCE
  10. #define _GNU_SOURCE
  11. #endif /* ndef _GNU_SOURCE */
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <unistd.h>
  15. #include <fcntl.h>
  16. #include <errno.h>
  17. #include <limits.h>
  18. #include <string.h> /* for swab(3) on Mac OS X */
  19. #include "map.h"
  20. #include "mph.h"
  21. G_BEGIN_DECLS
  22. mph_off_t
  23. Mono_Posix_Syscall_lseek (gint32 fd, mph_off_t offset, gint32 whence)
  24. {
  25. mph_return_if_off_t_overflow (offset);
  26. return lseek (fd, offset, whence);
  27. }
  28. mph_ssize_t
  29. Mono_Posix_Syscall_read (gint32 fd, void *buf, mph_size_t count)
  30. {
  31. mph_return_if_size_t_overflow (count);
  32. return read (fd, buf, (size_t) count);
  33. }
  34. mph_ssize_t
  35. Mono_Posix_Syscall_write (gint32 fd, void *buf, mph_size_t count)
  36. {
  37. mph_return_if_size_t_overflow (count);
  38. return write (fd, buf, (size_t) count);
  39. }
  40. mph_ssize_t
  41. Mono_Posix_Syscall_pread (gint32 fd, void *buf, mph_size_t count, mph_off_t offset)
  42. {
  43. mph_return_if_size_t_overflow (count);
  44. mph_return_if_off_t_overflow (offset);
  45. return pread (fd, buf, (size_t) count, (off_t) offset);
  46. }
  47. mph_ssize_t
  48. Mono_Posix_Syscall_pwrite (gint32 fd, void *buf, mph_size_t count, mph_off_t offset)
  49. {
  50. mph_return_if_size_t_overflow (count);
  51. mph_return_if_off_t_overflow (offset);
  52. return pwrite (fd, buf, (size_t) count, (off_t) offset);
  53. }
  54. gint32
  55. Mono_Posix_Syscall_pipe (gint32 *reading, gint32 *writing)
  56. {
  57. int filedes[2] = {-1, -1};
  58. int r;
  59. if (reading == NULL || writing == NULL) {
  60. errno = EFAULT;
  61. return -1;
  62. }
  63. r = pipe (filedes);
  64. *reading = filedes[0];
  65. *writing = filedes[1];
  66. return r;
  67. }
  68. void*
  69. Mono_Posix_Syscall_getcwd (char *buf, mph_size_t size)
  70. {
  71. mph_return_val_if_size_t_overflow (size, NULL);
  72. return getcwd (buf, (size_t) size);
  73. }
  74. gint64
  75. Mono_Posix_Syscall_fpathconf (int filedes, int name, int defaultError)
  76. {
  77. errno = defaultError;
  78. if (Mono_Posix_FromPathconfName (name, &name) == -1)
  79. return -1;
  80. return fpathconf (filedes, name);
  81. }
  82. gint64
  83. Mono_Posix_Syscall_pathconf (const char *path, int name, int defaultError)
  84. {
  85. errno = defaultError;
  86. if (Mono_Posix_FromPathconfName (name, &name) == -1)
  87. return -1;
  88. return pathconf (path, name);
  89. }
  90. gint64
  91. Mono_Posix_Syscall_sysconf (int name, int defaultError)
  92. {
  93. errno = defaultError;
  94. if (Mono_Posix_FromSysconfName (name, &name) == -1)
  95. return -1;
  96. return sysconf (name);
  97. }
  98. mph_size_t
  99. Mono_Posix_Syscall_confstr (int name, char *buf, mph_size_t len)
  100. {
  101. mph_return_if_size_t_overflow (len);
  102. if (Mono_Posix_FromConfstrName (name, &name) == -1)
  103. return -1;
  104. return confstr (name, buf, (size_t) len);
  105. }
  106. #ifdef HAVE_TTYNAME_R
  107. gint32
  108. Mono_Posix_Syscall_ttyname_r (int fd, char *buf, mph_size_t len)
  109. {
  110. mph_return_if_size_t_overflow (len);
  111. return ttyname_r (fd, buf, (size_t) len);
  112. }
  113. #endif /* ndef HAVE_TTYNAME_R */
  114. gint32
  115. Mono_Posix_Syscall_readlink (const char *path, char *buf, mph_size_t len)
  116. {
  117. int r;
  118. mph_return_if_size_t_overflow (len);
  119. r = readlink (path, buf, (size_t) len);
  120. if (r >= 0 && r < len)
  121. buf [r] = '\0';
  122. return r;
  123. }
  124. gint32
  125. Mono_Posix_Syscall_getlogin_r (char *buf, mph_size_t len)
  126. {
  127. mph_return_if_size_t_overflow (len);
  128. return getlogin_r (buf, (size_t) len);
  129. }
  130. gint32
  131. Mono_Posix_Syscall_gethostname (char *buf, mph_size_t len)
  132. {
  133. mph_return_if_size_t_overflow (len);
  134. return gethostname (buf, (size_t) len);
  135. }
  136. gint32
  137. Mono_Posix_Syscall_sethostname (const char *name, mph_size_t len)
  138. {
  139. mph_return_if_size_t_overflow (len);
  140. return sethostname (name, (size_t) len);
  141. }
  142. gint64
  143. Mono_Posix_Syscall_gethostid (void)
  144. {
  145. return gethostid ();
  146. }
  147. #ifdef HAVE_SETHOSTID
  148. gint32
  149. Mono_Posix_Syscall_sethostid (gint64 hostid)
  150. {
  151. mph_return_if_long_overflow (hostid);
  152. #ifdef MPH_ON_BSD
  153. sethostid ((long) hostid);
  154. return 0;
  155. #else
  156. return sethostid ((long) hostid);
  157. #endif
  158. }
  159. #endif /* def HAVE_SETHOSTID */
  160. #ifdef HAVE_GETDOMAINNAME
  161. gint32
  162. Mono_Posix_Syscall_getdomainname (char *name, mph_size_t len)
  163. {
  164. mph_return_if_size_t_overflow (len);
  165. return getdomainname (name, (size_t) len);
  166. }
  167. #endif /* def HAVE_GETDOMAINNAME */
  168. #ifdef HAVE_SETDOMAINNAME
  169. gint32
  170. Mono_Posix_Syscall_setdomainname (const char *name, mph_size_t len)
  171. {
  172. mph_return_if_size_t_overflow (len);
  173. return setdomainname (name, (size_t) len);
  174. }
  175. #endif /* def HAVE_SETDOMAINNAME */
  176. gint32
  177. Mono_Posix_Syscall_truncate (const char *path, mph_off_t length)
  178. {
  179. mph_return_if_off_t_overflow (length);
  180. return truncate (path, (off_t) length);
  181. }
  182. gint32
  183. Mono_Posix_Syscall_ftruncate (int fd, mph_off_t length)
  184. {
  185. mph_return_if_off_t_overflow (length);
  186. return ftruncate (fd, (off_t) length);
  187. }
  188. gint32
  189. Mono_Posix_Syscall_lockf (int fd, int cmd, mph_off_t len)
  190. {
  191. mph_return_if_off_t_overflow (len);
  192. if (Mono_Posix_FromLockfCommand (cmd, &cmd) == -1)
  193. return -1;
  194. return lockf (fd, cmd, (off_t) len);
  195. }
  196. int
  197. Mono_Posix_Syscall_swab (void *from, void *to, mph_ssize_t n)
  198. {
  199. if (mph_have_long_overflow (n))
  200. return -1;
  201. swab (from, to, (ssize_t) n);
  202. return 0;
  203. }
  204. int
  205. Mono_Posix_Syscall_setusershell (void)
  206. {
  207. setusershell ();
  208. return 0;
  209. }
  210. int
  211. Mono_Posix_Syscall_endusershell (void)
  212. {
  213. endusershell ();
  214. return 0;
  215. }
  216. int
  217. Mono_Posix_Syscall_sync (void)
  218. {
  219. sync ();
  220. return 0;
  221. }
  222. G_END_DECLS
  223. /*
  224. * vim: noexpandtab
  225. */