* Stdlib.cs: Add more <stdio.h> wrappers, such as fread(3) and fwrite(3).
[mono.git] / mcs / class / Mono.Posix / Mono.Unix / Stdlib.cs
1 //
2 // Mono.Unix/Stdlib.cs
3 //
4 // Authors:
5 //   Jonathan Pryor (jonpryor@vt.edu)
6 //
7 // (C) 2004 Jonathan Pryor
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 using System;
30 using System.IO;
31 using System.Runtime.InteropServices;
32 using System.Text;
33 using Mono.Unix;
34
35 namespace Mono.Unix {
36
37         public delegate void sighandler_t (int value);
38
39         internal class XPrintfFunctions
40         {
41                 internal delegate object XPrintf (object[] parameters);
42
43                 internal static XPrintf printf;
44                 internal static XPrintf fprintf;
45
46                 static XPrintfFunctions ()
47                 {
48                         CdeclFunction _printf = new CdeclFunction ("libc", "printf", typeof(int));
49                         printf = new XPrintf (_printf.Invoke);
50
51                         CdeclFunction _fprintf = new CdeclFunction ("libc", "fprintf", typeof(int));
52                         fprintf = new XPrintf (_fprintf.Invoke);
53                 }
54         }
55
56         //
57         // Convention: Functions that are part of the C standard library go here.
58         //
59         // For example, the man page should say something similar to:
60         //
61         //    CONFORMING TO
62         //           ISO 9899 (''ANSI C'')
63         //
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).
68         //
69         public class Stdlib
70         {
71                 private const string LIBC = "libc";
72                 private const string MPH = "MonoPosixHelper";
73
74                 internal Stdlib () {}
75
76                 //
77                 // <signal.h>
78                 //
79                 [DllImport (LIBC, SetLastError=true, EntryPoint="signal")]
80                 private static extern IntPtr sys_signal (int signum, sighandler_t handler);
81
82                 // FIXME: signal returns sighandler_t.  What should we do?
83                 public static int signal (Signum signum, sighandler_t handler)
84                 {
85                         int _sig = UnixConvert.FromSignum (signum);
86                         IntPtr r = sys_signal (_sig, handler);
87                         // handle `r'
88                         return 0;
89                 }
90
91                 // TODO: Need access to SIG_IGN, SIG_DFL, and SIG_ERR values.
92
93                 //
94                 // <stdio.h>
95                 //
96                 [DllImport (LIBC, SetLastError=true)]
97                 public static extern void perror (string s);
98
99                 [DllImport (LIBC, SetLastError=true)]
100                 public static extern int rename (string oldpath, string newpath);
101
102                 [DllImport (LIBC)]
103                 public static extern void clearerr (IntPtr stream);
104
105                 [DllImport (LIBC, SetLastError=true)]
106                 public static extern IntPtr fopen (string path, string mode);
107
108                 [DllImport (LIBC, SetLastError=true)]
109                 public static extern IntPtr fdopen (int filedes, string mode);
110
111                 [DllImport (LIBC, SetLastError=true)]
112                 public static extern IntPtr freopen (string path, string mode, IntPtr stream);
113
114                 [DllImport (LIBC, SetLastError=true)]
115                 public static extern int fclose (IntPtr stream);
116
117                 [DllImport (LIBC, SetLastError=true)]
118                 public static extern int fflush (IntPtr stream);
119
120                 [DllImport (LIBC)]
121                 public static extern int feof (IntPtr stream);
122
123                 [DllImport (LIBC)]
124                 public static extern int ferror (IntPtr stream);
125
126                 [DllImport (LIBC, SetLastError=true)]
127                 public static extern int fileno (IntPtr stream);
128
129                 public static int printf (string format, params object[] parameters)
130                 {
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);
135                 }
136
137                 public static int fprintf (IntPtr stream, string format, params object[] parameters)
138                 {
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);
144                 }
145
146                 [DllImport (LIBC, SetLastError=true)]
147                 public static extern int fputc (int c, IntPtr stream);
148
149                 [DllImport (LIBC, SetLastError=true)]
150                 public static extern int fputs (string s, IntPtr stream);
151
152                 // skip putc, as it may be a macro
153
154                 [DllImport (LIBC, SetLastError=true)]
155                 public static extern int putchar (int c);
156
157                 [DllImport (LIBC, SetLastError=true)]
158                 public static extern int puts (string s);
159
160                 [DllImport (LIBC, SetLastError=true)]
161                 public static extern int fgetc (IntPtr stream);
162
163                 [DllImport (LIBC, SetLastError=true, EntryPoint="fgetc")]
164                 private static extern IntPtr sys_fgetc (StringBuilder sb, int size, IntPtr stream);
165
166                 public static StringBuilder fgetc (StringBuilder sb, int size, IntPtr stream)
167                 {
168                         sys_fgetc (sb, size, stream);
169                         return sb;
170                 }
171
172                 public static StringBuilder fgetc (StringBuilder sb, IntPtr stream)
173                 {
174                         sys_fgetc (sb, sb.Capacity, stream);
175                         return sb;
176                 }
177
178                 [DllImport (LIBC, SetLastError=true)]
179                 public static extern int getc (IntPtr stream);
180
181                 [DllImport (LIBC, SetLastError=true)]
182                 public static extern int getchar ();
183
184                 // skip gets(3), it's evil.
185
186                 [DllImport (LIBC, SetLastError=true)]
187                 public static extern int ungetc (int c, IntPtr stream);
188
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);
191
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);
194
195                 public static ulong fread (byte[] ptr, IntPtr stream)
196                 {
197                         return fread (ptr, 1, (ulong) ptr.Length, stream);
198                 }
199
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);
202
203                 [DllImport (MPH, SetLastError=true, EntryPoint="Mono_Posix_Stdlib_fwrite")]
204                 public static extern ulong fwrite (byte[] ptr, ulong size, ulong nmemb, IntPtr stream);
205
206                 public static ulong fwrite (byte[] ptr, IntPtr stream)
207                 {
208                         return fwrite (ptr, 1, (ulong) ptr.Length, stream);
209                 }
210
211                 //
212                 // <stdlib.h>
213                 //
214
215                 // calloc(3):
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);
219
220                 [DllImport (LIBC)]
221                 public static extern void exit (int status);
222
223                 [DllImport (LIBC)]
224                 public static extern void free (IntPtr ptr);
225
226                 // malloc(3):
227                 //    void *malloc(size_t size);
228                 [DllImport (MPH, SetLastError=true, EntryPoint="Mono_Posix_Stdlib_malloc")]
229                 public static extern IntPtr malloc (ulong size);
230
231                 // realloc(3):
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);
235
236                 [DllImport (MPH, SetLastError=true)]
237                 public static extern int system (string @string);
238
239                 //
240                 // <string.h>
241                 //
242
243                 [DllImport ("libc", SetLastError=true, EntryPoint="strerror")]
244                 private static extern IntPtr sys_strerror (int errnum);
245
246                 public static IntPtr sys_strerror (Error errnum)
247                 {
248                         int e = UnixConvert.FromError (errnum);
249                         return sys_strerror (e);
250                 }
251
252                 public static string strerror (Error errnum)
253                 {
254                         IntPtr r = sys_strerror (errnum);
255                         return UnixMarshal.PtrToString (r);
256                 }
257         }
258 }
259
260 // vim: noexpandtab