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