Back to index

cell-binutils  2.17cvs20070401
_doprnt.c
Go to the documentation of this file.
00001 /* Provide a version of _doprnt in terms of fprintf.
00002    Copyright (C) 1998, 1999, 2000, 2001, 2002   Free Software Foundation, Inc.
00003    Contributed by Kaveh Ghazi  (ghazi@caip.rutgers.edu)  3/29/98
00004 
00005 This program is free software; you can redistribute it and/or modify it
00006 under the terms of the GNU General Public License as published by the
00007 Free Software Foundation; either version 2, or (at your option) any
00008 later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00018 
00019 #include "config.h"
00020 #include "ansidecl.h"
00021 #include "safe-ctype.h"
00022 
00023 #include <stdio.h>
00024 #include <stdarg.h>
00025 #ifdef HAVE_STRING_H
00026 #include <string.h>
00027 #endif
00028 #ifdef HAVE_STDLIB_H
00029 #include <stdlib.h>
00030 #endif
00031 
00032 #undef _doprnt
00033 
00034 #ifdef HAVE__DOPRNT
00035 #define TEST
00036 #endif
00037 
00038 #ifdef TEST /* Make sure to use the internal one.  */
00039 #define _doprnt my_doprnt
00040 #endif
00041 
00042 #define COPY_VA_INT \
00043   do { \
00044         const int value = abs (va_arg (ap, int)); \
00045         char buf[32]; \
00046         ptr++; /* Go past the asterisk.  */ \
00047         *sptr = '\0'; /* NULL terminate sptr.  */ \
00048         sprintf(buf, "%d", value); \
00049         strcat(sptr, buf); \
00050         while (*sptr) sptr++; \
00051      } while (0)
00052 
00053 #define PRINT_CHAR(CHAR) \
00054   do { \
00055         putc(CHAR, stream); \
00056         ptr++; \
00057         total_printed++; \
00058         continue; \
00059      } while (0)
00060 
00061 #define PRINT_TYPE(TYPE) \
00062   do { \
00063        int result; \
00064        TYPE value = va_arg (ap, TYPE); \
00065        *sptr++ = *ptr++; /* Copy the type specifier.  */ \
00066        *sptr = '\0'; /* NULL terminate sptr.  */ \
00067        result = fprintf(stream, specifier, value); \
00068        if (result == -1) \
00069          return -1; \
00070        else \
00071          { \
00072            total_printed += result; \
00073            continue; \
00074          } \
00075       } while (0)
00076 
00077 int
00078 _doprnt (const char *format, va_list ap, FILE *stream)
00079 {
00080   const char * ptr = format;
00081   char specifier[128];
00082   int total_printed = 0;
00083   
00084   while (*ptr != '\0')
00085     {
00086       if (*ptr != '%') /* While we have regular characters, print them.  */
00087        PRINT_CHAR(*ptr);
00088       else /* We got a format specifier! */
00089        {
00090          char * sptr = specifier;
00091          int wide_width = 0, short_width = 0;
00092          
00093          *sptr++ = *ptr++; /* Copy the % and move forward.  */
00094 
00095          while (strchr ("-+ #0", *ptr)) /* Move past flags.  */
00096            *sptr++ = *ptr++;
00097 
00098          if (*ptr == '*')
00099            COPY_VA_INT;
00100          else
00101            while (ISDIGIT(*ptr)) /* Handle explicit numeric value.  */
00102              *sptr++ = *ptr++;
00103          
00104          if (*ptr == '.')
00105            {
00106              *sptr++ = *ptr++; /* Copy and go past the period.  */
00107              if (*ptr == '*')
00108               COPY_VA_INT;
00109              else
00110               while (ISDIGIT(*ptr)) /* Handle explicit numeric value.  */
00111                 *sptr++ = *ptr++;
00112            }
00113          while (strchr ("hlL", *ptr))
00114            {
00115              switch (*ptr)
00116               {
00117               case 'h':
00118                 short_width = 1;
00119                 break;
00120               case 'l':
00121                 wide_width++;
00122                 break;
00123               case 'L':
00124                 wide_width = 2;
00125                 break;
00126               default:
00127                 abort();
00128               }
00129              *sptr++ = *ptr++;
00130            }
00131 
00132          switch (*ptr)
00133            {
00134            case 'd':
00135            case 'i':
00136            case 'o':
00137            case 'u':
00138            case 'x':
00139            case 'X':
00140            case 'c':
00141              {
00142               /* Short values are promoted to int, so just copy it
00143                    as an int and trust the C library printf to cast it
00144                    to the right width.  */
00145               if (short_width)
00146                 PRINT_TYPE(int);
00147               else
00148                 {
00149                   switch (wide_width)
00150                     {
00151                     case 0:
00152                      PRINT_TYPE(int);
00153                      break;
00154                     case 1:
00155                      PRINT_TYPE(long);
00156                      break;
00157                     case 2:
00158                     default:
00159 #if defined(__GNUC__) || defined(HAVE_LONG_LONG)
00160                      PRINT_TYPE(long long);
00161 #else
00162                      PRINT_TYPE(long); /* Fake it and hope for the best.  */
00163 #endif
00164                      break;
00165                     } /* End of switch (wide_width) */
00166                 } /* End of else statement */
00167              } /* End of integer case */
00168              break;
00169            case 'f':
00170            case 'e':
00171            case 'E':
00172            case 'g':
00173            case 'G':
00174              {
00175               if (wide_width == 0)
00176                 PRINT_TYPE(double);
00177               else
00178                 {
00179 #if defined(__GNUC__) || defined(HAVE_LONG_DOUBLE)
00180                   PRINT_TYPE(long double);
00181 #else
00182                   PRINT_TYPE(double); /* Fake it and hope for the best.  */
00183 #endif
00184                 }
00185              }
00186              break;
00187            case 's':
00188              PRINT_TYPE(char *);
00189              break;
00190            case 'p':
00191              PRINT_TYPE(void *);
00192              break;
00193            case '%':
00194              PRINT_CHAR('%');
00195              break;
00196            default:
00197              abort();
00198            } /* End of switch (*ptr) */
00199        } /* End of else statement */
00200     }
00201 
00202   return total_printed;
00203 }
00204 
00205 #ifdef TEST
00206 
00207 #include <math.h>
00208 #ifndef M_PI
00209 #define M_PI (3.1415926535897932385)
00210 #endif
00211 
00212 #define RESULT(x) do \
00213 { \
00214     int i = (x); \
00215     printf ("printed %d characters\n", i); \
00216     fflush(stdin); \
00217 } while (0)
00218 
00219 static int checkit (const char * format, ...) ATTRIBUTE_PRINTF_1;
00220 
00221 static int
00222 checkit (const char* format, ...)
00223 {
00224   int result;
00225   VA_OPEN (args, format);
00226   VA_FIXEDARG (args, char *, format);
00227 
00228   result = _doprnt (format, args, stdout);
00229   VA_CLOSE (args);
00230 
00231   return result;
00232 }
00233 
00234 int
00235 main (void)
00236 {
00237   RESULT(checkit ("<%d>\n", 0x12345678));
00238   RESULT(printf ("<%d>\n", 0x12345678));
00239 
00240   RESULT(checkit ("<%200d>\n", 5));
00241   RESULT(printf ("<%200d>\n", 5));
00242 
00243   RESULT(checkit ("<%.300d>\n", 6));
00244   RESULT(printf ("<%.300d>\n", 6));
00245 
00246   RESULT(checkit ("<%100.150d>\n", 7));
00247   RESULT(printf ("<%100.150d>\n", 7));
00248 
00249   RESULT(checkit ("<%s>\n",
00250                 "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
00251 777777777777777777333333333333366666666666622222222222777777777777733333"));
00252   RESULT(printf ("<%s>\n",
00253                "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
00254 777777777777777777333333333333366666666666622222222222777777777777733333"));
00255 
00256   RESULT(checkit ("<%f><%0+#f>%s%d%s>\n",
00257                 1.0, 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx"));
00258   RESULT(printf ("<%f><%0+#f>%s%d%s>\n",
00259                1.0, 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx"));
00260 
00261   RESULT(checkit ("<%4f><%.4f><%%><%4.4f>\n", M_PI, M_PI, M_PI));
00262   RESULT(printf ("<%4f><%.4f><%%><%4.4f>\n", M_PI, M_PI, M_PI));
00263 
00264   RESULT(checkit ("<%*f><%.*f><%%><%*.*f>\n", 3, M_PI, 3, M_PI, 3, 3, M_PI));
00265   RESULT(printf ("<%*f><%.*f><%%><%*.*f>\n", 3, M_PI, 3, M_PI, 3, 3, M_PI));
00266 
00267   RESULT(checkit ("<%d><%i><%o><%u><%x><%X><%c>\n",
00268                 75, 75, 75, 75, 75, 75, 75));
00269   RESULT(printf ("<%d><%i><%o><%u><%x><%X><%c>\n",
00270                75, 75, 75, 75, 75, 75, 75));
00271 
00272   RESULT(checkit ("<%d><%i><%o><%u><%x><%X><%c>\n",
00273                 75, 75, 75, 75, 75, 75, 75));
00274   RESULT(printf ("<%d><%i><%o><%u><%x><%X><%c>\n",
00275                75, 75, 75, 75, 75, 75, 75));
00276 
00277   RESULT(checkit ("Testing (hd) short: <%d><%ld><%hd><%hd><%d>\n", 123, (long)234, 345, 123456789, 456));
00278   RESULT(printf ("Testing (hd) short: <%d><%ld><%hd><%hd><%d>\n", 123, (long)234, 345, 123456789, 456));
00279 
00280 #if defined(__GNUC__) || defined (HAVE_LONG_LONG)
00281   RESULT(checkit ("Testing (lld) long long: <%d><%lld><%d>\n", 123, 234234234234234234LL, 345));
00282   RESULT(printf ("Testing (lld) long long: <%d><%lld><%d>\n", 123, 234234234234234234LL, 345));
00283   RESULT(checkit ("Testing (Ld) long long: <%d><%Ld><%d>\n", 123, 234234234234234234LL, 345));
00284   RESULT(printf ("Testing (Ld) long long: <%d><%Ld><%d>\n", 123, 234234234234234234LL, 345));
00285 #endif
00286 
00287 #if defined(__GNUC__) || defined (HAVE_LONG_DOUBLE)
00288   RESULT(checkit ("Testing (Lf) long double: <%.20f><%.20Lf><%0+#.20f>\n",
00289                 1.23456, 1.234567890123456789L, 1.23456));
00290   RESULT(printf ("Testing (Lf) long double: <%.20f><%.20Lf><%0+#.20f>\n",
00291                1.23456, 1.234567890123456789L, 1.23456));
00292 #endif
00293 
00294   return 0;
00295 }
00296 #endif /* TEST */