#include <stdio.h>
#include <stdarg.h>
#include "lib.h"

typedef int (*gettype) __PROTO((void *));
typedef int (*ungettype) __PROTO((int, void *));

static int sgetc __PROTO((unsigned char **s));
static int sungetc __PROTO((int c, unsigned char **s));

static int sgetc(s)
	unsigned char **s;
{
	register unsigned char c;

	c = *(*s)++;
	return((c == '\0') ? EOF : c);
}

static int sungetc(c, s)
	int c;
	unsigned char **s;
{
	if(c == EOF)
		c = '\0';
	return(*--(*s) = c);
}

#ifdef __STDC__
int sscanf(const char *buf, const char *fmt, ...)
{
	int retval;
	va_list args;
	
	va_start (args, fmt);
	retval = _scanf((void *)&buf, (gettype) sgetc, (ungettype) sungetc,
	 (unsigned char *) fmt, args);
	va_end (args);
	return retval;
}
#else
int
sscanf(buf, fmt, arg)
	const char *buf, *fmt;
	int arg;
{
	return(_scanf(&buf, sgetc, sungetc, fmt, &arg));
}
#endif /* __STDC__ */

#ifdef __STDC__
int vsscanf(const char *buf, const char *fmt, va_list arg)
#else
int
vsscanf(buf, fmt, arg)
	const char *buf, *fmt;
	char *arg;
#endif /* __STDC__ */
	{
	return(_scanf((void *)&buf, (gettype) sgetc, (ungettype) sungetc,
	 (unsigned char *) fmt, arg));
	}
