X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=support%2Fstdio.c;h=590ff8cf821dca7ef57b2182caadf3b6151bc52f;hb=ab0b591ca59d99a2370bf9f579b091c5edf09ae5;hp=5973adb85a994a1d4667b5fdfdddde98e5e0ecea;hpb=6b6435d1b3206b0162c37e5ecce8d9a699fe6467;p=mono.git diff --git a/support/stdio.c b/support/stdio.c index 5973adb85a9..590ff8cf821 100644 --- a/support/stdio.c +++ b/support/stdio.c @@ -4,51 +4,74 @@ * Authors: * Jonathan Pryor (jonpryor@vt.edu) * - * Copyright (C) 2004-2005 Jonathan Pryor + * Copyright (C) 2004-2006 Jonathan Pryor */ #include #include #include +#include "map.h" #include "mph.h" G_BEGIN_DECLS -#ifndef PLATFORM_WIN32 gint32 Mono_Posix_Syscall_L_ctermid (void) { +#ifndef HOST_WIN32 return L_ctermid; +#else + return -1; +#endif } gint32 Mono_Posix_Syscall_L_cuserid (void) { +#if defined(__APPLE__) || defined (__OpenBSD__) || defined (HOST_WIN32) + return -1; +#else return L_cuserid; +#endif } -#endif /* ndef PLATFORM_WIN32 */ mph_size_t -Mono_Posix_Stdlib_fread (void *ptr, mph_size_t size, mph_size_t nmemb, FILE *stream) +Mono_Posix_Stdlib_fread (unsigned char *ptr, mph_size_t size, mph_size_t nmemb, void *stream) { mph_return_if_size_t_overflow (size); mph_return_if_size_t_overflow (nmemb); - return fread (ptr, (size_t) size, (size_t) nmemb, stream); + return fread (ptr, (size_t) size, (size_t) nmemb, (FILE*) stream); } mph_size_t -Mono_Posix_Stdlib_fwrite (const void *ptr, mph_size_t size, mph_size_t nmemb, FILE *stream) +Mono_Posix_Stdlib_fwrite (unsigned char *ptr, mph_size_t size, mph_size_t nmemb, void *stream) { mph_return_if_size_t_overflow (size); mph_return_if_size_t_overflow (nmemb); - return fwrite (ptr, (size_t) size, (size_t) nmemb, stream); + size_t ret = fwrite (ptr, (size_t) size, (size_t) nmemb, (FILE*) stream); +#ifdef HOST_WIN32 + // Workaround for a particular weirdness on Windows triggered by the + // StdioFileStreamTest.Write() test method. The test writes 15 bytes to a + // file, then rewinds the file pointer and reads the same bytes. It then + // writes 15 additional bytes to the file. This second write fails on + // Windows with 0 returned from fwrite(). Calling fseek() followed by a retry + // of fwrite() like we do here fixes the issue. + if (ret != nmemb) + { + fseek (stream, 0, SEEK_CUR); + ret = fwrite (ptr + (ret * nmemb), (size_t) size, (size_t) nmemb - ret, (FILE*) stream); + } +#endif + return ret; } #ifdef HAVE_VSNPRINTF gint32 +Mono_Posix_Stdlib_snprintf (char *s, mph_size_t n, char *format, ...); +gint32 Mono_Posix_Stdlib_snprintf (char *s, mph_size_t n, char *format, ...) { va_list ap; @@ -61,7 +84,7 @@ Mono_Posix_Stdlib_snprintf (char *s, mph_size_t n, char *format, ...) return r; } -#endif /* def HAVE_SNPRINTF */ +#endif /* def HAVE_VSNPRINTF */ gint32 Mono_Posix_Stdlib__IOFBF (void) @@ -111,19 +134,19 @@ Mono_Posix_Stdlib_L_tmpnam (void) return L_tmpnam; } -FILE* +void* Mono_Posix_Stdlib_stdin (void) { return stdin; } -FILE* +void* Mono_Posix_Stdlib_stdout (void) { return stdout; } -FILE* +void* Mono_Posix_Stdlib_stderr (void) { return stderr; @@ -135,15 +158,82 @@ Mono_Posix_Stdlib_TMP_MAX (void) return TMP_MAX; } +void* +Mono_Posix_Stdlib_tmpfile (void) +{ + return tmpfile (); +} + gint32 -Mono_Posix_Stdlib_setvbuf (FILE *stream, char *buf, int mode, mph_size_t size) +Mono_Posix_Stdlib_setvbuf (void* stream, void *buf, int mode, mph_size_t size) { mph_return_if_size_t_overflow (size); - return setvbuf (stream, buf, mode, (size_t) size); + return setvbuf (stream, (char *) buf, mode, (size_t) size); +} + +int +Mono_Posix_Stdlib_setbuf (void* stream, void* buf) +{ + setbuf (stream, buf); + return 0; +} + +void* +Mono_Posix_Stdlib_fopen (char* path, char* mode) +{ + return fopen (path, mode); +} + +void* +Mono_Posix_Stdlib_freopen (char* path, char* mode, void *stream) +{ + return freopen (path, mode, stream); +} + +gint32 +Mono_Posix_Stdlib_fprintf (void* stream, char* format, char *message) +{ + return fprintf (stream, format, message); +} + +gint32 +Mono_Posix_Stdlib_fgetc (void* stream) +{ + return fgetc (stream); +} + +char* +Mono_Posix_Stdlib_fgets (char* str, gint32 size, void* stream) +{ + return fgets (str, size, stream); +} + +gint32 +Mono_Posix_Stdlib_fputc (gint32 c, void* stream) +{ + return fputc (c, stream); +} + +gint32 +Mono_Posix_Stdlib_fputs (char* s, void* stream) +{ + return fputs (s, stream); +} + +gint32 +Mono_Posix_Stdlib_fclose (void* stream) +{ + return fclose (stream); } gint32 -Mono_Posix_Stdlib_fseek (FILE* stream, gint64 offset, int origin) +Mono_Posix_Stdlib_fflush (void* stream) +{ + return fflush (stream); +} + +gint32 +Mono_Posix_Stdlib_fseek (void* stream, gint64 offset, int origin) { mph_return_if_long_overflow (offset); @@ -151,12 +241,12 @@ Mono_Posix_Stdlib_fseek (FILE* stream, gint64 offset, int origin) } gint64 -Mono_Posix_Stdlib_ftell (FILE* stream) +Mono_Posix_Stdlib_ftell (void* stream) { return ftell (stream); } -fpos_t* +void* Mono_Posix_Stdlib_CreateFilePosition (void) { fpos_t* pos = malloc (sizeof(fpos_t)); @@ -164,15 +254,89 @@ Mono_Posix_Stdlib_CreateFilePosition (void) } gint32 -Mono_Posix_Stdlib_fgetpos (FILE* stream, fpos_t *pos) +Mono_Posix_Stdlib_fgetpos (void* stream, void *pos) +{ + return fgetpos (stream, (fpos_t*) pos); +} + +gint32 +Mono_Posix_Stdlib_fsetpos (void* stream, void *pos) +{ + return fsetpos (stream, (fpos_t*) pos); +} + +int +Mono_Posix_Stdlib_rewind (void* stream) +{ + do { + rewind (stream); + } while (errno == EINTR); + mph_return_if_val_in_list5(errno, EAGAIN, EBADF, EFBIG, EINVAL, EIO); + mph_return_if_val_in_list5(errno, ENOSPC, ENXIO, EOVERFLOW, EPIPE, ESPIPE); + return 0; +} + +int +Mono_Posix_Stdlib_clearerr (void* stream) +{ + clearerr (((FILE*) stream)); + return 0; +} + +gint32 +Mono_Posix_Stdlib_ungetc (gint32 c, void* stream) { - return fgetpos (stream, pos); + return ungetc (c, stream); } gint32 -Mono_Posix_Stdlib_fsetpos (FILE* stream, fpos_t *pos) +Mono_Posix_Stdlib_feof (void* stream) { - return fsetpos (stream, pos); + return feof (((FILE*) stream)); +} + +gint32 +Mono_Posix_Stdlib_ferror (void* stream) +{ + return ferror (((FILE*) stream)); +} + +int +Mono_Posix_Stdlib_perror (const char* s, int err) +{ + errno = err; + perror (s); + return 0; +} + +#define MPH_FPOS_LENGTH (sizeof(fpos_t)*2) + +int +Mono_Posix_Stdlib_DumpFilePosition (char *dest, void *pos, gint32 len) +{ + char *destp; + unsigned char *posp, *pose; + + if (dest == NULL) + return MPH_FPOS_LENGTH; + + if (pos == NULL || len <= 0) { + errno = EINVAL; + return -1; + } + + posp = (unsigned char*) pos; + pose = posp + sizeof(fpos_t); + destp = dest; + + for ( ; posp < pose && len > 1; destp += 2, ++posp, len -= 2) { + sprintf (destp, "%02X", *posp); + } + + if (len) + dest[MPH_FPOS_LENGTH] = '\0'; + + return destp - dest; } G_END_DECLS