2013-12-06 12:04:52 +01:00
|
|
|
/* $NetBSD: scheck.c,v 1.11 2013/07/17 23:09:26 christos Exp $ */
|
2011-02-14 20:36:03 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
** This file is in the public domain, so clarified as of
|
|
|
|
** 2006-07-17 by Arthur David Olson.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if HAVE_NBTOOL_CONFIG_H
|
|
|
|
#include "nbtool_config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
#if 0
|
|
|
|
static char elsieid[] = "@(#)scheck.c 8.19";
|
|
|
|
#else
|
2013-12-06 12:04:52 +01:00
|
|
|
__RCSID("$NetBSD: scheck.c,v 1.11 2013/07/17 23:09:26 christos Exp $");
|
2011-02-14 20:36:03 +01:00
|
|
|
#endif
|
|
|
|
#endif /* !defined lint */
|
|
|
|
|
|
|
|
/*LINTLIBRARY*/
|
|
|
|
|
|
|
|
#include "private.h"
|
|
|
|
|
|
|
|
const char *
|
2013-12-06 12:04:52 +01:00
|
|
|
scheck(const char *const string, const char *const format)
|
2011-02-14 20:36:03 +01:00
|
|
|
{
|
2013-12-06 12:04:52 +01:00
|
|
|
char * fbuf;
|
|
|
|
const char * fp;
|
|
|
|
char * tp;
|
|
|
|
int c;
|
|
|
|
const char * result;
|
|
|
|
char dummy;
|
2011-02-14 20:36:03 +01:00
|
|
|
|
|
|
|
result = "";
|
|
|
|
if (string == NULL || format == NULL)
|
|
|
|
return result;
|
2013-12-06 12:04:52 +01:00
|
|
|
fbuf = malloc(2 * strlen(format) + 4);
|
2011-02-14 20:36:03 +01:00
|
|
|
if (fbuf == NULL)
|
|
|
|
return result;
|
|
|
|
fp = format;
|
|
|
|
tp = fbuf;
|
2013-12-06 12:04:52 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Copy directives, suppressing each conversion that is not
|
|
|
|
** already suppressed. Scansets containing '%' are not
|
|
|
|
** supported; e.g., the conversion specification "%[%]" is not
|
|
|
|
** supported. Also, multibyte characters containing a
|
|
|
|
** non-leading '%' byte are not supported.
|
|
|
|
*/
|
2011-02-14 20:36:03 +01:00
|
|
|
while ((*tp++ = c = *fp++) != '\0') {
|
|
|
|
if (c != '%')
|
|
|
|
continue;
|
2013-12-06 12:04:52 +01:00
|
|
|
if (is_digit(*fp)) {
|
|
|
|
char const *f = fp;
|
|
|
|
char *t = tp;
|
|
|
|
do {
|
|
|
|
*t++ = c = *f++;
|
|
|
|
} while (is_digit(c));
|
|
|
|
if (c == '$') {
|
|
|
|
fp = f;
|
|
|
|
tp = t;
|
|
|
|
}
|
2011-02-14 20:36:03 +01:00
|
|
|
}
|
|
|
|
*tp++ = '*';
|
|
|
|
if (*fp == '*')
|
|
|
|
++fp;
|
|
|
|
if ((*tp++ = *fp++) == '\0')
|
|
|
|
break;
|
|
|
|
}
|
2013-12-06 12:04:52 +01:00
|
|
|
|
2011-02-14 20:36:03 +01:00
|
|
|
*(tp - 1) = '%';
|
|
|
|
*tp++ = 'c';
|
|
|
|
*tp = '\0';
|
|
|
|
if (sscanf(string, fbuf, &dummy) != 1)
|
2013-12-06 12:04:52 +01:00
|
|
|
result = format;
|
|
|
|
free(fbuf);
|
2011-02-14 20:36:03 +01:00
|
|
|
return result;
|
|
|
|
}
|