Details | 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/i386/float.h> |
||
26 | #include <ll/i386/mem.h> |
||
27 | #include <ll/stdarg.h> |
||
28 | #include <ll/ctype.h> |
||
29 | #include <ll/math.h> |
||
30 | #include "sprintf.h" |
||
31 | |||
32 | FILE(ksprintf); |
||
33 | |||
34 | #define STD_SIZE 0 |
||
35 | #define SHORT_SIZE 1 |
||
36 | #define LONG_SIZE 2 |
||
37 | |||
38 | int vksprintf(char *buf,char *fmt,va_list parms) |
||
39 | { |
||
40 | int scanned = 0,w = 0,prec = 0,l = 0,size = 0; |
||
41 | int n1 = 0; |
||
42 | unsigned n2 = 0,parsing = 0,flag = 0; |
||
43 | char *base = buf; |
||
44 | char *sp = NULL; |
||
45 | |||
46 | while (*fmt != 0) { |
||
47 | if (*fmt != '%' && !parsing) { |
||
48 | /* No token detected */ |
||
49 | *buf++ = *fmt++; |
||
50 | scanned++; |
||
51 | } |
||
52 | else { |
||
53 | /* We need to make a conversion */ |
||
54 | if (*fmt == '%') { |
||
55 | fmt++; |
||
56 | parsing = 1; |
||
57 | w = 10; |
||
58 | prec = 4; |
||
59 | size = STD_SIZE; |
||
60 | flag = 0; |
||
61 | } |
||
62 | /* Parse token */ |
||
63 | switch(*fmt) { |
||
64 | case '%' : *buf++ = '%'; |
||
65 | scanned++; |
||
66 | parsing = 0; |
||
67 | break; |
||
68 | case 'c' : *buf++ = va_arg(parms, char); |
||
69 | scanned++; |
||
70 | parsing = 0; |
||
71 | break; |
||
72 | case 'i' : |
||
73 | case 'd' : switch (size) { |
||
74 | case STD_SIZE : n1 = va_arg(parms, int); |
||
75 | break; |
||
76 | case LONG_SIZE : n1 = va_arg(parms, long int); |
||
77 | break; |
||
78 | case SHORT_SIZE : n1 = va_arg(parms, short int); |
||
79 | break; |
||
80 | } |
||
81 | l = dcvt(n1,buf,10,w,flag); |
||
82 | scanned += l; |
||
83 | buf += l; |
||
84 | parsing = 0; |
||
85 | break; |
||
86 | case 'u' : switch (size) { |
||
87 | case STD_SIZE : n2 = va_arg(parms, unsigned); |
||
88 | break; |
||
89 | case LONG_SIZE : n2 = va_arg(parms, unsigned long); |
||
90 | break; |
||
91 | case SHORT_SIZE : n2 = va_arg(parms, unsigned short); |
||
92 | break; |
||
93 | } |
||
94 | l = ucvt(n2,buf,10,w,flag); |
||
95 | scanned += l; |
||
96 | buf += l; |
||
97 | parsing = 0; |
||
98 | break; |
||
99 | case 'x' : switch (size) { |
||
100 | case STD_SIZE : n2 = va_arg(parms, unsigned); |
||
101 | break; |
||
102 | case LONG_SIZE : n2 = va_arg(parms, unsigned long); |
||
103 | break; |
||
104 | case SHORT_SIZE : n2 = va_arg(parms, unsigned short); |
||
105 | break; |
||
106 | } |
||
107 | l = ucvt(n2,buf,16,w,flag); |
||
108 | scanned += l; |
||
109 | buf += l; |
||
110 | parsing = 0; |
||
111 | break; |
||
112 | case 's' : sp = va_arg(parms, char *); |
||
113 | while (*sp != 0) { |
||
114 | *buf++ = *sp++; |
||
115 | l++; |
||
116 | } |
||
117 | scanned += l; |
||
118 | parsing = 0; |
||
119 | break; |
||
120 | case 'l' : size = LONG_SIZE; |
||
121 | break; |
||
122 | case 'n' : |
||
123 | case 'h' : size = SHORT_SIZE; |
||
124 | break; |
||
125 | case '+' : flag |= ADD_PLUS; |
||
126 | break; |
||
127 | case '-' : flag |= LEFT_PAD; |
||
128 | break; |
||
129 | case '.' : parsing = 2; |
||
130 | flag |= RESPECT_WIDTH; |
||
131 | break; |
||
132 | case '1' : |
||
133 | case '2' : |
||
134 | case '3' : |
||
135 | case '4' : |
||
136 | case '5' : |
||
137 | case '6' : |
||
138 | case '7' : |
||
139 | case '8' : |
||
140 | case '9' : |
||
141 | case '0' : if (parsing == 1) { |
||
142 | w = strtou(fmt,10,&base); |
||
143 | /* MG */ |
||
144 | /* if the first numeral is zero a ZERO pad is */ |
||
145 | /* required */ |
||
146 | /* but not if LEFT_PAD is set*/ |
||
147 | if (*fmt!='0'||flag&LEFT_PAD) |
||
148 | flag |= SPACE_PAD ; |
||
149 | else |
||
150 | flag |= ZERO_PAD ; |
||
151 | fmt = base-1; |
||
152 | } else if (parsing == 2) { |
||
153 | prec = strtou(fmt,10,&base); |
||
154 | fmt = base-1; |
||
155 | parsing = 1; |
||
156 | } |
||
157 | break; |
||
158 | default : parsing = 0; |
||
159 | break; |
||
160 | } |
||
161 | fmt++; |
||
162 | } |
||
163 | } |
||
164 | *buf = 0; |
||
165 | return(scanned); |
||
166 | } |
||
167 | |||
168 | int ksprintf(char *buf,char *fmt,...) |
||
169 | { |
||
170 | va_list parms; |
||
171 | int result; |
||
172 | |||
173 | va_start(parms,fmt); |
||
174 | result = vksprintf(buf,fmt,parms); |
||
175 | va_end(parms); |
||
176 | return(result); |
||
177 | } |