5 // Jonathan Pryor (jonpryor@vt.edu)
7 // (C) 2004 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;
37 public delegate void sighandler_t (int value);
39 internal class XPrintfFunctions
41 internal delegate object XPrintf (object[] parameters);
43 internal static XPrintf printf;
44 internal static XPrintf fprintf;
46 static XPrintfFunctions ()
48 CdeclFunction _printf = new CdeclFunction ("libc", "printf", typeof(int));
49 printf = new XPrintf (_printf.Invoke);
51 CdeclFunction _fprintf = new CdeclFunction ("libc", "fprintf", typeof(int));
52 fprintf = new XPrintf (_fprintf.Invoke);
57 // Convention: Functions that are part of the C standard library go here.
59 // For example, the man page should say something similar to:
62 // ISO 9899 (''ANSI C'')
64 // We can also place logical "sibling" exports here -- exports which
65 // strongly relate to an ANSI C function, either as an overload, or which
66 // operates on the same datatype as an ANSI C function. Examples include
67 // fileno(3) and fdopen(3).
71 private const string LIBC = "libc";
72 private const string MPH = "MonoPosixHelper";
79 [DllImport (LIBC, SetLastError=true, EntryPoint="signal")]
80 private static extern IntPtr sys_signal (int signum, sighandler_t handler);
82 // FIXME: signal returns sighandler_t. What should we do?
83 public static int signal (Signum signum, sighandler_t handler)
85 int _sig = UnixConvert.FromSignum (signum);
86 IntPtr r = sys_signal (_sig, handler);
91 // TODO: Need access to SIG_IGN, SIG_DFL, and SIG_ERR values.
96 [DllImport (LIBC, SetLastError=true)]
97 public static extern void perror (string s);
99 [DllImport (LIBC, SetLastError=true)]
100 public static extern int rename (string oldpath, string newpath);
103 public static extern void clearerr (IntPtr stream);
105 [DllImport (LIBC, SetLastError=true)]
106 public static extern IntPtr fopen (string path, string mode);
108 [DllImport (LIBC, SetLastError=true)]
109 public static extern IntPtr fdopen (int filedes, string mode);
111 [DllImport (LIBC, SetLastError=true)]
112 public static extern IntPtr freopen (string path, string mode, IntPtr stream);
114 [DllImport (LIBC, SetLastError=true)]
115 public static extern int fclose (IntPtr stream);
117 [DllImport (LIBC, SetLastError=true)]
118 public static extern int fflush (IntPtr stream);
121 public static extern int feof (IntPtr stream);
124 public static extern int ferror (IntPtr stream);
126 [DllImport (LIBC, SetLastError=true)]
127 public static extern int fileno (IntPtr stream);
129 public static int printf (string format, params object[] parameters)
131 object[] _parameters = new object[checked(parameters.Length+1)];
132 _parameters [0] = format;
133 Array.Copy (parameters, 0, _parameters, 1, parameters.Length);
134 return (int) XPrintfFunctions.printf (_parameters);
137 public static int fprintf (IntPtr stream, string format, params object[] parameters)
139 object[] _parameters = new object[checked(parameters.Length+2)];
140 _parameters [0] = stream;
141 _parameters [1] = format;
142 Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
143 return (int) XPrintfFunctions.fprintf (_parameters);
146 [DllImport (LIBC, SetLastError=true)]
147 public static extern int fputc (int c, IntPtr stream);
149 [DllImport (LIBC, SetLastError=true)]
150 public static extern int fputs (string s, IntPtr stream);
152 // skip putc, as it may be a macro
154 [DllImport (LIBC, SetLastError=true)]
155 public static extern int putchar (int c);
157 [DllImport (LIBC, SetLastError=true)]
158 public static extern int puts (string s);
160 [DllImport (LIBC, SetLastError=true)]
161 public static extern int fgetc (IntPtr stream);
163 [DllImport (LIBC, SetLastError=true, EntryPoint="fgetc")]
164 private static extern IntPtr sys_fgetc (StringBuilder sb, int size, IntPtr stream);
166 public static StringBuilder fgetc (StringBuilder sb, int size, IntPtr stream)
168 sys_fgetc (sb, size, stream);
172 public static StringBuilder fgetc (StringBuilder sb, IntPtr stream)
174 sys_fgetc (sb, sb.Capacity, stream);
178 [DllImport (LIBC, SetLastError=true)]
179 public static extern int getc (IntPtr stream);
181 [DllImport (LIBC, SetLastError=true)]
182 public static extern int getchar ();
184 // skip gets(3), it's evil.
186 [DllImport (LIBC, SetLastError=true)]
187 public static extern int ungetc (int c, IntPtr stream);
189 [DllImport (MPH, SetLastError=true, EntryPoint="Mono_Posix_Stdlib_fread")]
190 public static extern unsafe ulong fread (void* ptr, ulong size, ulong nmemb, IntPtr stream);
192 [DllImport (MPH, SetLastError=true, EntryPoint="Mono_Posix_Stdlib_fread")]
193 public static extern ulong fread ([Out] byte[] ptr, ulong size, ulong nmemb, IntPtr stream);
195 public static ulong fread (byte[] ptr, IntPtr stream)
197 return fread (ptr, 1, (ulong) ptr.Length, stream);
200 [DllImport (MPH, SetLastError=true, EntryPoint="Mono_Posix_Stdlib_fwrite")]
201 public static extern unsafe ulong fwrite (void* ptr, ulong size, ulong nmemb, IntPtr stream);
203 [DllImport (MPH, SetLastError=true, EntryPoint="Mono_Posix_Stdlib_fwrite")]
204 public static extern ulong fwrite (byte[] ptr, ulong size, ulong nmemb, IntPtr stream);
206 public static ulong fwrite (byte[] ptr, IntPtr stream)
208 return fwrite (ptr, 1, (ulong) ptr.Length, stream);
216 // void *calloc (size_t nmemb, size_t size);
217 [DllImport (MPH, SetLastError=true, EntryPoint="Mono_Posix_Stdlib_calloc")]
218 public static extern IntPtr calloc (ulong nmemb, ulong size);
221 public static extern void exit (int status);
224 public static extern void free (IntPtr ptr);
227 // void *malloc(size_t size);
228 [DllImport (MPH, SetLastError=true, EntryPoint="Mono_Posix_Stdlib_malloc")]
229 public static extern IntPtr malloc (ulong size);
232 // void *realloc(void *ptr, size_t size);
233 [DllImport (MPH, SetLastError=true, EntryPoint="Mono_Posix_Stdlib_realloc")]
234 public static extern IntPtr realloc (IntPtr ptr, ulong size);
236 [DllImport (MPH, SetLastError=true)]
237 public static extern int system (string @string);
243 [DllImport ("libc", SetLastError=true, EntryPoint="strerror")]
244 private static extern IntPtr sys_strerror (int errnum);
246 public static IntPtr sys_strerror (Error errnum)
248 int e = UnixConvert.FromError (errnum);
249 return sys_strerror (e);
252 public static string strerror (Error errnum)
254 IntPtr r = sys_strerror (errnum);
255 return UnixMarshal.PtrToString (r);