From c042e13ef60b707fd104fcb938d2510b1c89e688 Mon Sep 17 00:00:00 2001 From: Alexandre Julliard Date: Thu, 19 Feb 2004 01:13:12 +0000 Subject: [PATCH] Fixed varargs handling in the scanf functions (spotted by Eric Pouech). --- dlls/msvcrt/file.c | 31 ---------- dlls/msvcrt/scanf.c | 126 +++++++++++++++++++++++++++++++++++------ dlls/msvcrt/scanf.h | 15 ++--- include/msvcrt/stdio.h | 8 +++ 4 files changed, 124 insertions(+), 56 deletions(-) diff --git a/dlls/msvcrt/file.c b/dlls/msvcrt/file.c index 668fed49efd..18732215d6a 100644 --- a/dlls/msvcrt/file.c +++ b/dlls/msvcrt/file.c @@ -73,9 +73,6 @@ MSVCRT_FILE* MSVCRT_files[MSVCRT_MAX_FILES]; int MSVCRT_flags[MSVCRT_MAX_FILES]; char *MSVCRT_tempfiles[MSVCRT_MAX_FILES]; MSVCRT_FILE MSVCRT__iob[3]; -#define MSVCRT_stdin (MSVCRT__iob+STDIN_FILENO) -#define MSVCRT_stdout (MSVCRT__iob+STDOUT_FILENO) -#define MSVCRT_stderr (MSVCRT__iob+STDERR_FILENO) static int MSVCRT_fdstart = 3; /* first unallocated fd */ static int MSVCRT_fdend = 3; /* highest allocated fd */ @@ -2374,34 +2371,6 @@ int _wremove(const MSVCRT_wchar_t *path) return -1; } -/********************************************************************* - * scanf (MSVCRT.@) - */ -int MSVCRT_scanf(const char *format, ...) -{ - va_list valist; - int res; - - va_start(valist, format); - res = MSVCRT_fscanf(MSVCRT_stdin, format, valist); - va_end(valist); - return res; -} - -/********************************************************************* - * wscanf (MSVCRT.@) - */ -int MSVCRT_wscanf(const MSVCRT_wchar_t *format, ...) -{ - va_list valist; - int res; - - va_start(valist, format); - res = MSVCRT_fwscanf(MSVCRT_stdin, format, valist); - va_end(valist); - return res; -} - /********************************************************************* * rename (MSVCRT.@) */ diff --git a/dlls/msvcrt/scanf.c b/dlls/msvcrt/scanf.c index 58cd6bca02e..e7b240b1a32 100644 --- a/dlls/msvcrt/scanf.c +++ b/dlls/msvcrt/scanf.c @@ -38,6 +38,8 @@ WINE_DEFAULT_DEBUG_CHANNEL(msvcrt); +extern MSVCRT_FILE MSVCRT__iob[]; + /* helper function for *scanf. Returns the value of character c in the * given base, or -1 if the given character is not a digit of the base. */ @@ -60,43 +62,135 @@ static int wchar2digit(MSVCRT_wchar_t c, int base) { return -1; } - -/********************************************************************* - * fscanf (MSVCRT.@) - */ +/* vfscanf */ #undef WIDE_SCANF #undef CONSOLE #undef STRING #include "scanf.h" -/********************************************************************* - * fwscanf (MSVCRT.@) - */ +/* vfwscanf */ #define WIDE_SCANF 1 #undef CONSOLE #undef STRING #include "scanf.h" -/********************************************************************* - * sscanf (MSVCRT.@) - */ +/* vsscanf */ #undef WIDE_SCANF #undef CONSOLE #define STRING 1 #include "scanf.h" -/********************************************************************* - * swscanf (MSVCRT.@) - */ +/* vswscanf */ #define WIDE_SCANF 1 #undef CONSOLE #define STRING 1 #include "scanf.h" -/********************************************************************* - * _cscanf (MSVCRT.@) - */ +/* vcscanf */ #undef WIDE_SCANF #define CONSOLE 1 #undef STRING #include "scanf.h" + + +/********************************************************************* + * fscanf (MSVCRT.@) + */ +int MSVCRT_fscanf(MSVCRT_FILE *file, const char *format, ...) +{ + va_list valist; + int res; + + va_start(valist, format); + res = MSVCRT_vfscanf(file, format, valist); + va_end(valist); + return res; +} + +/********************************************************************* + * scanf (MSVCRT.@) + */ +int MSVCRT_scanf(const char *format, ...) +{ + va_list valist; + int res; + + va_start(valist, format); + res = MSVCRT_vfscanf(MSVCRT_stdin, format, valist); + va_end(valist); + return res; +} + +/********************************************************************* + * fwscanf (MSVCRT.@) + */ +int MSVCRT_fwscanf(MSVCRT_FILE *file, const MSVCRT_wchar_t *format, ...) +{ + va_list valist; + int res; + + va_start(valist, format); + res = MSVCRT_vfwscanf(file, format, valist); + va_end(valist); + return res; +} + + +/********************************************************************* + * wscanf (MSVCRT.@) + */ +int MSVCRT_wscanf(const MSVCRT_wchar_t *format, ...) +{ + va_list valist; + int res; + + va_start(valist, format); + res = MSVCRT_vfwscanf(MSVCRT_stdin, format, valist); + va_end(valist); + return res; +} + + +/********************************************************************* + * sscanf (MSVCRT.@) + */ +int MSVCRT_sscanf(const char *str, const char *format, ...) +{ + va_list valist; + int res; + + va_start(valist, format); + res = MSVCRT_vsscanf(str, format, valist); + va_end(valist); + return res; +} + + +/********************************************************************* + * swscanf (MSVCRT.@) + */ +int MSVCRT_swscanf(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ...) +{ + va_list valist; + int res; + + va_start(valist, format); + res = MSVCRT_vswscanf(str, format, valist); + va_end(valist); + return res; +} + + +/********************************************************************* + * _cscanf (MSVCRT.@) + */ +int _cscanf(const char *format, ...) +{ + va_list valist; + int res; + + va_start(valist, format); + res = MSVCRT_vcscanf(format, valist); + va_end(valist); + return res; +} diff --git a/dlls/msvcrt/scanf.h b/dlls/msvcrt/scanf.h index 88c954b62f7..82552154c00 100644 --- a/dlls/msvcrt/scanf.h +++ b/dlls/msvcrt/scanf.h @@ -48,7 +48,7 @@ #ifdef CONSOLE #define _GETC_(file) (consumed++, _getch()) #define _UNGETC_(nch, file) do { _ungetch(nch); consumed--; } while(0) -#define _FUNCTION_ _cscanf(const _CHAR_ *format, ...) +#define _FUNCTION_ static int MSVCRT_vcscanf(const char *format, va_list ap) #else #ifdef STRING #undef _EOF_ @@ -56,19 +56,19 @@ #define _GETC_(file) (consumed++, *file++) #define _UNGETC_(nch, file) do { file--; consumed--; } while(0) #ifdef WIDE_SCANF -#define _FUNCTION_ MSVCRT_swscanf(const MSVCRT_wchar_t *file, const MSVCRT_wchar_t *format, ...) +#define _FUNCTION_ static int MSVCRT_vswscanf(const MSVCRT_wchar_t *file, const MSVCRT_wchar_t *format, va_list ap) #else /* WIDE_SCANF */ -#define _FUNCTION_ MSVCRT_sscanf(const char *file, const char *format, ...) +#define _FUNCTION_ static int MSVCRT_vsscanf(const char *file, const char *format, va_list ap) #endif /* WIDE_SCANF */ #else /* STRING */ #ifdef WIDE_SCANF #define _GETC_(file) (consumed++, MSVCRT_fgetwc(file)) #define _UNGETC_(nch, file) do { MSVCRT_ungetwc(nch, file); consumed--; } while(0) -#define _FUNCTION_ MSVCRT_fwscanf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...) +#define _FUNCTION_ static int MSVCRT_vfwscanf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list ap) #else /* WIDE_SCANF */ #define _GETC_(file) (consumed++, MSVCRT_fgetc(file)) #define _UNGETC_(nch, file) do { MSVCRT_ungetc(nch, file); consumed--; } while(0) -#define _FUNCTION_ MSVCRT_fscanf(MSVCRT_FILE* file, const char *format, ...) +#define _FUNCTION_ static int MSVCRT_vfscanf(MSVCRT_FILE* file, const char *format, va_list ap) #endif /* WIDE_SCANF */ #endif /* STRING */ #endif /* CONSOLE */ @@ -79,10 +79,9 @@ * Extended by C. Scott Ananian to handle * more types of format spec. */ -int _FUNCTION_ { +_FUNCTION_ { int rd = 0, consumed = 0; int nch; - va_list ap; if (!*format) return 0; #ifndef WIDE_SCANF #ifdef CONSOLE @@ -98,7 +97,6 @@ int _FUNCTION_ { nch = _GETC_(file); if (nch == _EOF_) return _EOF_RET; - va_start(ap, format); while (*format) { /* a whitespace character in the format string causes scanf to read, * but not store, all consecutive white-space characters in the input @@ -512,7 +510,6 @@ int _FUNCTION_ { if (nch!=_EOF_) { _UNGETC_(nch, file); } - va_end(ap); TRACE("returning %d\n", rd); return rd; } diff --git a/include/msvcrt/stdio.h b/include/msvcrt/stdio.h index c93816cdf9c..d665263e649 100644 --- a/include/msvcrt/stdio.h +++ b/include/msvcrt/stdio.h @@ -79,6 +79,10 @@ #else +#define MSVCRT_STDIN_FILENO 0 +#define MSVCRT_STDOUT_FILENO 1 +#define MSVCRT_STDERR_FILENO 2 + /* more file._flag flags, but these conflict with Unix */ #define MSVCRT__IOFBF 0x0000 #define MSVCRT__IONBF 0x0004 @@ -144,6 +148,10 @@ MSVCRT(FILE)* MSVCRT(__p__iob)(void); #define stdin (_iob+STDIN_FILENO) #define stdout (_iob+STDOUT_FILENO) #define stderr (_iob+STDERR_FILENO) +#else +#define MSVCRT_stdin (MSVCRT__iob+MSVCRT_STDIN_FILENO) +#define MSVCRT_stdout (MSVCRT__iob+MSVCRT_STDOUT_FILENO) +#define MSVCRT_stderr (MSVCRT__iob+MSVCRT_STDERR_FILENO) #endif /* USE_MSVCRT_PREFIX */ #ifndef MSVCRT_STDIO_DEFINED