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() */
27 int do_printf (const char *fmt
, va_list args
)
33 unsigned char *buf
= 0;
35 for (; *fmt
; fmt
++) {
36 /* probably argument is here */
43 if (*fmt
== 'd' || *fmt
== 'i' || *fmt
== 'u') {
44 num
= va_arg (args
, int);
48 for (i
= 0; str
[i
]; i
++)
57 num
= va_arg (args
, double);
69 f
= (int) ((double) num
* (double) 10000);
74 for (i
= 0; str
[i
]; i
++)
82 if (*fmt
== 'x' || *fmt
== 'X') {
83 num
= va_arg (args
, int);
87 for (i
= 0; str
[i
]; i
++)
96 num
= va_arg (args
, unsigned char);
98 putch ((unsigned char) num
);
106 buf
= va_arg (args
, unsigned char *);
108 for (i
= 0; buf
[i
]; i
++)
109 putch ((unsigned char) buf
[i
]);
127 int do_sprintf (const char *fmt
, va_list args
, char *ptr
)
133 unsigned char *buf
= 0;
135 for (; *fmt
; fmt
++) {
136 /* probably argument is here */
143 if (*fmt
== 'd' || *fmt
== 'i' || *fmt
== 'u') {
144 num
= va_arg (args
, int);
148 for (i
= 0; str
[i
]; i
++)
149 ptr
[i
+count
] = str
[i
];
156 if (*fmt
== 'x' || *fmt
== 'X') {
157 num
= va_arg (args
, int);
161 for (i
= 0; str
[i
]; i
++)
162 ptr
[i
+count
] = str
[i
];
170 num
= va_arg (args
, unsigned char);
172 ptr
[count
] = (unsigned char) num
;
180 buf
= va_arg (args
, unsigned char *);
182 for (i
= 0; buf
[i
]; i
++)
183 ptr
[i
+count
] = buf
[i
];
201 /*****************************************************************************
203 *****************************************************************************/
205 int vsprintf (char *buffer
, const char *fmt
, va_list args
)
209 ret_val
= do_sprintf (fmt
, args
, (char *) buffer
);
210 buffer
[ret_val
] = '\0';
215 /*****************************************************************************
216 *****************************************************************************/
217 int sprintf(char *buffer
, const char *fmt
, ...)
223 ret_val
= vsprintf(buffer
, fmt
, args
);
227 /*****************************************************************************
229 You must write your own putchar()
230 *****************************************************************************/
231 int vprintf_help(unsigned c
, void **ptr
)
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
, ...)
250 ret_val
= vprintf(fmt
, args
);
254 /*****************************************************************************
255 *****************************************************************************/
260 sprintf(buf
, "%u score and %i years ago...\n", 4, -7);
263 sprintf(buf
, "-1L == 0x%lX == octal %lo\n", -1L, -1L);
266 printf("<%-08s> and <%08s> justified strings\n", "left", "right");