[Facades] Implementation types from CoreFX
[mono-project.git] / support / stdio.c
blobb7cc689deb51087588140936b4f99f1cd10d5bf8
1 /*
2 * <stdio.h> wrapper functions.
4 * Authors:
5 * Jonathan Pryor (jonpryor@vt.edu)
7 * Copyright (C) 2004-2006 Jonathan Pryor
8 */
10 #include <stdarg.h>
11 #include <stdio.h>
12 #include <stdlib.h>
14 #include "map.h"
15 #include "mph.h"
17 G_BEGIN_DECLS
19 #ifndef HOST_WIN32
20 gint32
21 Mono_Posix_Syscall_L_ctermid (void)
23 return L_ctermid;
26 gint32
27 Mono_Posix_Syscall_L_cuserid (void)
29 #if defined(__APPLE__) || defined (__OpenBSD__)
30 return -1;
31 #else
32 return L_cuserid;
33 #endif
35 #endif /* ndef HOST_WIN32 */
37 mph_size_t
38 Mono_Posix_Stdlib_fread (unsigned char *ptr, mph_size_t size, mph_size_t nmemb, void *stream)
40 mph_return_if_size_t_overflow (size);
41 mph_return_if_size_t_overflow (nmemb);
43 return fread (ptr, (size_t) size, (size_t) nmemb, (FILE*) stream);
46 mph_size_t
47 Mono_Posix_Stdlib_fwrite (unsigned char *ptr, mph_size_t size, mph_size_t nmemb, void *stream)
49 mph_return_if_size_t_overflow (size);
50 mph_return_if_size_t_overflow (nmemb);
52 size_t ret = fwrite (ptr, (size_t) size, (size_t) nmemb, (FILE*) stream);
53 #ifdef HOST_WIN32
54 // Workaround for a particular weirdness on Windows triggered by the
55 // StdioFileStreamTest.Write() test method. The test writes 15 bytes to a
56 // file, then rewinds the file pointer and reads the same bytes. It then
57 // writes 15 additional bytes to the file. This second write fails on
58 // Windows with 0 returned from fwrite(). Calling fseek() followed by a retry
59 // of fwrite() like we do here fixes the issue.
60 if (ret != nmemb)
62 fseek (stream, 0, SEEK_CUR);
63 ret = fwrite (ptr + (ret * nmemb), (size_t) size, (size_t) nmemb - ret, (FILE*) stream);
65 #endif
66 return ret;
69 #ifdef HAVE_VSNPRINTF
70 gint32
71 Mono_Posix_Stdlib_snprintf (char *s, mph_size_t n, char *format, ...);
72 gint32
73 Mono_Posix_Stdlib_snprintf (char *s, mph_size_t n, char *format, ...)
75 va_list ap;
76 gint32 r;
77 mph_return_if_size_t_overflow (n);
79 va_start (ap, format);
80 r = vsnprintf (s, (size_t) n, format, ap);
81 va_end (ap);
83 return r;
85 #endif /* def HAVE_VSNPRINTF */
87 gint32
88 Mono_Posix_Stdlib__IOFBF (void)
90 return _IOFBF;
93 gint32
94 Mono_Posix_Stdlib__IOLBF (void)
96 return _IOLBF;
99 gint32
100 Mono_Posix_Stdlib__IONBF (void)
102 return _IONBF;
105 gint32
106 Mono_Posix_Stdlib_BUFSIZ (void)
108 return BUFSIZ;
111 gint32
112 Mono_Posix_Stdlib_EOF (void)
114 return EOF;
117 gint32
118 Mono_Posix_Stdlib_FOPEN_MAX (void)
120 return FOPEN_MAX;
123 gint32
124 Mono_Posix_Stdlib_FILENAME_MAX (void)
126 return FILENAME_MAX;
129 gint32
130 Mono_Posix_Stdlib_L_tmpnam (void)
132 return L_tmpnam;
135 void*
136 Mono_Posix_Stdlib_stdin (void)
138 return stdin;
141 void*
142 Mono_Posix_Stdlib_stdout (void)
144 return stdout;
147 void*
148 Mono_Posix_Stdlib_stderr (void)
150 return stderr;
153 gint32
154 Mono_Posix_Stdlib_TMP_MAX (void)
156 return TMP_MAX;
159 void*
160 Mono_Posix_Stdlib_tmpfile (void)
162 return tmpfile ();
165 gint32
166 Mono_Posix_Stdlib_setvbuf (void* stream, void *buf, int mode, mph_size_t size)
168 mph_return_if_size_t_overflow (size);
169 return setvbuf (stream, (char *) buf, mode, (size_t) size);
172 int
173 Mono_Posix_Stdlib_setbuf (void* stream, void* buf)
175 setbuf (stream, buf);
176 return 0;
179 void*
180 Mono_Posix_Stdlib_fopen (char* path, char* mode)
182 return fopen (path, mode);
185 void*
186 Mono_Posix_Stdlib_freopen (char* path, char* mode, void *stream)
188 return freopen (path, mode, stream);
191 gint32
192 Mono_Posix_Stdlib_fprintf (void* stream, char* format, char *message)
194 return fprintf (stream, format, message);
197 gint32
198 Mono_Posix_Stdlib_fgetc (void* stream)
200 return fgetc (stream);
203 char*
204 Mono_Posix_Stdlib_fgets (char* str, gint32 size, void* stream)
206 return fgets (str, size, stream);
209 gint32
210 Mono_Posix_Stdlib_fputc (gint32 c, void* stream)
212 return fputc (c, stream);
215 gint32
216 Mono_Posix_Stdlib_fputs (char* s, void* stream)
218 return fputs (s, stream);
221 gint32
222 Mono_Posix_Stdlib_fclose (void* stream)
224 return fclose (stream);
227 gint32
228 Mono_Posix_Stdlib_fflush (void* stream)
230 return fflush (stream);
233 gint32
234 Mono_Posix_Stdlib_fseek (void* stream, gint64 offset, int origin)
236 mph_return_if_long_overflow (offset);
238 return fseek (stream, offset, origin);
241 gint64
242 Mono_Posix_Stdlib_ftell (void* stream)
244 return ftell (stream);
247 void*
248 Mono_Posix_Stdlib_CreateFilePosition (void)
250 fpos_t* pos = malloc (sizeof(fpos_t));
251 return pos;
254 gint32
255 Mono_Posix_Stdlib_fgetpos (void* stream, void *pos)
257 return fgetpos (stream, (fpos_t*) pos);
260 gint32
261 Mono_Posix_Stdlib_fsetpos (void* stream, void *pos)
263 return fsetpos (stream, (fpos_t*) pos);
267 Mono_Posix_Stdlib_rewind (void* stream)
269 do {
270 rewind (stream);
271 } while (errno == EINTR);
272 mph_return_if_val_in_list5(errno, EAGAIN, EBADF, EFBIG, EINVAL, EIO);
273 mph_return_if_val_in_list5(errno, ENOSPC, ENXIO, EOVERFLOW, EPIPE, ESPIPE);
274 return 0;
278 Mono_Posix_Stdlib_clearerr (void* stream)
280 clearerr (((FILE*) stream));
281 return 0;
284 gint32
285 Mono_Posix_Stdlib_ungetc (gint32 c, void* stream)
287 return ungetc (c, stream);
290 gint32
291 Mono_Posix_Stdlib_feof (void* stream)
293 return feof (((FILE*) stream));
296 gint32
297 Mono_Posix_Stdlib_ferror (void* stream)
299 return ferror (((FILE*) stream));
303 Mono_Posix_Stdlib_perror (const char* s, int err)
305 errno = err;
306 perror (s);
307 return 0;
310 #define MPH_FPOS_LENGTH (sizeof(fpos_t)*2)
313 Mono_Posix_Stdlib_DumpFilePosition (char *dest, void *pos, gint32 len)
315 char *destp;
316 unsigned char *posp, *pose;
318 if (dest == NULL)
319 return MPH_FPOS_LENGTH;
321 if (pos == NULL || len <= 0) {
322 errno = EINVAL;
323 return -1;
326 posp = (unsigned char*) pos;
327 pose = posp + sizeof(fpos_t);
328 destp = dest;
330 for ( ; posp < pose && len > 1; destp += 2, ++posp, len -= 2) {
331 sprintf (destp, "%02X", *posp);
334 if (len)
335 dest[MPH_FPOS_LENGTH] = '\0';
337 return destp - dest;
340 G_END_DECLS
343 * vim: noexpandtab