Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. int
  2. _doprnt (const char *format, va_list ap, FILE *stream)
  3. {
  4. const char * ptr = format;
  5. char specifier[128];
  6. int total_printed = 0;
  7.  
  8. while (*ptr != '\0')
  9. {
  10. if (*ptr != '%') /* While we have regular characters, print them. */
  11. PRINT_CHAR(*ptr);
  12. else /* We got a format specifier! */
  13. {
  14. char * sptr = specifier;
  15. int wide_width = 0, short_width = 0;
  16.  
  17. *sptr++ = *ptr++; /* Copy the % and move forward. */
  18.  
  19. while (strchr ("-+ #0", *ptr)) /* Move past flags. */
  20. *sptr++ = *ptr++;
  21.  
  22. if (*ptr == '*')
  23. COPY_VA_INT;
  24. else
  25. while (ISDIGIT(*ptr)) /* Handle explicit numeric value. */
  26. *sptr++ = *ptr++;
  27.  
  28. if (*ptr == '.')
  29. {
  30. *sptr++ = *ptr++; /* Copy and go past the period. */
  31. if (*ptr == '*')
  32. COPY_VA_INT;
  33. else
  34. while (ISDIGIT(*ptr)) /* Handle explicit numeric value. */
  35. *sptr++ = *ptr++;
  36. }
  37. while (strchr ("hlL", *ptr))
  38. {
  39. switch (*ptr)
  40. {
  41. case 'h':
  42. short_width = 1;
  43. break;
  44. case 'l':
  45. wide_width++;
  46. break;
  47. case 'L':
  48. wide_width = 2;
  49. break;
  50. default:
  51. abort();
  52. }
  53. *sptr++ = *ptr++;
  54. }
  55.  
  56. switch (*ptr)
  57. {
  58. case 'd':
  59. case 'i':
  60. case 'o':
  61. case 'u':
  62. case 'x':
  63. case 'X':
  64. case 'c':
  65. {
  66. /* Short values are promoted to int, so just copy it
  67. as an int and trust the C library printf to cast it
  68. to the right width. */
  69. if (short_width)
  70. PRINT_TYPE(int);
  71. else
  72. {
  73. switch (wide_width)
  74. {
  75. case 0:
  76. PRINT_TYPE(int);
  77. break;
  78. case 1:
  79. PRINT_TYPE(long);
  80. break;
  81. case 2:
  82. default:
  83. #if defined(__GNUC__) || defined(HAVE_LONG_LONG)
  84. PRINT_TYPE(long long);
  85. #else
  86. PRINT_TYPE(long); /* Fake it and hope for the best. */
  87. #endif
  88. break;
  89. } /* End of switch (wide_width) */
  90. } /* End of else statement */
  91. } /* End of integer case */
  92. break;
  93. case 'f':
  94. case 'e':
  95. case 'E':
  96. case 'g':
  97. case 'G':
  98. {
  99. if (wide_width == 0)
  100. PRINT_TYPE(double);
  101. else
  102. {
  103. #if defined(__GNUC__) || defined(HAVE_LONG_DOUBLE)
  104. PRINT_TYPE(long double);
  105. #else
  106. PRINT_TYPE(double); /* Fake it and hope for the best. */
  107. #endif
  108. }
  109. }
  110. break;
  111. case 's':
  112. PRINT_TYPE(char *);
  113. break;
  114. case 'p':
  115. PRINT_TYPE(void *);
  116. break;
  117. case '%':
  118. PRINT_CHAR('%');
  119. break;
  120. default:
  121. abort();
  122. } /* End of switch (*ptr) */
  123. } /* End of else statement */
  124. }
  125.  
  126. return total_printed;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement