2018-10-10 03:39:54 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
extern char * _argv_0;
|
2018-10-12 09:03:22 +03:00
|
|
|
extern int __libc_debug;
|
2018-10-10 03:39:54 +03:00
|
|
|
|
|
|
|
int vsscanf(const char *str, const char *format, va_list ap) {
|
2018-10-12 09:03:22 +03:00
|
|
|
if (__libc_debug) fprintf(stderr, "%s: sscanf(\"%s\", format=\"%s\", ...);\n", _argv_0, str, format);
|
2018-10-12 07:51:26 +03:00
|
|
|
int count = 0;
|
2018-10-10 03:39:54 +03:00
|
|
|
while (*format) {
|
|
|
|
if (*format == ' ') {
|
|
|
|
/* handle white space */
|
|
|
|
while (*str && isspace(*str)) {
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
} else if (*format == '%') {
|
|
|
|
/* Parse */
|
2018-10-12 07:51:26 +03:00
|
|
|
format++;
|
2018-10-12 09:03:22 +03:00
|
|
|
int _long = 0;
|
|
|
|
|
|
|
|
if (*format == 'l') {
|
|
|
|
format++;
|
|
|
|
if (*format == 'l') {
|
|
|
|
_long = 1;
|
|
|
|
if (__libc_debug) fprintf(stderr, "%s: \033[33;3mWarning\033[0m: Need to scan a large pointer (%d)\n", _argv_0, _long);
|
|
|
|
format++;
|
|
|
|
}
|
|
|
|
}
|
2018-10-12 07:51:26 +03:00
|
|
|
|
|
|
|
if (*format == 'd') {
|
|
|
|
int i = 0;
|
2018-10-12 09:03:22 +03:00
|
|
|
int sign = 1;
|
2019-06-29 14:36:28 +03:00
|
|
|
while (isspace(*str)) str++;
|
2018-10-12 09:03:22 +03:00
|
|
|
if (*str == '-') {
|
|
|
|
sign = -1;
|
|
|
|
str++;
|
|
|
|
}
|
2018-11-23 04:41:45 +03:00
|
|
|
while (*str && *str >= '0' && *str <= '9') {
|
2018-10-12 07:51:26 +03:00
|
|
|
i = i * 10 + *str - '0';
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
int * out = (int *)va_arg(ap, int*);
|
2018-10-12 09:03:22 +03:00
|
|
|
if (__libc_debug) fprintf(stderr, "%s: sscanf: out %d\n", _argv_0, i);
|
|
|
|
count++;
|
|
|
|
*out = i * sign;
|
|
|
|
} else if (*format == 'u') {
|
|
|
|
unsigned int i = 0;
|
2019-06-29 14:36:28 +03:00
|
|
|
while (isspace(*str)) str++;
|
2018-11-23 04:41:45 +03:00
|
|
|
while (*str && *str >= '0' && *str <= '9') {
|
2018-10-12 09:03:22 +03:00
|
|
|
i = i * 10 + *str - '0';
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
unsigned int * out = (unsigned int *)va_arg(ap, unsigned int*);
|
|
|
|
if (__libc_debug) fprintf(stderr, "%s: sscanf: out %d\n", _argv_0, i);
|
2018-10-12 07:51:26 +03:00
|
|
|
count++;
|
|
|
|
*out = i;
|
|
|
|
}
|
2018-10-10 03:39:54 +03:00
|
|
|
} else {
|
|
|
|
/* Expect exact character? */
|
2018-10-12 07:51:26 +03:00
|
|
|
if (*str == *format) {
|
|
|
|
str++;
|
2018-12-19 08:53:32 +03:00
|
|
|
} else {
|
|
|
|
break;
|
2018-10-12 07:51:26 +03:00
|
|
|
}
|
2018-10-10 03:39:54 +03:00
|
|
|
}
|
|
|
|
format++;
|
|
|
|
}
|
2018-10-12 07:51:26 +03:00
|
|
|
return count;
|
2018-10-10 03:39:54 +03:00
|
|
|
}
|
|
|
|
|
2018-10-10 05:30:25 +03:00
|
|
|
int vfscanf(FILE * stream, const char *format, va_list ap) {
|
2018-10-12 09:03:22 +03:00
|
|
|
if (__libc_debug) fprintf(stderr, "%s: fscanf(%d, format=%s, ...);\n", _argv_0, fileno(stream), format);
|
2018-10-10 05:30:25 +03:00
|
|
|
while (*format) {
|
|
|
|
if (*format == ' ') {
|
|
|
|
/* Handle whitespace */
|
|
|
|
} else if (*format == '%') {
|
|
|
|
/* Parse */
|
|
|
|
} else {
|
|
|
|
/* Expect exact character? */
|
|
|
|
}
|
|
|
|
format++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-10 03:39:54 +03:00
|
|
|
int sscanf(const char *str, const char *format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
int out = vsscanf(str, format, args);
|
|
|
|
va_end(args);
|
|
|
|
return out;
|
|
|
|
}
|
2018-10-10 05:30:25 +03:00
|
|
|
|
|
|
|
int fscanf(FILE *stream, const char *format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
int out = vfscanf(stream, format, args);
|
|
|
|
va_end(args);
|
|
|
|
return out;
|
|
|
|
}
|
2018-10-12 04:35:28 +03:00
|
|
|
|
|
|
|
int scanf(const char *format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
int out = vfscanf(stdin, format, args);
|
|
|
|
va_end(args);
|
|
|
|
return out;
|
|
|
|
}
|