Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / libc / stdio / doscanf.c
blob42c48840f2d2dfb0d5e907d127ff056b3c3b84c3
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <_printf.h> /* fnptr_t */
22 #include <string.h> /* strlen() */
23 #include <stdarg.h> /* va_list, va_arg() */
24 #include <stdio.h>
25 #include <stdlib.h>
27 static int scanf_getint (int *n)
29 char *str = (char *) malloc (sizeof (char) * 128);
31 if (!str)
32 return 0;
34 unsigned i = 0;
36 for (;;) {
37 schedule ();
39 char c = getch ();
41 if (!c)
42 continue;
44 if (c == '\n' || i > 127)
45 break;
47 putchar (c);
49 if (c == ' ' && i)
50 break;
52 str[i] = c;
54 if (c == '\b' && i)
55 i --;
56 else
57 i ++;
60 if (!i)
61 return 0;
63 str[i] = '\0';
65 i = 0;
67 *n = atoi (str);
69 free (str);
71 return 1;
74 /* TODO: implement real floatint point format */
75 static int scanf_getfloat (float *n)
77 char *str = (char *) malloc (sizeof (char) * 128);
79 if (!str)
80 return 0;
82 unsigned i = 0;
84 for (;;) {
85 schedule ();
87 char c = getch ();
89 if (!c)
90 continue;
92 if (c == '\n' || i > 127)
93 break;
95 putchar (c);
97 if (c == ' ' && i)
98 break;
100 str[i] = c;
102 if (c == '\b' && i)
103 i --;
104 else
105 i ++;
108 if (!i)
109 return 0;
111 str[i] = '\0';
113 i = 0;
115 *n = (float) atoi (str);
117 free (str);
119 return 1;
122 static int scanf_gethex (int *n)
124 char *str = (char *) malloc (sizeof (char) * 128);
126 if (!str)
127 return 0;
129 unsigned i = 0;
131 for (;;) {
132 schedule ();
134 char c = getch ();
136 if (!c)
137 continue;
139 if (c == '\n' || i > 127)
140 break;
142 putchar (c);
144 if (c == ' ' && i)
145 break;
147 str[i] = c;
149 if (c == '\b' && i)
150 i --;
151 else
152 i ++;
155 if (!i)
156 return 0;
158 str[i] = '\0';
160 i = 0;
162 char *endptr;
164 *n = strtol (str, &endptr, 16);
166 free (str);
168 return 1;
171 static int scanf_getstring (unsigned char *n)
173 unsigned i = 0;
175 for (;;) {
176 schedule ();
178 char c = getch ();
180 if (!c)
181 continue;
183 if (c == '\n')
184 break;
186 putchar (c);
188 if (c == ' ' && i)
189 break;
191 n[i] = c;
193 if (c == '\b' && i)
194 i --;
195 else
196 i ++;
199 if (!i)
200 return 0;
202 n[i] = '\0';
204 return 1;
207 static int scanf_getchar (unsigned char *n)
209 unsigned i = 0;
211 for (;;) {
212 schedule ();
214 char c = getch ();
216 if (!c)
217 continue;
219 if (c == '\n')
220 break;
222 putchar (c);
224 if (c == ' ' && i)
225 break;
227 if (i == 0)
228 n[i] = c;
230 if (c == '\b' && i)
231 i --;
232 else
233 i ++;
236 if (!i)
237 return 0;
239 return 1;
242 int vscanf (const char *fmt, va_list args)
244 unsigned count = 0;
245 int *num = 0;
246 float *fp = 0;
247 unsigned char *buf = 0;
249 for (; *fmt; fmt ++) {
250 /* probably argument is here */
251 if (*fmt == '%') {
252 fmt ++;
254 if (!*fmt)
255 break;
257 if (*fmt == 'd' || *fmt == 'i' || *fmt == 'u') {
258 num = va_arg (args, int *);
260 if (scanf_getint (num))
261 count ++;
263 continue;
266 if (*fmt == 'x' || *fmt == 'X') {
267 num = va_arg (args, int *);
269 if (scanf_gethex (num))
270 count ++;
272 continue;
275 if (*fmt == 'c') {
276 buf = va_arg (args, unsigned char *);
278 if (scanf_getchar (buf))
279 count ++;
281 continue;
284 if (*fmt == 'f') {
285 fp = va_arg (args, float *);
287 if (scanf_getfloat (fp))
288 count ++;
290 continue;
293 if (*fmt == 's') {
294 buf = va_arg (args, unsigned char *);
296 if (scanf_getstring (buf))
297 count ++;
299 continue;
302 continue;
305 count ++;
309 putchar ('\n');
311 return count;