1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
|
//
// Mono.Unix/Stdlib.cs
//
// Authors:
// Jonathan Pryor (jonpryor@vt.edu)
//
// (C) 2004-2006 Jonathan Pryor
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Mono.Unix.Native;
namespace Mono.Unix.Native {
#region Enumerations
[Map]
public enum Errno : int {
// errors & their values liberally copied from
// FC2 /usr/include/asm/errno.h
EPERM = 1, // Operation not permitted
ENOENT = 2, // No such file or directory
ESRCH = 3, // No such process
EINTR = 4, // Interrupted system call
EIO = 5, // I/O error
ENXIO = 6, // No such device or address
E2BIG = 7, // Arg list too long
ENOEXEC = 8, // Exec format error
EBADF = 9, // Bad file number
ECHILD = 10, // No child processes
EAGAIN = 11, // Try again
ENOMEM = 12, // Out of memory
EACCES = 13, // Permission denied
EFAULT = 14, // Bad address
ENOTBLK = 15, // Block device required
EBUSY = 16, // Device or resource busy
EEXIST = 17, // File exists
EXDEV = 18, // Cross-device link
ENODEV = 19, // No such device
ENOTDIR = 20, // Not a directory
EISDIR = 21, // Is a directory
EINVAL = 22, // Invalid argument
ENFILE = 23, // File table overflow
EMFILE = 24, // Too many open files
ENOTTY = 25, // Not a typewriter
ETXTBSY = 26, // Text file busy
EFBIG = 27, // File too large
ENOSPC = 28, // No space left on device
ESPIPE = 29, // Illegal seek
EROFS = 30, // Read-only file system
EMLINK = 31, // Too many links
EPIPE = 32, // Broken pipe
EDOM = 33, // Math argument out of domain of func
ERANGE = 34, // Math result not representable
EDEADLK = 35, // Resource deadlock would occur
ENAMETOOLONG = 36, // File name too long
ENOLCK = 37, // No record locks available
ENOSYS = 38, // Function not implemented
ENOTEMPTY = 39, // Directory not empty
ELOOP = 40, // Too many symbolic links encountered
EWOULDBLOCK = EAGAIN, // Operation would block
ENOMSG = 42, // No message of desired type
EIDRM = 43, // Identifier removed
ECHRNG = 44, // Channel number out of range
EL2NSYNC = 45, // Level 2 not synchronized
EL3HLT = 46, // Level 3 halted
EL3RST = 47, // Level 3 reset
ELNRNG = 48, // Link number out of range
EUNATCH = 49, // Protocol driver not attached
ENOCSI = 50, // No CSI structure available
EL2HLT = 51, // Level 2 halted
EBADE = 52, // Invalid exchange
EBADR = 53, // Invalid request descriptor
EXFULL = 54, // Exchange full
ENOANO = 55, // No anode
EBADRQC = 56, // Invalid request code
EBADSLT = 57, // Invalid slot
EDEADLOCK = EDEADLK,
EBFONT = 59, // Bad font file format
ENOSTR = 60, // Device not a stream
ENODATA = 61, // No data available
ETIME = 62, // Timer expired
ENOSR = 63, // Out of streams resources
ENONET = 64, // Machine is not on the network
ENOPKG = 65, // Package not installed
EREMOTE = 66, // Object is remote
ENOLINK = 67, // Link has been severed
EADV = 68, // Advertise error
ESRMNT = 69, // Srmount error
ECOMM = 70, // Communication error on send
EPROTO = 71, // Protocol error
EMULTIHOP = 72, // Multihop attempted
EDOTDOT = 73, // RFS specific error
EBADMSG = 74, // Not a data message
EOVERFLOW = 75, // Value too large for defined data type
ENOTUNIQ = 76, // Name not unique on network
EBADFD = 77, // File descriptor in bad state
EREMCHG = 78, // Remote address changed
ELIBACC = 79, // Can not access a needed shared library
ELIBBAD = 80, // Accessing a corrupted shared library
ELIBSCN = 81, // .lib section in a.out corrupted
ELIBMAX = 82, // Attempting to link in too many shared libraries
ELIBEXEC = 83, // Cannot exec a shared library directly
EILSEQ = 84, // Illegal byte sequence
ERESTART = 85, // Interrupted system call should be restarted
ESTRPIPE = 86, // Streams pipe error
EUSERS = 87, // Too many users
ENOTSOCK = 88, // Socket operation on non-socket
EDESTADDRREQ = 89, // Destination address required
EMSGSIZE = 90, // Message too long
EPROTOTYPE = 91, // Protocol wrong type for socket
ENOPROTOOPT = 92, // Protocol not available
EPROTONOSUPPORT = 93, // Protocol not supported
ESOCKTNOSUPPORT = 94, // Socket type not supported
EOPNOTSUPP = 95, // Operation not supported on transport endpoint
EPFNOSUPPORT = 96, // Protocol family not supported
EAFNOSUPPORT = 97, // Address family not supported by protocol
EADDRINUSE = 98, // Address already in use
EADDRNOTAVAIL = 99, // Cannot assign requested address
ENETDOWN = 100, // Network is down
ENETUNREACH = 101, // Network is unreachable
ENETRESET = 102, // Network dropped connection because of reset
ECONNABORTED = 103, // Software caused connection abort
ECONNRESET = 104, // Connection reset by peer
ENOBUFS = 105, // No buffer space available
EISCONN = 106, // Transport endpoint is already connected
ENOTCONN = 107, // Transport endpoint is not connected
ESHUTDOWN = 108, // Cannot send after transport endpoint shutdown
ETOOMANYREFS = 109, // Too many references: cannot splice
ETIMEDOUT = 110, // Connection timed out
ECONNREFUSED = 111, // Connection refused
EHOSTDOWN = 112, // Host is down
EHOSTUNREACH = 113, // No route to host
EALREADY = 114, // Operation already in progress
EINPROGRESS = 115, // Operation now in progress
ESTALE = 116, // Stale NFS file handle
EUCLEAN = 117, // Structure needs cleaning
ENOTNAM = 118, // Not a XENIX named type file
ENAVAIL = 119, // No XENIX semaphores available
EISNAM = 120, // Is a named type file
EREMOTEIO = 121, // Remote I/O error
EDQUOT = 122, // Quota exceeded
ENOMEDIUM = 123, // No medium found
EMEDIUMTYPE = 124, // Wrong medium type
ECANCELED = 125,
ENOKEY = 126,
EKEYEXPIRED = 127,
EKEYREVOKED = 128,
EKEYREJECTED = 129,
EOWNERDEAD = 130,
ENOTRECOVERABLE = 131,
// OS X-specific values: OS X value + 1000
EPROCLIM = 1067, // Too many processes
EBADRPC = 1072, // RPC struct is bad
ERPCMISMATCH = 1073, // RPC version wrong
EPROGUNAVAIL = 1074, // RPC prog. not avail
EPROGMISMATCH = 1075, // Program version wrong
EPROCUNAVAIL = 1076, // Bad procedure for program
EFTYPE = 1079, // Inappropriate file type or format
EAUTH = 1080, // Authentication error
ENEEDAUTH = 1081, // Need authenticator
EPWROFF = 1082, // Device power is off
EDEVERR = 1083, // Device error, e.g. paper out
EBADEXEC = 1085, // Bad executable
EBADARCH = 1086, // Bad CPU type in executable
ESHLIBVERS = 1087, // Shared library version mismatch
EBADMACHO = 1088, // Malformed Macho file
ENOATTR = 1093, // Attribute not found
ENOPOLICY = 1103, // No such policy registered
}
#endregion
#region Classes
public sealed class FilePosition : MarshalByRefObject, IDisposable
, IEquatable <FilePosition>
{
private static readonly int FilePositionDumpSize =
Stdlib.DumpFilePosition (null, new HandleRef (null, IntPtr.Zero), 0);
private HandleRef pos;
public FilePosition ()
{
IntPtr p = Stdlib.CreateFilePosition ();
if (p == IntPtr.Zero)
throw new OutOfMemoryException ("Unable to malloc fpos_t!");
pos = new HandleRef (this, p);
}
internal HandleRef Handle {
get {return pos;}
}
public void Dispose ()
{
Cleanup ();
GC.SuppressFinalize (this);
}
private void Cleanup ()
{
if (pos.Handle != IntPtr.Zero) {
Stdlib.free (pos.Handle);
pos = new HandleRef (this, IntPtr.Zero);
}
}
public override string ToString ()
{
return "(" + base.ToString () + " " + GetDump () + ")";
}
private string GetDump ()
{
if (FilePositionDumpSize <= 0)
return "internal error";
StringBuilder buf = new StringBuilder (FilePositionDumpSize+1);
if (Stdlib.DumpFilePosition (buf, Handle, FilePositionDumpSize+1) <= 0)
return "internal error dumping fpos_t";
return buf.ToString ();
}
public override bool Equals (object obj)
{
FilePosition fp = obj as FilePosition;
if (obj == null || fp == null)
return false;
return ToString().Equals (obj.ToString());
}
public bool Equals (FilePosition value)
{
if (object.ReferenceEquals (this, value))
return true;
return ToString().Equals (value.ToString());
}
public override int GetHashCode ()
{
return ToString ().GetHashCode ();
}
~FilePosition ()
{
Cleanup ();
}
public static bool operator== (FilePosition lhs, FilePosition rhs)
{
return Object.Equals (lhs, rhs);
}
public static bool operator!= (FilePosition lhs, FilePosition rhs)
{
return !Object.Equals (lhs, rhs);
}
}
public enum SignalAction {
Default,
Ignore,
Error
}
//
// Right now using this attribute gives an assert because it
// isn't implemented.
//
#if UNMANAGED_FN_PTR_SUPPORT_FIXED
[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
#endif
public delegate void SignalHandler (int signal);
internal class XPrintfFunctions
{
internal delegate object XPrintf (object[] parameters);
internal static XPrintf printf;
internal static XPrintf fprintf;
internal static XPrintf snprintf;
internal static XPrintf syslog;
static XPrintfFunctions ()
{
CdeclFunction _printf = new CdeclFunction (Stdlib.LIBC, "printf", typeof(int));
printf = new XPrintf (_printf.Invoke);
CdeclFunction _fprintf = new CdeclFunction (Stdlib.LIBC, "fprintf", typeof(int));
fprintf = new XPrintf (_fprintf.Invoke);
CdeclFunction _snprintf = new CdeclFunction (Stdlib.MPH,
"Mono_Posix_Stdlib_snprintf", typeof(int));
snprintf = new XPrintf (_snprintf.Invoke);
CdeclFunction _syslog = new CdeclFunction (Syscall.MPH,
"Mono_Posix_Stdlib_syslog2", typeof(int));
syslog = new XPrintf (_syslog.Invoke);
}
}
//
// Convention: Functions that are part of the C standard library go here.
//
// For example, the man page should say something similar to:
//
// CONFORMING TO
// ISO 9899 (''ANSI C'')
//
// The intent is that members of this class should be portable to any system
// supporting the C runtime (read: non-Unix, including Windows). Using
// anything from Syscall is non-portable, but restricting yourself to just
// Stdlib is intended to be portable.
//
// The only methods in here should be:
// (1) low-level functions (as defined above).
// (2) "Trivial" function overloads. For example, if the parameters to a
// function are related (e.g. fwrite(3))
// (3) The return type SHOULD NOT be changed. If you want to provide a
// convenience function with a nicer return type, place it into one of
// the Mono.Unix.Std* wrapper classes, and give it a .NET-styled name.
// - EXCEPTION: No public functions should have a `void' return type.
// `void' return types should be replaced with `int'.
// Rationality: `void'-return functions typically require a
// complicated call sequence, such as clear errno, then call, then
// check errno to see if any errors occurred. This sequence can't
// be done safely in managed code, as errno may change as part of
// the P/Invoke mechanism.
// Instead, add a MonoPosixHelper export which does:
// errno = 0;
// INVOKE SYSCALL;
// return errno == 0 ? 0 : -1;
// This lets managed code check the return value in the usual manner.
// (4) Exceptions SHOULD NOT be thrown. EXCEPTIONS:
// - If you're wrapping *broken* methods which make assumptions about
// input data, such as that an argument refers to N bytes of data.
// This is currently limited to cuserid(3) and encrypt(3).
// - If you call functions which themselves generate exceptions.
// This is the case for using NativeConvert, which will throw an
// exception if an invalid/unsupported value is used.
//
public class Stdlib
{
internal const string LIBC = "msvcrt";
internal const string MPH = "MonoPosixHelper";
internal Stdlib () {}
#region <errno.h> Declarations
//
// <errno.h> -- COMPLETE
//
public static Errno GetLastError ()
{
int errno = Marshal.GetLastWin32Error ();
return NativeConvert.ToErrno (errno);
}
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib_SetLastError")]
private static extern void SetLastError (int error);
protected static void SetLastError (Errno error)
{
int _error = NativeConvert.FromErrno (error);
SetLastError (_error);
}
#endregion
//
// <signal.h>
//
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib_InvokeSignalHandler")]
internal static extern void InvokeSignalHandler (int signum, IntPtr handler);
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib_SIG_DFL")]
private static extern IntPtr GetDefaultSignal ();
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib_SIG_ERR")]
private static extern IntPtr GetErrorSignal ();
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib_SIG_IGN")]
private static extern IntPtr GetIgnoreSignal ();
private static readonly IntPtr _SIG_DFL = GetDefaultSignal ();
private static readonly IntPtr _SIG_ERR = GetErrorSignal ();
private static readonly IntPtr _SIG_IGN = GetIgnoreSignal ();
private static void _ErrorHandler (int signum)
{
Console.Error.WriteLine ("Error handler invoked for signum " +
signum + ". Don't do that.");
}
private static void _DefaultHandler (int signum)
{
Console.Error.WriteLine ("Default handler invoked for signum " +
signum + ". Don't do that.");
}
private static void _IgnoreHandler (int signum)
{
Console.Error.WriteLine ("Ignore handler invoked for signum " +
signum + ". Don't do that.");
}
[CLSCompliant (false)]
public static readonly SignalHandler SIG_DFL = new SignalHandler (_DefaultHandler);
[CLSCompliant (false)]
public static readonly SignalHandler SIG_ERR = new SignalHandler (_ErrorHandler);
[CLSCompliant (false)]
public static readonly SignalHandler SIG_IGN = new SignalHandler (_IgnoreHandler);
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="signal")]
private static extern IntPtr sys_signal (int signum, SignalHandler handler);
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="signal")]
private static extern IntPtr sys_signal (int signum, IntPtr handler);
[CLSCompliant (false)]
[Obsolete ("This is not safe; " +
"use Mono.Unix.UnixSignal for signal delivery or SetSignalAction()")]
public static SignalHandler signal (Signum signum, SignalHandler handler)
{
int _sig = NativeConvert.FromSignum (signum);
Delegate[] handlers = handler.GetInvocationList ();
for (int i = 0; i < handlers.Length; ++i) {
Marshal.Prelink (handlers [i].Method);
}
IntPtr r;
if (handler == SIG_DFL)
r = sys_signal (_sig, _SIG_DFL);
else if (handler == SIG_ERR)
r = sys_signal (_sig, _SIG_ERR);
else if (handler == SIG_IGN)
r = sys_signal (_sig, _SIG_IGN);
else
r = sys_signal (_sig, handler);
return TranslateHandler (r);
}
private static SignalHandler TranslateHandler (IntPtr handler)
{
if (handler == _SIG_DFL)
return SIG_DFL;
if (handler == _SIG_ERR)
return SIG_ERR;
if (handler == _SIG_IGN)
return SIG_IGN;
return (SignalHandler) Marshal.GetDelegateForFunctionPointer (handler, typeof(SignalHandler));
}
public static int SetSignalAction (Signum signal, SignalAction action)
{
return SetSignalAction (NativeConvert.FromSignum (signal), action);
}
public static int SetSignalAction (RealTimeSignum rts, SignalAction action)
{
return SetSignalAction (NativeConvert.FromRealTimeSignum (rts), action);
}
private static int SetSignalAction (int signum, SignalAction action)
{
IntPtr handler = IntPtr.Zero;
switch (action) {
case SignalAction.Default:
handler = _SIG_DFL;
break;
case SignalAction.Ignore:
handler = _SIG_IGN;
break;
case SignalAction.Error:
handler = _SIG_ERR;
break;
default:
throw new ArgumentException ("Invalid action value.", "action");
}
IntPtr r = sys_signal (signum, handler);
if (r == _SIG_ERR)
return -1;
return 0;
}
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, EntryPoint="raise")]
private static extern int sys_raise (int sig);
[CLSCompliant (false)]
public static int raise (Signum sig)
{
return sys_raise (NativeConvert.FromSignum (sig));
}
public static int raise (RealTimeSignum rts)
{
return sys_raise (NativeConvert.FromRealTimeSignum (rts));
}
//
// <stdio.h> -- COMPLETE except for :
// - the scanf(3) family .
// - vararg functions.
// - Horribly unsafe functions (gets(3)).
//
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib__IOFBF")]
private static extern int GetFullyBuffered ();
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib__IOLBF")]
private static extern int GetLineBuffered ();
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib__IONBF")]
private static extern int GetNonBuffered ();
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib_BUFSIZ")]
private static extern int GetBufferSize ();
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib_CreateFilePosition")]
internal static extern IntPtr CreateFilePosition ();
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib_DumpFilePosition")]
internal static extern int DumpFilePosition (StringBuilder buf, HandleRef handle, int len);
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib_EOF")]
private static extern int GetEOF ();
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib_FILENAME_MAX")]
private static extern int GetFilenameMax ();
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib_FOPEN_MAX")]
private static extern int GetFopenMax ();
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib_L_tmpnam")]
private static extern int GetTmpnamLength ();
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib_stdin")]
private static extern IntPtr GetStandardInput ();
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib_stdout")]
private static extern IntPtr GetStandardOutput ();
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib_stderr")]
private static extern IntPtr GetStandardError ();
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib_TMP_MAX")]
private static extern int GetTmpMax ();
[CLSCompliant (false)]
public static readonly int _IOFBF = GetFullyBuffered ();
[CLSCompliant (false)]
public static readonly int _IOLBF = GetLineBuffered ();
[CLSCompliant (false)]
public static readonly int _IONBF = GetNonBuffered ();
[CLSCompliant (false)]
public static readonly int BUFSIZ = GetBufferSize ();
[CLSCompliant (false)]
public static readonly int EOF = GetEOF ();
[CLSCompliant (false)]
public static readonly int FOPEN_MAX = GetFopenMax ();
[CLSCompliant (false)]
public static readonly int FILENAME_MAX = GetFilenameMax ();
[CLSCompliant (false)]
public static readonly int L_tmpnam = GetTmpnamLength ();
public static readonly IntPtr stderr = GetStandardError ();
public static readonly IntPtr stdin = GetStandardInput ();
public static readonly IntPtr stdout = GetStandardOutput ();
[CLSCompliant (false)]
public static readonly int TMP_MAX = GetTmpMax ();
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
public static extern int remove (
[MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
string filename);
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
public static extern int rename (
[MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
string oldpath,
[MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
string newpath);
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
public static extern IntPtr tmpfile ();
private static object tmpnam_lock = new object ();
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="tmpnam")]
private static extern IntPtr sys_tmpnam (StringBuilder s);
[Obsolete ("Syscall.mkstemp() should be preferred.")]
public static string tmpnam (StringBuilder s)
{
if (s != null && s.Capacity < L_tmpnam)
throw new ArgumentOutOfRangeException ("s", "s.Capacity < L_tmpnam");
lock (tmpnam_lock) {
IntPtr r = sys_tmpnam (s);
return UnixMarshal.PtrToString (r);
}
}
[Obsolete ("Syscall.mkstemp() should be preferred.")]
public static string tmpnam ()
{
lock (tmpnam_lock) {
IntPtr r = sys_tmpnam (null);
return UnixMarshal.PtrToString (r);
}
}
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
public static extern int fclose (IntPtr stream);
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
public static extern int fflush (IntPtr stream);
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
public static extern IntPtr fopen (
[MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
string path, string mode);
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
public static extern IntPtr freopen (
[MarshalAs (UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(FileNameMarshaler))]
string path, string mode, IntPtr stream);
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="Mono_Posix_Stdlib_setbuf")]
public static extern int setbuf (IntPtr stream, IntPtr buf);
[CLSCompliant (false)]
public static unsafe int setbuf (IntPtr stream, byte* buf)
{
return setbuf (stream, (IntPtr) buf);
}
[CLSCompliant (false)]
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="Mono_Posix_Stdlib_setvbuf")]
public static extern int setvbuf (IntPtr stream, IntPtr buf, int mode, ulong size);
[CLSCompliant (false)]
public static unsafe int setvbuf (IntPtr stream, byte* buf, int mode, ulong size)
{
return setvbuf (stream, (IntPtr) buf, mode, size);
}
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl,
EntryPoint="fprintf")]
private static extern int sys_fprintf (IntPtr stream, string format, string message);
public static int fprintf (IntPtr stream, string message)
{
return sys_fprintf (stream, "%s", message);
}
[Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
"Use fprintf (IntPtr, string) instead.")]
public static int fprintf (IntPtr stream, string format, params object[] parameters)
{
object[] _parameters = new object[checked(parameters.Length+2)];
_parameters [0] = stream;
_parameters [1] = format;
Array.Copy (parameters, 0, _parameters, 2, parameters.Length);
return (int) XPrintfFunctions.fprintf (_parameters);
}
/* SKIP: fscanf(3) */
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl,
EntryPoint="printf")]
private static extern int sys_printf (string format, string message);
public static int printf (string message)
{
return sys_printf ("%s", message);
}
[Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
"Use printf (string) instead.")]
public static int printf (string format, params object[] parameters)
{
object[] _parameters = new object[checked(parameters.Length+1)];
_parameters [0] = format;
Array.Copy (parameters, 0, _parameters, 1, parameters.Length);
return (int) XPrintfFunctions.printf (_parameters);
}
/* SKIP: scanf(3) */
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib_snprintf")]
private static extern int sys_snprintf (StringBuilder s, ulong n,
string format, string message);
[CLSCompliant (false)]
public static int snprintf (StringBuilder s, ulong n, string message)
{
if (n > (ulong) s.Capacity)
throw new ArgumentOutOfRangeException ("n", "n must be <= s.Capacity");
return sys_snprintf (s, n, "%s", message);
}
public static int snprintf (StringBuilder s, string message)
{
return sys_snprintf (s, (ulong) s.Capacity, "%s", message);
}
[CLSCompliant (false)]
[Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
"Use snprintf (StringBuilder, string) instead.")]
public static int snprintf (StringBuilder s, ulong n,
string format, params object[] parameters)
{
if (n > (ulong) s.Capacity)
throw new ArgumentOutOfRangeException ("n", "n must be <= s.Capacity");
object[] _parameters = new object[checked(parameters.Length+3)];
_parameters [0] = s;
_parameters [1] = n;
_parameters [2] = format;
Array.Copy (parameters, 0, _parameters, 3, parameters.Length);
return (int) XPrintfFunctions.snprintf (_parameters);
}
[CLSCompliant (false)]
[Obsolete ("Not necessarily portable due to cdecl restrictions.\n" +
"Use snprintf (StringBuilder, string) instead.")]
public static int snprintf (StringBuilder s,
string format, params object[] parameters)
{
object[] _parameters = new object[checked(parameters.Length+3)];
_parameters [0] = s;
_parameters [1] = (ulong) s.Capacity;
_parameters [2] = format;
Array.Copy (parameters, 0, _parameters, 3, parameters.Length);
return (int) XPrintfFunctions.snprintf (_parameters);
}
/*
* SKIP:
* sprintf(3)
* sscanf(3)
* vfprintf(3)
* vfscanf(3)
* vprintf(3)
* vscanf(3)
* vsnprintf(3)
* vsprint(3)
* vsscanf(3)
*/
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
public static extern int fgetc (IntPtr stream);
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="fgets")]
private static extern IntPtr sys_fgets (StringBuilder sb, int size, IntPtr stream);
public static StringBuilder fgets (StringBuilder sb, int size, IntPtr stream)
{
IntPtr r = sys_fgets (sb, size, stream);
if (r == IntPtr.Zero)
return null;
return sb;
}
public static StringBuilder fgets (StringBuilder sb, IntPtr stream)
{
return fgets (sb, sb.Capacity, stream);
}
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
public static extern int fputc (int c, IntPtr stream);
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
public static extern int fputs (string s, IntPtr stream);
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
public static extern int getc (IntPtr stream);
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
public static extern int getchar ();
/* SKIP: gets(3) */
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
public static extern int putc (int c, IntPtr stream);
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
public static extern int putchar (int c);
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
public static extern int puts (string s);
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
public static extern int ungetc (int c, IntPtr stream);
[CLSCompliant (false)]
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="Mono_Posix_Stdlib_fread")]
public static extern ulong fread (IntPtr ptr, ulong size, ulong nmemb, IntPtr stream);
[CLSCompliant (false)]
public static unsafe ulong fread (void* ptr, ulong size, ulong nmemb, IntPtr stream)
{
return fread ((IntPtr) ptr, size, nmemb, stream);
}
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="Mono_Posix_Stdlib_fread")]
private static extern ulong sys_fread ([Out] byte[] ptr,
ulong size, ulong nmemb, IntPtr stream);
[CLSCompliant (false)]
public static ulong fread (byte[] ptr, ulong size, ulong nmemb, IntPtr stream)
{
if ((size * nmemb) > (ulong) ptr.Length)
throw new ArgumentOutOfRangeException ("nmemb");
return sys_fread (ptr, size, nmemb, stream);
}
[CLSCompliant (false)]
public static ulong fread (byte[] ptr, IntPtr stream)
{
return fread (ptr, 1, (ulong) ptr.Length, stream);
}
[CLSCompliant (false)]
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="Mono_Posix_Stdlib_fwrite")]
public static extern ulong fwrite (IntPtr ptr, ulong size, ulong nmemb, IntPtr stream);
[CLSCompliant (false)]
public static unsafe ulong fwrite (void* ptr, ulong size, ulong nmemb, IntPtr stream)
{
return fwrite ((IntPtr) ptr, size, nmemb, stream);
}
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="Mono_Posix_Stdlib_fwrite")]
private static extern ulong sys_fwrite (byte[] ptr,
ulong size, ulong nmemb, IntPtr stream);
[CLSCompliant (false)]
public static ulong fwrite (byte[] ptr, ulong size, ulong nmemb, IntPtr stream)
{
if ((size * nmemb) > (ulong) ptr.Length)
throw new ArgumentOutOfRangeException ("nmemb");
return sys_fwrite (ptr, size, nmemb, stream);
}
[CLSCompliant (false)]
public static ulong fwrite (byte[] ptr, IntPtr stream)
{
return fwrite (ptr, 1, (ulong) ptr.Length, stream);
}
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="Mono_Posix_Stdlib_fgetpos")]
private static extern int sys_fgetpos (IntPtr stream, HandleRef pos);
public static int fgetpos (IntPtr stream, FilePosition pos)
{
return sys_fgetpos (stream, pos.Handle);
}
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="Mono_Posix_Stdlib_fseek")]
private static extern int sys_fseek (IntPtr stream, long offset, int origin);
[CLSCompliant (false)]
public static int fseek (IntPtr stream, long offset, SeekFlags origin)
{
int _origin = NativeConvert.FromSeekFlags (origin);
return sys_fseek (stream, offset, _origin);
}
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="Mono_Posix_Stdlib_fsetpos")]
private static extern int sys_fsetpos (IntPtr stream, HandleRef pos);
public static int fsetpos (IntPtr stream, FilePosition pos)
{
return sys_fsetpos (stream, pos.Handle);
}
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="Mono_Posix_Stdlib_ftell")]
public static extern long ftell (IntPtr stream);
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="Mono_Posix_Stdlib_rewind")]
public static extern int rewind (IntPtr stream);
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="Mono_Posix_Stdlib_clearerr")]
public static extern int clearerr (IntPtr stream);
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl)]
public static extern int feof (IntPtr stream);
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl)]
public static extern int ferror (IntPtr stream);
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="Mono_Posix_Stdlib_perror")]
private static extern int perror (string s, int err);
public static int perror (string s)
{
return perror (s, Marshal.GetLastWin32Error ());
}
//
// <stdlib.h>
//
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib_EXIT_FAILURE")]
private static extern int GetExitFailure();
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib_EXIT_SUCCESS")]
private static extern int GetExitSuccess ();
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib_MB_CUR_MAX")]
private static extern int GetMbCurMax ();
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
EntryPoint="Mono_Posix_Stdlib_RAND_MAX")]
private static extern int GetRandMax ();
[CLSCompliant (false)]
public static readonly int EXIT_FAILURE = GetExitFailure ();
[CLSCompliant (false)]
public static readonly int EXIT_SUCCESS = GetExitSuccess ();
[CLSCompliant (false)]
public static readonly int MB_CUR_MAX = GetMbCurMax ();
[CLSCompliant (false)]
public static readonly int RAND_MAX = GetRandMax ();
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl)]
public static extern int rand ();
[CLSCompliant (false)]
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl)]
public static extern void srand (uint seed);
// calloc(3):
// void *calloc (size_t nmemb, size_t size);
[CLSCompliant (false)]
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="Mono_Posix_Stdlib_calloc")]
public static extern IntPtr calloc (ulong nmemb, ulong size);
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl)]
public static extern void free (IntPtr ptr);
// malloc(3):
// void *malloc(size_t size);
[CLSCompliant (false)]
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="Mono_Posix_Stdlib_malloc")]
public static extern IntPtr malloc (ulong size);
// realloc(3):
// void *realloc(void *ptr, size_t size);
[CLSCompliant (false)]
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="Mono_Posix_Stdlib_realloc")]
public static extern IntPtr realloc (IntPtr ptr, ulong size);
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl)]
public static extern void abort ();
/* SKIP: atexit(3) -- the GC should have collected most references by the
* time this runs, so no delegates should exist, making it pointless. */
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl)]
public static extern void exit (int status);
[CLSCompliant (false)]
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl)]
public static extern void _Exit (int status);
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, EntryPoint="getenv")]
private static extern IntPtr sys_getenv (string name);
public static string getenv (string name)
{
IntPtr r = sys_getenv (name);
return UnixMarshal.PtrToString (r);
}
[CLSCompliant (false)]
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl, SetLastError=true)]
public static extern int system (string @string);
//
// <string.h>
//
private static object strerror_lock = new object ();
[DllImport (LIBC, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="strerror")]
private static extern IntPtr sys_strerror (int errnum);
[CLSCompliant (false)]
public static string strerror (Errno errnum)
{
int e = NativeConvert.FromErrno (errnum);
lock (strerror_lock) {
IntPtr r = sys_strerror (e);
return UnixMarshal.PtrToString (r);
}
}
// strlen(3):
// size_t strlen(const char *s);
[CLSCompliant (false)]
[DllImport (MPH, CallingConvention=CallingConvention.Cdecl,
SetLastError=true, EntryPoint="Mono_Posix_Stdlib_strlen")]
public static extern ulong strlen (IntPtr s);
}
#endregion // Classes
}
// vim: noexpandtab
|