31#include "printf-stdarg.h"
33static void printchar(
char **str,
int c)
45int puts(
const char *s)
55static int prints(
char **out,
const char *
string,
int width,
int pad)
57 register int pc = 0, padchar =
' ';
61 register const char *ptr;
62 for (ptr =
string; *ptr; ++ptr) ++len;
63 if (len >= width) width = 0;
65 if (pad & PAD_ZERO) padchar =
'0';
67 if (!(pad & PAD_RIGHT)) {
68 for ( ; width > 0; --width) {
69 printchar (out, padchar);
73 for ( ; *string ; ++string) {
74 printchar (out, *
string);
77 for ( ; width > 0; --width) {
78 printchar (out, padchar);
86#define PRINT_BUF_LEN 12
88static int printi(
char **out,
int i,
int b,
int sg,
int width,
int pad,
int letbase)
90 char print_buf[PRINT_BUF_LEN];
92 register int t, neg = 0, pc = 0;
93 register unsigned int u = (
unsigned int)i;
98 return prints (out, print_buf, width, pad);
101 if (sg && b == 10 && i < 0) {
103 u = (
unsigned int)-i;
106 s = print_buf + PRINT_BUF_LEN-1;
110 t = (
unsigned int)u % b;
112 t += letbase -
'0' - 10;
113 *--s = (char)(t +
'0');
118 if( width && (pad & PAD_ZERO) ) {
119 printchar (out,
'-');
128 return pc + prints (out, s, width, pad);
131static int print(
char **out,
const char *format, va_list args )
133 register int width, pad;
137 for (; *format != 0; ++format) {
138 if (*format ==
'%') {
141 if (*format ==
'\0')
break;
142 if (*format ==
'%')
goto out;
143 if (*format ==
'-') {
147 while (*format ==
'0') {
151 for ( ; *format >=
'0' && *format <=
'9'; ++format) {
153 width += *format -
'0';
155 if( *format ==
's' ) {
156 register char *s = (
char *)va_arg( args,
int );
157 pc += prints (out, s?s:
"(null)", width, pad);
160 if( *format ==
'l' ) {
162 if( *format ==
'd' ) {
163 pc += printi (out, va_arg( args,
long ), 10, 1, width, pad,
'a');
166 if( *format ==
'u' ) {
167 pc += printi (out, va_arg( args,
long ), 10, 0, width, pad,
'a');
171 if( *format ==
'd' ) {
172 pc += printi (out, va_arg( args,
int ), 10, 1, width, pad,
'a');
175 if( *format ==
'x' ) {
176 pc += printi (out, va_arg( args,
int ), 16, 0, width, pad,
'a');
179 if( *format ==
'p' ) {
180 pc += prints (out,
"0x", width, pad);
181 pc += printi (out, va_arg( args,
int ), 16, 0, 8, PAD_ZERO,
'a');
184 if( *format ==
'X' ) {
185 pc += printi (out, va_arg( args,
int ), 16, 0, width, pad,
'A');
188 if( *format ==
'u' ) {
189 pc += printi (out, va_arg( args,
int ), 10, 0, width, pad,
'a');
192 if( *format ==
'c' ) {
194 scr[0] = (char)va_arg( args,
int );
196 pc += prints (out, scr, width, pad);
202 printchar (out, *format);
204 if(
'\n' == *format) {
205 printchar(out,
'\r');
210 if (out) **out =
'\0';
215int printf(
const char *format, ...)
219 va_start( args, format );
220 return print( 0, format, args );
223int sprintf(
char *out,
const char *format, ...)
227 va_start( args, format );
228 return print( &out, format, args );
232int snprintf(
char *buf,
unsigned int count,
const char *format, ... )
238 va_start( args, format );
239 return print( &buf, format, args );
246 char *ptr =
"Hello world!";
249 unsigned int bs =
sizeof(int)*8;
253 mi = (1 << (bs-1)) + 1;
255 printf(
"printf test\n");
256 printf(
"%s is null pointer\n", np);
257 printf(
"%d = 5\n", i);
258 printf(
"%d = - max int\n", mi);
259 printf(
"char %c = 'a'\n",
'a');
260 printf(
"hex %x = ff\n", 0xff);
261 printf(
"hex %02x = 00\n", 0);
262 printf(
"signed %d = unsigned %u = hex %x\n", -3, -3, -3);
263 printf(
"%d %s(s)%", 0,
"message");
265 printf(
"%d %s(s) with %%\n", 0,
"message");
266 sprintf(buf,
"justif: \"%-10s\"\n",
"left"); printf(
"%s", buf);
267 sprintf(buf,
"justif: \"%10s\"\n",
"right"); printf(
"%s", buf);
268 sprintf(buf,
" 3: %04d zero padded\n", 3); printf(
"%s", buf);
269 sprintf(buf,
" 3: %-4d left justif.\n", 3); printf(
"%s", buf);
270 sprintf(buf,
" 3: %4d right justif.\n", 3); printf(
"%s", buf);
271 sprintf(buf,
"-3: %04d zero padded\n", -3); printf(
"%s", buf);
272 sprintf(buf,
"-3: %-4d left justif.\n", -3); printf(
"%s", buf);
273 sprintf(buf,
"-3: %4d right justif.\n", -3); printf(
"%s", buf);