Merge branch 'master' of github.com:/mono/mono
[mono-project/dkf.git] / support / stdio.c
blob78c900e9d9b2afeb460a3d24d7d0614d7bf7a027
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 #ifdef __APPLE__
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 return fwrite (ptr, (size_t) size, (size_t) nmemb, (FILE*) stream);
55 #ifdef HAVE_VSNPRINTF
56 gint32
57 Mono_Posix_Stdlib_snprintf (char *s, mph_size_t n, char *format, ...);
58 gint32
59 Mono_Posix_Stdlib_snprintf (char *s, mph_size_t n, char *format, ...)
61 va_list ap;
62 gint32 r;
63 mph_return_if_size_t_overflow (n);
65 va_start (ap, format);
66 r = vsnprintf (s, (size_t) n, format, ap);
67 va_end (ap);
69 return r;
71 #endif /* def HAVE_VSNPRINTF */
73 gint32
74 Mono_Posix_Stdlib__IOFBF (void)
76 return _IOFBF;
79 gint32
80 Mono_Posix_Stdlib__IOLBF (void)
82 return _IOLBF;
85 gint32
86 Mono_Posix_Stdlib__IONBF (void)
88 return _IONBF;
91 gint32
92 Mono_Posix_Stdlib_BUFSIZ (void)
94 return BUFSIZ;
97 gint32
98 Mono_Posix_Stdlib_EOF (void)
100 return EOF;
103 gint32
104 Mono_Posix_Stdlib_FOPEN_MAX (void)
106 return FOPEN_MAX;
109 gint32
110 Mono_Posix_Stdlib_FILENAME_MAX (void)
112 return FILENAME_MAX;
115 gint32
116 Mono_Posix_Stdlib_L_tmpnam (void)
118 return L_tmpnam;
121 void*
122 Mono_Posix_Stdlib_stdin (void)
124 return stdin;
127 void*
128 Mono_Posix_Stdlib_stdout (void)
130 return stdout;
133 void*
134 Mono_Posix_Stdlib_stderr (void)
136 return stderr;
139 gint32
140 Mono_Posix_Stdlib_TMP_MAX (void)
142 return TMP_MAX;
145 gint32
146 Mono_Posix_Stdlib_setvbuf (void* stream, void *buf, int mode, mph_size_t size)
148 mph_return_if_size_t_overflow (size);
149 return setvbuf (stream, (char *) buf, mode, (size_t) size);
152 int
153 Mono_Posix_Stdlib_setbuf (void* stream, void* buf)
155 setbuf (stream, buf);
156 return 0;
159 gint32
160 Mono_Posix_Stdlib_fseek (void* stream, gint64 offset, int origin)
162 mph_return_if_long_overflow (offset);
164 return fseek (stream, offset, origin);
167 gint64
168 Mono_Posix_Stdlib_ftell (void* stream)
170 return ftell (stream);
173 void*
174 Mono_Posix_Stdlib_CreateFilePosition (void)
176 fpos_t* pos = malloc (sizeof(fpos_t));
177 return pos;
180 gint32
181 Mono_Posix_Stdlib_fgetpos (void* stream, void *pos)
183 return fgetpos (stream, (fpos_t*) pos);
186 gint32
187 Mono_Posix_Stdlib_fsetpos (void* stream, void *pos)
189 return fsetpos (stream, (fpos_t*) pos);
193 Mono_Posix_Stdlib_rewind (void* stream)
195 do {
196 rewind (stream);
197 } while (errno == EINTR);
198 mph_return_if_val_in_list5(errno, EAGAIN, EBADF, EFBIG, EINVAL, EIO);
199 mph_return_if_val_in_list5(errno, ENOSPC, ENXIO, EOVERFLOW, EPIPE, ESPIPE);
200 return 0;
204 Mono_Posix_Stdlib_clearerr (void* stream)
206 clearerr (((FILE*) stream));
207 return 0;
211 Mono_Posix_Stdlib_perror (const char* s, int err)
213 errno = err;
214 perror (s);
215 return 0;
218 #define MPH_FPOS_LENGTH (sizeof(fpos_t)*2)
221 Mono_Posix_Stdlib_DumpFilePosition (char *dest, void *pos, gint32 len)
223 char *destp;
224 unsigned char *posp, *pose;
226 if (dest == NULL)
227 return MPH_FPOS_LENGTH;
229 if (pos == NULL || len <= 0) {
230 errno = EINVAL;
231 return -1;
234 posp = (unsigned char*) pos;
235 pose = posp + sizeof(fpos_t);
236 destp = dest;
238 for ( ; posp < pose && len > 1; destp += 2, ++posp, len -= 2) {
239 sprintf (destp, "%02X", *posp);
242 if (len)
243 dest[MPH_FPOS_LENGTH] = '\0';
245 return destp - dest;
248 G_END_DECLS
251 * vim: noexpandtab