fmtcheck.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* $NetBSD: fmtcheck.c,v 1.8 2008/04/28 20:22:59 martin Exp $ */
  2. /*-
  3. * Copyright (c) 2000 The NetBSD Foundation, Inc.
  4. * All rights reserved.
  5. *
  6. * This code was contributed to The NetBSD Foundation by Allen Briggs.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  19. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  20. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  21. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include "file.h"
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <ctype.h>
  33. enum __e_fmtcheck_types {
  34. FMTCHECK_START,
  35. FMTCHECK_SHORT,
  36. FMTCHECK_INT,
  37. FMTCHECK_LONG,
  38. FMTCHECK_QUAD,
  39. FMTCHECK_SHORTPOINTER,
  40. FMTCHECK_INTPOINTER,
  41. FMTCHECK_LONGPOINTER,
  42. FMTCHECK_QUADPOINTER,
  43. FMTCHECK_DOUBLE,
  44. FMTCHECK_LONGDOUBLE,
  45. FMTCHECK_STRING,
  46. FMTCHECK_WIDTH,
  47. FMTCHECK_PRECISION,
  48. FMTCHECK_DONE,
  49. FMTCHECK_UNKNOWN
  50. };
  51. typedef enum __e_fmtcheck_types EFT;
  52. #define RETURN(pf,f,r) do { \
  53. *(pf) = (f); \
  54. return r; \
  55. } /*NOTREACHED*/ /*CONSTCOND*/ while (0)
  56. static EFT
  57. get_next_format_from_precision(const char **pf)
  58. {
  59. int sh, lg, quad, longdouble;
  60. const char *f;
  61. sh = lg = quad = longdouble = 0;
  62. f = *pf;
  63. switch (*f) {
  64. case 'h':
  65. f++;
  66. sh = 1;
  67. break;
  68. case 'l':
  69. f++;
  70. if (!*f) RETURN(pf,f,FMTCHECK_UNKNOWN);
  71. if (*f == 'l') {
  72. f++;
  73. quad = 1;
  74. } else {
  75. lg = 1;
  76. }
  77. break;
  78. case 'q':
  79. f++;
  80. quad = 1;
  81. break;
  82. case 'L':
  83. f++;
  84. longdouble = 1;
  85. break;
  86. #ifdef WIN32
  87. case 'I':
  88. f++;
  89. if (!*f) RETURN(pf,f,FMTCHECK_UNKNOWN);
  90. if (*f == '3' && f[1] == '2') {
  91. f += 2;
  92. } else if (*f == '6' && f[1] == '4') {
  93. f += 2;
  94. quad = 1;
  95. }
  96. #ifdef _WIN64
  97. else {
  98. quad = 1;
  99. }
  100. #endif
  101. break;
  102. #endif
  103. default:
  104. break;
  105. }
  106. if (!*f) RETURN(pf,f,FMTCHECK_UNKNOWN);
  107. if (strchr("diouxX", *f)) {
  108. if (longdouble)
  109. RETURN(pf,f,FMTCHECK_UNKNOWN);
  110. if (lg)
  111. RETURN(pf,f,FMTCHECK_LONG);
  112. if (quad)
  113. RETURN(pf,f,FMTCHECK_QUAD);
  114. RETURN(pf,f,FMTCHECK_INT);
  115. }
  116. if (*f == 'n') {
  117. if (longdouble)
  118. RETURN(pf,f,FMTCHECK_UNKNOWN);
  119. if (sh)
  120. RETURN(pf,f,FMTCHECK_SHORTPOINTER);
  121. if (lg)
  122. RETURN(pf,f,FMTCHECK_LONGPOINTER);
  123. if (quad)
  124. RETURN(pf,f,FMTCHECK_QUADPOINTER);
  125. RETURN(pf,f,FMTCHECK_INTPOINTER);
  126. }
  127. if (strchr("DOU", *f)) {
  128. if (sh + lg + quad + longdouble)
  129. RETURN(pf,f,FMTCHECK_UNKNOWN);
  130. RETURN(pf,f,FMTCHECK_LONG);
  131. }
  132. if (strchr("eEfg", *f)) {
  133. if (longdouble)
  134. RETURN(pf,f,FMTCHECK_LONGDOUBLE);
  135. if (sh + lg + quad)
  136. RETURN(pf,f,FMTCHECK_UNKNOWN);
  137. RETURN(pf,f,FMTCHECK_DOUBLE);
  138. }
  139. if (*f == 'c') {
  140. if (sh + lg + quad + longdouble)
  141. RETURN(pf,f,FMTCHECK_UNKNOWN);
  142. RETURN(pf,f,FMTCHECK_INT);
  143. }
  144. if (*f == 's') {
  145. if (sh + lg + quad + longdouble)
  146. RETURN(pf,f,FMTCHECK_UNKNOWN);
  147. RETURN(pf,f,FMTCHECK_STRING);
  148. }
  149. if (*f == 'p') {
  150. if (sh + lg + quad + longdouble)
  151. RETURN(pf,f,FMTCHECK_UNKNOWN);
  152. RETURN(pf,f,FMTCHECK_LONG);
  153. }
  154. RETURN(pf,f,FMTCHECK_UNKNOWN);
  155. /*NOTREACHED*/
  156. }
  157. static EFT
  158. get_next_format_from_width(const char **pf)
  159. {
  160. const char *f;
  161. f = *pf;
  162. if (*f == '.') {
  163. f++;
  164. if (*f == '*') {
  165. RETURN(pf,f,FMTCHECK_PRECISION);
  166. }
  167. /* eat any precision (empty is allowed) */
  168. while (isdigit((unsigned char)*f)) f++;
  169. if (!*f) RETURN(pf,f,FMTCHECK_UNKNOWN);
  170. }
  171. RETURN(pf,f,get_next_format_from_precision(pf));
  172. /*NOTREACHED*/
  173. }
  174. static EFT
  175. get_next_format(const char **pf, EFT eft)
  176. {
  177. int infmt;
  178. const char *f;
  179. if (eft == FMTCHECK_WIDTH) {
  180. (*pf)++;
  181. return get_next_format_from_width(pf);
  182. } else if (eft == FMTCHECK_PRECISION) {
  183. (*pf)++;
  184. return get_next_format_from_precision(pf);
  185. }
  186. f = *pf;
  187. infmt = 0;
  188. while (!infmt) {
  189. f = strchr(f, '%');
  190. if (f == NULL)
  191. RETURN(pf,f,FMTCHECK_DONE);
  192. f++;
  193. if (!*f)
  194. RETURN(pf,f,FMTCHECK_UNKNOWN);
  195. if (*f != '%')
  196. infmt = 1;
  197. else
  198. f++;
  199. }
  200. /* Eat any of the flags */
  201. while (*f && (strchr("#0- +", *f)))
  202. f++;
  203. if (*f == '*') {
  204. RETURN(pf,f,FMTCHECK_WIDTH);
  205. }
  206. /* eat any width */
  207. while (isdigit((unsigned char)*f)) f++;
  208. if (!*f) {
  209. RETURN(pf,f,FMTCHECK_UNKNOWN);
  210. }
  211. RETURN(pf,f,get_next_format_from_width(pf));
  212. /*NOTREACHED*/
  213. }
  214. const char *
  215. fmtcheck(const char *f1, const char *f2)
  216. {
  217. const char *f1p, *f2p;
  218. EFT f1t, f2t;
  219. if (!f1) return f2;
  220. f1p = f1;
  221. f1t = FMTCHECK_START;
  222. f2p = f2;
  223. f2t = FMTCHECK_START;
  224. while ((f1t = get_next_format(&f1p, f1t)) != FMTCHECK_DONE) {
  225. if (f1t == FMTCHECK_UNKNOWN)
  226. return f2;
  227. f2t = get_next_format(&f2p, f2t);
  228. if (f1t != f2t)
  229. return f2;
  230. }
  231. return f1;
  232. }