2 Copyright 2001, 2002 Georges Menie (www.menie.org)
\r
3 stdarg version contributed by Christian Ettinger
\r
5 This program is free software; you can redistribute it and/or modify
\r
6 it under the terms of the GNU Lesser General Public License as published by
\r
7 the Free Software Foundation; either version 2 of the License, or
\r
8 (at your option) any later version.
\r
10 This program is distributed in the hope that it will be useful,
\r
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 GNU Lesser General Public License for more details.
\r
15 You should have received a copy of the GNU Lesser General Public License
\r
16 along with this program; if not, write to the Free Software
\r
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
21 putchar is the only external dependency for this file,
\r
22 if you have a working putchar, leave it commented out.
\r
23 If not, uncomment the define below and
\r
24 replace outbyte(c) by your own function call.
\r
28 #define putchar(c) c
\r
32 static int tiny_print( char **out, const char *format, va_list args, unsigned int buflen );
\r
34 static void printchar(char **str, int c, char *buflimit)
\r
36 //extern int putchar(int c);
\r
39 if( buflimit == ( char * ) 0 ) {
\r
40 /* Limit of buffer not known, write charater to buffer. */
\r
44 else if( ( ( unsigned long ) *str ) < ( ( unsigned long ) buflimit ) ) {
\r
45 /* Withing known limit of buffer, write character. */
\r
59 static int prints(char **out, const char *string, int width, int pad, char *buflimit)
\r
61 register int pc = 0, padchar = ' ';
\r
64 register int len = 0;
\r
65 register const char *ptr;
\r
66 for (ptr = string; *ptr; ++ptr) ++len;
\r
67 if (len >= width) width = 0;
\r
69 if (pad & PAD_ZERO) padchar = '0';
\r
71 if (!(pad & PAD_RIGHT)) {
\r
72 for ( ; width > 0; --width) {
\r
73 printchar (out, padchar, buflimit);
\r
77 for ( ; *string ; ++string) {
\r
78 printchar (out, *string, buflimit);
\r
81 for ( ; width > 0; --width) {
\r
82 printchar (out, padchar, buflimit);
\r
89 /* the following should be enough for 32 bit int */
\r
90 #define PRINT_BUF_LEN 12
\r
92 static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase, char *buflimit)
\r
94 char print_buf[PRINT_BUF_LEN];
\r
96 register int t, neg = 0, pc = 0;
\r
97 register unsigned int u = (unsigned int)i;
\r
100 print_buf[0] = '0';
\r
101 print_buf[1] = '\0';
\r
102 return prints (out, print_buf, width, pad, buflimit);
\r
105 if (sg && b == 10 && i < 0) {
\r
107 u = (unsigned int)-i;
\r
110 s = print_buf + PRINT_BUF_LEN-1;
\r
114 t = (unsigned int)u % b;
\r
116 t += letbase - '0' - 10;
\r
117 *--s = (char)(t + '0');
\r
122 if( width && (pad & PAD_ZERO) ) {
\r
123 printchar (out, '-', buflimit);
\r
132 return pc + prints (out, s, width, pad, buflimit);
\r
135 static int tiny_print( char **out, const char *format, va_list args, unsigned int buflen )
\r
137 register int width, pad;
\r
138 register int pc = 0;
\r
139 char scr[2], *buflimit;
\r
142 buflimit = ( char * ) 0;
\r
145 /* Calculate the last valid buffer space, leaving space for the NULL
\r
147 buflimit = ( *out ) + ( buflen - 1 );
\r
150 for (; *format != 0; ++format) {
\r
151 if (*format == '%') {
\r
154 if (*format == '\0') break;
\r
155 if (*format == '%') goto out;
\r
156 if (*format == '-') {
\r
160 while (*format == '0') {
\r
164 for ( ; *format >= '0' && *format <= '9'; ++format) {
\r
166 width += *format - '0';
\r
168 if( *format == 's' ) {
\r
169 register char *s = (char *)va_arg( args, int );
\r
170 pc += prints (out, s?s:"(null)", width, pad, buflimit);
\r
173 if( *format == 'd' ) {
\r
174 pc += printi (out, va_arg( args, int ), 10, 1, width, pad, 'a', buflimit);
\r
177 if( *format == 'x' ) {
\r
178 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'a', buflimit);
\r
181 if( *format == 'X' ) {
\r
182 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'A', buflimit);
\r
185 if( *format == 'u' ) {
\r
186 pc += printi (out, va_arg( args, int ), 10, 0, width, pad, 'a', buflimit);
\r
189 if( *format == 'c' ) {
\r
190 /* char are converted to int then pushed on the stack */
\r
191 scr[0] = (char)va_arg( args, int );
\r
193 pc += prints (out, scr, width, pad, buflimit);
\r
199 printchar (out, *format, buflimit);
\r
203 if (out) **out = '\0';
\r
208 int printf(const char *format, ...)
\r
212 va_start( args, format );
\r
213 return tiny_print( 0, format, args, 0 );
\r
216 int sprintf(char *out, const char *format, ...)
\r
220 va_start( args, format );
\r
221 return tiny_print( &out, format, args, 0 );
\r
225 int snprintf( char *buf, unsigned int count, const char *format, ... )
\r
231 va_start( args, format );
\r
232 return tiny_print( &buf, format, args, count );
\r
239 char *ptr = "Hello world!";
\r
242 unsigned int bs = sizeof(int)*8;
\r
246 mi = (1 << (bs-1)) + 1;
\r
247 printf("%s\n", ptr);
\r
248 printf("printf test\n");
\r
249 printf("%s is null pointer\n", np);
\r
250 printf("%d = 5\n", i);
\r
251 printf("%d = - max int\n", mi);
\r
252 printf("char %c = 'a'\n", 'a');
\r
253 printf("hex %x = ff\n", 0xff);
\r
254 printf("hex %02x = 00\n", 0);
\r
255 printf("signed %d = unsigned %u = hex %x\n", -3, -3, -3);
\r
256 printf("%d %s(s)%", 0, "message");
\r
258 printf("%d %s(s) with %%\n", 0, "message");
\r
259 sprintf(buf, "justif: \"%-10s\"\n", "left"); printf("%s", buf);
\r
260 sprintf(buf, "justif: \"%10s\"\n", "right"); printf("%s", buf);
\r
261 sprintf(buf, " 3: %04d zero padded\n", 3); printf("%s", buf);
\r
262 sprintf(buf, " 3: %-4d left justif.\n", 3); printf("%s", buf);
\r
263 sprintf(buf, " 3: %4d right justif.\n", 3); printf("%s", buf);
\r
264 sprintf(buf, "-3: %04d zero padded\n", -3); printf("%s", buf);
\r
265 sprintf(buf, "-3: %-4d left justif.\n", -3); printf("%s", buf);
\r
266 sprintf(buf, "-3: %4d right justif.\n", -3); printf("%s", buf);
\r
272 * if you compile this file with
\r
273 * gcc -Wall $(YOUR_C_OPTIONS) -DTEST_PRINTF -c printf.c
\r
274 * you will get a normal warning:
\r
275 * printf.c:214: warning: spurious trailing `%' in format
\r
276 * this line is testing an invalid % at the end of the format string.
\r
278 * this should display (on 32bit int machine) :
\r
282 * (null) is null pointer
\r
284 * -2147483647 = - max int
\r
288 * signed -3 = unsigned 4294967293 = hex fffffffd
\r
290 * 0 message(s) with %
\r
293 * 3: 0003 zero padded
\r
294 * 3: 3 left justif.
\r
295 * 3: 3 right justif.
\r
296 * -3: -003 zero padded
\r
297 * -3: -3 left justif.
\r
298 * -3: -3 right justif.
\r
304 /* To keep linker happy. */
\r
305 int write( int i, char* c, int n)
\r