[BZ #4276]
[platform/upstream/glibc.git] / timezone / scheck.c
1 /*
2 ** This file is in the public domain, so clarified as of
3 ** 2006-07-17 by Arthur David Olson.
4 */
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*/
13
14 #include "private.h"
15
16 const char *
17 scheck(string, format)
18 const char * const      string;
19 const char * const      format;
20 {
21         register char *         fbuf;
22         register const char *   fp;
23         register char *         tp;
24         register int            c;
25         register const char *   result;
26         char                    dummy;
27
28         result = "";
29         if (string == NULL || format == NULL)
30                 return result;
31         fbuf = imalloc((int) (2 * strlen(format) + 4));
32         if (fbuf == NULL)
33                 return result;
34         fp = format;
35         tp = fbuf;
36         while ((*tp++ = c = *fp++) != '\0') {
37                 if (c != '%')
38                         continue;
39                 if (*fp == '%') {
40                         *tp++ = *fp++;
41                         continue;
42                 }
43                 *tp++ = '*';
44                 if (*fp == '*')
45                         ++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')
54                         break;
55         }
56         *(tp - 1) = '%';
57         *tp++ = 'c';
58         *tp = '\0';
59         if (sscanf(string, fbuf, &dummy) != 1)
60                 result = (char *) format;
61         ifree(fbuf);
62         return result;
63 }