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