[runtime] Don't do GC Unsafe transition in mono_gchandle_free
[mono-project.git] / support / stdio.c
blob590ff8cf821dca7ef57b2182caadf3b6151bc52f
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 gint32
20 Mono_Posix_Syscall_L_ctermid (void)
22 #ifndef HOST_WIN32
23 return L_ctermid;
24 #else
25 return -1;
26 #endif
29 gint32
30 Mono_Posix_Syscall_L_cuserid (void)
32 #if defined(__APPLE__) || defined (__OpenBSD__) || defined (HOST_WIN32)
33 return -1;
34 #else
35 return L_cuserid;
36 #endif
39 mph_size_t
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);
48 mph_size_t
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);
55 #ifdef HOST_WIN32
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.
62 if (ret != nmemb)
64 fseek (stream, 0, SEEK_CUR);
65 ret = fwrite (ptr + (ret * nmemb), (size_t) size, (size_t) nmemb - ret, (FILE*) stream);
67 #endif
68 return ret;
71 #ifdef HAVE_VSNPRINTF
72 gint32
73 Mono_Posix_Stdlib_snprintf (char *s, mph_size_t n, char *format, ...);
74 gint32
75 Mono_Posix_Stdlib_snprintf (char *s, mph_size_t n, char *format, ...)
77 va_list ap;
78 gint32 r;
79 mph_return_if_size_t_overflow (n);
81 va_start (ap, format);
82 r = vsnprintf (s, (size_t) n, format, ap);
83 va_end (ap);
85 return r;
87 #endif /* def HAVE_VSNPRINTF */
89 gint32
90 Mono_Posix_Stdlib__IOFBF (void)
92 return _IOFBF;
95 gint32
96 Mono_Posix_Stdlib__IOLBF (void)
98 return _IOLBF;
101 gint32
102 Mono_Posix_Stdlib__IONBF (void)
104 return _IONBF;
107 gint32
108 Mono_Posix_Stdlib_BUFSIZ (void)
110 return BUFSIZ;
113 gint32
114 Mono_Posix_Stdlib_EOF (void)
116 return EOF;
119 gint32
120 Mono_Posix_Stdlib_FOPEN_MAX (void)
122 return FOPEN_MAX;
125 gint32
126 Mono_Posix_Stdlib_FILENAME_MAX (void)
128 return FILENAME_MAX;
131 gint32
132 Mono_Posix_Stdlib_L_tmpnam (void)
134 return L_tmpnam;
137 void*
138 Mono_Posix_Stdlib_stdin (void)
140 return stdin;
143 void*
144 Mono_Posix_Stdlib_stdout (void)
146 return stdout;
149 void*
150 Mono_Posix_Stdlib_stderr (void)
152 return stderr;
155 gint32
156 Mono_Posix_Stdlib_TMP_MAX (void)
158 return TMP_MAX;
161 void*
162 Mono_Posix_Stdlib_tmpfile (void)
164 return tmpfile ();
167 gint32
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);
174 int
175 Mono_Posix_Stdlib_setbuf (void* stream, void* buf)
177 setbuf (stream, buf);
178 return 0;
181 void*
182 Mono_Posix_Stdlib_fopen (char* path, char* mode)
184 return fopen (path, mode);
187 void*
188 Mono_Posix_Stdlib_freopen (char* path, char* mode, void *stream)
190 return freopen (path, mode, stream);
193 gint32
194 Mono_Posix_Stdlib_fprintf (void* stream, char* format, char *message)
196 return fprintf (stream, format, message);
199 gint32
200 Mono_Posix_Stdlib_fgetc (void* stream)
202 return fgetc (stream);
205 char*
206 Mono_Posix_Stdlib_fgets (char* str, gint32 size, void* stream)
208 return fgets (str, size, stream);
211 gint32
212 Mono_Posix_Stdlib_fputc (gint32 c, void* stream)
214 return fputc (c, stream);
217 gint32
218 Mono_Posix_Stdlib_fputs (char* s, void* stream)
220 return fputs (s, stream);
223 gint32
224 Mono_Posix_Stdlib_fclose (void* stream)
226 return fclose (stream);
229 gint32
230 Mono_Posix_Stdlib_fflush (void* stream)
232 return fflush (stream);
235 gint32
236 Mono_Posix_Stdlib_fseek (void* stream, gint64 offset, int origin)
238 mph_return_if_long_overflow (offset);
240 return fseek (stream, offset, origin);
243 gint64
244 Mono_Posix_Stdlib_ftell (void* stream)
246 return ftell (stream);
249 void*
250 Mono_Posix_Stdlib_CreateFilePosition (void)
252 fpos_t* pos = malloc (sizeof(fpos_t));
253 return pos;
256 gint32
257 Mono_Posix_Stdlib_fgetpos (void* stream, void *pos)
259 return fgetpos (stream, (fpos_t*) pos);
262 gint32
263 Mono_Posix_Stdlib_fsetpos (void* stream, void *pos)
265 return fsetpos (stream, (fpos_t*) pos);
269 Mono_Posix_Stdlib_rewind (void* stream)
271 do {
272 rewind (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);
276 return 0;
280 Mono_Posix_Stdlib_clearerr (void* stream)
282 clearerr (((FILE*) stream));
283 return 0;
286 gint32
287 Mono_Posix_Stdlib_ungetc (gint32 c, void* stream)
289 return ungetc (c, stream);
292 gint32
293 Mono_Posix_Stdlib_feof (void* stream)
295 return feof (((FILE*) stream));
298 gint32
299 Mono_Posix_Stdlib_ferror (void* stream)
301 return ferror (((FILE*) stream));
305 Mono_Posix_Stdlib_perror (const char* s, int err)
307 errno = err;
308 perror (s);
309 return 0;
312 #define MPH_FPOS_LENGTH (sizeof(fpos_t)*2)
315 Mono_Posix_Stdlib_DumpFilePosition (char *dest, void *pos, gint32 len)
317 char *destp;
318 unsigned char *posp, *pose;
320 if (dest == NULL)
321 return MPH_FPOS_LENGTH;
323 if (pos == NULL || len <= 0) {
324 errno = EINVAL;
325 return -1;
328 posp = (unsigned char*) pos;
329 pose = posp + sizeof(fpos_t);
330 destp = dest;
332 for ( ; posp < pose && len > 1; destp += 2, ++posp, len -= 2) {
333 sprintf (destp, "%02X", *posp);
336 if (len)
337 dest[MPH_FPOS_LENGTH] = '\0';
339 return destp - dest;
342 G_END_DECLS
345 * vim: noexpandtab