fmtcheck.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. #ifndef lint
  31. FILE_RCSID("@(#)$File: fmtcheck.c,v 1.4 2022/09/13 18:46:07 christos Exp $")
  32. #endif /* lint */
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <ctype.h>
  36. enum __e_fmtcheck_types {
  37. FMTCHECK_START,
  38. FMTCHECK_SHORT,
  39. FMTCHECK_INT,
  40. FMTCHECK_LONG,
  41. FMTCHECK_QUAD,
  42. FMTCHECK_SHORTPOINTER,
  43. FMTCHECK_INTPOINTER,
  44. FMTCHECK_LONGPOINTER,
  45. FMTCHECK_QUADPOINTER,
  46. FMTCHECK_DOUBLE,
  47. FMTCHECK_LONGDOUBLE,
  48. FMTCHECK_STRING,
  49. FMTCHECK_WIDTH,
  50. FMTCHECK_PRECISION,
  51. FMTCHECK_DONE,
  52. FMTCHECK_UNKNOWN
  53. };
  54. typedef enum __e_fmtcheck_types EFT;
  55. #define RETURN(pf,f,r) do { \
  56. *(pf) = (f); \
  57. return r; \
  58. } /*NOTREACHED*/ /*CONSTCOND*/ while (0)
  59. static EFT
  60. get_next_format_from_precision(const char **pf)
  61. {
  62. int sh, lg, quad, longdouble;
  63. const char *f;
  64. sh = lg = quad = longdouble = 0;
  65. f = *pf;
  66. switch (*f) {
  67. case 'h':
  68. f++;
  69. sh = 1;
  70. break;
  71. case 'l':
  72. f++;
  73. if (!*f) RETURN(pf,f,FMTCHECK_UNKNOWN);
  74. if (*f == 'l') {
  75. f++;
  76. quad = 1;
  77. } else {
  78. lg = 1;
  79. }
  80. break;
  81. case 'q':
  82. f++;
  83. quad = 1;
  84. break;
  85. case 'L':
  86. f++;
  87. longdouble = 1;
  88. break;
  89. #ifdef WIN32
  90. case 'I':
  91. f++;
  92. if (!*f) RETURN(pf,f,FMTCHECK_UNKNOWN);
  93. if (*f == '3' && f[1] == '2') {
  94. f += 2;
  95. } else if (*f == '6' && f[1] == '4') {
  96. f += 2;
  97. quad = 1;
  98. }
  99. #ifdef _WIN64
  100. else {
  101. quad = 1;
  102. }
  103. #endif
  104. break;
  105. #endif
  106. default:
  107. break;
  108. }
  109. if (!*f) RETURN(pf,f,FMTCHECK_UNKNOWN);
  110. if (strchr("diouxX", *f)) {
  111. if (longdouble)
  112. RETURN(pf,f,FMTCHECK_UNKNOWN);
  113. if (lg)
  114. RETURN(pf,f,FMTCHECK_LONG);
  115. if (quad)
  116. RETURN(pf,f,FMTCHECK_QUAD);
  117. RETURN(pf,f,FMTCHECK_INT);
  118. }
  119. if (*f == 'n') {
  120. if (longdouble)
  121. RETURN(pf,f,FMTCHECK_UNKNOWN);
  122. if (sh)
  123. RETURN(pf,f,FMTCHECK_SHORTPOINTER);
  124. if (lg)
  125. RETURN(pf,f,FMTCHECK_LONGPOINTER);
  126. if (quad)
  127. RETURN(pf,f,FMTCHECK_QUADPOINTER);
  128. RETURN(pf,f,FMTCHECK_INTPOINTER);
  129. }
  130. if (strchr("DOU", *f)) {
  131. if (sh + lg + quad + longdouble)
  132. RETURN(pf,f,FMTCHECK_UNKNOWN);
  133. RETURN(pf,f,FMTCHECK_LONG);
  134. }
  135. if (strchr("eEfg", *f)) {
  136. if (longdouble)
  137. RETURN(pf,f,FMTCHECK_LONGDOUBLE);
  138. if (sh + lg + quad)
  139. RETURN(pf,f,FMTCHECK_UNKNOWN);
  140. RETURN(pf,f,FMTCHECK_DOUBLE);
  141. }
  142. if (*f == 'c') {
  143. if (sh + lg + quad + longdouble)
  144. RETURN(pf,f,FMTCHECK_UNKNOWN);
  145. RETURN(pf,f,FMTCHECK_INT);
  146. }
  147. if (*f == 's') {
  148. if (sh + lg + quad + longdouble)
  149. RETURN(pf,f,FMTCHECK_UNKNOWN);
  150. RETURN(pf,f,FMTCHECK_STRING);
  151. }
  152. if (*f == 'p') {
  153. if (sh + lg + quad + longdouble)
  154. RETURN(pf,f,FMTCHECK_UNKNOWN);
  155. RETURN(pf,f,FMTCHECK_LONG);
  156. }
  157. RETURN(pf,f,FMTCHECK_UNKNOWN);
  158. /*NOTREACHED*/
  159. }
  160. static EFT
  161. get_next_format_from_width(const char **pf)
  162. {
  163. const char *f;
  164. f = *pf;
  165. if (*f == '.') {
  166. f++;
  167. if (*f == '*') {
  168. RETURN(pf,f,FMTCHECK_PRECISION);
  169. }
  170. /* eat any precision (empty is allowed) */
  171. while (isdigit((unsigned char)*f)) f++;
  172. if (!*f) RETURN(pf,f,FMTCHECK_UNKNOWN);
  173. }
  174. RETURN(pf,f,get_next_format_from_precision(pf));
  175. /*NOTREACHED*/
  176. }
  177. static EFT
  178. get_next_format(const char **pf, EFT eft)
  179. {
  180. int infmt;
  181. const char *f;
  182. if (eft == FMTCHECK_WIDTH) {
  183. (*pf)++;
  184. return get_next_format_from_width(pf);
  185. } else if (eft == FMTCHECK_PRECISION) {
  186. (*pf)++;
  187. return get_next_format_from_precision(pf);
  188. }
  189. f = *pf;
  190. infmt = 0;
  191. while (!infmt) {
  192. f = strchr(f, '%');
  193. if (f == NULL)
  194. RETURN(pf,f,FMTCHECK_DONE);
  195. f++;
  196. if (!*f)
  197. RETURN(pf,f,FMTCHECK_UNKNOWN);
  198. if (*f != '%')
  199. infmt = 1;
  200. else
  201. f++;
  202. }
  203. /* Eat any of the flags */
  204. while (*f && (strchr("#0- +", *f)))
  205. f++;
  206. if (*f == '*') {
  207. RETURN(pf,f,FMTCHECK_WIDTH);
  208. }
  209. /* eat any width */
  210. while (isdigit((unsigned char)*f)) f++;
  211. if (!*f) {
  212. RETURN(pf,f,FMTCHECK_UNKNOWN);
  213. }
  214. RETURN(pf,f,get_next_format_from_width(pf));
  215. /*NOTREACHED*/
  216. }
  217. const char *
  218. fmtcheck(const char *f1, const char *f2)
  219. {
  220. const char *f1p, *f2p;
  221. EFT f1t, f2t;
  222. if (!f1) return f2;
  223. f1p = f1;
  224. f1t = FMTCHECK_START;
  225. f2p = f2;
  226. f2t = FMTCHECK_START;
  227. while ((f1t = get_next_format(&f1p, f1t)) != FMTCHECK_DONE) {
  228. if (f1t == FMTCHECK_UNKNOWN)
  229. return f2;
  230. f2t = get_next_format(&f2p, f2t);
  231. if (f1t != f2t)
  232. return f2;
  233. }
  234. return f1;
  235. }