2 Copyright 2001, 2002 Georges Menie (www.menie.org)
3 stdarg version contributed by Christian Ettinger
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 putchar is the only external dependency for this file,
22 if you have a working putchar, leave it commented out.
23 If not, uncomment the define below and
24 replace outbyte(c) by your own function call.
32 static void printchar(char **str, int c)
34 //extern int putchar(int c);
49 static int prints(char **out, const char *string, int width, int pad)
51 register int pc = 0, padchar = ' ';
55 register const char *ptr;
56 for (ptr = string; *ptr; ++ptr) ++len;
57 if (len >= width) width = 0;
59 if (pad & PAD_ZERO) padchar = '0';
61 if (!(pad & PAD_RIGHT)) {
62 for ( ; width > 0; --width) {
63 printchar (out, padchar);
67 for ( ; *string ; ++string) {
68 printchar (out, *string);
71 for ( ; width > 0; --width) {
72 printchar (out, padchar);
79 /* the following should be enough for 32 bit int */
80 #define PRINT_BUF_LEN 12
82 static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase)
84 char print_buf[PRINT_BUF_LEN];
86 register int t, neg = 0, pc = 0;
87 register unsigned int u = (unsigned int)i;
92 return prints (out, print_buf, width, pad);
95 if (sg && b == 10 && i < 0) {
100 s = print_buf + PRINT_BUF_LEN-1;
104 t = (unsigned int)u % b;
106 t += letbase - '0' - 10;
107 *--s = (char)(t + '0');
112 if( width && (pad & PAD_ZERO) ) {
113 printchar (out, '-');
122 return pc + prints (out, s, width, pad);
125 static int print( char **out, const char *format, va_list args )
127 register int width, pad;
131 for (; *format != 0; ++format) {
132 if (*format == '%') {
135 if (*format == '\0') break;
136 if (*format == '%') goto out;
137 if (*format == '-') {
141 while (*format == '0') {
145 for ( ; *format >= '0' && *format <= '9'; ++format) {
147 width += *format - '0';
149 if( *format == 's' ) {
150 register char *s = (char *)va_arg( args, int );
151 pc += prints (out, s?s:"(null)", width, pad);
154 if( *format == 'd' ) {
155 pc += printi (out, va_arg( args, int ), 10, 1, width, pad, 'a');
158 if( *format == 'x' ) {
159 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'a');
162 if( *format == 'X' ) {
163 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'A');
166 if( *format == 'u' ) {
167 pc += printi (out, va_arg( args, int ), 10, 0, width, pad, 'a');
170 if( *format == 'c' ) {
171 /* char are converted to int then pushed on the stack */
172 scr[0] = (char)va_arg( args, int );
174 pc += prints (out, scr, width, pad);
180 printchar (out, *format);
184 if (out) **out = '\0';
189 int printf(const char *format, ...)
193 va_start( args, format );
194 return print( 0, format, args );
197 int sprintf(char *out, const char *format, ...)
201 va_start( args, format );
202 return print( &out, format, args );
206 int snprintf( char *buf, unsigned int count, const char *format, ... )
212 va_start( args, format );
213 return print( &buf, format, args );
220 char *ptr = "Hello world!";
223 unsigned int bs = sizeof(int)*8;
227 mi = (1 << (bs-1)) + 1;
229 printf("printf test\n");
230 printf("%s is null pointer\n", np);
231 printf("%d = 5\n", i);
232 printf("%d = - max int\n", mi);
233 printf("char %c = 'a'\n", 'a');
234 printf("hex %x = ff\n", 0xff);
235 printf("hex %02x = 00\n", 0);
236 printf("signed %d = unsigned %u = hex %x\n", -3, -3, -3);
237 printf("%d %s(s)%", 0, "message");
239 printf("%d %s(s) with %%\n", 0, "message");
240 sprintf(buf, "justif: \"%-10s\"\n", "left"); printf("%s", buf);
241 sprintf(buf, "justif: \"%10s\"\n", "right"); printf("%s", buf);
242 sprintf(buf, " 3: %04d zero padded\n", 3); printf("%s", buf);
243 sprintf(buf, " 3: %-4d left justif.\n", 3); printf("%s", buf);
244 sprintf(buf, " 3: %4d right justif.\n", 3); printf("%s", buf);
245 sprintf(buf, "-3: %04d zero padded\n", -3); printf("%s", buf);
246 sprintf(buf, "-3: %-4d left justif.\n", -3); printf("%s", buf);
247 sprintf(buf, "-3: %4d right justif.\n", -3); printf("%s", buf);
253 * if you compile this file with
254 * gcc -Wall $(YOUR_C_OPTIONS) -DTEST_PRINTF -c printf.c
255 * you will get a normal warning:
256 * printf.c:214: warning: spurious trailing `%' in format
257 * this line is testing an invalid % at the end of the format string.
259 * this should display (on 32bit int machine) :
263 * (null) is null pointer
265 * -2147483647 = - max int
269 * signed -3 = unsigned 4294967293 = hex fffffffd
271 * 0 message(s) with %
274 * 3: 0003 zero padded
277 * -3: -003 zero padded
278 * -3: -3 left justif.
279 * -3: -3 right justif.
285 /* To keep linker happy. */
286 int write( int i, char* c, int n)