* lib/libc+tcc.c (vsscanf): New function.
(sscanf): Use it.
(fflush): Remove stub notice; we have no buffering.
* include/stdarg.h: Declare it.
* scaffold/tests/87-sscanf.c: Test it.
* build-aux/check-mescc.sh (tests): Run it.
84-struct-field-list
85-sizeof
86-strncpy
+87-sscanf
"
broken="$broken
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)
int
fflush (FILE *stream)
{
- eputs ("fflush stub\n");
return 0;
}
}
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;
}
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;
+}
--- /dev/null
+/* -*-comment-start: "//";comment-end:""-*-
+ * Mes --- Maxwell Equations of Software
+ * Copyright © 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "30-test.i"
+#include <stdio.h>
+
+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;
+}