xref: /onnv-gate/usr/src/cmd/zic/scheck.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
2*0Sstevel@tonic-gate 
3*0Sstevel@tonic-gate /* static char	elsieid[] = "@(#)scheck.c	8.15"; */
4*0Sstevel@tonic-gate 
5*0Sstevel@tonic-gate /*LINTLIBRARY*/
6*0Sstevel@tonic-gate 
7*0Sstevel@tonic-gate #include "private.h"
8*0Sstevel@tonic-gate 
9*0Sstevel@tonic-gate char *
10*0Sstevel@tonic-gate scheck(string, format)
11*0Sstevel@tonic-gate const char * const	string;
12*0Sstevel@tonic-gate const char * const	format;
13*0Sstevel@tonic-gate {
14*0Sstevel@tonic-gate 	register char *		fbuf;
15*0Sstevel@tonic-gate 	register const char *	fp;
16*0Sstevel@tonic-gate 	register char *		tp;
17*0Sstevel@tonic-gate 	register int		c;
18*0Sstevel@tonic-gate 	register char *		result;
19*0Sstevel@tonic-gate 	char			dummy;
20*0Sstevel@tonic-gate 	static char		nada;
21*0Sstevel@tonic-gate 
22*0Sstevel@tonic-gate 	result = &nada;
23*0Sstevel@tonic-gate 	if (string == NULL || format == NULL)
24*0Sstevel@tonic-gate 		return (result);
25*0Sstevel@tonic-gate 	fbuf = imalloc((int) (2 * strlen(format) + 4));
26*0Sstevel@tonic-gate 	if (fbuf == NULL)
27*0Sstevel@tonic-gate 		return (result);
28*0Sstevel@tonic-gate 	fp = format;
29*0Sstevel@tonic-gate 	tp = fbuf;
30*0Sstevel@tonic-gate 	while ((*tp++ = c = *fp++) != '\0') {
31*0Sstevel@tonic-gate 		if (c != '%')
32*0Sstevel@tonic-gate 			continue;
33*0Sstevel@tonic-gate 		if (*fp == '%') {
34*0Sstevel@tonic-gate 			*tp++ = *fp++;
35*0Sstevel@tonic-gate 			continue;
36*0Sstevel@tonic-gate 		}
37*0Sstevel@tonic-gate 		*tp++ = '*';
38*0Sstevel@tonic-gate 		if (*fp == '*')
39*0Sstevel@tonic-gate 			++fp;
40*0Sstevel@tonic-gate 		while (is_digit(*fp))
41*0Sstevel@tonic-gate 			*tp++ = *fp++;
42*0Sstevel@tonic-gate 		if (*fp == 'l' || *fp == 'h')
43*0Sstevel@tonic-gate 			*tp++ = *fp++;
44*0Sstevel@tonic-gate 		else if (*fp == '[')
45*0Sstevel@tonic-gate 			do *tp++ = *fp++;
46*0Sstevel@tonic-gate 				while (*fp != '\0' && *fp != ']');
47*0Sstevel@tonic-gate 		if ((*tp++ = *fp++) == '\0')
48*0Sstevel@tonic-gate 			break;
49*0Sstevel@tonic-gate 	}
50*0Sstevel@tonic-gate 	*(tp - 1) = '%';
51*0Sstevel@tonic-gate 	*tp++ = 'c';
52*0Sstevel@tonic-gate 	*tp = '\0';
53*0Sstevel@tonic-gate 	if (sscanf(string, fbuf, &dummy) != 1)
54*0Sstevel@tonic-gate 		result = (char *) format;
55*0Sstevel@tonic-gate 	ifree(fbuf);
56*0Sstevel@tonic-gate 	return (result);
57*0Sstevel@tonic-gate }
58