fmtcheck.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. default:
  87. break;
  88. }
  89. if (!*f) RETURN(pf,f,FMTCHECK_UNKNOWN);
  90. if (strchr("diouxX", *f)) {
  91. if (longdouble)
  92. RETURN(pf,f,FMTCHECK_UNKNOWN);
  93. if (lg)
  94. RETURN(pf,f,FMTCHECK_LONG);
  95. if (quad)
  96. RETURN(pf,f,FMTCHECK_QUAD);
  97. RETURN(pf,f,FMTCHECK_INT);
  98. }
  99. if (*f == 'n') {
  100. if (longdouble)
  101. RETURN(pf,f,FMTCHECK_UNKNOWN);
  102. if (sh)
  103. RETURN(pf,f,FMTCHECK_SHORTPOINTER);
  104. if (lg)
  105. RETURN(pf,f,FMTCHECK_LONGPOINTER);
  106. if (quad)
  107. RETURN(pf,f,FMTCHECK_QUADPOINTER);
  108. RETURN(pf,f,FMTCHECK_INTPOINTER);
  109. }
  110. if (strchr("DOU", *f)) {
  111. if (sh + lg + quad + longdouble)
  112. RETURN(pf,f,FMTCHECK_UNKNOWN);
  113. RETURN(pf,f,FMTCHECK_LONG);
  114. }
  115. if (strchr("eEfg", *f)) {
  116. if (longdouble)
  117. RETURN(pf,f,FMTCHECK_LONGDOUBLE);
  118. if (sh + lg + quad)
  119. RETURN(pf,f,FMTCHECK_UNKNOWN);
  120. RETURN(pf,f,FMTCHECK_DOUBLE);
  121. }
  122. if (*f == 'c') {
  123. if (sh + lg + quad + longdouble)
  124. RETURN(pf,f,FMTCHECK_UNKNOWN);
  125. RETURN(pf,f,FMTCHECK_INT);
  126. }
  127. if (*f == 's') {
  128. if (sh + lg + quad + longdouble)
  129. RETURN(pf,f,FMTCHECK_UNKNOWN);
  130. RETURN(pf,f,FMTCHECK_STRING);
  131. }
  132. if (*f == 'p') {
  133. if (sh + lg + quad + longdouble)
  134. RETURN(pf,f,FMTCHECK_UNKNOWN);
  135. RETURN(pf,f,FMTCHECK_LONG);
  136. }
  137. RETURN(pf,f,FMTCHECK_UNKNOWN);
  138. /*NOTREACHED*/
  139. }
  140. static EFT
  141. get_next_format_from_width(const char **pf)
  142. {
  143. const char *f;
  144. f = *pf;
  145. if (*f == '.') {
  146. f++;
  147. if (*f == '*') {
  148. RETURN(pf,f,FMTCHECK_PRECISION);
  149. }
  150. /* eat any precision (empty is allowed) */
  151. while (isdigit((unsigned char)*f)) f++;
  152. if (!*f) RETURN(pf,f,FMTCHECK_UNKNOWN);
  153. }
  154. RETURN(pf,f,get_next_format_from_precision(pf));
  155. /*NOTREACHED*/
  156. }
  157. static EFT
  158. get_next_format(const char **pf, EFT eft)
  159. {
  160. int infmt;
  161. const char *f;
  162. if (eft == FMTCHECK_WIDTH) {
  163. (*pf)++;
  164. return get_next_format_from_width(pf);
  165. } else if (eft == FMTCHECK_PRECISION) {
  166. (*pf)++;
  167. return get_next_format_from_precision(pf);
  168. }
  169. f = *pf;
  170. infmt = 0;
  171. while (!infmt) {
  172. f = strchr(f, '%');
  173. if (f == NULL)
  174. RETURN(pf,f,FMTCHECK_DONE);
  175. f++;
  176. if (!*f)
  177. RETURN(pf,f,FMTCHECK_UNKNOWN);
  178. if (*f != '%')
  179. infmt = 1;
  180. else
  181. f++;
  182. }
  183. /* Eat any of the flags */
  184. while (*f && (strchr("#0- +", *f)))
  185. f++;
  186. if (*f == '*') {
  187. RETURN(pf,f,FMTCHECK_WIDTH);
  188. }
  189. /* eat any width */
  190. while (isdigit((unsigned char)*f)) f++;
  191. if (!*f) {
  192. RETURN(pf,f,FMTCHECK_UNKNOWN);
  193. }
  194. RETURN(pf,f,get_next_format_from_width(pf));
  195. /*NOTREACHED*/
  196. }
  197. const char *
  198. fmtcheck(const char *f1, const char *f2)
  199. {
  200. const char *f1p, *f2p;
  201. EFT f1t, f2t;
  202. if (!f1) return f2;
  203. f1p = f1;
  204. f1t = FMTCHECK_START;
  205. f2p = f2;
  206. f2t = FMTCHECK_START;
  207. while ((f1t = get_next_format(&f1p, f1t)) != FMTCHECK_DONE) {
  208. if (f1t == FMTCHECK_UNKNOWN)
  209. return f2;
  210. f2t = get_next_format(&f2p, f2t);
  211. if (f1t != f2t)
  212. return f2;
  213. }
  214. return f1;
  215. }