Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / libc / stdio / doprintf.c
blob1f54f00763d754b93b4d796386db21ceca810ef0
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <_printf.h> /* fnptr_t */
23 #include <string.h> /* strlen() */
24 #include <stdarg.h> /* va_list, va_arg() */
25 #include <stdio.h>
26 #include <stdlib.h>
28 int do_printf (const char *fmt, va_list args)
30 unsigned i = 0;
31 unsigned count = 0;
32 long num = 0;
33 char str[16];
34 unsigned char *buf = 0;
36 for (; *fmt; fmt ++) {
37 /* probably argument is here */
38 if (*fmt == '%') {
39 fmt ++;
41 if (!*fmt)
42 break;
44 if (*fmt == 'd' || *fmt == 'i' || *fmt == 'u') {
45 num = va_arg (args, int);
47 itoa (num, str, 10);
49 for (i = 0; str[i]; i ++)
50 putch (str[i]);
52 count += i;
54 continue;
57 if (*fmt == 'f') {
58 num = va_arg (args, double);
60 int f;
62 if (num >= 1)
63 f = (int) num;
64 else {
65 count += 2;
67 putch ('0');
68 putch ('.');
70 f = (int) ((double) num * (double) 10000);
73 itoa (f, str, 10);
75 for (i = 0; str[i]; i ++)
76 putch (str[i]);
78 count += i;
80 continue;
83 if (*fmt == 'x' || *fmt == 'X') {
84 num = va_arg (args, int);
86 itoa (num, str, 16);
88 for (i = 0; str[i]; i ++)
89 putch (str[i]);
91 count += i;
93 continue;
96 if (*fmt == 'c') {
97 num = va_arg (args, unsigned char);
99 putch ((unsigned char) num);
101 count ++;
103 continue;
106 if (*fmt == 's') {
107 buf = va_arg (args, unsigned char *);
109 for (i = 0; buf[i]; i ++)
110 putch ((unsigned char) buf[i]);
112 count += i;
114 continue;
117 continue;
120 putch (*fmt);
121 count ++;
125 return count;
128 int do_sprintf (const char *fmt, va_list args, char *ptr, int len)
130 unsigned i = 0;
131 unsigned count = 0;
132 long num = 0;
133 char str[16];
134 unsigned char *buf = 0;
136 for (; *fmt; fmt ++) {
137 /* probably argument is here */
138 if (*fmt == '%') {
139 fmt ++;
141 if (!*fmt)
142 break;
144 if (*fmt == 'd' || *fmt == 'i' || *fmt == 'u') {
145 num = va_arg (args, int);
147 itoa (num, str, 10);
149 for (i = 0; str[i]; i ++)
150 ptr[i+count] = str[i];
152 count += i;
154 continue;
157 if (*fmt == 'x' || *fmt == 'X') {
158 num = va_arg (args, int);
160 itoa (num, str, 16);
162 for (i = 0; str[i]; i ++)
163 ptr[i+count] = str[i];
165 count += i;
167 continue;
170 if (*fmt == 'c') {
171 num = va_arg (args, unsigned char);
173 ptr[count] = (unsigned char) num;
175 count ++;
177 continue;
180 if (*fmt == 's') {
181 buf = va_arg (args, unsigned char *);
183 for (i = 0; buf[i]; i ++)
184 ptr[i+count] = buf[i];
186 count += i;
188 continue;
191 continue;
194 ptr[count] = *fmt;
195 count ++;
197 if (len != -1)
198 if (count >= (unsigned) len)
199 break;
202 return count;
205 /*****************************************************************************
206 VSPRINTF
207 *****************************************************************************/
209 int vsprintf (char *buffer, const char *fmt, va_list args)
211 if (!fmt)
212 return 0;
214 int ret_val = do_sprintf (fmt, args, (char *) buffer, -1);
215 buffer[ret_val] = '\0';
217 return ret_val;
220 /*****************************************************************************
221 VSNPRINTF
222 *****************************************************************************/
224 int vsnprintf (char *buffer, size_t size, const char *fmt, va_list args)
226 if (!fmt)
227 return 0;
229 int ret_val = do_sprintf (fmt, args, (char *) buffer, size);
230 buffer[ret_val] = '\0';
232 return ret_val;
235 /*****************************************************************************
236 VFPRINTF
237 *****************************************************************************/
239 int vfprintf (FILE *stream, const char *fmt, va_list args)
241 if (!stream || !fmt)
242 return 0;
244 char *buffer = (char *) malloc (sizeof (char) * 256);
246 if (!buffer)
247 return 0;
249 int ret_val = do_sprintf (fmt, args, (char *) buffer, 255);
250 buffer[ret_val] = '\0';
252 fwrite (buffer, ret_val, 1, stream);
254 free (buffer);
256 return ret_val;
260 #if 0 /* testing */
261 /*****************************************************************************
262 *****************************************************************************/
263 int sprintf(char *buffer, const char *fmt, ...)
265 va_list args;
266 int ret_val;
268 va_start(args, fmt);
269 ret_val = vsprintf(buffer, fmt, args);
270 va_end(args);
271 return ret_val;
273 /*****************************************************************************
274 PRINTF
275 You must write your own putchar()
276 *****************************************************************************/
277 int vprintf_help(unsigned c, void **ptr)
279 putchar(c);
280 return 0 ;
282 /*****************************************************************************
283 *****************************************************************************/
284 int vprintf(const char *fmt, va_list args)
286 return do_printf(fmt, args, vprintf_help, NULL);
288 /*****************************************************************************
289 *****************************************************************************/
290 int printf(const char *fmt, ...)
292 va_list args;
293 int ret_val;
295 va_start(args, fmt);
296 ret_val = vprintf(fmt, args);
297 va_end(args);
298 return ret_val;
300 /*****************************************************************************
301 *****************************************************************************/
302 int main(void)
304 char buf[64];
306 sprintf(buf, "%u score and %i years ago...\n", 4, -7);
307 puts(buf);
309 sprintf(buf, "-1L == 0x%lX == octal %lo\n", -1L, -1L);
310 puts(buf);
312 printf("<%-08s> and <%08s> justified strings\n", "left", "right");
313 return 0;
315 #endif