2 * <stdio.h> wrapper functions.
5 * Jonathan Pryor (jonpryor@vt.edu)
7 * Copyright (C) 2004-2006 Jonathan Pryor
20 Mono_Posix_Syscall_L_ctermid (void)
30 Mono_Posix_Syscall_L_cuserid (void)
32 #if defined(__APPLE__) || defined (__OpenBSD__) || defined (HOST_WIN32)
40 Mono_Posix_Stdlib_fread (unsigned char *ptr
, mph_size_t size
, mph_size_t nmemb
, void *stream
)
42 mph_return_if_size_t_overflow (size
);
43 mph_return_if_size_t_overflow (nmemb
);
45 return fread (ptr
, (size_t) size
, (size_t) nmemb
, (FILE*) stream
);
49 Mono_Posix_Stdlib_fwrite (unsigned char *ptr
, mph_size_t size
, mph_size_t nmemb
, void *stream
)
51 mph_return_if_size_t_overflow (size
);
52 mph_return_if_size_t_overflow (nmemb
);
54 size_t ret
= fwrite (ptr
, (size_t) size
, (size_t) nmemb
, (FILE*) stream
);
56 // Workaround for a particular weirdness on Windows triggered by the
57 // StdioFileStreamTest.Write() test method. The test writes 15 bytes to a
58 // file, then rewinds the file pointer and reads the same bytes. It then
59 // writes 15 additional bytes to the file. This second write fails on
60 // Windows with 0 returned from fwrite(). Calling fseek() followed by a retry
61 // of fwrite() like we do here fixes the issue.
64 fseek (stream
, 0, SEEK_CUR
);
65 ret
= fwrite (ptr
+ (ret
* nmemb
), (size_t) size
, (size_t) nmemb
- ret
, (FILE*) stream
);
73 Mono_Posix_Stdlib_snprintf (char *s
, mph_size_t n
, char *format
, ...);
75 Mono_Posix_Stdlib_snprintf (char *s
, mph_size_t n
, char *format
, ...)
79 mph_return_if_size_t_overflow (n
);
81 va_start (ap
, format
);
82 r
= vsnprintf (s
, (size_t) n
, format
, ap
);
87 #endif /* def HAVE_VSNPRINTF */
90 Mono_Posix_Stdlib__IOFBF (void)
96 Mono_Posix_Stdlib__IOLBF (void)
102 Mono_Posix_Stdlib__IONBF (void)
108 Mono_Posix_Stdlib_BUFSIZ (void)
114 Mono_Posix_Stdlib_EOF (void)
120 Mono_Posix_Stdlib_FOPEN_MAX (void)
126 Mono_Posix_Stdlib_FILENAME_MAX (void)
132 Mono_Posix_Stdlib_L_tmpnam (void)
138 Mono_Posix_Stdlib_stdin (void)
144 Mono_Posix_Stdlib_stdout (void)
150 Mono_Posix_Stdlib_stderr (void)
156 Mono_Posix_Stdlib_TMP_MAX (void)
162 Mono_Posix_Stdlib_tmpfile (void)
168 Mono_Posix_Stdlib_setvbuf (void* stream
, void *buf
, int mode
, mph_size_t size
)
170 mph_return_if_size_t_overflow (size
);
171 return setvbuf (stream
, (char *) buf
, mode
, (size_t) size
);
175 Mono_Posix_Stdlib_setbuf (void* stream
, void* buf
)
177 setbuf (stream
, buf
);
182 Mono_Posix_Stdlib_fopen (char* path
, char* mode
)
184 return fopen (path
, mode
);
188 Mono_Posix_Stdlib_freopen (char* path
, char* mode
, void *stream
)
190 return freopen (path
, mode
, stream
);
194 Mono_Posix_Stdlib_fprintf (void* stream
, char* format
, char *message
)
196 return fprintf (stream
, format
, message
);
200 Mono_Posix_Stdlib_fgetc (void* stream
)
202 return fgetc (stream
);
206 Mono_Posix_Stdlib_fgets (char* str
, gint32 size
, void* stream
)
208 return fgets (str
, size
, stream
);
212 Mono_Posix_Stdlib_fputc (gint32 c
, void* stream
)
214 return fputc (c
, stream
);
218 Mono_Posix_Stdlib_fputs (char* s
, void* stream
)
220 return fputs (s
, stream
);
224 Mono_Posix_Stdlib_fclose (void* stream
)
226 return fclose (stream
);
230 Mono_Posix_Stdlib_fflush (void* stream
)
232 return fflush (stream
);
236 Mono_Posix_Stdlib_fseek (void* stream
, gint64 offset
, int origin
)
238 mph_return_if_long_overflow (offset
);
240 return fseek (stream
, offset
, origin
);
244 Mono_Posix_Stdlib_ftell (void* stream
)
246 return ftell (stream
);
250 Mono_Posix_Stdlib_CreateFilePosition (void)
252 fpos_t* pos
= malloc (sizeof(fpos_t));
257 Mono_Posix_Stdlib_fgetpos (void* stream
, void *pos
)
259 return fgetpos (stream
, (fpos_t*) pos
);
263 Mono_Posix_Stdlib_fsetpos (void* stream
, void *pos
)
265 return fsetpos (stream
, (fpos_t*) pos
);
269 Mono_Posix_Stdlib_rewind (void* stream
)
273 } while (errno
== EINTR
);
274 mph_return_if_val_in_list5(errno
, EAGAIN
, EBADF
, EFBIG
, EINVAL
, EIO
);
275 mph_return_if_val_in_list5(errno
, ENOSPC
, ENXIO
, EOVERFLOW
, EPIPE
, ESPIPE
);
280 Mono_Posix_Stdlib_clearerr (void* stream
)
282 clearerr (((FILE*) stream
));
287 Mono_Posix_Stdlib_ungetc (gint32 c
, void* stream
)
289 return ungetc (c
, stream
);
293 Mono_Posix_Stdlib_feof (void* stream
)
295 return feof (((FILE*) stream
));
299 Mono_Posix_Stdlib_ferror (void* stream
)
301 return ferror (((FILE*) stream
));
305 Mono_Posix_Stdlib_perror (const char* s
, int err
)
312 #define MPH_FPOS_LENGTH (sizeof(fpos_t)*2)
315 Mono_Posix_Stdlib_DumpFilePosition (char *dest
, void *pos
, gint32 len
)
318 unsigned char *posp
, *pose
;
321 return MPH_FPOS_LENGTH
;
323 if (pos
== NULL
|| len
<= 0) {
328 posp
= (unsigned char*) pos
;
329 pose
= posp
+ sizeof(fpos_t);
332 for ( ; posp
< pose
&& len
> 1; destp
+= 2, ++posp
, len
-= 2) {
333 sprintf (destp
, "%02X", *posp
);
337 dest
[MPH_FPOS_LENGTH
] = '\0';