Rev 3 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | pj | 1 | /* Project: OSLib |
2 | * Description: The OS Construction Kit |
||
3 | * Date: 1.6.2000 |
||
4 | * Idea by: Luca Abeni & Gerardo Lamastra |
||
5 | * |
||
6 | * OSLib is an SO project aimed at developing a common, easy-to-use |
||
7 | * low-level infrastructure for developing OS kernels and Embedded |
||
8 | * Applications; it partially derives from the HARTIK project but it |
||
9 | * currently is independently developed. |
||
10 | * |
||
11 | * OSLib is distributed under GPL License, and some of its code has |
||
12 | * been derived from the Linux kernel source; also some important |
||
13 | * ideas come from studying the DJGPP go32 extender. |
||
14 | * |
||
15 | * We acknowledge the Linux Community, Free Software Foundation, |
||
16 | * D.J. Delorie and all the other developers who believe in the |
||
17 | * freedom of software and ideas. |
||
18 | * |
||
19 | * For legalese, check out the included GPL license. |
||
20 | */ |
||
21 | |||
22 | #include <ll/i386/stdlib.h> |
||
23 | #include <ll/i386/string.h> |
||
24 | #include <ll/i386/limits.h> |
||
25 | #include <ll/stdarg.h> |
||
26 | #include <ll/ctype.h> |
||
27 | #include "sprintf.h" |
||
28 | |||
29 | FILE(sscanf); |
||
30 | |||
31 | #define STD_SIZE 0 |
||
32 | #define SHORT_SIZE 1 |
||
33 | #define LONG_SIZE 2 |
||
34 | |||
35 | int vsscanf(char *buf,char *fmt,va_list parms) |
||
36 | { |
||
37 | int scanned = 0,size = 0,suppress = 0; |
||
38 | int w = 0,flag = 0,l = 0; |
||
39 | char c,*c_ptr; |
||
40 | long int n1,*n1l; |
||
41 | int *n1b; |
||
42 | short int *n1s; |
||
43 | long unsigned n2,*n2l,parsing = 0; |
||
44 | unsigned *n2b; |
||
45 | short unsigned *n2s; |
||
46 | double n3,*n3l; |
||
47 | float *n3s; |
||
48 | char *base = buf; |
||
49 | while (*fmt != 0) { |
||
50 | if (*fmt != '%' && !parsing) { |
||
51 | /* No token detected */ |
||
52 | fmt++; |
||
53 | } |
||
54 | else { |
||
55 | /* We need to make a conversion */ |
||
56 | if (*fmt == '%') { |
||
57 | fmt++; |
||
58 | parsing = 1; |
||
59 | size = STD_SIZE; |
||
60 | suppress = 0; |
||
61 | w = 0; |
||
62 | flag = 0; |
||
63 | l = 0; |
||
64 | } |
||
65 | /* Parse token */ |
||
66 | switch(*fmt) { |
||
67 | case '1' : |
||
68 | case '2' : |
||
69 | case '3' : |
||
70 | case '4' : |
||
71 | case '5' : |
||
72 | case '6' : |
||
73 | case '7' : |
||
74 | case '8' : |
||
75 | case '9' : |
||
76 | case '0' : if (parsing == 1) { |
||
77 | w = strtou(fmt,10,&base); |
||
78 | /* We use SPACE_PAD to parse %10s |
||
79 | commands where the number is the |
||
80 | maximum number of char to store! |
||
81 | */ |
||
82 | flag |= SPACE_PAD; |
||
83 | fmt = base-1; |
||
84 | } |
||
85 | break; |
||
86 | case 'c' : c = *buf++; |
||
87 | c_ptr = va_arg(parms, char *); |
||
88 | *c_ptr = c; |
||
89 | scanned++; |
||
90 | parsing = 0; |
||
91 | break; |
||
92 | case 's' : c_ptr = va_arg(parms, char *); |
||
93 | while (*buf != 0 && isspace(*buf)) buf++; |
||
94 | l = 0; |
||
95 | while (*buf != 0 && !isspace(*buf)) { |
||
96 | if (!(flag & SPACE_PAD)) *c_ptr++ = *buf; |
||
97 | else if (l < w) { |
||
98 | *c_ptr++ = *buf; |
||
99 | l++; |
||
100 | } |
||
101 | buf++; |
||
102 | } |
||
103 | *c_ptr = 0; |
||
104 | scanned++; |
||
105 | parsing = 0; |
||
106 | break; |
||
107 | case 'i' : |
||
108 | case 'd' : buf = strscn(buf,"1234567890-+"); |
||
109 | n1 = strtoi(buf,10,&base); |
||
110 | buf = base; |
||
111 | if (!suppress) { |
||
112 | switch (size) { |
||
113 | case STD_SIZE : n1b = va_arg(parms, int *); |
||
114 | *n1b = (int)n1; |
||
115 | break; |
||
116 | case LONG_SIZE : n1l = va_arg(parms, long int *); |
||
117 | *n1l = n1; |
||
118 | break; |
||
119 | case SHORT_SIZE : n1s = va_arg(parms, short int *); |
||
120 | *n1s = (short)(n1); |
||
121 | break; |
||
122 | } |
||
123 | scanned++; |
||
124 | } |
||
125 | parsing = 0; |
||
126 | break; |
||
127 | case 'u' : buf = strscn(buf,"1234567890"); |
||
128 | n2 = strtou(buf,10,&base); |
||
129 | buf = base; |
||
130 | if (!suppress) { |
||
131 | switch (size) { |
||
132 | case STD_SIZE : n2b = va_arg(parms, unsigned *); |
||
133 | *n2b = (unsigned)n2; |
||
134 | break; |
||
135 | case LONG_SIZE : n2l = va_arg(parms, long unsigned *); |
||
136 | *n2l = n2; |
||
137 | break; |
||
138 | case SHORT_SIZE : n2s = va_arg(parms, short unsigned *); |
||
139 | *n2s = (short)(n2); |
||
140 | break; |
||
141 | } |
||
142 | scanned++; |
||
143 | } |
||
144 | parsing = 0; |
||
145 | break; |
||
146 | case 'x' : buf = strscn(buf,"1234567890xabcdefABCDEF"); |
||
147 | n2 = strtou(buf,16,&base); |
||
148 | buf = base; |
||
149 | if (!suppress) { |
||
150 | switch (size) { |
||
151 | case STD_SIZE : n2b = va_arg(parms, unsigned *); |
||
152 | *n2b = (unsigned)n2; |
||
153 | break; |
||
154 | case LONG_SIZE : n2l = va_arg(parms, long unsigned *); |
||
155 | *n2l = n2; |
||
156 | break; |
||
157 | case SHORT_SIZE : n2s = va_arg(parms, short unsigned *); |
||
158 | *n2s = (short)(n2); |
||
159 | break; |
||
160 | } |
||
161 | scanned++; |
||
162 | } |
||
163 | parsing = 0; |
||
164 | break; |
||
165 | case 'f' : |
||
166 | case 'g' : |
||
167 | case 'e' : buf = strscn(buf,"1234567890.e+-"); |
||
168 | n3 = strtod(buf,&base); |
||
169 | buf = base; |
||
170 | if (!suppress) { |
||
171 | switch (size) { |
||
172 | case STD_SIZE : n3l = va_arg(parms, double *); |
||
173 | *n3l = n3; |
||
174 | break; |
||
175 | case LONG_SIZE : n3l = va_arg(parms, double *); |
||
176 | *n3l = n3; |
||
177 | break; |
||
178 | case SHORT_SIZE : n3s = va_arg(parms, float *); |
||
179 | *n3s = (float)(n3); |
||
180 | break; |
||
181 | } |
||
182 | scanned++; |
||
183 | } |
||
184 | parsing = 0; |
||
185 | break; |
||
186 | case 'l' : size = LONG_SIZE; |
||
187 | break; |
||
188 | case 'h' : |
||
189 | case 'n' : size = SHORT_SIZE; |
||
190 | break; |
||
191 | case '*' : suppress = 1; |
||
192 | break; |
||
193 | default : parsing = 0; |
||
194 | break; |
||
195 | } |
||
196 | fmt++; |
||
197 | } |
||
198 | } |
||
199 | return(scanned); |
||
200 | } |
||
201 | |||
202 | int sscanf(char *buf,char *fmt,...) |
||
203 | { |
||
204 | va_list parms; |
||
205 | int result; |
||
206 | va_start(parms,fmt); |
||
207 | result = vsscanf(buf,fmt,parms); |
||
208 | va_end(parms); |
||
209 | return(result); |
||
210 | } |