5 // Jonathan Pryor (jonpryor@vt.edu)
7 // (C) 2004-2005 Jonathan Pryor
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Runtime.InteropServices;
40 public enum Error : int {
41 // errors & their values liberally copied from
42 // FC2 /usr/include/asm/errno.h
44 EPERM = 1, // Operation not permitted
45 ENOENT = 2, // No such file or directory
46 ESRCH = 3, // No such process
47 EINTR = 4, // Interrupted system call
49 ENXIO = 6, // No such device or address
50 E2BIG = 7, // Arg list too long
51 ENOEXEC = 8, // Exec format error
52 EBADF = 9, // Bad file number
53 ECHILD = 10, // No child processes
54 EAGAIN = 11, // Try again
55 ENOMEM = 12, // Out of memory
56 EACCES = 13, // Permission denied
57 EFAULT = 14, // Bad address
58 ENOTBLK = 15, // Block device required
59 EBUSY = 16, // Device or resource busy
60 EEXIST = 17, // File exists
61 EXDEV = 18, // Cross-device link
62 ENODEV = 19, // No such device
63 ENOTDIR = 20, // Not a directory
64 EISDIR = 21, // Is a directory
65 EINVAL = 22, // Invalid argument
66 ENFILE = 23, // File table overflow
67 EMFILE = 24, // Too many open files
68 ENOTTY = 25, // Not a typewriter
69 ETXTBSY = 26, // Text file busy
70 EFBIG = 27, // File too large
71 ENOSPC = 28, // No space left on device
72 ESPIPE = 29, // Illegal seek
73 EROFS = 30, // Read-only file system
74 EMLINK = 31, // Too many links
75 EPIPE = 32, // Broken pipe
76 EDOM = 33, // Math argument out of domain of func
77 ERANGE = 34, // Math result not representable
78 EDEADLK = 35, // Resource deadlock would occur
79 ENAMETOOLONG = 36, // File name too long
80 ENOLCK = 37, // No record locks available
81 ENOSYS = 38, // Function not implemented
82 ENOTEMPTY = 39, // Directory not empty
83 ELOOP = 40, // Too many symbolic links encountered
84 EWOULDBLOCK = EAGAIN, // Operation would block
85 ENOMSG = 42, // No message of desired type
86 EIDRM = 43, // Identifier removed
87 ECHRNG = 44, // Channel number out of range
88 EL2NSYNC = 45, // Level 2 not synchronized
89 EL3HLT = 46, // Level 3 halted
90 EL3RST = 47, // Level 3 reset
91 ELNRNG = 48, // Link number out of range
92 EUNATCH = 49, // Protocol driver not attached
93 ENOCSI = 50, // No CSI structure available
94 EL2HLT = 51, // Level 2 halted
95 EBADE = 52, // Invalid exchange
96 EBADR = 53, // Invalid request descriptor
97 EXFULL = 54, // Exchange full
98 ENOANO = 55, // No anode
99 EBADRQC = 56, // Invalid request code
100 EBADSLT = 57, // Invalid slot
104 EBFONT = 59, // Bad font file format
105 ENOSTR = 60, // Device not a stream
106 ENODATA = 61, // No data available
107 ETIME = 62, // Timer expired
108 ENOSR = 63, // Out of streams resources
109 ENONET = 64, // Machine is not on the network
110 ENOPKG = 65, // Package not installed
111 EREMOTE = 66, // Object is remote
112 ENOLINK = 67, // Link has been severed
113 EADV = 68, // Advertise error
114 ESRMNT = 69, // Srmount error
115 ECOMM = 70, // Communication error on send
116 EPROTO = 71, // Protocol error
117 EMULTIHOP = 72, // Multihop attempted
118 EDOTDOT = 73, // RFS specific error
119 EBADMSG = 74, // Not a data message
120 EOVERFLOW = 75, // Value too large for defined data type
121 ENOTUNIQ = 76, // Name not unique on network
122 EBADFD = 77, // File descriptor in bad state
123 EREMCHG = 78, // Remote address changed
124 ELIBACC = 79, // Can not access a needed shared library
125 ELIBBAD = 80, // Accessing a corrupted shared library
126 ELIBSCN = 81, // .lib section in a.out corrupted
127 ELIBMAX = 82, // Attempting to link in too many shared libraries
128 ELIBEXEC = 83, // Cannot exec a shared library directly
129 EILSEQ = 84, // Illegal byte sequence
130 ERESTART = 85, // Interrupted system call should be restarted
131 ESTRPIPE = 86, // Streams pipe error
132 EUSERS = 87, // Too many users
133 ENOTSOCK = 88, // Socket operation on non-socket
134 EDESTADDRREQ = 89, // Destination address required
135 EMSGSIZE = 90, // Message too long
136 EPROTOTYPE = 91, // Protocol wrong type for socket
137 ENOPROTOOPT = 92, // Protocol not available
138 EPROTONOSUPPORT = 93, // Protocol not supported
139 ESOCKTNOSUPPORT = 94, // Socket type not supported
140 EOPNOTSUPP = 95, // Operation not supported on transport endpoint
141 EPFNOSUPPORT = 96, // Protocol family not supported
142 EAFNOSUPPORT = 97, // Address family not supported by protocol
143 EADDRINUSE = 98, // Address already in use
144 EADDRNOTAVAIL = 99, // Cannot assign requested address
145 ENETDOWN = 100, // Network is down
146 ENETUNREACH = 101, // Network is unreachable
147 ENETRESET = 102, // Network dropped connection because of reset
148 ECONNABORTED = 103, // Software caused connection abort
149 ECONNRESET = 104, // Connection reset by peer
150 ENOBUFS = 105, // No buffer space available
151 EISCONN = 106, // Transport endpoint is already connected
152 ENOTCONN = 107, // Transport endpoint is not connected
153 ESHUTDOWN = 108, // Cannot send after transport endpoint shutdown
154 ETOOMANYREFS = 109, // Too many references: cannot splice
155 ETIMEDOUT = 110, // Connection timed out
156 ECONNREFUSED = 111, // Connection refused
157 EHOSTDOWN = 112, // Host is down
158 EHOSTUNREACH = 113, // No route to host
159 EALREADY = 114, // Operation already in progress
160 EINPROGRESS = 115, // Operation now in progress
161 ESTALE = 116, // Stale NFS file handle
162 EUCLEAN = 117, // Structure needs cleaning
163 ENOTNAM = 118, // Not a XENIX named type file
164 ENAVAIL = 119, // No XENIX semaphores available
165 EISNAM = 120, // Is a named type file
166 EREMOTEIO = 121, // Remote I/O error
167 EDQUOT = 122, // Quota exceeded
169 ENOMEDIUM = 123, // No medium found
170 EMEDIUMTYPE = 124, // Wrong medium type
177 public sealed class FilePosition : IDisposable {
179 private static readonly int FilePositionDumpSize =
180 Stdlib.DumpFilePosition (null, new HandleRef (null, IntPtr.Zero), 0);
182 private HandleRef pos;
184 public FilePosition ()
186 IntPtr p = Stdlib.CreateFilePosition ();
187 if (p == IntPtr.Zero)
188 throw new OutOfMemoryException ("Unable to malloc fpos_t!");
189 pos = new HandleRef (this, p);
192 internal HandleRef Handle {
196 public void Dispose ()
198 GC.SuppressFinalize (this);
202 private void Cleanup ()
204 if (pos.Handle != IntPtr.Zero) {
205 Stdlib.free (pos.Handle);
206 pos = new HandleRef (this, IntPtr.Zero);
210 public override string ToString ()
212 return "(" + base.ToString () + " " + GetDump () + ")";
215 private string GetDump ()
217 if (FilePositionDumpSize <= 0)
218 return "internal error";
220 StringBuilder buf = new StringBuilder (FilePositionDumpSize+1);
222 if (Stdlib.DumpFilePosition (buf, Handle, FilePositionDumpSize+1) <= 0)
223 return "internal error dumping fpos_t";
225 return buf.ToString ();
228 public override bool Equals (object obj)
230 if (obj == null || GetType() != obj.GetType())
232 return ToString().Equals (obj.ToString());
235 public override int GetHashCode ()
237 return ToString ().GetHashCode ();
245 public static bool operator== (FilePosition lhs, FilePosition rhs)
247 return Object.Equals (lhs, rhs);
250 public static bool operator!= (FilePosition lhs, FilePosition rhs)
252 return !Object.Equals (lhs, rhs);
257 [UnmanagedFunctionPointer (CallingConvention.Cdecl)]
259 public delegate void SignalHandler (int signal);
261 internal sealed class SignalWrapper {
262 private IntPtr handler;
264 internal SignalWrapper (IntPtr handler)
266 this.handler = handler;
269 public void InvokeSignalHandler (int signum)
271 Stdlib.InvokeSignalHandler (signum, handler);
275 internal class XPrintfFunctions
277 internal delegate object XPrintf (object[] parameters);
279 internal static XPrintf printf;
280 internal static XPrintf fprintf;
281 internal static XPrintf snprintf;
282 internal static XPrintf syslog;
284 static XPrintfFunctions ()
286 CdeclFunction _printf = new CdeclFunction (Stdlib.LIBC, "printf", typeof(int));
287 printf = new XPrintf (_printf.Invoke);
289 CdeclFunction _fprintf = new CdeclFunction (Stdlib.LIBC, "fprintf", typeof(int));
290 fprintf = new XPrintf (_fprintf.Invoke);
292 CdeclFunction _snprintf = new CdeclFunction (Stdlib.MPH,
293 "Mono_Posix_Stdlib_snprintf", typeof(int));
294 snprintf = new XPrintf (_snprintf.Invoke);
296 CdeclFunction _syslog = new CdeclFunction (Syscall.LIBC, "syslog", typeof(void));
297 syslog = new XPrintf (_syslog.Invoke);
302 // Convention: Functions that are part of the C standard library go here.
304 // For example, the man page should say something similar to:
307 // ISO 9899 (''ANSI C'')
309 // The intent is that members of this class should be portable to any system
310 // supporting the C runtime (read: non-Unix, including Windows). Using
311 // anything from Syscall is non-portable, but restricting yourself to just
312 // Stdlib is intended to be portable.
316 internal const string LIBC = "msvcrt";
317 internal const string MPH = "MonoPosixHelper";
319 internal Stdlib () {}
321 #region <errno.h> Declarations
323 // <errno.h> -- COMPLETE
326 public static Error GetLastError ()
328 int errno = Marshal.GetLastWin32Error ();
329 return UnixConvert.ToError (errno);
332 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
333 EntryPoint="Mono_Posix_Stdlib_SetLastError")]
334 private static extern void SetLastError (int error);
336 public static void SetLastError (Error error)
338 int _error = UnixConvert.FromError (error);
339 SetLastError (_error);
346 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
347 EntryPoint="Mono_Posix_Syscall_InvokeSignalHandler")]
348 internal static extern void InvokeSignalHandler (int signum, IntPtr handler);
350 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
351 EntryPoint="Mono_Posix_Stdlib_SIG_DFL")]
352 private static extern IntPtr GetDefaultSignal ();
354 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
355 EntryPoint="Mono_Posix_Stdlib_SIG_ERR")]
356 private static extern IntPtr GetErrorSignal ();
358 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
359 EntryPoint="Mono_Posix_Stdlib_SIG_IGN")]
360 private static extern IntPtr GetIgnoreSignal ();
362 private static readonly IntPtr _SIG_DFL = GetDefaultSignal ();
363 private static readonly IntPtr _SIG_ERR = GetErrorSignal ();
364 private static readonly IntPtr _SIG_IGN = GetIgnoreSignal ();
366 private static void _ErrorHandler (int signum)
368 Console.Error.WriteLine ("Error handler invoked for signum " +
369 signum + ". Don't do that.");
372 private static void _DefaultHandler (int signum)
374 Console.Error.WriteLine ("Default handler invoked for signum " +
375 signum + ". Don't do that.");
378 private static void _IgnoreHandler (int signum)
380 Console.Error.WriteLine ("Ignore handler invoked for signum " +
381 signum + ". Don't do that.");
384 public static readonly SignalHandler SIG_DFL = new SignalHandler (_DefaultHandler);
385 public static readonly SignalHandler SIG_ERR = new SignalHandler (_ErrorHandler);
386 public static readonly SignalHandler SIG_IGN = new SignalHandler (_IgnoreHandler);
388 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl,
389 SetLastError=true, EntryPoint="signal")]
390 private static extern IntPtr sys_signal (int signum, SignalHandler handler);
392 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl,
393 SetLastError=true, EntryPoint="signal")]
394 private static extern IntPtr sys_signal (int signum, IntPtr handler);
396 public static SignalHandler signal (Signum signum, SignalHandler handler)
398 int _sig = UnixConvert.FromSignum (signum);
400 if (handler == SIG_DFL)
401 r = sys_signal (_sig, _SIG_DFL);
402 else if (handler == SIG_ERR)
403 r = sys_signal (_sig, _SIG_ERR);
404 else if (handler == SIG_IGN)
405 r = sys_signal (_sig, _SIG_IGN);
407 r = sys_signal (_sig, handler);
408 return TranslateHandler (r);
411 private static SignalHandler TranslateHandler (IntPtr handler)
413 if (handler == _SIG_DFL)
415 if (handler == _SIG_ERR)
417 if (handler == _SIG_IGN)
419 return new SignalHandler (new SignalWrapper (handler).InvokeSignalHandler);
422 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, EntryPoint="raise")]
423 private static extern int sys_raise (int sig);
425 public static int raise (Signum sig)
427 int _sig = UnixConvert.FromSignum (sig);
428 return sys_raise (_sig);
432 // <stdio.h> -- COMPLETE except for :
433 // - the scanf(3) family .
434 // - vararg functions.
435 // - Horribly unsafe functions (gets(3)).
437 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
438 EntryPoint="Mono_Posix_Stdlib__IOFBF")]
439 private static extern int GetFullyBuffered ();
441 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
442 EntryPoint="Mono_Posix_Stdlib__IOLBF")]
443 private static extern int GetLineBuffered ();
445 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
446 EntryPoint="Mono_Posix_Stdlib__IONBF")]
447 private static extern int GetNonBuffered ();
449 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
450 EntryPoint="Mono_Posix_Stdlib_BUFSIZ")]
451 private static extern int GetBufferSize ();
453 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
454 EntryPoint="Mono_Posix_Stdlib_CreateFilePosition")]
455 internal static extern IntPtr CreateFilePosition ();
457 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
458 EntryPoint="Mono_Posix_Stdlib_DumpFilePosition")]
459 internal static extern int DumpFilePosition (StringBuilder buf, HandleRef handle, int len);
461 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
462 EntryPoint="Mono_Posix_Stdlib_EOF")]
463 private static extern int GetEOF ();
465 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
466 EntryPoint="Mono_Posix_Stdlib_FILENAME_MAX")]
467 private static extern int GetFilenameMax ();
469 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
470 EntryPoint="Mono_Posix_Stdlib_FOPEN_MAX")]
471 private static extern int GetFopenMax ();
473 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
474 EntryPoint="Mono_Posix_Stdlib_L_tmpnam")]
475 private static extern int GetTmpnamLength ();
477 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
478 EntryPoint="Mono_Posix_Stdlib_stdin")]
479 private static extern IntPtr GetStandardInput ();
481 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
482 EntryPoint="Mono_Posix_Stdlib_stdout")]
483 private static extern IntPtr GetStandardOutput ();
485 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
486 EntryPoint="Mono_Posix_Stdlib_stderr")]
487 private static extern IntPtr GetStandardError ();
489 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
490 EntryPoint="Mono_Posix_Stdlib_TMP_MAX")]
491 private static extern int GetTmpMax ();
493 public static readonly int _IOFBF = GetFullyBuffered ();
494 public static readonly int _IOLBF = GetLineBuffered ();
495 public static readonly int _IONBF = GetNonBuffered ();
496 public static readonly int BUFSIZ = GetBufferSize ();
497 public static readonly int EOF = GetEOF ();
498 public static readonly int FOPEN_MAX = GetFopenMax ();
499 public static readonly int FILENAME_MAX = GetFilenameMax ();
500 public static readonly int L_tmpnam = GetTmpnamLength ();
501 public static readonly IntPtr stderr = GetStandardError ();
502 public static readonly IntPtr stdin = GetStandardInput ();
503 public static readonly IntPtr stdout = GetStandardOutput ();
504 public static readonly int TMP_MAX = GetTmpMax ();
506 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
507 public static extern int remove (string filename);
509 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
510 public static extern int rename (string oldpath, string newpath);
512 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
513 public static extern IntPtr tmpfile ();
515 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl,
516 SetLastError=true, EntryPoint="tmpnam")]
517 private static extern IntPtr sys_tmpnam (StringBuilder s);
519 [Obsolete ("Syscall.mkstemp() should be preferred.")]
520 public static string tmpnam (StringBuilder s)
522 if (s != null && s.Capacity < L_tmpnam)
523 throw new ArgumentOutOfRangeException ("s", "s.Capacity < L_tmpnam");
524 IntPtr r = sys_tmpnam (s);
525 return UnixMarshal.PtrToString (r);
528 [Obsolete ("Syscall.mkstemp() should be preferred.")]
529 public static string tmpnam ()
531 IntPtr r = sys_tmpnam (null);
532 return UnixMarshal.PtrToString (r);
535 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
536 public static extern int fclose (IntPtr stream);
538 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
539 public static extern int fflush (IntPtr stream);
541 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
542 public static extern IntPtr fopen (string path, string mode);
544 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
545 public static extern IntPtr freopen (string path, string mode, IntPtr stream);
547 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
548 public static extern void setbuf (IntPtr stream, IntPtr buf);
550 public static unsafe void setbuf (IntPtr stream, byte* buf)
552 setbuf (stream, (IntPtr) buf);
555 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
556 SetLastError=true, EntryPoint="Mono_Posix_Stdlib_setvbuf")]
557 public static extern int setvbuf (IntPtr stream, IntPtr buf, int mode, ulong size);
559 public static unsafe int setvbuf (IntPtr stream, byte* buf, int mode, ulong size)
561 return setvbuf (stream, (IntPtr) buf, mode, size);
564 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl,
565 EntryPoint="fprintf")]
566 private static extern int sys_fprintf (IntPtr stream, string format, string message);
568 public static int fprintf (IntPtr stream, string message)
570 return sys_fprintf (stream, "%s", message);
573 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
574 "Use fprintf (IntPtr, string) instead.")]
575 public static int fprintf (IntPtr stream, string format, params object[] parameters)
577 object[] _parameters = new object[checked(parameters.Length+2)];
578 _parameters [0] = stream;
579 _parameters [1] = format;
580 Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
581 return (int) XPrintfFunctions.fprintf (_parameters);
584 /* SKIP: fscanf(3) */
586 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl,
587 EntryPoint="printf")]
588 private static extern int sys_printf (string format, string message);
590 public static int printf (string message)
592 return sys_printf ("%s", message);
595 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
596 "Use printf (string) instead.")]
597 public static int printf (string format, params object[] parameters)
599 object[] _parameters = new object[checked(parameters.Length+1)];
600 _parameters [0] = format;
601 Array.Copy (parameters, 0, _parameters, 1, parameters.Length);
602 return (int) XPrintfFunctions.printf (_parameters);
607 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
608 EntryPoint="Mono_Posix_Stdlib_snprintf")]
609 private static extern int sys_snprintf (StringBuilder s, ulong n,
610 string format, string message);
612 public static int snprintf (StringBuilder s, ulong n, string message)
614 return sys_snprintf (s, n, "%s", message);
617 public static int snprintf (StringBuilder s, string message)
619 return sys_snprintf (s, (ulong) s.Capacity, "%s", message);
622 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
623 "Use snprintf (StringBuilder, string) instead.")]
624 public static int snprintf (StringBuilder s, ulong n,
625 string format, params object[] parameters)
627 object[] _parameters = new object[checked(parameters.Length+3)];
630 _parameters [2] = format;
631 Array.Copy (parameters, 0, _parameters, 3, parameters.Length);
632 return (int) XPrintfFunctions.snprintf (_parameters);
635 [Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
636 "Use snprintf (StringBuilder, string) instead.")]
637 public static int snprintf (StringBuilder s,
638 string format, params object[] parameters)
640 object[] _parameters = new object[checked(parameters.Length+3)];
642 _parameters [1] = (ulong) s.Capacity;
643 _parameters [2] = format;
644 Array.Copy (parameters, 0, _parameters, 3, parameters.Length);
645 return (int) XPrintfFunctions.snprintf (_parameters);
661 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
662 public static extern int fgetc (IntPtr stream);
664 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl,
665 SetLastError=true, EntryPoint="fgets")]
666 private static extern IntPtr sys_fgets (StringBuilder sb, int size, IntPtr stream);
668 public static StringBuilder fgets (StringBuilder sb, int size, IntPtr stream)
670 IntPtr r = sys_fgets (sb, size, stream);
671 if (r == IntPtr.Zero)
676 public static StringBuilder fgets (StringBuilder sb, IntPtr stream)
678 return fgets (sb, sb.Capacity, stream);
681 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
682 public static extern int fputc (int c, IntPtr stream);
684 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
685 public static extern int fputs (string s, IntPtr stream);
687 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
688 public static extern int getc (IntPtr stream);
690 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
691 public static extern int getchar ();
695 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
696 public static extern int putc (int c, IntPtr stream);
698 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
699 public static extern int putchar (int c);
701 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
702 public static extern int puts (string s);
704 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
705 public static extern int ungetc (int c, IntPtr stream);
707 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
708 SetLastError=true, EntryPoint="Mono_Posix_Stdlib_fread")]
709 public static extern ulong fread (IntPtr ptr, ulong size, ulong nmemb, IntPtr stream);
711 public static unsafe ulong fread (void* ptr, ulong size, ulong nmemb, IntPtr stream)
713 return fread ((IntPtr) ptr, size, nmemb, stream);
716 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
717 SetLastError=true, EntryPoint="Mono_Posix_Stdlib_fread")]
718 private static extern ulong sys_fread ([Out] byte[] ptr,
719 ulong size, ulong nmemb, IntPtr stream);
721 public static ulong fread (byte[] ptr, ulong size, ulong nmemb, IntPtr stream)
723 if ((size * nmemb) > (ulong) ptr.Length)
724 throw new ArgumentOutOfRangeException ("nmemb");
725 return sys_fread (ptr, size, nmemb, stream);
728 public static ulong fread (byte[] ptr, IntPtr stream)
730 return fread (ptr, 1, (ulong) ptr.Length, stream);
733 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
734 SetLastError=true, EntryPoint="Mono_Posix_Stdlib_fwrite")]
735 public static extern ulong fwrite (IntPtr ptr, ulong size, ulong nmemb, IntPtr stream);
737 public static unsafe ulong fwrite (void* ptr, ulong size, ulong nmemb, IntPtr stream)
739 return fwrite ((IntPtr) ptr, size, nmemb, stream);
742 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
743 SetLastError=true, EntryPoint="Mono_Posix_Stdlib_fwrite")]
744 private static extern ulong sys_fwrite (byte[] ptr,
745 ulong size, ulong nmemb, IntPtr stream);
747 public static ulong fwrite (byte[] ptr, ulong size, ulong nmemb, IntPtr stream)
749 if ((size * nmemb) > (ulong) ptr.Length)
750 throw new ArgumentOutOfRangeException ("nmemb");
751 return sys_fwrite (ptr, size, nmemb, stream);
754 public static ulong fwrite (byte[] ptr, IntPtr stream)
756 return fwrite (ptr, 1, (ulong) ptr.Length, stream);
759 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
760 SetLastError=true, EntryPoint="Mono_Posix_Stdlib_fgetpos")]
761 private static extern int sys_fgetpos (IntPtr stream, HandleRef pos);
763 public static int fgetpos (IntPtr stream, FilePosition pos)
765 return sys_fgetpos (stream, pos.Handle);
768 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
769 SetLastError=true, EntryPoint="Mono_Posix_Stdlib_fseek")]
770 private static extern int sys_fseek (IntPtr stream, long offset, int origin);
772 public static int fseek (IntPtr stream, long offset, SeekFlags origin)
774 int _origin = UnixConvert.FromSeekFlags (origin);
775 return sys_fseek (stream, offset, _origin);
778 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
779 SetLastError=true, EntryPoint="Mono_Posix_Stdlib_fsetpos")]
780 private static extern int sys_fsetpos (IntPtr stream, HandleRef pos);
782 public static int fsetpos (IntPtr stream, FilePosition pos)
784 return sys_fsetpos (stream, pos.Handle);
787 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
788 SetLastError=true, EntryPoint="Mono_Posix_Stdlib_ftell")]
789 public static extern long ftell (IntPtr stream);
791 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl)]
792 public static extern void rewind (IntPtr stream);
794 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl)]
795 public static extern void clearerr (IntPtr stream);
797 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl)]
798 public static extern int feof (IntPtr stream);
800 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl)]
801 public static extern int ferror (IntPtr stream);
803 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
804 public static extern void perror (string s);
809 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
810 EntryPoint="Mono_Posix_Stdlib_EXIT_FAILURE")]
811 private static extern int GetExitFailure();
813 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
814 EntryPoint="Mono_Posix_Stdlib_EXIT_SUCCESS")]
815 private static extern int GetExitSuccess ();
817 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
818 EntryPoint="Mono_Posix_Stdlib_MB_CUR_MAX")]
819 private static extern int GetMbCurMax ();
821 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
822 EntryPoint="Mono_Posix_Stdlib_RAND_MAX")]
823 private static extern int GetRandMax ();
825 public static readonly int EXIT_FAILURE = GetExitFailure ();
826 public static readonly int EXIT_SUCCESS = GetExitSuccess ();
827 public static readonly int MB_CUR_MAX = GetMbCurMax ();
828 public static readonly int RAND_MAX = GetRandMax ();
830 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl)]
831 public static extern int rand ();
833 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl)]
834 public static extern void srand (uint seed);
837 // void *calloc (size_t nmemb, size_t size);
838 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
839 SetLastError=true, EntryPoint="Mono_Posix_Stdlib_calloc")]
840 public static extern IntPtr calloc (ulong nmemb, ulong size);
842 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl)]
843 public static extern void free (IntPtr ptr);
846 // void *malloc(size_t size);
847 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
848 SetLastError=true, EntryPoint="Mono_Posix_Stdlib_malloc")]
849 public static extern IntPtr malloc (ulong size);
852 // void *realloc(void *ptr, size_t size);
853 [DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
854 SetLastError=true, EntryPoint="Mono_Posix_Stdlib_realloc")]
855 public static extern IntPtr realloc (IntPtr ptr, ulong size);
857 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl)]
858 public static extern void abort ();
860 /* SKIP: atexit(3) */
862 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl)]
863 public static extern void exit (int status);
865 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl)]
866 public static extern void _Exit (int status);
868 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, EntryPoint="getenv")]
869 private static extern IntPtr sys_getenv (string name);
871 public static string getenv (string name)
873 IntPtr r = sys_getenv (name);
874 return UnixMarshal.PtrToString (r);
877 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
878 public static extern int system (string @string);
884 [DllImport (LIBC, CallingConvention=CallingConvention.Cdecl,
885 SetLastError=true, EntryPoint="strerror")]
886 private static extern IntPtr sys_strerror (int errnum);
888 public static string strerror (Error errnum)
890 int e = UnixConvert.FromError (errnum);
891 IntPtr r = sys_strerror (e);
892 return UnixMarshal.PtrToString (r);
896 #endregion // Classes