3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <_printf.h> /* fnptr_t */
21 #include <string.h> /* strlen() */
22 #include <stdarg.h> /* va_list, va_arg() */
26 static int scanf_getint (int *n
)
28 char *str
= (char *) malloc (sizeof (char) * 128);
43 if (c
== '\n' || i
> 127)
73 /* TODO: implement real floatint point format */
74 static int scanf_getfloat (float *n
)
76 char *str
= (char *) malloc (sizeof (char) * 128);
91 if (c
== '\n' || i
> 127)
114 *n
= (float) atoi (str
);
121 static int scanf_gethex (int *n
)
123 char *str
= (char *) malloc (sizeof (char) * 128);
138 if (c
== '\n' || i
> 127)
163 *n
= strtol (str
, &endptr
, 16);
170 static int scanf_getstring (unsigned char *n
)
206 static int scanf_getchar (unsigned char *n
)
241 int do_scanf (const char *fmt
, va_list args
)
246 unsigned char *buf
= 0;
248 for (; *fmt
; fmt
++) {
249 /* probably argument is here */
256 if (*fmt
== 'd' || *fmt
== 'i' || *fmt
== 'u') {
257 num
= va_arg (args
, int *);
259 if (scanf_getint (num
))
265 if (*fmt
== 'x' || *fmt
== 'X') {
266 num
= va_arg (args
, int *);
268 if (scanf_gethex (num
))
275 buf
= va_arg (args
, unsigned char *);
277 if (scanf_getchar (buf
))
284 fp
= va_arg (args
, float *);
286 if (scanf_getfloat (fp
))
293 buf
= va_arg (args
, unsigned char *);
295 if (scanf_getstring (buf
))