Updated and fixed edit app; system call sys_getch () was rewrited for using memory...
[ZeXOS.git] / libc / stdio / doprintf.c
blob35b83e111ac3ee8121ed2cae4e10d6cd0899e154
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <_printf.h> /* fnptr_t */
22 #include <string.h> /* strlen() */
23 #include <stdarg.h> /* va_list, va_arg() */
24 #include <stdio.h>
25 #include <stdlib.h>
27 int do_printf (const char *fmt, va_list args)
29 unsigned i = 0;
30 unsigned count = 0;
31 long num = 0;
32 char str[16];
33 unsigned char *buf = 0;
35 for (; *fmt; fmt ++) {
36 /* probably argument is here */
37 if (*fmt == '%') {
38 fmt ++;
40 if (!*fmt)
41 break;
43 if (*fmt == 'd' || *fmt == 'i' || *fmt == 'u') {
44 num = va_arg (args, int);
46 itoa (num, str, 10);
48 for (i = 0; str[i]; i ++)
49 putch (str[i]);
51 count += i;
53 continue;
56 if (*fmt == 'f') {
57 num = va_arg (args, double);
59 int f;
61 if (num >= 1)
62 f = (int) num;
63 else {
64 count += 2;
66 putch ('0');
67 putch ('.');
69 f = (int) ((double) num * (double) 10000);
72 itoa (f, str, 10);
74 for (i = 0; str[i]; i ++)
75 putch (str[i]);
77 count += i;
79 continue;
82 if (*fmt == 'x' || *fmt == 'X') {
83 num = va_arg (args, int);
85 itoa (num, str, 16);
87 for (i = 0; str[i]; i ++)
88 putch (str[i]);
90 count += i;
92 continue;
95 if (*fmt == 'c') {
96 num = va_arg (args, unsigned char);
98 putch ((unsigned char) num);
100 count ++;
102 continue;
105 if (*fmt == 's') {
106 buf = va_arg (args, unsigned char *);
108 for (i = 0; buf[i]; i ++)
109 putch ((unsigned char) buf[i]);
111 count += i;
113 continue;
116 continue;
119 putch (*fmt);
120 count ++;
124 return count;
127 int do_sprintf (const char *fmt, va_list args, char *ptr)
129 unsigned i = 0;
130 unsigned count = 0;
131 long num = 0;
132 char str[16];
133 unsigned char *buf = 0;
135 for (; *fmt; fmt ++) {
136 /* probably argument is here */
137 if (*fmt == '%') {
138 fmt ++;
140 if (!*fmt)
141 break;
143 if (*fmt == 'd' || *fmt == 'i' || *fmt == 'u') {
144 num = va_arg (args, int);
146 itoa (num, str, 10);
148 for (i = 0; str[i]; i ++)
149 ptr[i+count] = str[i];
151 count += i;
153 continue;
156 if (*fmt == 'x' || *fmt == 'X') {
157 num = va_arg (args, int);
159 itoa (num, str, 16);
161 for (i = 0; str[i]; i ++)
162 ptr[i+count] = str[i];
164 count += i;
166 continue;
169 if (*fmt == 'c') {
170 num = va_arg (args, unsigned char);
172 ptr[count] = (unsigned char) num;
174 count ++;
176 continue;
179 if (*fmt == 's') {
180 buf = va_arg (args, unsigned char *);
182 for (i = 0; buf[i]; i ++)
183 ptr[i+count] = buf[i];
185 count += i;
187 continue;
190 continue;
193 ptr[count] = *fmt;
194 count ++;
198 return count;
201 /*****************************************************************************
202 SPRINTF
203 *****************************************************************************/
205 int vsprintf (char *buffer, const char *fmt, va_list args)
207 int ret_val;
209 ret_val = do_sprintf (fmt, args, (char *) buffer);
210 buffer[ret_val] = '\0';
211 return ret_val;
214 #if 0 /* testing */
215 /*****************************************************************************
216 *****************************************************************************/
217 int sprintf(char *buffer, const char *fmt, ...)
219 va_list args;
220 int ret_val;
222 va_start(args, fmt);
223 ret_val = vsprintf(buffer, fmt, args);
224 va_end(args);
225 return ret_val;
227 /*****************************************************************************
228 PRINTF
229 You must write your own putchar()
230 *****************************************************************************/
231 int vprintf_help(unsigned c, void **ptr)
233 putchar(c);
234 return 0 ;
236 /*****************************************************************************
237 *****************************************************************************/
238 int vprintf(const char *fmt, va_list args)
240 return do_printf(fmt, args, vprintf_help, NULL);
242 /*****************************************************************************
243 *****************************************************************************/
244 int printf(const char *fmt, ...)
246 va_list args;
247 int ret_val;
249 va_start(args, fmt);
250 ret_val = vprintf(fmt, args);
251 va_end(args);
252 return ret_val;
254 /*****************************************************************************
255 *****************************************************************************/
256 int main(void)
258 char buf[64];
260 sprintf(buf, "%u score and %i years ago...\n", 4, -7);
261 puts(buf);
263 sprintf(buf, "-1L == 0x%lX == octal %lo\n", -1L, -1L);
264 puts(buf);
266 printf("<%-08s> and <%08s> justified strings\n", "left", "right");
267 return 0;
269 #endif