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