int sscanf(const char* str, const char* fmt, ...) {
/* Minimal sscanf: only supports %d and %s */
- /* U02: %s limited to 255 chars by default to prevent buffer overflow */
+ /* U02: %s limited to 255 chars by default, or explicit field width like %20s */
va_list ap;
va_start(ap, fmt);
int count = 0;
while (*f && *s) {
if (*f == '%') {
f++;
+ /* Parse optional field width */
+ int width = 255; /* Default limit */
+ if (*f >= '0' && *f <= '9') {
+ width = 0;
+ while (*f >= '0' && *f <= '9') {
+ width = width * 10 + (*f - '0');
+ f++;
+ }
+ }
if (*f == 'd' || *f == 'i') {
f++;
int* out = va_arg(ap, int*);
char* out = va_arg(ap, char*);
while (*s == ' ') s++;
int i = 0;
- while (*s && *s != ' ' && *s != '\n' && *s != '\t' && i < 255) out[i++] = *s++;
+ while (*s && *s != ' ' && *s != '\n' && *s != '\t' && i < width) out[i++] = *s++;
out[i] = '\0';
count++;
} else {
int fscanf(FILE* fp, const char* fmt, ...) {
/* Read a line, then delegate to sscanf */
- /* U02: %s limited to 255 chars by default to prevent buffer overflow */
+ /* U02: %s limited to 255 chars by default, or explicit field width like %20s */
char line[512];
if (!fgets(line, (int)sizeof(line), fp)) return EOF;
va_list ap;
while (*f && *s) {
if (*f == '%') {
f++;
+ /* Parse optional field width */
+ int width = 255; /* Default limit */
+ if (*f >= '0' && *f <= '9') {
+ width = 0;
+ while (*f >= '0' && *f <= '9') {
+ width = width * 10 + (*f - '0');
+ f++;
+ }
+ }
if (*f == 'd' || *f == 'i') {
f++;
int* out = va_arg(ap, int*);
char* out = va_arg(ap, char*);
while (*s == ' ') s++;
int i = 0;
- while (*s && *s != ' ' && *s != '\n' && *s != '\t' && i < 255) out[i++] = *s++;
+ while (*s && *s != ' ' && *s != '\n' && *s != '\t' && i < width) out[i++] = *s++;
out[i] = '\0';
count++;
} else if (*f == 'c') {
}
int scanf(const char* fmt, ...) {
- /* U02: %s limited to 255 chars by default to prevent buffer overflow */
+ /* U02: %s limited to 255 chars by default, or explicit field width like %20s */
char line[512];
if (!fgets(line, (int)sizeof(line), stdin)) return EOF;
va_list ap;
while (*f && *s) {
if (*f == '%') {
f++;
+ /* Parse optional field width */
+ int width = 255; /* Default limit */
+ if (*f >= '0' && *f <= '9') {
+ width = 0;
+ while (*f >= '0' && *f <= '9') {
+ width = width * 10 + (*f - '0');
+ f++;
+ }
+ }
if (*f == 'd' || *f == 'i') {
f++;
int* out = va_arg(ap, int*);
char* out = va_arg(ap, char*);
while (*s == ' ') s++;
int i = 0;
- while (*s && *s != ' ' && *s != '\n' && *s != '\t' && i < 255) out[i++] = *s++;
+ while (*s && *s != ' ' && *s != '\n' && *s != '\t' && i < width) out[i++] = *s++;
out[i] = '\0';
count++;
} else {