* Syscall.cs: Fix the sys_kill and sys_crypt declarations.
[mono.git] / mcs / class / Mono.Posix / Mono.Unix / Syscall.cs
1 //
2 // Mono.Unix/Syscall.cs
3 //
4 // Authors:
5 //   Miguel de Icaza (miguel@novell.com)
6 //   Jonathan Pryor (jonpryor@vt.edu)
7 //
8 // (C) 2003 Novell, Inc.
9 // (C) 2004 Jonathan Pryor
10 //
11 // This file implements the low-level syscall interface to the POSIX
12 // subsystem.
13 //
14 // This file tries to stay close to the low-level API as much as possible
15 // using enumerations, structures and in a few cases, using existing .NET
16 // data types.
17 //
18 // Implementation notes:
19 //
20 //    Since the values for the various constants on the API changes
21 //    from system to system (even Linux on different architectures will
22 //    have different values), we define our own set of values, and we
23 //    use a set of C helper routines to map from the constants we define
24 //    to the values of the native OS.
25 //
26 //    Bitfields are flagged with the [Map] attribute, and a helper program
27 //    generates a set of routines that we can call to convert from our value 
28 //    definitions to the value definitions expected by the OS; see
29 //    UnixConvert for the conversion routines.
30 //
31 //    Methods that require tuning are bound as `private sys_NAME' methods
32 //    and then a `NAME' method is exposed.
33 //
34
35 //
36 // Permission is hereby granted, free of charge, to any person obtaining
37 // a copy of this software and associated documentation files (the
38 // "Software"), to deal in the Software without restriction, including
39 // without limitation the rights to use, copy, modify, merge, publish,
40 // distribute, sublicense, and/or sell copies of the Software, and to
41 // permit persons to whom the Software is furnished to do so, subject to
42 // the following conditions:
43 // 
44 // The above copyright notice and this permission notice shall be
45 // included in all copies or substantial portions of the Software.
46 // 
47 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
48 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
49 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
50 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
51 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
52 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
53 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
54 //
55
56 using System;
57 using System.Collections;
58 using System.Runtime.InteropServices;
59 using System.Text;
60 using Mono.Unix;
61
62 [assembly:Mono.Unix.IncludeAttribute (
63         new string [] {"sys/types.h", "sys/stat.h", "sys/poll.h", "sys/wait.h",
64                 "unistd.h", "fcntl.h", "signal.h", "poll.h", "grp.h", "errno.h"}, 
65         new string [] {"_GNU_SOURCE", "_XOPEN_SOURCE"})]
66
67 namespace Mono.Unix {
68
69         [Map]
70         public enum Error : int {
71                 // errors & their values liberally copied from
72                 // FC2 /usr/include/asm/errno.h
73                 
74                 EPERM           =   1, // Operation not permitted 
75                 ENOENT          =   2, // No such file or directory 
76                 ESRCH           =   3, // No such process 
77                 EINTR           =   4, // Interrupted system call 
78                 EIO             =   5, // I/O error 
79                 ENXIO           =   6, // No such device or address 
80                 E2BIG           =   7, // Arg list too long 
81                 ENOEXEC         =   8, // Exec format error 
82                 EBADF           =   9, // Bad file number 
83                 ECHILD          =  10, // No child processes 
84                 EAGAIN          =  11, // Try again 
85                 ENOMEM          =  12, // Out of memory 
86                 EACCES          =  13, // Permission denied 
87                 EFAULT          =  14, // Bad address 
88                 ENOTBLK         =  15, // Block device required 
89                 EBUSY           =  16, // Device or resource busy 
90                 EEXIST          =  17, // File exists 
91                 EXDEV           =  18, // Cross-device link 
92                 ENODEV          =  19, // No such device 
93                 ENOTDIR         =  20, // Not a directory 
94                 EISDIR          =  21, // Is a directory 
95                 EINVAL          =  22, // Invalid argument 
96                 ENFILE          =  23, // File table overflow 
97                 EMFILE          =  24, // Too many open files 
98                 ENOTTY          =  25, // Not a typewriter 
99                 ETXTBSY         =  26, // Text file busy 
100                 EFBIG           =  27, // File too large 
101                 ENOSPC          =  28, // No space left on device 
102                 ESPIPE          =  29, // Illegal seek 
103                 EROFS           =  30, // Read-only file system 
104                 EMLINK          =  31, // Too many links 
105                 EPIPE           =  32, // Broken pipe 
106                 EDOM            =  33, // Math argument out of domain of func 
107                 ERANGE          =  34, // Math result not representable 
108                 EDEADLK         =  35, // Resource deadlock would occur 
109                 ENAMETOOLONG    =  36, // File name too long 
110                 ENOLCK          =  37, // No record locks available 
111                 ENOSYS          =  38, // Function not implemented 
112                 ENOTEMPTY       =  39, // Directory not empty 
113                 ELOOP           =  40, // Too many symbolic links encountered 
114                 EWOULDBLOCK     =  EAGAIN, // Operation would block 
115                 ENOMSG          =  42, // No message of desired type 
116                 EIDRM           =  43, // Identifier removed 
117                 ECHRNG          =  44, // Channel number out of range 
118                 EL2NSYNC        =  45, // Level 2 not synchronized 
119                 EL3HLT          =  46, // Level 3 halted 
120                 EL3RST          =  47, // Level 3 reset 
121                 ELNRNG          =  48, // Link number out of range 
122                 EUNATCH         =  49, // Protocol driver not attached 
123                 ENOCSI          =  50, // No CSI structure available 
124                 EL2HLT          =  51, // Level 2 halted 
125                 EBADE           =  52, // Invalid exchange 
126                 EBADR           =  53, // Invalid request descriptor 
127                 EXFULL          =  54, // Exchange full 
128                 ENOANO          =  55, // No anode 
129                 EBADRQC         =  56, // Invalid request code 
130                 EBADSLT         =  57, // Invalid slot 
131                       
132                 EDEADLOCK             =  EDEADLK,
133                       
134                 EBFONT          =  59, // Bad font file format 
135                 ENOSTR          =  60, // Device not a stream 
136                 ENODATA         =  61, // No data available 
137                 ETIME           =  62, // Timer expired 
138                 ENOSR           =  63, // Out of streams resources 
139                 ENONET          =  64, // Machine is not on the network 
140                 ENOPKG          =  65, // Package not installed 
141                 EREMOTE         =  66, // Object is remote 
142                 ENOLINK         =  67, // Link has been severed 
143                 EADV            =  68, // Advertise error 
144                 ESRMNT          =  69, // Srmount error 
145                 ECOMM           =  70, // Communication error on send 
146                 EPROTO          =  71, // Protocol error 
147                 EMULTIHOP       =  72, // Multihop attempted 
148                 EDOTDOT         =  73, // RFS specific error 
149                 EBADMSG         =  74, // Not a data message 
150                 EOVERFLOW       =  75, // Value too large for defined data type 
151                 ENOTUNIQ        =  76, // Name not unique on network 
152                 EBADFD          =  77, // File descriptor in bad state 
153                 EREMCHG         =  78, // Remote address changed 
154                 ELIBACC         =  79, // Can not access a needed shared library 
155                 ELIBBAD         =  80, // Accessing a corrupted shared library 
156                 ELIBSCN         =  81, // .lib section in a.out corrupted 
157                 ELIBMAX         =  82, // Attempting to link in too many shared libraries 
158                 ELIBEXEC        =  83, // Cannot exec a shared library directly 
159                 EILSEQ          =  84, // Illegal byte sequence 
160                 ERESTART        =  85, // Interrupted system call should be restarted 
161                 ESTRPIPE        =  86, // Streams pipe error 
162                 EUSERS          =  87, // Too many users 
163                 ENOTSOCK        =  88, // Socket operation on non-socket 
164                 EDESTADDRREQ    =  89, // Destination address required 
165                 EMSGSIZE        =  90, // Message too long 
166                 EPROTOTYPE      =  91, // Protocol wrong type for socket 
167                 ENOPROTOOPT     =  92, // Protocol not available 
168                 EPROTONOSUPPORT =  93, // Protocol not supported 
169                 ESOCKTNOSUPPORT =  94, // Socket type not supported 
170                 EOPNOTSUPP      =  95, // Operation not supported on transport endpoint 
171                 EPFNOSUPPORT    =  96, // Protocol family not supported 
172                 EAFNOSUPPORT    =  97, // Address family not supported by protocol 
173                 EADDRINUSE      =  98, // Address already in use 
174                 EADDRNOTAVAIL   =  99, // Cannot assign requested address 
175                 ENETDOWN        = 100, // Network is down 
176                 ENETUNREACH     = 101, // Network is unreachable 
177                 ENETRESET       = 102, // Network dropped connection because of reset 
178                 ECONNABORTED    = 103, // Software caused connection abort 
179                 ECONNRESET      = 104, // Connection reset by peer 
180                 ENOBUFS         = 105, // No buffer space available 
181                 EISCONN         = 106, // Transport endpoint is already connected 
182                 ENOTCONN        = 107, // Transport endpoint is not connected 
183                 ESHUTDOWN       = 108, // Cannot send after transport endpoint shutdown 
184                 ETOOMANYREFS    = 109, // Too many references: cannot splice 
185                 ETIMEDOUT       = 110, // Connection timed out 
186                 ECONNREFUSED    = 111, // Connection refused 
187                 EHOSTDOWN       = 112, // Host is down 
188                 EHOSTUNREACH    = 113, // No route to host 
189                 EALREADY        = 114, // Operation already in progress 
190                 EINPROGRESS     = 115, // Operation now in progress 
191                 ESTALE          = 116, // Stale NFS file handle 
192                 EUCLEAN         = 117, // Structure needs cleaning 
193                 ENOTNAM         = 118, // Not a XENIX named type file 
194                 ENAVAIL         = 119, // No XENIX semaphores available 
195                 EISNAM          = 120, // Is a named type file 
196                 EREMOTEIO       = 121, // Remote I/O error 
197                 EDQUOT          = 122, // Quota exceeded 
198
199                 ENOMEDIUM       = 123, // No medium found 
200                 EMEDIUMTYPE     = 124, // Wrong medium type 
201         }
202
203         [Map][Flags]
204         public enum OpenFlags : int {
205                 //
206                 // One of these
207                 //
208                 O_RDONLY    = 0,
209                 O_WRONLY    = 1,
210                 O_RDWR      = 2,
211
212                 //
213                 // Or-ed with zero or more of these
214                 //
215                 O_CREAT     = 4,
216                 O_EXCL      = 8,
217                 O_NOCTTY    = 16,
218                 O_TRUNC     = 32,
219                 O_APPEND    = 64,
220                 O_NONBLOCK  = 128,
221                 O_SYNC      = 256,
222
223                 //
224                 // These are non-Posix.  Using them will result in errors/exceptions on
225                 // non-supported platforms.
226                 //
227                 // (For example, "C-wrapped" system calls -- calls with implementation in
228                 // MonoPosixHelper -- will return -1 with errno=EINVAL.  C#-wrapped system
229                 // calls will generate an exception in UnixConvert, as the value can't be
230                 // converted on the target platform.)
231                 //
232                 
233                 O_NOFOLLOW  = 512,
234                 O_DIRECTORY = 1024,
235                 O_DIRECT    = 2048,
236                 O_ASYNC     = 4096,
237                 O_LARGEFILE = 8192
238         }
239         
240         // mode_t
241         [Flags][Map]
242         public enum FilePermissions : uint {
243                 S_ISUID     = 0x0800, // Set user ID on execution
244                 S_ISGID     = 0x0400, // Set gorup ID on execution
245                 S_ISVTX     = 0x0200, // Save swapped text after use (sticky).
246                 S_IRUSR     = 0x0100, // Read by owner
247                 S_IWUSR     = 0x0080, // Write by owner
248                 S_IXUSR     = 0x0040, // Execute by owner
249                 S_IRGRP     = 0x0020, // Read by group
250                 S_IWGRP     = 0x0010, // Write by group
251                 S_IXGRP     = 0x0008, // Execute by group
252                 S_IROTH     = 0x0004, // Read by other
253                 S_IWOTH     = 0x0002, // Write by other
254                 S_IXOTH     = 0x0001, // Execute by other
255
256                 S_IRWXG     = (S_IRGRP | S_IWGRP | S_IXGRP),
257                 S_IRWXU     = (S_IRUSR | S_IWUSR | S_IXUSR),
258                 S_IRWXO     = (S_IROTH | S_IWOTH | S_IXOTH),
259                 ACCESSPERMS = (S_IRWXU | S_IRWXG | S_IRWXO), // 0777
260                 ALLPERMS    = (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO), // 07777
261                 DEFFILEMODE = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH), // 0666
262
263                 // Device types
264                 // Why these are held in "mode_t" is beyond me...
265                 S_IFMT      = 0xF000, // Bits which determine file type
266                 S_IFDIR     = 0x4000, // Directory
267                 S_IFCHR     = 0x2000, // Character device
268                 S_IFBLK     = 0x6000, // Block device
269                 S_IFREG     = 0x8000, // Regular file
270                 S_IFIFO     = 0x1000, // FIFO
271                 S_IFLNK     = 0xA000, // Symbolic link
272                 S_IFSOCK    = 0xC000, // Socket
273         }
274
275         public struct Flock {
276                 public LockType         l_type;    // Type of lock: F_RDLCK, F_WRLCK, F_UNLCK
277                 public SeekFlags        l_whence;  // How to interpret l_start
278                 public /* off_t */ long l_start;   // Starting offset for lock
279                 public /* off_t */ long l_len;     // Number of bytes to lock
280                 public /* pid_t */ int  l_pid;     // PID of process blocking our lock (F_GETLK only)
281         }
282
283         [Map]
284         public enum FcntlCommand : int {
285                 // Form /usr/include/bits/fcntl.h
286                 F_DUPFD    =    0, // Duplicate file descriptor.
287                 F_GETFD    =    1, // Get file descriptor flags.
288                 F_SETFD    =    2, // Set file descriptor flags.
289                 F_GETFL    =    3, // Get file status flags.
290                 F_SETFL    =    4, // Set file status flags.
291                 F_GETLK    =   12, // Get record locking info. [64]
292                 F_SETLK    =   13, // Set record locking info (non-blocking). [64]
293                 F_SETLKW   =   14, // Set record locking info (blocking). [64]
294                 F_SETOWN   =    8, // Set owner of socket (receiver of SIGIO).
295                 F_GETOWN   =    9, // Get owner of socket (receiver of SIGIO).
296                 F_SETSIG   =   10, // Set number of signal to be sent.
297                 F_GETSIG   =   11, // Get number of signal to be sent.
298                 F_SETLEASE = 1024, // Set a lease.
299                 F_GETLEASE = 1025, // Enquire what lease is active.
300                 F_NOTIFY   = 1026, // Required notifications on a directory
301         }
302
303         [Map]
304         public enum LockType : short {
305                 F_RDLCK = 0, // Read lock.
306                 F_WRLCK = 1, // Write lock.
307                 F_UNLCK = 2, // Remove lock.
308         }
309
310         [Map]
311         public enum SeekFlags : short {
312                 // values liberally copied from /usr/include/unistd.h
313                 SEEK_SET = 0, // Seek from beginning of file.
314                 SEEK_CUR = 1, // Seek from current position.
315                 SEEK_END = 2, // Seek from end of file.
316
317                 L_SET    = SEEK_SET, // BSD alias for SEEK_SET
318                 L_INCR   = SEEK_CUR, // BSD alias for SEEK_CUR
319                 L_XTND   = SEEK_END, // BSD alias for SEEK_END
320         }
321         
322         [Map, Flags]
323         public enum DirectoryNotifyFlags : int {
324                 // from /usr/include/bits/fcntl.h
325                 DN_ACCESS    = 0x00000001, // File accessed.
326                 DN_MODIFY    = 0x00000002, // File modified.
327                 DN_CREATE    = 0x00000004, // File created.
328                 DN_DELETE    = 0x00000008, // File removed.
329                 DN_RENAME    = 0x00000010, // File renamed.
330                 DN_ATTRIB    = 0x00000020, // File changed attributes.
331                 DN_MULTISHOT = unchecked ((int)0x80000000), // Don't remove notifier
332         }
333
334         [Map]
335         public enum PosixFadviseAdvice : int {
336                 POSIX_FADV_NORMAL     = 0,  // No further special treatment.
337                 POSIX_FADV_RANDOM     = 1,  // Expect random page references.
338                 POSIX_FADV_SEQUENTIAL = 2,  // Expect sequential page references.
339                 POSIX_FADV_WILLNEED   = 3,  // Will need these pages.
340                 POSIX_FADV_DONTNEED   = 4,  // Don't need these pages.
341                 POSIX_FADV_NOREUSE    = 5,  // Data will be accessed once.
342         }
343
344         [Map]
345         public enum PosixMadviseAdvice : int {
346                 POSIX_MADV_NORMAL     = 0,  // No further special treatment.
347                 POSIX_MADV_RANDOM     = 1,  // Expect random page references.
348                 POSIX_MADV_SEQUENTIAL = 2,  // Expect sequential page references.
349                 POSIX_MADV_WILLNEED   = 3,  // Will need these pages.
350                 POSIX_MADV_DONTNEED   = 4,  // Don't need these pages.
351         }
352
353         [Map]
354         public enum Signum : int {
355                 SIGHUP    =  1, // Hangup (POSIX).
356                 SIGINT    =  2, // Interrupt (ANSI).
357                 SIGQUIT   =  3, // Quit (POSIX).
358                 SIGILL    =  4, // Illegal instruction (ANSI).
359                 SIGTRAP   =  5, // Trace trap (POSIX).
360                 SIGABRT   =  6, // Abort (ANSI).
361                 SIGIOT    =  6, // IOT trap (4.2 BSD).
362                 SIGBUS    =  7, // BUS error (4.2 BSD).
363                 SIGFPE    =  8, // Floating-point exception (ANSI).
364                 SIGKILL   =  9, // Kill, unblockable (POSIX).
365                 SIGUSR1   = 10, // User-defined signal 1 (POSIX).
366                 SIGSEGV   = 11, // Segmentation violation (ANSI).
367                 SIGUSR2   = 12, // User-defined signal 2 (POSIX).
368                 SIGPIPE   = 13, // Broken pipe (POSIX).
369                 SIGALRM   = 14, // Alarm clock (POSIX).
370                 SIGTERM   = 15, // Termination (ANSI).
371                 SIGSTKFLT = 16, // Stack fault.
372                 SIGCLD    = SIGCHLD, // Same as SIGCHLD (System V).
373                 SIGCHLD   = 17, // Child status has changed (POSIX).
374                 SIGCONT   = 18, // Continue (POSIX).
375                 SIGSTOP   = 19, // Stop, unblockable (POSIX).
376                 SIGTSTP   = 20, // Keyboard stop (POSIX).
377                 SIGTTIN   = 21, // Background read from tty (POSIX).
378                 SIGTTOU   = 22, // Background write to tty (POSIX).
379                 SIGURG    = 23, // Urgent condition on socket (4.2 BSD).
380                 SIGXCPU   = 24, // CPU limit exceeded (4.2 BSD).
381                 SIGXFSZ   = 25, // File size limit exceeded (4.2 BSD).
382                 SIGVTALRM = 26, // Virtual alarm clock (4.2 BSD).
383                 SIGPROF   = 27, // Profiling alarm clock (4.2 BSD).
384                 SIGWINCH  = 28, // Window size change (4.3 BSD, Sun).
385                 SIGPOLL   = SIGIO, // Pollable event occurred (System V).
386                 SIGIO     = 29, // I/O now possible (4.2 BSD).
387                 SIGPWR    = 30, // Power failure restart (System V).
388                 SIGSYS    = 31, // Bad system call.
389                 SIGUNUSED = 31
390         }
391
392         [Flags][Map]
393         public enum WaitOptions : int {
394                 WNOHANG   = 1,  // Don't block waiting
395                 WUNTRACED = 2,  // Report status of stopped children
396         }
397
398   [Flags][Map]
399         public enum AccessMode : int {
400                 R_OK = 1,
401                 W_OK = 2,
402                 X_OK = 4,
403                 F_OK = 8,
404         }
405
406         [Map]
407         public enum PathConf : int {
408                 _PC_LINK_MAX,
409                 _PC_MAX_CANON,
410                 _PC_MAX_INPUT,
411                 _PC_NAME_MAX,
412                 _PC_PATH_MAX,
413                 _PC_PIPE_BUF,
414                 _PC_CHOWN_RESTRICTED,
415                 _PC_NO_TRUNC,
416                 _PC_VDISABLE,
417                 _PC_SYNC_IO,
418                 _PC_ASYNC_IO,
419                 _PC_PRIO_IO,
420                 _PC_SOCK_MAXBUF,
421                 _PC_FILESIZEBITS,
422                 _PC_REC_INCR_XFER_SIZE,
423                 _PC_REC_MAX_XFER_SIZE,
424                 _PC_REC_MIN_XFER_SIZE,
425                 _PC_REC_XFER_ALIGN,
426                 _PC_ALLOC_SIZE_MIN,
427                 _PC_SYMLINK_MAX,
428                 _PC_2_SYMLINKS
429         }
430
431         [Map]
432         public enum SysConf : int {
433                 _SC_ARG_MAX,
434                 _SC_CHILD_MAX,
435                 _SC_CLK_TCK,
436                 _SC_NGROUPS_MAX,
437                 _SC_OPEN_MAX,
438                 _SC_STREAM_MAX,
439                 _SC_TZNAME_MAX,
440                 _SC_JOB_CONTROL,
441                 _SC_SAVED_IDS,
442                 _SC_REALTIME_SIGNALS,
443                 _SC_PRIORITY_SCHEDULING,
444                 _SC_TIMERS,
445                 _SC_ASYNCHRONOUS_IO,
446                 _SC_PRIORITIZED_IO,
447                 _SC_SYNCHRONIZED_IO,
448                 _SC_FSYNC,
449                 _SC_MAPPED_FILES,
450                 _SC_MEMLOCK,
451                 _SC_MEMLOCK_RANGE,
452                 _SC_MEMORY_PROTECTION,
453                 _SC_MESSAGE_PASSING,
454                 _SC_SEMAPHORES,
455                 _SC_SHARED_MEMORY_OBJECTS,
456                 _SC_AIO_LISTIO_MAX,
457                 _SC_AIO_MAX,
458                 _SC_AIO_PRIO_DELTA_MAX,
459                 _SC_DELAYTIMER_MAX,
460                 _SC_MQ_OPEN_MAX,
461                 _SC_MQ_PRIO_MAX,
462                 _SC_VERSION,
463                 _SC_PAGESIZE,
464                 _SC_RTSIG_MAX,
465                 _SC_SEM_NSEMS_MAX,
466                 _SC_SEM_VALUE_MAX,
467                 _SC_SIGQUEUE_MAX,
468                 _SC_TIMER_MAX,
469                 /* Values for the argument to `sysconf'
470                          corresponding to _POSIX2_* symbols.  */
471                 _SC_BC_BASE_MAX,
472                 _SC_BC_DIM_MAX,
473                 _SC_BC_SCALE_MAX,
474                 _SC_BC_STRING_MAX,
475                 _SC_COLL_WEIGHTS_MAX,
476                 _SC_EQUIV_CLASS_MAX,
477                 _SC_EXPR_NEST_MAX,
478                 _SC_LINE_MAX,
479                 _SC_RE_DUP_MAX,
480                 _SC_CHARCLASS_NAME_MAX,
481                 _SC_2_VERSION,
482                 _SC_2_C_BIND,
483                 _SC_2_C_DEV,
484                 _SC_2_FORT_DEV,
485                 _SC_2_FORT_RUN,
486                 _SC_2_SW_DEV,
487                 _SC_2_LOCALEDEF,
488                 _SC_PII,
489                 _SC_PII_XTI,
490                 _SC_PII_SOCKET,
491                 _SC_PII_INTERNET,
492                 _SC_PII_OSI,
493                 _SC_POLL,
494                 _SC_SELECT,
495                 _SC_UIO_MAXIOV,
496                 _SC_IOV_MAX = _SC_UIO_MAXIOV,
497                 _SC_PII_INTERNET_STREAM,
498                 _SC_PII_INTERNET_DGRAM,
499                 _SC_PII_OSI_COTS,
500                 _SC_PII_OSI_CLTS,
501                 _SC_PII_OSI_M,
502                 _SC_T_IOV_MAX,
503                 /* Values according to POSIX 1003.1c (POSIX threads).  */
504                 _SC_THREADS,
505                 _SC_THREAD_SAFE_FUNCTIONS,
506                 _SC_GETGR_R_SIZE_MAX,
507                 _SC_GETPW_R_SIZE_MAX,
508                 _SC_LOGIN_NAME_MAX,
509                 _SC_TTY_NAME_MAX,
510                 _SC_THREAD_DESTRUCTOR_ITERATIONS,
511                 _SC_THREAD_KEYS_MAX,
512                 _SC_THREAD_STACK_MIN,
513                 _SC_THREAD_THREADS_MAX,
514                 _SC_THREAD_ATTR_STACKADDR,
515                 _SC_THREAD_ATTR_STACKSIZE,
516                 _SC_THREAD_PRIORITY_SCHEDULING,
517                 _SC_THREAD_PRIO_INHERIT,
518                 _SC_THREAD_PRIO_PROTECT,
519                 _SC_THREAD_PROCESS_SHARED,
520                 _SC_NPROCESSORS_CONF,
521                 _SC_NPROCESSORS_ONLN,
522                 _SC_PHYS_PAGES,
523                 _SC_AVPHYS_PAGES,
524                 _SC_ATEXIT_MAX,
525                 _SC_PASS_MAX,
526                 _SC_XOPEN_VERSION,
527                 _SC_XOPEN_XCU_VERSION,
528                 _SC_XOPEN_UNIX,
529                 _SC_XOPEN_CRYPT,
530                 _SC_XOPEN_ENH_I18N,
531                 _SC_XOPEN_SHM,
532                 _SC_2_CHAR_TERM,
533                 _SC_2_C_VERSION,
534                 _SC_2_UPE,
535                 _SC_XOPEN_XPG2,
536                 _SC_XOPEN_XPG3,
537                 _SC_XOPEN_XPG4,
538                 _SC_CHAR_BIT,
539                 _SC_CHAR_MAX,
540                 _SC_CHAR_MIN,
541                 _SC_INT_MAX,
542                 _SC_INT_MIN,
543                 _SC_LONG_BIT,
544                 _SC_WORD_BIT,
545                 _SC_MB_LEN_MAX,
546                 _SC_NZERO,
547                 _SC_SSIZE_MAX,
548                 _SC_SCHAR_MAX,
549                 _SC_SCHAR_MIN,
550                 _SC_SHRT_MAX,
551                 _SC_SHRT_MIN,
552                 _SC_UCHAR_MAX,
553                 _SC_UINT_MAX,
554                 _SC_ULONG_MAX,
555                 _SC_USHRT_MAX,
556                 _SC_NL_ARGMAX,
557                 _SC_NL_LANGMAX,
558                 _SC_NL_MSGMAX,
559                 _SC_NL_NMAX,
560                 _SC_NL_SETMAX,
561                 _SC_NL_TEXTMAX,
562                 _SC_XBS5_ILP32_OFF32,
563                 _SC_XBS5_ILP32_OFFBIG,
564                 _SC_XBS5_LP64_OFF64,
565                 _SC_XBS5_LPBIG_OFFBIG,
566                 _SC_XOPEN_LEGACY,
567                 _SC_XOPEN_REALTIME,
568                 _SC_XOPEN_REALTIME_THREADS,
569                 _SC_ADVISORY_INFO,
570                 _SC_BARRIERS,
571                 _SC_BASE,
572                 _SC_C_LANG_SUPPORT,
573                 _SC_C_LANG_SUPPORT_R,
574                 _SC_CLOCK_SELECTION,
575                 _SC_CPUTIME,
576                 _SC_THREAD_CPUTIME,
577                 _SC_DEVICE_IO,
578                 _SC_DEVICE_SPECIFIC,
579                 _SC_DEVICE_SPECIFIC_R,
580                 _SC_FD_MGMT,
581                 _SC_FIFO,
582                 _SC_PIPE,
583                 _SC_FILE_ATTRIBUTES,
584                 _SC_FILE_LOCKING,
585                 _SC_FILE_SYSTEM,
586                 _SC_MONOTONIC_CLOCK,
587                 _SC_MULTI_PROCESS,
588                 _SC_SINGLE_PROCESS,
589                 _SC_NETWORKING,
590                 _SC_READER_WRITER_LOCKS,
591                 _SC_SPIN_LOCKS,
592                 _SC_REGEXP,
593                 _SC_REGEX_VERSION,
594                 _SC_SHELL,
595                 _SC_SIGNALS,
596                 _SC_SPAWN,
597                 _SC_SPORADIC_SERVER,
598                 _SC_THREAD_SPORADIC_SERVER,
599                 _SC_SYSTEM_DATABASE,
600                 _SC_SYSTEM_DATABASE_R,
601                 _SC_TIMEOUTS,
602                 _SC_TYPED_MEMORY_OBJECTS,
603                 _SC_USER_GROUPS,
604                 _SC_USER_GROUPS_R,
605                 _SC_2_PBS,
606                 _SC_2_PBS_ACCOUNTING,
607                 _SC_2_PBS_LOCATE,
608                 _SC_2_PBS_MESSAGE,
609                 _SC_2_PBS_TRACK,
610                 _SC_SYMLOOP_MAX,
611                 _SC_STREAMS,
612                 _SC_2_PBS_CHECKPOINT,
613                 _SC_V6_ILP32_OFF32,
614                 _SC_V6_ILP32_OFFBIG,
615                 _SC_V6_LP64_OFF64,
616                 _SC_V6_LPBIG_OFFBIG,
617                 _SC_HOST_NAME_MAX,
618                 _SC_TRACE,
619                 _SC_TRACE_EVENT_FILTER,
620                 _SC_TRACE_INHERIT,
621                 _SC_TRACE_LOG,
622                 _SC_LEVEL1_ICACHE_SIZE,
623                 _SC_LEVEL1_ICACHE_ASSOC,
624                 _SC_LEVEL1_ICACHE_LINESIZE,
625                 _SC_LEVEL1_DCACHE_SIZE,
626                 _SC_LEVEL1_DCACHE_ASSOC,
627                 _SC_LEVEL1_DCACHE_LINESIZE,
628                 _SC_LEVEL2_CACHE_SIZE,
629                 _SC_LEVEL2_CACHE_ASSOC,
630                 _SC_LEVEL2_CACHE_LINESIZE,
631                 _SC_LEVEL3_CACHE_SIZE,
632                 _SC_LEVEL3_CACHE_ASSOC,
633                 _SC_LEVEL3_CACHE_LINESIZE,
634                 _SC_LEVEL4_CACHE_SIZE,
635                 _SC_LEVEL4_CACHE_ASSOC,
636                 _SC_LEVEL4_CACHE_LINESIZE
637         }
638
639         [Map]
640         public enum ConfStr : int {
641                 _CS_PATH,                       /* The default search path.  */
642                 _CS_V6_WIDTH_RESTRICTED_ENVS,
643                 _CS_GNU_LIBC_VERSION,
644                 _CS_GNU_LIBPTHREAD_VERSION,
645                 _CS_LFS_CFLAGS = 1000,
646                 _CS_LFS_LDFLAGS,
647                 _CS_LFS_LIBS,
648                 _CS_LFS_LINTFLAGS,
649                 _CS_LFS64_CFLAGS,
650                 _CS_LFS64_LDFLAGS,
651                 _CS_LFS64_LIBS,
652                 _CS_LFS64_LINTFLAGS,
653                 _CS_XBS5_ILP32_OFF32_CFLAGS = 1100,
654                 _CS_XBS5_ILP32_OFF32_LDFLAGS,
655                 _CS_XBS5_ILP32_OFF32_LIBS,
656                 _CS_XBS5_ILP32_OFF32_LINTFLAGS,
657                 _CS_XBS5_ILP32_OFFBIG_CFLAGS,
658                 _CS_XBS5_ILP32_OFFBIG_LDFLAGS,
659                 _CS_XBS5_ILP32_OFFBIG_LIBS,
660                 _CS_XBS5_ILP32_OFFBIG_LINTFLAGS,
661                 _CS_XBS5_LP64_OFF64_CFLAGS,
662                 _CS_XBS5_LP64_OFF64_LDFLAGS,
663                 _CS_XBS5_LP64_OFF64_LIBS,
664                 _CS_XBS5_LP64_OFF64_LINTFLAGS,
665                 _CS_XBS5_LPBIG_OFFBIG_CFLAGS,
666                 _CS_XBS5_LPBIG_OFFBIG_LDFLAGS,
667                 _CS_XBS5_LPBIG_OFFBIG_LIBS,
668                 _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS,
669                 _CS_POSIX_V6_ILP32_OFF32_CFLAGS,
670                 _CS_POSIX_V6_ILP32_OFF32_LDFLAGS,
671                 _CS_POSIX_V6_ILP32_OFF32_LIBS,
672                 _CS_POSIX_V6_ILP32_OFF32_LINTFLAGS,
673                 _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS,
674                 _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS,
675                 _CS_POSIX_V6_ILP32_OFFBIG_LIBS,
676                 _CS_POSIX_V6_ILP32_OFFBIG_LINTFLAGS,
677                 _CS_POSIX_V6_LP64_OFF64_CFLAGS,
678                 _CS_POSIX_V6_LP64_OFF64_LDFLAGS,
679                 _CS_POSIX_V6_LP64_OFF64_LIBS,
680                 _CS_POSIX_V6_LP64_OFF64_LINTFLAGS,
681                 _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS,
682                 _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS,
683                 _CS_POSIX_V6_LPBIG_OFFBIG_LIBS,
684                 _CS_POSIX_V6_LPBIG_OFFBIG_LINTFLAGS
685         }
686
687         [Map]
688         public enum LockFlags : int {
689                 F_ULOCK = 0, // Unlock a previously locked region.
690                 F_LOCK  = 1, // Lock a region for exclusive use.
691                 F_TLOCK = 2, // Test and lock a region for exclusive use.
692                 F_TEST  = 3, // Test a region for other process locks.
693         }
694
695         [Map][Flags]
696         public enum PollEvents : short {
697                 POLLIN      = 0x0001, // There is data to read
698                 POLLPRI     = 0x0002, // There is urgent data to read
699                 POLLOUT     = 0x0004, // Writing now will not block
700                 POLLERR     = 0x0008, // Error condition
701                 POLLHUP     = 0x0010, // Hung up
702                 POLLNVAL    = 0x0020, // Invalid request; fd not open
703                 // XPG4.2 definitions (via _XOPEN_SOURCE)
704                 POLLRDNORM  = 0x0040, // Normal data bay be read
705                 POLLRDBAND  = 0x0080, // Priority data may be read
706                 POLLWRNORM  = 0x0100, // Writing now will not block
707                 POLLWRBAND  = 0x0200, // Priority data may be written
708         }
709
710         [StructLayout(LayoutKind.Sequential)]
711         public struct Pollfd {
712                 public int fd;
713                 public PollEvents events;
714                 public PollEvents revents;
715         }
716
717         public sealed class Dirent
718         {
719                 public /* ino_t */ ulong  d_ino;
720                 public /* off_t */ long   d_off;
721                 public ushort             d_reclen;
722                 public byte               d_type;
723                 public string             d_name;
724
725                 public override int GetHashCode ()
726                 {
727                         return d_ino.GetHashCode () ^ d_off.GetHashCode () ^ 
728                                 d_reclen.GetHashCode () ^ d_type.GetHashCode () ^
729                                 d_name.GetHashCode ();
730                 }
731
732                 public override bool Equals (object obj)
733                 {
734                         if (obj == null || GetType() != obj.GetType())
735                                 return false;
736                         Dirent d = (Dirent) obj;
737                         return d.d_ino == d_ino && d.d_off == d_off &&
738                                 d.d_reclen == d_reclen && d.d_type == d_type &&
739                                 d.d_name == d.d_name;
740                 }
741
742                 public override string ToString ()
743                 {
744                         return d_name;
745                 }
746
747                 public static bool operator== (Dirent lhs, Dirent rhs)
748                 {
749                         return Object.Equals (lhs, rhs);
750                 }
751
752                 public static bool operator!= (Dirent lhs, Dirent rhs)
753                 {
754                         return Object.Equals (lhs, rhs);
755                 }
756         }
757
758         public sealed class Group
759         {
760                 public string           gr_name;
761                 public string           gr_passwd;
762                 public /* gid_t */ uint gr_gid;
763                 public string[]         gr_mem;
764
765                 public override int GetHashCode ()
766                 {
767                         int memhc = 0;
768                         for (int i = 0; i < gr_mem.Length; ++i)
769                                 memhc ^= gr_mem[i].GetHashCode ();
770
771                         return gr_name.GetHashCode () ^ gr_passwd.GetHashCode () ^ 
772                                 gr_gid.GetHashCode () ^ memhc;
773                 }
774
775                 public override bool Equals (object obj)
776                 {
777                         if (obj == null || GetType() != obj.GetType())
778                                 return false;
779                         Group g = (Group) obj;
780                         if (g.gr_gid != gr_gid)
781                                 return false;
782                         if (g.gr_gid == gr_gid && g.gr_name == g.gr_name &&
783                                 g.gr_passwd == g.gr_passwd && g.gr_mem.Length == gr_mem.Length) {
784                                 for (int i = 0; i < gr_mem.Length; ++i)
785                                         if (gr_mem[i] != g.gr_mem[i])
786                                                 return false;
787                                 return true;
788                         }
789                         return false;
790                 }
791
792                 // Generate string in /etc/group format
793                 public override string ToString ()
794                 {
795                         StringBuilder sb = new StringBuilder ();
796                         sb.AppendFormat ("{0}:{1}:{2}:", gr_name, gr_passwd, gr_gid);
797                         GetMembers (sb, gr_mem);
798                         return sb.ToString ();
799                 }
800
801                 private static void GetMembers (StringBuilder sb, string[] members)
802                 {
803                         if (members.Length > 0)
804                                 sb.Append (members[0]);
805                         for (int i = 1; i < members.Length; ++i) {
806                                 sb.Append (",");
807                                 sb.Append (members[i]);
808                         }
809                 }
810
811                 public static bool operator== (Group lhs, Group rhs)
812                 {
813                         return Object.Equals (lhs, rhs);
814                 }
815
816                 public static bool operator!= (Group lhs, Group rhs)
817                 {
818                         return Object.Equals (lhs, rhs);
819                 }
820         }
821
822         public sealed class Passwd
823         {
824                 public string           pw_name;
825                 public string           pw_passwd;
826                 public /* uid_t */ uint pw_uid;
827                 public /* gid_t */ uint pw_gid;
828                 public string           pw_gecos;
829                 public string           pw_dir;
830                 public string           pw_shell;
831
832                 public override int GetHashCode ()
833                 {
834                         return pw_name.GetHashCode () ^ pw_passwd.GetHashCode () ^ 
835                                 pw_uid.GetHashCode () ^ pw_gid.GetHashCode () ^
836                                 pw_gecos.GetHashCode () ^ pw_dir.GetHashCode () ^
837                                 pw_dir.GetHashCode () ^ pw_shell.GetHashCode ();
838                 }
839
840                 public override bool Equals (object obj)
841                 {
842                         if (obj == null || GetType() != obj.GetType())
843                                 return false;
844                         Passwd p = (Passwd) obj;
845                         return p.pw_uid == pw_uid && p.pw_gid == pw_gid && p.pw_name == pw_name && 
846                                 p.pw_passwd == pw_passwd && p.pw_gecos == p.pw_gecos && 
847                                 p.pw_dir == p.pw_dir && p.pw_shell == p.pw_shell;
848                 }
849
850                 // Generate string in /etc/passwd format
851                 public override string ToString ()
852                 {
853                         return string.Format ("{0}:{1}:{2}:{3}:{4}:{5}:{6}",
854                                 pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell);
855                 }
856
857                 public static bool operator== (Passwd lhs, Passwd rhs)
858                 {
859                         return Object.Equals (lhs, rhs);
860                 }
861
862                 public static bool operator!= (Passwd lhs, Passwd rhs)
863                 {
864                         return Object.Equals (lhs, rhs);
865                 }
866         }
867
868         public struct Stat {
869                 public  /* dev_t */     ulong   st_dev;     // device
870                 public  /* ino_t */     ulong   st_ino;     // inode
871                 public  FilePermissions         st_mode;    // protection
872                 private uint                    _padding_;  // padding for structure alignment
873                 public  /* nlink_t */   ulong   st_nlink;   // number of hard links
874                 public  /* uid_t */     uint    st_uid;     // user ID of owner
875                 public  /* gid_t */     uint    st_gid;     // group ID of owner
876                 public  /* dev_t */     ulong   st_rdev;    // device type (if inode device)
877                 public  /* off_t */     long    st_size;    // total size, in bytes
878                 public  /* blksize_t */ long    st_blksize; // blocksize for filesystem I/O
879                 public  /* blkcnt_t */  long    st_blocks;  // number of blocks allocated
880                 public  /* time_t */    long    st_atime;   // time of last access
881                 public  /* time_t */    long    st_mtime;   // time of last modification
882                 public  /* time_t */    long    st_ctime;   // time of last status change
883         }
884
885         public struct Timeval {
886                 public  /* time_t */      long    tv_sec;   // seconds
887                 public  /* suseconds_t */ long    tv_usec;  // microseconds
888         }
889
890         public struct Timezone {
891                 public  int tz_minuteswest; // minutes W of Greenwich
892                 private int tz_dsttime;     // type of dst correction (OBSOLETE)
893         }
894
895         public struct Utimbuf {
896                 public  /* time_t */      long    actime;   // access time
897                 public  /* time_t */      long    modtime;  // modification time
898         }
899
900         //
901         // Convention: Functions *not* part of the standard C library AND part of
902         // a POSIX and/or Unix standard (X/Open, SUS, XPG, etc.) go here.
903         //
904         // For example, the man page should be similar to:
905         //
906         //    CONFORMING TO (or CONFORMS TO)
907         //           XPG2, SUSv2, POSIX, etc.
908         //
909         // BSD- and GNU-specific exports can also be placed here.
910         //
911         // The only methods in here should be:
912         //  (1) low-level functions
913         //  (2) "Trivial" function overloads.  For example, if the parameters to a
914         //      function are related (e.g. getgroups(2))
915         //  (3) The return type SHOULD NOT be changed.  If you want to provide a
916         //      convenience function with a nicer return type, place it into one of
917         //      the Unix* wrapper classes, and give it a .NET-styled name.
918         //  (4) Exceptions SHOULD NOT be thrown.  EXCEPTIONS: 
919         //      - If you're wrapping *broken* methods which make assumptions about 
920         //        input data, such as that an argument refers to N bytes of data.  
921         //        This is currently limited to cuserid(3) and encrypt(3).
922         //      - If you call functions which themselves generate exceptions.  
923         //        This is the case for using UnixConvert, which will throw an
924         //        exception if an invalid/unsupported value is used.
925         //
926         public sealed class Syscall : Stdlib
927         {
928                 private const string LIBC = "libc";
929                 private const string MPH = "MonoPosixHelper";
930                 private const string CRYPT = "crypt";
931
932                 private Syscall () {}
933
934                 //
935                 // <aio.h>
936                 //
937
938                 // TODO: aio_cancel(3), aio_error(3), aio_fsync(3), aio_read(3), 
939                 // aio_return(3), aio_suspend(3), aio_write(3)
940                 //
941                 // Then update UnixStream.BeginRead to use the aio* functions.
942
943                 #region <dirent.h> Declarations
944                 //
945                 // <dirent.h>
946                 //
947                 // TODO: scandir(3), alphasort(3), versionsort(3), getdirentries(3)
948
949                 [DllImport (LIBC, SetLastError=true)]
950                 public static extern IntPtr opendir (string name);
951
952                 [DllImport (LIBC, SetLastError=true)]
953                 public static extern int closedir (IntPtr dir);
954
955                 // seekdir(3):
956                 //    void seekdir (DIR *dir, off_t offset);
957                 //    Slight modification.  Returns -1 on error, 0 on success.
958                 [DllImport (MPH, SetLastError=true,
959                                 EntryPoint="Mono_Posix_Syscall_seekdir")]
960                 public static extern int seekdir (IntPtr dir, long offset);
961
962                 // telldir(3)
963                 //    off_t telldir(DIR *dir);
964                 [DllImport (MPH, SetLastError=true,
965                                 EntryPoint="Mono_Posix_Syscall_telldir")]
966                 public static extern long telldir (IntPtr dir);
967
968                 [DllImport (LIBC, SetLastError=true)]
969                 public static extern void rewinddir (IntPtr dir);
970
971                 private struct _Dirent {
972                         public /* ino_t */ ulong  d_ino;
973                         public /* off_t */ long   d_off;
974                         public ushort             d_reclen;
975                         public byte               d_type;
976                         public IntPtr             d_name;
977                 }
978
979                 private static void CopyDirent (Dirent to, ref _Dirent from)
980                 {
981                         try {
982                                 to.d_ino    = from.d_ino;
983                                 to.d_off    = from.d_off;
984                                 to.d_reclen = from.d_reclen;
985                                 to.d_type   = from.d_type;
986                                 if (from.d_name != IntPtr.Zero)
987                                         to.d_name = UnixMarshal.PtrToString (from.d_name);
988                                 else 
989                                         to.d_name = null;
990                         }
991                         finally {
992                                 Stdlib.free (from.d_name);
993                                 from.d_name = IntPtr.Zero;
994                         }
995                 }
996
997                 [DllImport (MPH, SetLastError=true,
998                                 EntryPoint="Mono_Posix_Syscall_readdir")]
999                 private static extern int sys_readdir (IntPtr dir, out _Dirent dentry);
1000
1001                 public static Dirent readdir (IntPtr dir)
1002                 {
1003                         _Dirent dentry;
1004                         int r = sys_readdir (dir, out dentry);
1005                         if (r != 0)
1006                                 return null;
1007                         Dirent d = new Dirent ();
1008                         CopyDirent (d, ref dentry);
1009                         return d;
1010                 }
1011
1012                 [DllImport (MPH, SetLastError=true,
1013                                 EntryPoint="Mono_Posix_Syscall_readdir_r")]
1014                 private static extern int sys_readdir_r (IntPtr dirp, out _Dirent entry, out IntPtr result);
1015
1016                 public static int readdir_r (IntPtr dirp, Dirent entry, out IntPtr result)
1017                 {
1018                         entry.d_ino    = 0;
1019                         entry.d_off    = 0;
1020                         entry.d_reclen = 0;
1021                         entry.d_type   = 0;
1022                         entry.d_name   = null;
1023
1024                         _Dirent _d;
1025                         int r = sys_readdir_r (dirp, out _d, out result);
1026
1027                         if (r == 0 && result != IntPtr.Zero) {
1028                                 CopyDirent (entry, ref _d);
1029                         }
1030
1031                         return r;
1032                 }
1033
1034                 [DllImport (LIBC, SetLastError=true)]
1035                 public static extern int dirfd (IntPtr dir);
1036                 #endregion
1037
1038                 #region <errno.h> Declarations
1039                 //
1040                 // <errno.h>  -- COMPLETE
1041                 //
1042
1043                 public static Error GetLastError ()
1044                 {
1045                         int errno = Marshal.GetLastWin32Error ();
1046                         return UnixConvert.ToError (errno);
1047                 }
1048
1049                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_SetLastError")]
1050                 private static extern void SetLastError (int error);
1051
1052                 public static void SetLastError (Error error)
1053                 {
1054                         int _error = UnixConvert.FromError (error);
1055                         SetLastError (_error);
1056                 }
1057                 #endregion
1058
1059                 #region <fcntl.h> Declarations
1060                 //
1061                 // <fcntl.h> -- COMPLETE
1062                 //
1063
1064                 [DllImport (MPH, SetLastError=true, 
1065                                 EntryPoint="Mono_Posix_Syscall_fcntl")]
1066                 public static extern int fcntl (int fd, FcntlCommand cmd);
1067
1068                 [DllImport (MPH, SetLastError=true, 
1069                                 EntryPoint="Mono_Posix_Syscall_fcntl_arg")]
1070                 public static extern int fcntl (int fd, FcntlCommand cmd, long arg);
1071
1072                 [DllImport (MPH, SetLastError=true, 
1073                                 EntryPoint="Mono_Posix_Syscall_fcntl_lock")]
1074                 public static extern int fcntl (int fd, FcntlCommand cmd, ref Flock @lock);
1075
1076                 [DllImport (MPH, SetLastError=true, 
1077                                 EntryPoint="Mono_Posix_Syscall_open")]
1078                 public static extern int open (string pathname, OpenFlags flags);
1079
1080                 // open(2)
1081                 //    int open(const char *pathname, int flags, mode_t mode);
1082                 [DllImport (MPH, SetLastError=true, 
1083                                 EntryPoint="Mono_Posix_Syscall_open_mode")]
1084                 public static extern int open (string pathname, OpenFlags flags, FilePermissions mode);
1085
1086                 // creat(2)
1087                 //    int creat(const char *pathname, mode_t mode);
1088                 [DllImport (MPH, SetLastError=true, 
1089                                 EntryPoint="Mono_Posix_Syscall_creat")]
1090                 public static extern int creat (string pathname, FilePermissions mode);
1091
1092                 // posix_fadvise(2)
1093                 //    int posix_fadvise(int fd, off_t offset, off_t len, int advice);
1094                 [DllImport (MPH, SetLastError=true, 
1095                                 EntryPoint="Mono_Posix_Syscall_posix_fadvise")]
1096                 public static extern int posix_fadvise (int fd, long offset, 
1097                         long len, PosixFadviseAdvice advice);
1098
1099                 // posix_fallocate(P)
1100                 //    int posix_fallocate(int fd, off_t offset, size_t len);
1101                 [DllImport (MPH, SetLastError=true, 
1102                                 EntryPoint="Mono_Posix_Syscall_posix_fallocate")]
1103                 public static extern int posix_fallocate (int fd, long offset, long len);
1104                 #endregion
1105
1106                 #region <grp.h> Declarations
1107                 //
1108                 // <grp.h>
1109                 //
1110                 // TODO: putgrent(3), fgetgrent_r(), getgrouplist(2), initgroups(3)
1111
1112                 // setgroups(2)
1113                 //    int setgroups (size_t size, const gid_t *list);
1114                 [DllImport (MPH, SetLastError=true, 
1115                                 EntryPoint="Mono_Posix_Syscall_setgroups")]
1116                 public static extern int setgroups (ulong size, uint[] list);
1117
1118                 public static int setgroups (uint [] list)
1119                 {
1120                         return setgroups ((ulong) list.Length, list);
1121                 }
1122
1123                 private struct _Group
1124                 {
1125                         public IntPtr           gr_name;
1126                         public IntPtr           gr_passwd;
1127                         public /* gid_t */ uint gr_gid;
1128                         public int              _gr_nmem_;
1129                         public IntPtr           gr_mem;
1130                         public IntPtr           _gr_buf_;
1131                 }
1132
1133                 private static void CopyGroup (Group to, ref _Group from)
1134                 {
1135                         try {
1136                                 to.gr_gid    = from.gr_gid;
1137                                 to.gr_name   = UnixMarshal.PtrToString (from.gr_name);
1138                                 to.gr_passwd = UnixMarshal.PtrToString (from.gr_passwd);
1139                                 to.gr_mem    = UnixMarshal.PtrToStringArray (from._gr_nmem_, from.gr_mem);
1140                         }
1141                         finally {
1142                                 Stdlib.free (from.gr_mem);
1143                                 Stdlib.free (from._gr_buf_);
1144                                 from.gr_mem   = IntPtr.Zero;
1145                                 from._gr_buf_ = IntPtr.Zero;
1146                         }
1147                 }
1148
1149                 [DllImport (MPH, SetLastError=true,
1150                                 EntryPoint="Mono_Posix_Syscall_getgrnam")]
1151                 private static extern int sys_getgrnam (string name, out _Group group);
1152
1153                 public static Group getgrnam (string name)
1154                 {
1155                         _Group group;
1156                         int r = sys_getgrnam (name, out group);
1157                         if (r != 0)
1158                                 return null;
1159                         Group gr = new Group ();
1160                         CopyGroup (gr, ref group);
1161                         return gr;
1162                 }
1163
1164                 // getgrgid(3)
1165                 //    struct group *getgrgid(gid_t gid);
1166                 [DllImport (MPH, SetLastError=true,
1167                                 EntryPoint="Mono_Posix_Syscall_getgrgid")]
1168                 private static extern int sys_getgrgid (uint uid, out _Group group);
1169
1170                 public static Group getgrgid (uint uid)
1171                 {
1172                         _Group group;
1173                         int r = sys_getgrgid (uid, out group);
1174                         if (r != 0)
1175                                 return null;
1176                         Group gr = new Group ();
1177                         CopyGroup (gr, ref group);
1178                         return gr;
1179                 }
1180
1181                 [DllImport (MPH, SetLastError=true,
1182                                 EntryPoint="Mono_Posix_Syscall_getgrnam_r")]
1183                 private static extern int sys_getgrnam_r (string name, out _Group grbuf, out IntPtr grbufp);
1184
1185                 public static int getgrnam_r (string name, Group grbuf, out Group grbufp)
1186                 {
1187                         grbufp = null;
1188                         _Group group;
1189                         IntPtr _grbufp;
1190                         int r = sys_getgrnam_r (name, out group, out _grbufp);
1191                         if (r == 0 && _grbufp != IntPtr.Zero) {
1192                                 CopyGroup (grbuf, ref group);
1193                                 grbufp = grbuf;
1194                         }
1195                         return r;
1196                 }
1197
1198                 // getgrgid_r(3)
1199                 //    int getgrgid_r(gid_t gid, struct group *gbuf, char *buf,
1200                 //        size_t buflen, struct group **gbufp);
1201                 [DllImport (MPH, SetLastError=true,
1202                                 EntryPoint="Mono_Posix_Syscall_getgrgid_r")]
1203                 private static extern int sys_getgrgid_r (uint uid, out _Group grbuf, out IntPtr grbufp);
1204
1205                 public static int getgrgid_r (uint uid, Group grbuf, out Group grbufp)
1206                 {
1207                         grbufp = null;
1208                         _Group group;
1209                         IntPtr _grbufp;
1210                         int r = sys_getgrgid_r (uid, out group, out _grbufp);
1211                         if (r == 0 && _grbufp != IntPtr.Zero) {
1212                                 CopyGroup (grbuf, ref group);
1213                                 grbufp = grbuf;
1214                         }
1215                         return r;
1216                 }
1217
1218                 [DllImport (MPH, SetLastError=true,
1219                                 EntryPoint="Mono_Posix_Syscall_getgrent")]
1220                 private static extern int sys_getgrent (out _Group grbuf);
1221
1222                 public static Group getgrent ()
1223                 {
1224                         _Group group;
1225                         int r = sys_getgrent (out group);
1226                         if (r != 0)
1227                                 return null;
1228                         Group gr = new Group();
1229                         CopyGroup (gr, ref group);
1230                         return gr;
1231                 }
1232
1233                 [DllImport (LIBC, SetLastError=true)]
1234                 public static extern void setgrent ();
1235
1236                 [DllImport (LIBC, SetLastError=true)]
1237                 public static extern void endgrent ();
1238
1239                 [DllImport (MPH, SetLastError=true,
1240                                 EntryPoint="Mono_Posix_Syscall_fgetgrent")]
1241                 private static extern int sys_fgetgrent (IntPtr stream, out _Group grbuf);
1242
1243                 public static Group fgetgrent (IntPtr stream)
1244                 {
1245                         _Group group;
1246                         int r = sys_fgetgrent (stream, out group);
1247                         if (r != 0)
1248                                 return null;
1249                         Group gr = new Group ();
1250                         CopyGroup (gr, ref group);
1251                         return gr;
1252                 }
1253                 #endregion
1254
1255                 #region <pwd.h> Declarations
1256                 //
1257                 // <pwd.h>
1258                 //
1259                 // TODO: putpwent(3), fgetpwent_r()
1260                 //
1261                 // SKIPPING: getpw(3): it's dangerous.  Use getpwuid(3) instead.
1262
1263                 private struct _Passwd
1264                 {
1265                         public IntPtr           pw_name;
1266                         public IntPtr           pw_passwd;
1267                         public /* uid_t */ uint pw_uid;
1268                         public /* gid_t */ uint pw_gid;
1269                         public IntPtr           pw_gecos;
1270                         public IntPtr           pw_dir;
1271                         public IntPtr           pw_shell;
1272                         public IntPtr           _pw_buf_;
1273                 }
1274
1275                 private static void CopyPasswd (Passwd to, ref _Passwd from)
1276                 {
1277                         try {
1278                                 to.pw_name   = UnixMarshal.PtrToString (from.pw_name);
1279                                 to.pw_passwd = UnixMarshal.PtrToString (from.pw_passwd);
1280                                 to.pw_uid    = from.pw_uid;
1281                                 to.pw_gid    = from.pw_gid;
1282                                 to.pw_gecos  = UnixMarshal.PtrToString (from.pw_gecos);
1283                                 to.pw_dir    = UnixMarshal.PtrToString (from.pw_dir);
1284                                 to.pw_shell  = UnixMarshal.PtrToString (from.pw_shell);
1285                         }
1286                         finally {
1287                                 Stdlib.free (from._pw_buf_);
1288                                 from._pw_buf_ = IntPtr.Zero;
1289                         }
1290                 }
1291
1292                 [DllImport (MPH, SetLastError=true,
1293                                 EntryPoint="Mono_Posix_Syscall_getpwnam")]
1294                 private static extern int sys_getpwnam (string name, out _Passwd passwd);
1295
1296                 public static Passwd getpwnam (string name)
1297                 {
1298                         _Passwd passwd;
1299                         int r = sys_getpwnam (name, out passwd);
1300                         if (r != 0)
1301                                 return null;
1302                         Passwd pw = new Passwd ();
1303                         CopyPasswd (pw, ref passwd);
1304                         return pw;
1305                 }
1306
1307                 // getpwuid(3)
1308                 //    struct passwd *getpwnuid(uid_t uid);
1309                 [DllImport (MPH, SetLastError=true,
1310                                 EntryPoint="Mono_Posix_Syscall_getpwuid")]
1311                 private static extern int sys_getpwuid (uint uid, out _Passwd passwd);
1312
1313                 public static Passwd getpwuid (uint uid)
1314                 {
1315                         _Passwd passwd;
1316                         int r = sys_getpwuid (uid, out passwd);
1317                         if (r != 0)
1318                                 return null;
1319                         Passwd pw = new Passwd ();
1320                         CopyPasswd (pw, ref passwd);
1321                         return pw;
1322                 }
1323
1324                 [DllImport (MPH, SetLastError=true,
1325                                 EntryPoint="Mono_Posix_Syscall_getpwnam_r")]
1326                 private static extern int sys_getpwnam_r (string name, out _Passwd pwbuf, out IntPtr pwbufp);
1327
1328                 public static int getpwnam_r (string name, Passwd pwbuf, out Passwd pwbufp)
1329                 {
1330                         pwbufp = null;
1331                         _Passwd passwd;
1332                         IntPtr _pwbufp;
1333                         int r = sys_getpwnam_r (name, out passwd, out _pwbufp);
1334                         if (r == 0 && _pwbufp != IntPtr.Zero) {
1335                                 CopyPasswd (pwbuf, ref passwd);
1336                                 pwbufp = pwbuf;
1337                         }
1338                         return r;
1339                 }
1340
1341                 // getpwuid_r(3)
1342                 //    int getpwuid_r(uid_t uid, struct passwd *pwbuf, char *buf, size_t
1343                 //        buflen, struct passwd **pwbufp);
1344                 [DllImport (MPH, SetLastError=true,
1345                                 EntryPoint="Mono_Posix_Syscall_getpwuid_r")]
1346                 private static extern int sys_getpwuid_r (uint uid, out _Passwd pwbuf, out IntPtr pwbufp);
1347
1348                 public static int getpwuid_r (uint uid, Passwd pwbuf, out Passwd pwbufp)
1349                 {
1350                         pwbufp = null;
1351                         _Passwd passwd;
1352                         IntPtr _pwbufp;
1353                         int r = sys_getpwuid_r (uid, out passwd, out _pwbufp);
1354                         if (r == 0 && _pwbufp != IntPtr.Zero) {
1355                                 CopyPasswd (pwbuf, ref passwd);
1356                                 pwbufp = pwbuf;
1357                         }
1358                         return r;
1359                 }
1360
1361                 [DllImport (MPH, SetLastError=true,
1362                                 EntryPoint="Mono_Posix_Syscall_getpwent")]
1363                 private static extern int sys_getpwent (out _Passwd pwbuf);
1364
1365                 public static Passwd getpwent ()
1366                 {
1367                         _Passwd passwd;
1368                         int r = sys_getpwent (out passwd);
1369                         if (r != 0)
1370                                 return null;
1371                         Passwd pw = new Passwd ();
1372                         CopyPasswd (pw, ref passwd);
1373                         return pw;
1374                 }
1375
1376                 [DllImport (LIBC, SetLastError=true)]
1377                 public static extern void setpwent ();
1378
1379                 [DllImport (LIBC, SetLastError=true)]
1380                 public static extern void endpwent ();
1381
1382                 [DllImport (MPH, SetLastError=true,
1383                                 EntryPoint="Mono_Posix_Syscall_fgetpwent")]
1384                 private static extern int sys_fgetpwent (IntPtr stream, out _Passwd pwbuf);
1385
1386                 public static Passwd fgetpwent (IntPtr stream)
1387                 {
1388                         _Passwd passwd;
1389                         int r = sys_fgetpwent (stream, out passwd);
1390                         if (r != 0)
1391                                 return null;
1392                         Passwd pw = new Passwd ();
1393                         CopyPasswd (pw, ref passwd);
1394                         return pw;
1395                 }
1396                 #endregion
1397
1398                 #region <signal.h> Declarations
1399                 //
1400                 // <signal.h>
1401                 //
1402                 [DllImport (LIBC, SetLastError=true)]
1403                 private static extern void psignal (int sig, string s);
1404
1405                 public static void psignal (Signum sig, string s)
1406                 {
1407                         int signum = UnixConvert.FromSignum (sig);
1408                         psignal (signum, s);
1409                 }
1410
1411                 // kill(2)
1412                 //    int kill(pid_t pid, int sig);
1413                 [DllImport (LIBC, SetLastError=true, EntryPoint="kill")]
1414                 private static extern int sys_kill (int pid, int sig);
1415
1416                 public static int kill (int pid, Signum sig)
1417                 {
1418                         int _sig = UnixConvert.FromSignum (sig);
1419                         return sys_kill (pid, _sig);
1420                 }
1421
1422                 [DllImport (LIBC, SetLastError=true, EntryPoint="strsignal")]
1423                 private static extern IntPtr sys_strsignal (int sig);
1424
1425                 public static IntPtr sys_strsignal (Signum sig)
1426                 {
1427                         int s = UnixConvert.FromSignum (sig);
1428                         return sys_strsignal (s);
1429                 }
1430
1431                 public static string strsignal (Signum sig)
1432                 {
1433                         IntPtr r = sys_strsignal (sig);
1434                         return UnixMarshal.PtrToString (r);
1435                 }
1436
1437                 // TODO: sigaction(2)
1438                 // TODO: sigsuspend(2)
1439                 // TODO: sigpending(2)
1440
1441                 #endregion
1442
1443                 #region <stdio.h> Declarations
1444                 //
1445                 // <stdio.h>
1446                 //
1447                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_ctermid")]
1448                 private static extern int _L_ctermid ();
1449
1450                 public static readonly int L_ctermid = _L_ctermid ();
1451
1452                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_L_cuserid")]
1453                 private static extern int _L_cuserid ();
1454
1455                 public static readonly int L_cuserid = _L_cuserid ();
1456
1457                 [DllImport (LIBC, SetLastError=true, EntryPoint="cuserid")]
1458                 private static extern IntPtr sys_cuserid ([Out] StringBuilder @string);
1459
1460                 [Obsolete ("\"Nobody knows precisely what cuserid() does... DO NOT USE cuserid()." +
1461                                 "`string' must hold L_cuserid characters.  Use Unistd.getlogin_r instead.")]
1462                 public static string cuserid (StringBuilder @string)
1463                 {
1464                         if (@string.Capacity < L_cuserid) {
1465                                 throw new ArgumentOutOfRangeException ("string", "string.Capacity < L_cuserid");
1466                         }
1467                         IntPtr r = sys_cuserid (@string);
1468                         return UnixMarshal.PtrToString (r);
1469                 }
1470
1471                 #endregion
1472
1473                 #region <stdlib.h> Declarations
1474                 //
1475                 // <stdlib.h>
1476                 //
1477                 [DllImport (LIBC, SetLastError=true)]
1478                 public static extern int ttyslot ();
1479
1480                 [DllImport (CRYPT, SetLastError=true)]
1481                 public static extern void setkey (string key);
1482
1483                 #endregion
1484
1485                 #region <string.h> Declarations
1486                 //
1487                 // <string.h>
1488                 //
1489
1490                 // strerror_r(3)
1491                 //    int strerror_r(int errnum, char *buf, size_t n);
1492                 [DllImport (LIBC, SetLastError=true, 
1493                                 EntryPoint="Mono_Posix_Syscall_strerror_r")]
1494                 private static extern int sys_strerror_r (int errnum, 
1495                                 [Out] StringBuilder buf, ulong n);
1496
1497                 public static int strerror_r (Error errnum, StringBuilder buf, ulong n)
1498                 {
1499                         int e = UnixConvert.FromError (errnum);
1500                         return sys_strerror_r (e, buf, n);
1501                 }
1502
1503                 public static int strerror_r (Error errnum, StringBuilder buf)
1504                 {
1505                         return strerror_r (errnum, buf, (ulong) buf.Capacity);
1506                 }
1507
1508                 #endregion
1509
1510                 #region <sys/mman.h> Declarations
1511                 //
1512                 // <sys/mman.h>
1513                 //
1514
1515                 // posix_madvise(P)
1516                 //    int posix_madvise(void *addr, size_t len, int advice);
1517                 [DllImport (MPH, SetLastError=true, 
1518                                 EntryPoint="Mono_Posix_Syscall_posix_madvise")]
1519                 public static extern int posix_madvise (IntPtr addr, ulong len, 
1520                         PosixMadviseAdvice advice);
1521
1522                 #endregion
1523
1524                 #region <sys/poll.h> Declarations
1525                 //
1526                 // <sys/poll.h> -- COMPLETE
1527                 //
1528
1529                 private struct _pollfd {
1530                         public int fd;
1531                         public short events;
1532                         public short revents;
1533                 }
1534
1535                 [DllImport (LIBC, SetLastError=true, EntryPoint="poll")]
1536                 private static extern int sys_poll (_pollfd[] ufds, uint nfds, int timeout);
1537
1538                 public static int poll (Pollfd [] fds, uint nfds, int timeout)
1539                 {
1540                         if (fds.Length < nfds)
1541                                 throw new ArgumentOutOfRangeException ("fds", "Must refer to at least `nfds' elements");
1542
1543                         _pollfd[] send = new _pollfd[nfds];
1544
1545                         for (int i = 0; i < send.Length; i++) {
1546                                 send [i].fd     = fds [i].fd;
1547                                 send [i].events = UnixConvert.FromPollEvents (fds [i].events);
1548                         }
1549
1550                         int r = sys_poll (send, nfds, timeout);
1551
1552                         for (int i = 0; i < send.Length; i++) {
1553                                 fds [i].revents = UnixConvert.ToPollEvents (send [i].revents);
1554                         }
1555
1556                         return r;
1557                 }
1558
1559                 public static int poll (Pollfd [] fds, int timeout)
1560                 {
1561                         return poll (fds, (uint) fds.Length, timeout);
1562                 }
1563
1564                 //
1565                 // <sys/ptrace.h>
1566                 //
1567
1568                 // TODO: ptrace(2)
1569
1570                 //
1571                 // <sys/resource.h>
1572                 //
1573
1574                 // TODO: setrlimit(2)
1575                 // TODO: getrlimit(2)
1576                 // TODO: getrusage(2)
1577
1578                 #endregion
1579
1580                 #region <sys/sendfile.h> Declarations
1581                 //
1582                 // <sys/sendfile.h> -- COMPLETE
1583                 //
1584
1585                 [DllImport (MPH, SetLastError=true,
1586                                 EntryPoint="Mono_Posix_Syscall_sendfile")]
1587                 public static extern long sendfile (int out_fd, int in_fd, 
1588                                 ref long offset, ulong count);
1589
1590                 #endregion
1591
1592                 #region <sys/stat.h> Declarations
1593                 //
1594                 // <sys/stat.h>  -- COMPLETE
1595                 //
1596                 [DllImport (MPH, SetLastError=true, 
1597                                 EntryPoint="Mono_Posix_Syscall_stat")]
1598                 public static extern int stat (string file_name, out Stat buf);
1599
1600                 [DllImport (MPH, SetLastError=true, 
1601                                 EntryPoint="Mono_Posix_Syscall_fstat")]
1602                 public static extern int fstat (int filedes, out Stat buf);
1603
1604                 [DllImport (MPH, SetLastError=true, 
1605                                 EntryPoint="Mono_Posix_Syscall_lstat")]
1606                 public static extern int lstat (string file_name, out Stat buf);
1607
1608                 // chmod(2)
1609                 //    int chmod(const char *path, mode_t mode);
1610                 [DllImport (LIBC, SetLastError=true, EntryPoint="chmod")]
1611                 private static extern int sys_chmod (string path, uint mode);
1612
1613                 public static int chmod (string path, FilePermissions mode)
1614                 {
1615                         uint _mode = UnixConvert.FromFilePermissions (mode);
1616                         return sys_chmod (path, _mode);
1617                 }
1618
1619                 // fchmod(2)
1620                 //    int chmod(int filedes, mode_t mode);
1621                 [DllImport (LIBC, SetLastError=true, EntryPoint="fchmod")]
1622                 private static extern int sys_fchmod (int filedes, uint mode);
1623
1624                 public static int fchmod (int filedes, FilePermissions mode)
1625                 {
1626                         uint _mode = UnixConvert.FromFilePermissions (mode);
1627                         return sys_fchmod (filedes, _mode);
1628                 }
1629
1630                 // umask(2)
1631                 //    mode_t umask(mode_t mask);
1632                 [DllImport (LIBC, SetLastError=true, EntryPoint="umask")]
1633                 private static extern uint sys_umask (uint mask);
1634
1635                 public static FilePermissions umask (FilePermissions mask)
1636                 {
1637                         uint _mask = UnixConvert.FromFilePermissions (mask);
1638                         uint r = sys_umask (_mask);
1639                         return UnixConvert.ToFilePermissions (r);
1640                 }
1641
1642                 // mkdir(2)
1643                 //    int mkdir(const char *pathname, mode_t mode);
1644                 [DllImport (LIBC, SetLastError=true, EntryPoint="mkdir")]
1645                 public static extern int sys_mkdir (string oldpath, uint mode);
1646
1647                 public static int mkdir (string oldpath, FilePermissions mode)
1648                 {
1649                         uint _mode = UnixConvert.FromFilePermissions (mode);
1650                         return sys_mkdir (oldpath, _mode);
1651                 }
1652
1653                 // mknod(2)
1654                 //    int mknod (const char *pathname, mode_t mode, dev_t dev);
1655                 [DllImport (MPH, SetLastError=true,
1656                                 EntryPoint="Mono_Posix_Syscall_mknod")]
1657                 public static extern int mknod (string pathname, FilePermissions mode, ulong dev);
1658
1659                 // mkfifo(3)
1660                 //    int mkfifo(const char *pathname, mode_t mode);
1661                 [DllImport (LIBC, SetLastError=true, EntryPoint="mkfifo")]
1662                 private static extern int sys_mkfifo (string pathname, uint mode);
1663
1664                 public static int mkfifo (string pathname, FilePermissions mode)
1665                 {
1666                         uint _mode = UnixConvert.FromFilePermissions (mode);
1667                         return sys_mkfifo (pathname, _mode);
1668                 }
1669
1670                 #endregion
1671
1672                 #region <sys/time.h> Declarations
1673                 //
1674                 // <sys/time.h>
1675                 //
1676                 // TODO: adjtime(), getitimer(2), setitimer(2), lutimes(), futimes()
1677
1678                 [DllImport (MPH, SetLastError=true, 
1679                                 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
1680                 public static extern int gettimeofday (out Timeval tv, out Timezone tz);
1681
1682                 [DllImport (MPH, SetLastError=true,
1683                                 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
1684                 private static extern int gettimeofday (out Timeval tv, IntPtr ignore);
1685
1686                 public static int gettimeofday (out Timeval tv)
1687                 {
1688                         return gettimeofday (out tv, IntPtr.Zero);
1689                 }
1690
1691                 [DllImport (MPH, SetLastError=true,
1692                                 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
1693                 private static extern int gettimeofday (IntPtr ignore, out Timezone tz);
1694
1695                 public static int gettimeofday (out Timezone tz)
1696                 {
1697                         return gettimeofday (IntPtr.Zero, out tz);
1698                 }
1699
1700                 [DllImport (MPH, SetLastError=true,
1701                                 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
1702                 public static extern int settimeofday (ref Timeval tv, ref Timezone tz);
1703
1704                 [DllImport (MPH, SetLastError=true,
1705                                 EntryPoint="Mono_Posix_Syscall_gettimeofday")]
1706                 private static extern int settimeofday (ref Timeval tv, IntPtr ignore);
1707
1708                 public static int settimeofday (ref Timeval tv)
1709                 {
1710                         return settimeofday (ref tv, IntPtr.Zero);
1711                 }
1712
1713                 [DllImport (MPH, SetLastError=true, 
1714                                 EntryPoint="Mono_Posix_Syscall_utimes")]
1715                 public static extern int utimes (string filename, ref Timeval tvp);
1716
1717                 #endregion
1718
1719                 //
1720                 // <sys/timeb.h>
1721                 //
1722
1723                 // TODO: ftime(3)
1724
1725                 //
1726                 // <sys/times.h>
1727                 //
1728
1729                 // TODO: times(2)
1730
1731                 #region <sys/wait.h> Declarations
1732                 //
1733                 // <sys/wait.h>
1734                 //
1735
1736                 // wait(2)
1737                 //    pid_t wait(int *status);
1738                 [DllImport (LIBC, SetLastError=true)]
1739                 public static extern int wait (out int status);
1740
1741                 // waitpid(2)
1742                 //    pid_t waitpid(pid_t pid, int *status, int options);
1743                 [DllImport (LIBC, SetLastError=true)]
1744                 private static extern int waitpid (int pid, out int status, int options);
1745
1746                 public static int waitpid (int pid, out int status, WaitOptions options)
1747                 {
1748                         int _options = UnixConvert.FromWaitOptions (options);
1749                         return waitpid (pid, out status, _options);
1750                 }
1751
1752                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFEXITED")]
1753                 private static extern int _WIFEXITED (int status);
1754
1755                 public static bool WIFEXITED (int status)
1756                 {
1757                         return _WIFEXITED (status) != 0;
1758                 }
1759
1760                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WEXITSTATUS")]
1761                 public static extern int WEXITSTATUS (int status);
1762
1763                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSIGNALED")]
1764                 private static extern int _WIFSIGNALED (int status);
1765
1766                 public static bool WIFSIGNALED (int status)
1767                 {
1768                         return _WIFSIGNALED (status) != 0;
1769                 }
1770
1771                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WTERMSIG")]
1772                 private static extern int _WTERMSIG (int status);
1773
1774                 public static Signum WTERMSIG (int status)
1775                 {
1776                         int r = _WTERMSIG (status);
1777                         return UnixConvert.ToSignum (r);
1778                 }
1779
1780                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WIFSTOPPED")]
1781                 private static extern int _WIFSTOPPED (int status);
1782
1783                 public static bool WIFSTOPPED (int status)
1784                 {
1785                         return _WIFSTOPPED (status) != 0;
1786                 }
1787
1788                 [DllImport (MPH, EntryPoint="Mono_Posix_Syscall_WSTOPSIG")]
1789                 private static extern int _WSTOPSIG (int status);
1790
1791                 public static Signum WSTOPSIG (int status)
1792                 {
1793                         int r = _WSTOPSIG (status);
1794                         return UnixConvert.ToSignum (r);
1795                 }
1796
1797                 //
1798                 // <termios.h>
1799                 //
1800
1801                 #endregion
1802
1803                 #region <time.h> Declarations
1804                 //
1805                 // <time.h>
1806                 //
1807
1808                 // stime(2)
1809                 //    int stime(time_t *t);
1810                 [DllImport (MPH, SetLastError=true,
1811                                 EntryPoint="Mono_Posix_Syscall_stime")]
1812                 public static extern int stime (ref long t);
1813
1814                 // time(2)
1815                 //    time_t time(time_t *t);
1816                 [DllImport (MPH, SetLastError=true,
1817                                 EntryPoint="Mono_Posix_Syscall_time")]
1818                 public static extern long time (out long t);
1819
1820                 //
1821                 // <ulimit.h>
1822                 //
1823
1824                 // TODO: ulimit(3)
1825
1826                 #endregion
1827
1828                 #region <unistd.h> Declarations
1829                 //
1830                 // <unistd.h>
1831                 //
1832                 // TODO: euidaccess(), usleep(3), get_current_dir_name(), group_member(),
1833                 //       other TODOs listed below.
1834
1835                 [DllImport (LIBC, SetLastError=true, EntryPoint="access")]
1836                 private static extern int sys_access (string pathname, int mode);
1837
1838                 public static int access (string pathname, AccessMode mode)
1839                 {
1840                         int _mode = UnixConvert.FromAccessMode (mode);
1841                         return sys_access (pathname, _mode);
1842                 }
1843
1844                 // lseek(2)
1845                 //    off_t lseek(int filedes, off_t offset, int whence);
1846                 [DllImport (MPH, SetLastError=true, 
1847                                 EntryPoint="Mono_Posix_Syscall_lseek")]
1848                 public static extern long lseek (int fd, long offset, SeekFlags whence);
1849
1850     [DllImport (LIBC, SetLastError=true)]
1851                 public static extern int close (int fd);
1852
1853                 // read(2)
1854                 //    ssize_t read(int fd, void *buf, size_t count);
1855                 [DllImport (MPH, SetLastError=true, 
1856                                 EntryPoint="Mono_Posix_Syscall_read")]
1857                 public static extern long read (int fd, IntPtr buf, ulong count);
1858
1859                 public static unsafe long read (int fd, void *buf, ulong count)
1860                 {
1861                         return read (fd, (IntPtr) buf, count);
1862                 }
1863
1864                 // write(2)
1865                 //    ssize_t write(int fd, const void *buf, size_t count);
1866                 [DllImport (MPH, SetLastError=true, 
1867                                 EntryPoint="Mono_Posix_Syscall_write")]
1868                 public static extern long write (int fd, IntPtr buf, ulong count);
1869
1870                 public static unsafe long write (int fd, void *buf, ulong count)
1871                 {
1872                         return write (fd, (IntPtr) buf, count);
1873                 }
1874
1875                 // pread(2)
1876                 //    ssize_t pread(int fd, void *buf, size_t count, off_t offset);
1877                 [DllImport (MPH, SetLastError=true, 
1878                                 EntryPoint="Mono_Posix_Syscall_pread")]
1879                 public static extern long pread (int fd, IntPtr buf, ulong count, long offset);
1880
1881                 public static unsafe long pread (int fd, void *buf, ulong count, long offset)
1882                 {
1883                         return pread (fd, (IntPtr) buf, count, offset);
1884                 }
1885
1886                 // pwrite(2)
1887                 //    ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
1888                 [DllImport (MPH, SetLastError=true, 
1889                                 EntryPoint="Mono_Posix_Syscall_pwrite")]
1890                 public static extern long pwrite (int fd, IntPtr buf, ulong count, long offset);
1891
1892                 public static unsafe long pwrite (int fd, void *buf, ulong count, long offset)
1893                 {
1894                         return pwrite (fd, (IntPtr) buf, count, offset);
1895                 }
1896
1897                 [DllImport (MPH, SetLastError=true, 
1898                                 EntryPoint="Mono_Posix_Syscall_pipe")]
1899                 public static extern int pipe (out int reading, out int writing);
1900
1901                 public static int pipe (int[] filedes)
1902                 {
1903                         if (filedes == null || filedes.Length != 2) {
1904                                 // TODO: set errno
1905                                 return -1;
1906                         }
1907                         int reading, writing;
1908                         int r = pipe (out reading, out writing);
1909                         filedes[0] = reading;
1910                         filedes[1] = writing;
1911                         return r;
1912                 }
1913
1914                 [DllImport (LIBC, SetLastError=true)]
1915                 public static extern uint alarm (uint seconds);
1916
1917                 [DllImport (LIBC, SetLastError=true)]
1918                 public static extern uint sleep (uint seconds);
1919
1920                 [DllImport (LIBC, SetLastError=true)]
1921                 public static extern uint ualarm (uint usecs, uint interval);
1922
1923                 [DllImport (LIBC, SetLastError=true)]
1924                 public static extern int pause ();
1925
1926                 // chown(2)
1927                 //    int chown(const char *path, uid_t owner, gid_t group);
1928                 [DllImport (LIBC, SetLastError=true)]
1929                 public static extern int chown (string path, uint owner, uint group);
1930
1931                 // fchown(2)
1932                 //    int fchown(int fd, uid_t owner, gid_t group);
1933                 [DllImport (LIBC, SetLastError=true)]
1934                 public static extern int fchown (int fd, uint owner, uint group);
1935
1936                 // lchown(2)
1937                 //    int lchown(const char *path, uid_t owner, gid_t group);
1938                 [DllImport (LIBC, SetLastError=true)]
1939                 public static extern int lchown (string path, uint owner, uint group);
1940
1941                 [DllImport (LIBC, SetLastError=true)]
1942                 public static extern int chdir (string path);
1943
1944                 [DllImport (LIBC, SetLastError=true)]
1945                 public static extern int fchdir (int fd);
1946
1947                 // getcwd(3)
1948                 //    char *getcwd(char *buf, size_t size);
1949                 [DllImport (MPH, SetLastError=true,
1950                                 EntryPoint="Mono_Posix_Syscall_getcwd")]
1951                 public static extern IntPtr getcwd ([Out] StringBuilder buf, ulong size);
1952
1953                 public static StringBuilder getcwd (StringBuilder buf)
1954                 {
1955                         getcwd (buf, (ulong) buf.Capacity);
1956                         return buf;
1957                 }
1958
1959                 // getwd(2) is deprecated; don't expose it.
1960
1961                 [DllImport (LIBC, SetLastError=true)]
1962                 public static extern int dup (int fd);
1963
1964                 [DllImport (LIBC, SetLastError=true)]
1965                 public static extern int dup2 (int fd, int fd2);
1966
1967                 // TODO: does Mono marshal arrays properly?
1968                 [DllImport (LIBC, SetLastError=true)]
1969                 public static extern int execve (string path, string[] argv, string[] envp);
1970
1971                 [DllImport (LIBC, SetLastError=true)]
1972                 public static extern int fexecve (int fd, string[] argv, string[] envp);
1973
1974                 [DllImport (LIBC, SetLastError=true)]
1975                 public static extern int execv (string path, string[] argv);
1976
1977                 // TODO: execle, execl, execlp
1978                 [DllImport (LIBC, SetLastError=true)]
1979                 public static extern int execvp (string path, string[] argv);
1980
1981                 [DllImport (LIBC, SetLastError=true)]
1982                 public static extern int nice (int inc);
1983
1984                 [DllImport (LIBC, SetLastError=true)]
1985                 public static extern int _exit (int status);
1986
1987                 [DllImport (MPH, SetLastError=true,
1988                                 EntryPoint="Mono_Posix_Syscall_fpathconf")]
1989                 public static extern long fpathconf (int filedes, PathConf name);
1990
1991                 [DllImport (MPH, SetLastError=true,
1992                                 EntryPoint="Mono_Posix_Syscall_pathconf")]
1993                 public static extern long pathconf (string path, PathConf name);
1994
1995                 [DllImport (MPH, SetLastError=true,
1996                                 EntryPoint="Mono_Posix_Syscall_sysconf")]
1997                 public static extern long sysconf (SysConf name);
1998
1999                 // confstr(3)
2000                 //    size_t confstr(int name, char *buf, size_t len);
2001                 [DllImport (MPH, SetLastError=true,
2002                                 EntryPoint="Mono_Posix_Syscall_confstr")]
2003                 public static extern ulong confstr (ConfStr name, [Out] StringBuilder buf, ulong len);
2004
2005                 // getpid(2)
2006                 //    pid_t getpid(void);
2007                 [DllImport (LIBC, SetLastError=true)]
2008                 public static extern int getpid ();
2009
2010                 // getppid(2)
2011                 //    pid_t getppid(void);
2012                 [DllImport (LIBC, SetLastError=true)]
2013                 public static extern int getppid ();
2014
2015                 // setpgid(2)
2016                 //    int setpgid(pid_t pid, pid_t pgid);
2017                 [DllImport (LIBC, SetLastError=true)]
2018                 public static extern int setpgid (int pid, int pgid);
2019
2020                 // getpgid(2)
2021                 //    pid_t getpgid(pid_t pid);
2022                 [DllImport (LIBC, SetLastError=true)]
2023                 public static extern int getpgid (int pid);
2024
2025                 [DllImport (LIBC, SetLastError=true)]
2026                 public static extern int setpgrp ();
2027
2028                 // getpgrp(2)
2029                 //    pid_t getpgrp(void);
2030                 [DllImport (LIBC, SetLastError=true)]
2031                 public static extern int getpgrp ();
2032
2033                 // setsid(2)
2034                 //    pid_t setsid(void);
2035                 [DllImport (LIBC, SetLastError=true)]
2036                 public static extern int setsid ();
2037
2038                 // getsid(2)
2039                 //    pid_t getsid(pid_t pid);
2040                 [DllImport (LIBC, SetLastError=true)]
2041                 public static extern int getsid (int pid);
2042
2043                 // getuid(2)
2044                 //    uid_t getuid(void);
2045                 [DllImport (LIBC, SetLastError=true)]
2046                 public static extern uint getuid ();
2047
2048                 // geteuid(2)
2049                 //    uid_t geteuid(void);
2050                 [DllImport (LIBC, SetLastError=true)]
2051                 public static extern uint geteuid ();
2052
2053                 // getgid(2)
2054                 //    gid_t getgid(void);
2055                 [DllImport (LIBC, SetLastError=true)]
2056                 public static extern uint getgid ();
2057
2058                 // getegid(2)
2059                 //    gid_t getgid(void);
2060                 [DllImport (LIBC, SetLastError=true)]
2061                 public static extern uint getegid ();
2062
2063                 // getgroups(2)
2064                 //    int getgroups(int size, gid_t list[]);
2065                 [DllImport (LIBC, SetLastError=true)]
2066                 public static extern int getgroups (int size, uint[] list);
2067
2068                 public static int getgroups (uint[] list)
2069                 {
2070                         return getgroups (list.Length, list);
2071                 }
2072
2073                 // setuid(2)
2074                 //    int setuid(uid_t uid);
2075                 [DllImport (LIBC, SetLastError=true)]
2076                 public static extern int setuid (uint uid);
2077
2078                 // setreuid(2)
2079                 //    int setreuid(uid_t ruid, uid_t euid);
2080                 [DllImport (LIBC, SetLastError=true)]
2081                 public static extern int setreuid (uint ruid, uint euid);
2082
2083                 // setregid(2)
2084                 //    int setregid(gid_t ruid, gid_t euid);
2085                 [DllImport (LIBC, SetLastError=true)]
2086                 public static extern int setregid (uint rgid, uint egid);
2087
2088                 // seteuid(2)
2089                 //    int seteuid(uid_t euid);
2090                 [DllImport (LIBC, SetLastError=true)]
2091                 public static extern int seteuid (uint euid);
2092
2093                 // setegid(2)
2094                 //    int setegid(gid_t euid);
2095                 [DllImport (LIBC, SetLastError=true)]
2096                 public static extern int setegid (uint uid);
2097
2098                 // setgid(2)
2099                 //    int setgid(gid_t gid);
2100                 [DllImport (LIBC, SetLastError=true)]
2101                 public static extern int setgid (uint gid);
2102
2103                 // getresuid(2)
2104                 //    int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
2105                 [DllImport (LIBC, SetLastError=true)]
2106                 public static extern int getresuid (out uint ruid, out uint euid, out uint suid);
2107
2108                 // getresgid(2)
2109                 //    int getresgid(gid_t *ruid, gid_t *euid, gid_t *suid);
2110                 [DllImport (LIBC, SetLastError=true)]
2111                 public static extern int getresgid (out uint rgid, out uint egid, out uint sgid);
2112
2113                 // setresuid(2)
2114                 //    int setresuid(uid_t ruid, uid_t euid, uid_t suid);
2115                 [DllImport (LIBC, SetLastError=true)]
2116                 public static extern int setresuid (uint ruid, uint euid, uint suid);
2117
2118                 // setresgid(2)
2119                 //    int setresgid(gid_t ruid, gid_t euid, gid_t suid);
2120                 [DllImport (LIBC, SetLastError=true)]
2121                 public static extern int setresgid (uint rgid, uint egid, uint sgid);
2122
2123                 // fork(2)
2124                 //    pid_t fork(void);
2125                 [DllImport (LIBC, SetLastError=true)]
2126                 [Obsolete ("DO NOT directly call fork(2); it bypasses essential " + 
2127                                 "shutdown code.\nUse System.Diagnostics.Process instead")]
2128                 public static extern int fork ();
2129
2130                 // vfork(2)
2131                 //    pid_t vfork(void);
2132                 [DllImport (LIBC, SetLastError=true)]
2133                 [Obsolete ("DO NOT directly call vfork(2); it bypasses essential " + 
2134                                 "shutdown code.\nUse System.Diagnostics.Process instead")]
2135                 public static extern int vfork ();
2136
2137                 [DllImport (LIBC, SetLastError=true, EntryPoint="ttyname")]
2138                 [Obsolete ("Not re-entrant.  Use ttyname_r instead.")]
2139                 public static extern IntPtr sys_ttyname (int fd);
2140
2141                 [Obsolete ("Not re-entrant.  Use ttyname_r instead.")]
2142                 public static string ttyname (int fd)
2143                 {
2144                         IntPtr r = sys_ttyname (fd);
2145                         return UnixMarshal.PtrToString (r);
2146                 }
2147
2148                 // ttyname_r(3)
2149                 //    int ttyname_r(int fd, char *buf, size_t buflen);
2150                 [DllImport (MPH, SetLastError=true,
2151                                 EntryPoint="Mono_Posix_Syscall_ttyname_r")]
2152                 public static extern int ttyname_r (int fd, [Out] StringBuilder buf, ulong buflen);
2153
2154                 public static int ttyname_r (int fd, StringBuilder buf)
2155                 {
2156                         return ttyname_r (fd, buf, (ulong) buf.Capacity);
2157                 }
2158
2159                 [DllImport (LIBC, EntryPoint="isatty")]
2160                 private static extern int sys_isatty (int fd);
2161
2162                 public static bool isatty (int fd)
2163                 {
2164                         return sys_isatty (fd) == 1;
2165                 }
2166
2167                 [DllImport (LIBC, SetLastError=true)]
2168                 public static extern int link (string oldpath, string newpath);
2169
2170                 [DllImport (LIBC, SetLastError=true)]
2171                 public static extern int symlink (string oldpath, string newpath);
2172
2173                 // readlink(2)
2174                 //    int readlink(const char *path, char *buf, size_t bufsize);
2175                 [DllImport (MPH, SetLastError=true,
2176                                 EntryPoint="Mono_Posix_Syscall_readlink")]
2177                 public static extern int readlink (string path, [Out] StringBuilder buf, ulong bufsiz);
2178
2179                 public static int readlink (string path, [Out] StringBuilder buf)
2180                 {
2181                         return readlink (path, buf, (ulong) buf.Capacity);
2182                 }
2183
2184                 [DllImport (LIBC, SetLastError=true)]
2185                 public static extern int unlink (string pathname);
2186
2187                 [DllImport (LIBC, SetLastError=true)]
2188                 public static extern int rmdir (string pathname);
2189
2190                 // tcgetpgrp(3)
2191                 //    pid_t tcgetpgrp(int fd);
2192                 [DllImport (LIBC, SetLastError=true)]
2193                 public static extern int tcgetpgrp (int fd);
2194
2195                 // tcsetpgrp(3)
2196                 //    int tcsetpgrp(int fd, pid_t pgrp);
2197                 [DllImport (LIBC, SetLastError=true)]
2198                 public static extern int tcsetpgrp (int fd, int pgrp);
2199
2200                 [DllImport (LIBC, SetLastError=true, EntryPoint="getlogin")]
2201                 [Obsolete ("Not re-entrant.  Use getlogin_r instead.")]
2202                 public static extern IntPtr sys_getlogin ();
2203
2204                 [Obsolete ("Not re-entrant.  Use getlogin_r instead.")]
2205                 public static string getlogin ()
2206                 {
2207                         IntPtr r = sys_getlogin ();
2208                         return UnixMarshal.PtrToString (r);
2209                 }
2210
2211                 // getlogin_r(3)
2212                 //    int getlogin_r(char *buf, size_t bufsize);
2213                 [DllImport (MPH, SetLastError=true,
2214                                 EntryPoint="Mono_Posix_Syscall_getlogin_r")]
2215                 public static extern int getlogin_r ([Out] StringBuilder name, ulong bufsize);
2216
2217                 public static int getlogin_r (StringBuilder name)
2218                 {
2219                         return getlogin_r (name, (ulong) name.Capacity);
2220                 }
2221
2222                 [DllImport (LIBC, SetLastError=true)]
2223                 public static extern int setlogin (string name);
2224
2225                 // gethostname(2)
2226                 //    int gethostname(char *name, size_t len);
2227                 [DllImport (MPH, SetLastError=true,
2228                                 EntryPoint="Mono_Posix_Syscall_gethostname")]
2229                 public static extern int gethostname ([Out] StringBuilder name, ulong len);
2230
2231                 public static int gethostname (StringBuilder name)
2232                 {
2233                         return gethostname (name, (ulong) name.Capacity);
2234                 }
2235
2236                 // sethostname(2)
2237                 //    int gethostname(const char *name, size_t len);
2238                 [DllImport (MPH, SetLastError=true,
2239                                 EntryPoint="Mono_Posix_Syscall_gethostname")]
2240                 public static extern int sethostname (string name, ulong len);
2241
2242                 public static int sethostname (string name)
2243                 {
2244                         return sethostname (name, (ulong) name.Length);
2245                 }
2246
2247                 [DllImport (MPH, SetLastError=true,
2248                                 EntryPoint="Mono_Posix_Syscall_gethostid")]
2249                 public static extern long gethostid ();
2250
2251                 [DllImport (MPH, SetLastError=true,
2252                                 EntryPoint="Mono_Posix_Syscall_sethostid")]
2253                 public static extern int sethostid (long hostid);
2254
2255                 // getdomainname(2)
2256                 //    int getdomainname(char *name, size_t len);
2257                 [DllImport (MPH, SetLastError=true,
2258                                 EntryPoint="Mono_Posix_Syscall_getdomainname")]
2259                 public static extern int getdomainname ([Out] StringBuilder name, ulong len);
2260
2261                 public static int getdomainname (StringBuilder name)
2262                 {
2263                         return getdomainname (name, (ulong) name.Capacity);
2264                 }
2265
2266                 // setdomainname(2)
2267                 //    int setdomainname(const char *name, size_t len);
2268                 [DllImport (MPH, SetLastError=true,
2269                                 EntryPoint="Mono_Posix_Syscall_setdomainname")]
2270                 public static extern int setdomainname (string name, ulong len);
2271
2272                 public static int setdomainname (string name)
2273                 {
2274                         return setdomainname (name, (ulong) name.Length);
2275                 }
2276
2277                 [DllImport (LIBC, SetLastError=true)]
2278                 public static extern int vhangup ();
2279
2280                 // Revoke doesn't appear to be POSIX.  Include it?
2281                 [DllImport (LIBC, SetLastError=true)]
2282                 public static extern int revoke (string file);
2283
2284                 // TODO: profil?  It's not POSIX.
2285
2286                 [DllImport (LIBC, SetLastError=true)]
2287                 public static extern int acct (string filename);
2288
2289                 [DllImport (LIBC, SetLastError=true, EntryPoint="getusershell")]
2290                 public static extern IntPtr sys_getusershell ();
2291
2292                 public static string getusershell ()
2293                 {
2294                         IntPtr r = sys_getusershell ();
2295                         return UnixMarshal.PtrToString (r);
2296                 }
2297
2298                 [DllImport (LIBC, SetLastError=true)]
2299                 public static extern void setusershell ();
2300
2301                 [DllImport (LIBC, SetLastError=true)]
2302                 public static extern void endusershell ();
2303
2304                 [DllImport (LIBC, SetLastError=true)]
2305                 private static extern int daemon (int nochdir, int noclose);
2306
2307                 public static int daemon (bool nochdir, bool noclose)
2308                 {
2309                         return daemon (nochdir ? 1 : 0, noclose ? 1 : 0);
2310                 }
2311
2312                 [DllImport (LIBC, SetLastError=true)]
2313                 public static extern int chroot (string path);
2314
2315                 // skipping getpass(3) as the man page states:
2316                 //   This function is obsolete.  Do not use it.
2317
2318                 [DllImport (LIBC, SetLastError=true)]
2319                 public static extern int fsync (int fd);
2320
2321                 [DllImport (LIBC, SetLastError=true)]
2322                 public static extern int fdatasync (int fd);
2323
2324                 [DllImport (LIBC, SetLastError=true)]
2325                 public static extern void sync ();
2326
2327                 [DllImport (LIBC, SetLastError=true)]
2328                 [Obsolete ("Dropped in POSIX 1003.1-2001.  " +
2329                                 "Use Unistd.sysconf (SysConf._SC_PAGESIZE).")]
2330                 public static extern int getpagesize ();
2331
2332                 // truncate(2)
2333                 //    int truncate(const char *path, off_t length);
2334                 [DllImport (MPH, SetLastError=true, 
2335                                 EntryPoint="Mono_Posix_Syscall_truncate")]
2336                 public static extern int truncate (string path, long length);
2337
2338                 // ftruncate(2)
2339                 //    int ftruncate(int fd, off_t length);
2340                 [DllImport (MPH, SetLastError=true, 
2341                                 EntryPoint="Mono_Posix_Syscall_ftruncate")]
2342                 public static extern int ftruncate (int fd, long length);
2343
2344                 [DllImport (LIBC, SetLastError=true)]
2345                 public static extern int getdtablesize ();
2346
2347                 [DllImport (LIBC, SetLastError=true)]
2348                 public static extern int brk (IntPtr end_data_segment);
2349
2350                 [DllImport (LIBC, SetLastError=true)]
2351                 public static extern IntPtr sbrk (IntPtr increment);
2352
2353                 // TODO: syscall(2)?
2354                 // Probably safer to skip entirely.
2355
2356                 // lockf(3)
2357                 //    int lockf(int fd, int cmd, off_t len);
2358                 [DllImport (MPH, SetLastError=true, 
2359                                 EntryPoint="Mono_Posix_Syscall_lockf")]
2360                 public static extern int lockf (int fd, LockFlags cmd, long len);
2361
2362                 [DllImport (CRYPT, SetLastError=true, EntryPoint="crypt")]
2363                 public static extern IntPtr sys_crypt (string key, string salt);
2364
2365                 public static string crypt (string key, string salt)
2366                 {
2367                         IntPtr r = sys_crypt (key, salt);
2368                         return UnixMarshal.PtrToString (r);
2369                 }
2370
2371                 [DllImport (CRYPT, SetLastError=true, EntryPoint="encrypt")]
2372                 private static extern void sys_encrypt ([In, Out] byte[] block, int edflag);
2373
2374                 public static void encrypt (byte[] block, bool decode)
2375                 {
2376                         if (block.Length < 64)
2377                                 throw new ArgumentOutOfRangeException ("block", "Must refer to at least 64 bytes");
2378                         sys_encrypt (block, decode ? 1 : 0);
2379                 }
2380
2381                 // swab(3)
2382                 //    void swab(const void *from, void *to, ssize_t n);
2383                 [DllImport (MPH, SetLastError=true, 
2384                                 EntryPoint="Mono_Posix_Syscall_swab")]
2385                 public static extern void swab (IntPtr from, IntPtr to, long n);
2386
2387                 public static unsafe void swab (void* from, void* to, long n)
2388                 {
2389                         swab ((IntPtr) from, (IntPtr) to, n);
2390                 }
2391
2392                 #endregion
2393
2394                 #region <utime.h> Declarations
2395                 //
2396                 // <utime.h>  -- COMPLETE
2397                 //
2398
2399                 [DllImport (MPH, SetLastError=true, 
2400                                 EntryPoint="Mono_Posix_Syscall_utime")]
2401                 public static extern int utime (string filename, ref Utimbuf buf);
2402
2403                 [DllImport (MPH, SetLastError=true, 
2404                                 EntryPoint="Mono_Posix_Syscall_utime")]
2405                 private static extern int utime (string filename, IntPtr buf);
2406
2407                 public static int utime (string filename)
2408                 {
2409                         return utime (filename, IntPtr.Zero);
2410                 }
2411                 #endregion
2412         }
2413 }
2414
2415 // vim: noexpandtab