diff --git a/build-aux/check-mescc.sh b/build-aux/check-mescc.sh index ea4002ed..84d8f18d 100755 --- a/build-aux/check-mescc.sh +++ b/build-aux/check-mescc.sh @@ -133,6 +133,7 @@ t 84-struct-field-list 85-sizeof 86-strncpy +87-sscanf " broken="$broken diff --git a/include/stdarg.h b/include/stdarg.h index 5d7b7eea..6675bced 100644 --- a/include/stdarg.h +++ b/include/stdarg.h @@ -46,6 +46,7 @@ typedef int va_list; int vprintf (char const* format, va_list ap); int vsprintf (char *str, char const *format, va_list ap); int vsnprintf (char *str, size_t size, char const *format, va_list ap); +int vsscanf (char const *s, char const *template, va_list ap); #endif // ! (__GNUC__ && POSIX) diff --git a/lib/libc+tcc.c b/lib/libc+tcc.c index 1152e726..cd94d5c0 100644 --- a/lib/libc+tcc.c +++ b/lib/libc+tcc.c @@ -90,7 +90,6 @@ ferror (FILE *stream) int fflush (FILE *stream) { - eputs ("fflush stub\n"); return 0; } @@ -254,9 +253,13 @@ snprintf(char *str, size_t size, char const *format, ...) } int -sscanf (char const *str, const char *format, ...) +sscanf (char const *str, const char *template, ...) { - eputs ("sscanf stub\n"); + va_list ap; + va_start (ap, template); + int r = vsscanf (str, template, ap); + va_end (ap); + return r; return 0; } @@ -449,3 +452,38 @@ vfprintf (FILE* f, char const* format, va_list ap) va_end (ap); return 0; } + +int +vsscanf (char const *s, char const *template, va_list ap) +{ + char const *p = s; + char const *t = template; + while (*t && *p) + if (*t != '%') + { + t++; + p++; + } + else + { + t++; + char c = *t; + switch (c) + { + case '%': {p++; break;} + case 'c': {char *c = va_arg (ap, char*); *c = *p++; break;} + case 'd': {int *d = va_arg (ap, int*); *d = abtoi (&p, 10); break;} + default: + { + eputs ("vsscanf: not supported: %"); + eputc (c); + eputs ("\n"); + t++; + p++; + } + } + t++; + } + va_end (ap); + return 0; +} diff --git a/scaffold/tests/87-sscanf.c b/scaffold/tests/87-sscanf.c new file mode 100644 index 00000000..adfde721 --- /dev/null +++ b/scaffold/tests/87-sscanf.c @@ -0,0 +1,42 @@ +/* -*-comment-start: "//";comment-end:""-*- + * Mes --- Maxwell Equations of Software + * Copyright © 2018 Jan (janneke) Nieuwenhuizen + * + * This file is part of Mes. + * + * Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Mes. If not, see . + */ + +#include "30-test.i" +#include + +int +test () +{ + int i; + int r = sscanf ("42", "%d", &i); + if (r) + return 1; + if (i != 42) + return 2; + + char c; + r = sscanf ("foo bar", "foo%cbar", &c); + if (r) + return 3; + if (c != ' ') + return 4; + + return 0; +}