--with-gnu-ld uses different x- fiile under aix 4.1
[official-gcc.git] / gcc / doprint.c
blob2dc4ddef9761afd6b24b805f70bac86436219ff5
1 /* Provide a version _doprnt in terms of fprintf.
2 Copyright (C) 1998 Free Software Foundation, Inc.
3 Contributed by Kaveh Ghazi (ghazi@caip.rutgers.edu) 3/29/98
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #undef _doprnt
25 #ifdef TEST /* Make sure to use the internal one. */
26 #define _doprnt my_doprnt
27 #endif
29 #define COPY_VA_INT \
30 do { \
31 const int value = abs (va_arg (ap, int)); \
32 char buf[32]; \
33 ptr++; /* Go past the asterisk. */ \
34 *sptr = '\0'; /* NULL terminate sptr. */ \
35 sprintf(buf, "%d", value); \
36 strcat(sptr, buf); \
37 while (*sptr) sptr++; \
38 } while (0)
40 #define PRINT_CHAR(CHAR) \
41 do { \
42 putc(CHAR, stream); \
43 ptr++; \
44 total_printed++; \
45 continue; \
46 } while (0)
48 #define PRINT_TYPE(TYPE) \
49 do { \
50 int result; \
51 TYPE value = va_arg (ap, TYPE); \
52 *sptr++ = *ptr++; /* Copy the type specifier. */ \
53 *sptr = '\0'; /* NULL terminate sptr. */ \
54 result = fprintf(stream, specifier, value); \
55 if (result == -1) \
56 return -1; \
57 else \
58 { \
59 total_printed += result; \
60 continue; \
61 } \
62 } while (0)
64 int
65 _doprnt (format, ap, stream)
66 const char * format;
67 va_list ap;
68 FILE * stream;
70 const char * ptr = format;
71 char specifier[128];
72 int total_printed = 0;
74 while (*ptr != '\0')
76 if (*ptr != '%') /* While we have regular characters, print them. */
77 PRINT_CHAR(*ptr);
78 else /* We got a format specifier! */
80 char * sptr = specifier;
81 int wide_width = 0, short_width = 0;
83 *sptr++ = *ptr++; /* Copy the % and move forward. */
85 while (strchr ("-+ #0", *ptr)) /* Move past flags. */
86 *sptr++ = *ptr++;
88 if (*ptr == '*')
89 COPY_VA_INT;
90 else
91 while (isdigit(*ptr)) /* Handle explicit numeric value. */
92 *sptr++ = *ptr++;
94 if (*ptr == '.')
96 *sptr++ = *ptr++; /* Copy and go past the period. */
97 if (*ptr == '*')
98 COPY_VA_INT;
99 else
100 while (isdigit(*ptr)) /* Handle explicit numeric value. */
101 *sptr++ = *ptr++;
103 while (strchr ("hlL", *ptr))
105 switch (*ptr)
107 case 'h':
108 short_width = 1;
109 break;
110 case 'l':
111 wide_width++;
112 break;
113 case 'L':
114 wide_width = 2;
115 break;
116 default:
117 abort();
119 *sptr++ = *ptr++;
122 switch (*ptr)
124 case 'd':
125 case 'i':
126 case 'o':
127 case 'u':
128 case 'x':
129 case 'X':
130 case 'c':
132 /* Short values are promoted to int, so just copy it
133 as an int and trust the C library printf to cast it
134 to the right width. */
135 if (short_width)
136 PRINT_TYPE(int);
137 else
139 switch (wide_width)
141 case 0:
142 PRINT_TYPE(int);
143 break;
144 case 1:
145 PRINT_TYPE(long);
146 break;
147 case 2:
148 default:
149 #if defined(__GNUC__) || defined(HAVE_LONG_LONG)
150 PRINT_TYPE(long long);
151 #else
152 PRINT_TYPE(long); /* Fake it and hope for the best. */
153 #endif
154 break;
155 } /* End of switch (wide_width) */
156 } /* End of else statement */
157 } /* End of integer case */
158 break;
159 case 'f':
160 case 'e':
161 case 'E':
162 case 'g':
163 case 'G':
165 if (wide_width == 0)
166 PRINT_TYPE(double);
167 else
169 #if defined(__GNUC__) || defined(HAVE_LONG_DOUBLE)
170 PRINT_TYPE(long double);
171 #else
172 PRINT_TYPE(double); /* Fake it and hope for the best. */
173 #endif
176 break;
177 case 's':
178 PRINT_TYPE(char *);
179 break;
180 case 'p':
181 PRINT_TYPE(void *);
182 break;
183 case '%':
184 PRINT_CHAR('%');
185 break;
186 default:
187 abort();
188 } /* End of switch (*ptr) */
189 } /* End of else statement */
192 return total_printed;
195 #ifdef TEST
197 #include <math.h>
198 #ifndef M_PI
199 #define M_PI (3.1415926535897932385)
200 #endif
202 #define RESULT(x) do \
204 int i = (x); \
205 printf ("printed %d characters\n", i); \
206 fflush(stdin); \
207 } while (0)
209 static int checkit PVPROTO ((const char * format, ...)) ATTRIBUTE_PRINTF_1;
211 static int
212 checkit VPROTO ((const char* format, ...))
214 va_list args;
215 int result;
217 #ifndef ANSI_PROTOTYPES
218 char *format;
219 #endif
221 VA_START (args, format);
223 #ifndef ANSI_PROTOTYPES
224 format = va_arg (args, char *);
225 #endif
227 result = _doprnt (format, args, stdout);
228 va_end(args);
230 return result;
234 main ()
236 RESULT(checkit ("<%d>\n", 0x12345678));
237 RESULT(printf ("<%d>\n", 0x12345678));
239 RESULT(checkit ("<%200d>\n", 5));
240 RESULT(printf ("<%200d>\n", 5));
242 RESULT(checkit ("<%.300d>\n", 6));
243 RESULT(printf ("<%.300d>\n", 6));
245 RESULT(checkit ("<%100.150d>\n", 7));
246 RESULT(printf ("<%100.150d>\n", 7));
248 RESULT(checkit ("<%s>\n",
249 "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
250 777777777777777777333333333333366666666666622222222222777777777777733333"));
251 RESULT(printf ("<%s>\n",
252 "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
253 777777777777777777333333333333366666666666622222222222777777777777733333"));
255 RESULT(checkit ("<%f><%0+#f>%s%d%s>\n",
256 1.0, 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx"));
257 RESULT(printf ("<%f><%0+#f>%s%d%s>\n",
258 1.0, 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx"));
260 RESULT(checkit ("<%4f><%.4f><%%><%4.4f>\n", M_PI, M_PI, M_PI));
261 RESULT(printf ("<%4f><%.4f><%%><%4.4f>\n", M_PI, M_PI, M_PI));
263 RESULT(checkit ("<%*f><%.*f><%%><%*.*f>\n", 3, M_PI, 3, M_PI, 3, 3, M_PI));
264 RESULT(printf ("<%*f><%.*f><%%><%*.*f>\n", 3, M_PI, 3, M_PI, 3, 3, M_PI));
266 RESULT(checkit ("<%d><%i><%o><%u><%x><%X><%c>\n",
267 75, 75, 75, 75, 75, 75, 75));
268 RESULT(printf ("<%d><%i><%o><%u><%x><%X><%c>\n",
269 75, 75, 75, 75, 75, 75, 75));
271 RESULT(checkit ("<%d><%i><%o><%u><%x><%X><%c>\n",
272 75, 75, 75, 75, 75, 75, 75));
273 RESULT(printf ("<%d><%i><%o><%u><%x><%X><%c>\n",
274 75, 75, 75, 75, 75, 75, 75));
276 RESULT(checkit ("Testing (hd) short: <%d><%ld><%hd><%hd><%d>\n", 123, (long)234, 345, 123456789, 456));
277 RESULT(printf ("Testing (hd) short: <%d><%ld><%hd><%hd><%d>\n", 123, (long)234, 345, 123456789, 456));
279 #if defined(__GNUC__) || defined (HAVE_LONG_LONG)
280 RESULT(checkit ("Testing (lld) long long: <%d><%lld><%d>\n", 123, 234234234234234234LL, 345));
281 RESULT(printf ("Testing (lld) long long: <%d><%lld><%d>\n", 123, 234234234234234234LL, 345));
282 RESULT(checkit ("Testing (Ld) long long: <%d><%Ld><%d>\n", 123, 234234234234234234LL, 345));
283 RESULT(printf ("Testing (Ld) long long: <%d><%Ld><%d>\n", 123, 234234234234234234LL, 345));
284 #endif
286 #if defined(__GNUC__) || defined (HAVE_LONG_DOUBLE)
287 RESULT(checkit ("Testing (Lf) long double: <%.20f><%.20Lf><%0+#.20f>\n",
288 1.23456, 1.234567890123456789L, 1.23456));
289 RESULT(printf ("Testing (Lf) long double: <%.20f><%.20Lf><%0+#.20f>\n",
290 1.23456, 1.234567890123456789L, 1.23456));
291 #endif
293 return 0;
295 #endif /* TEST */