Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / libc / stdio / dosscanf.c
blobf07bc9c48ec242ca273653c5a8dfecf0414d985e
1 /*
2 * ZeX/OS
3 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
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() */
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <errno.h>
27 static int scanf_getint (const char *str, int *n)
29 *n = atoi (str);
31 return 1;
34 /* TODO: implement real floatint point format */
35 static int scanf_getfloat (const char *str, float *n)
37 *n = (float) atoi (str);
39 return 1;
42 static int scanf_gethex (const char *str, int *n)
44 char *endptr;
46 *n = strtol (str, &endptr, 16);
48 return 1;
51 static int scanf_getstring (const char *str, unsigned char *n)
53 unsigned i = strlen (str);
55 memcpy (n, str, i);
56 n[i] = '\0';
58 return 1;
61 static int scanf_getchar (const char *str, unsigned char *n)
63 *n = *str;
65 return 1;
68 int vsscanf (const char *str, const char *fmt, va_list args)
70 unsigned count = 0;
71 int *num = 0;
72 float *fp = 0;
73 unsigned char *buf = 0;
75 unsigned s_len = strlen (str);
76 char *s = (char *) malloc (sizeof (char) * s_len);
78 if (!s) {
79 errno = ENOMEM;
80 errno_update ();
81 return 0;
84 for (count = 0; count < s_len; count ++) {
85 if (str[count] != ' ')
86 s[count] = str[count];
87 else
88 s[count] = '\0';
91 count = 0;
93 for (; *fmt; fmt ++) {
94 /* probably argument is here */
95 if (*fmt == '%') {
96 fmt ++;
98 if (!*fmt)
99 break;
101 if (*fmt == 'd' || *fmt == 'i' || *fmt == 'u') {
102 num = va_arg (args, int *);
104 if (scanf_getint (s, num))
105 count ++;
107 continue;
110 if (*fmt == 'x' || *fmt == 'X') {
111 num = va_arg (args, int *);
113 if (scanf_gethex (s, num))
114 count ++;
116 continue;
119 if (*fmt == 'c') {
120 buf = va_arg (args, unsigned char *);
122 if (scanf_getchar (s, buf))
123 count ++;
125 continue;
128 if (*fmt == 'f') {
129 fp = va_arg (args, float *);
131 if (scanf_getfloat (s, fp))
132 count ++;
134 continue;
137 if (*fmt == 's') {
138 buf = va_arg (args, unsigned char *);
140 if (scanf_getstring (s, buf))
141 count ++;
143 continue;
146 continue;
149 count ++;
152 free (s);
154 return count;