| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** This file is in the public domain, so clarified as of | 2 ** This file is in the public domain, so clarified as of |
| 3 ** 2006-07-17 by Arthur David Olson. | 3 ** 2006-07-17 by Arthur David Olson. |
| 4 */ | 4 */ |
| 5 | 5 |
| 6 #ifndef lint | |
| 7 #ifndef NOID | |
| 8 static char elsieid[] = "@(#)scheck.c 8.19"; | |
| 9 #endif /* !defined lint */ | |
| 10 #endif /* !defined NOID */ | |
| 11 | |
| 12 /*LINTLIBRARY*/ | 6 /*LINTLIBRARY*/ |
| 13 | 7 |
| 14 #include "private.h" | 8 #include "private.h" |
| 15 | 9 |
| 16 const char * | 10 const char * |
| 17 scheck(string, format) | 11 scheck(const char *const string, const char *const format) |
| 18 const char * const» string; | |
| 19 const char * const» format; | |
| 20 { | 12 { |
| 21 register char * fbuf; | 13 register char * fbuf; |
| 22 register const char * fp; | 14 register const char * fp; |
| 23 register char * tp; | 15 register char * tp; |
| 24 register int c; | 16 register int c; |
| 25 register const char * result; | 17 register const char * result; |
| 26 char dummy; | 18 char dummy; |
| 27 | 19 |
| 28 result = ""; | 20 result = ""; |
| 29 if (string == NULL || format == NULL) | 21 if (string == NULL || format == NULL) |
| 30 return result; | 22 return result; |
| 31 » fbuf = imalloc((int) (2 * strlen(format) + 4)); | 23 » fbuf = malloc(2 * strlen(format) + 4); |
| 32 if (fbuf == NULL) | 24 if (fbuf == NULL) |
| 33 return result; | 25 return result; |
| 34 fp = format; | 26 fp = format; |
| 35 tp = fbuf; | 27 tp = fbuf; |
| 28 |
| 29 /* |
| 30 ** Copy directives, suppressing each conversion that is not |
| 31 ** already suppressed. Scansets containing '%' are not |
| 32 ** supported; e.g., the conversion specification "%[%]" is not |
| 33 ** supported. Also, multibyte characters containing a |
| 34 ** non-leading '%' byte are not supported. |
| 35 */ |
| 36 while ((*tp++ = c = *fp++) != '\0') { | 36 while ((*tp++ = c = *fp++) != '\0') { |
| 37 if (c != '%') | 37 if (c != '%') |
| 38 continue; | 38 continue; |
| 39 » » if (*fp == '%') { | 39 » » if (is_digit(*fp)) { |
| 40 » » » *tp++ = *fp++; | 40 » » » char const *f = fp; |
| 41 » » » continue; | 41 » » » char *t = tp; |
| 42 » » » do { |
| 43 » » » » *t++ = c = *f++; |
| 44 » » » } while (is_digit(c)); |
| 45 » » » if (c == '$') { |
| 46 » » » » fp = f; |
| 47 » » » » tp = t; |
| 48 » » » } |
| 42 } | 49 } |
| 43 *tp++ = '*'; | 50 *tp++ = '*'; |
| 44 if (*fp == '*') | 51 if (*fp == '*') |
| 45 ++fp; | 52 ++fp; |
| 46 while (is_digit(*fp)) | |
| 47 *tp++ = *fp++; | |
| 48 if (*fp == 'l' || *fp == 'h') | |
| 49 *tp++ = *fp++; | |
| 50 else if (*fp == '[') | |
| 51 do *tp++ = *fp++; | |
| 52 while (*fp != '\0' && *fp != ']'); | |
| 53 if ((*tp++ = *fp++) == '\0') | 53 if ((*tp++ = *fp++) == '\0') |
| 54 break; | 54 break; |
| 55 } | 55 } |
| 56 |
| 56 *(tp - 1) = '%'; | 57 *(tp - 1) = '%'; |
| 57 *tp++ = 'c'; | 58 *tp++ = 'c'; |
| 58 *tp = '\0'; | 59 *tp = '\0'; |
| 59 if (sscanf(string, fbuf, &dummy) != 1) | 60 if (sscanf(string, fbuf, &dummy) != 1) |
| 60 » » result = (char *) format; | 61 » » result = format; |
| 61 » ifree(fbuf); | 62 » free(fbuf); |
| 62 return result; | 63 return result; |
| 63 } | 64 } |
| OLD | NEW |