make wide scanf %[ respect width
[musl] / src / stdio / vfwscanf.c
index 4426a12..beb8e8f 100644 (file)
@@ -83,7 +83,7 @@ static int in_set(const wchar_t *set, int c)
 
 #undef ungetwc
 #define ungetwc(c,f) \
 
 #undef ungetwc
 #define ungetwc(c,f) \
-       ((f)->rend && (c)<128 ? *--(f)->rpos : ungetwc((c),(f)))
+       ((f)->rend && (c)<128U ? *--(f)->rpos : ungetwc((c),(f)))
 #endif
 
 int vfwscanf(FILE *f, const wchar_t *fmt, va_list ap)
 #endif
 
 int vfwscanf(FILE *f, const wchar_t *fmt, va_list ap)
@@ -215,19 +215,22 @@ int vfwscanf(FILE *f, const wchar_t *fmt, va_list ap)
 
                case 's':
                        s = dest;
 
                case 's':
                        s = dest;
-                       while (!iswspace(c=getwc(f)) && c!=EOF) {
+                       while (width && !iswspace(c=getwc(f)) && c!=EOF) {
                                int l = wctomb(s?s:tmp, c);
                                if (l<0) goto input_fail;
                                if (s) s+=l;
                                pos++;
                                int l = wctomb(s?s:tmp, c);
                                if (l<0) goto input_fail;
                                if (s) s+=l;
                                pos++;
+                               width--;
                        }
                        }
+                       if (width) ungetwc(c, f);
                        if (s) *s = 0;
                        break;
 
                case 'S':
                        wcs = dest;
                        if (s) *s = 0;
                        break;
 
                case 'S':
                        wcs = dest;
-                       while (!iswspace(c=getwc(f)) && c!=EOF)
-                               pos++, *wcs++ = c;
+                       while (width && !iswspace(c=getwc(f)) && c!=EOF)
+                               width--, pos++, *wcs++ = c;
+                       if (width) ungetwc(c, f);
                        if (wcs) *wcs = 0;
                        break;
 
                        if (wcs) *wcs = 0;
                        break;
 
@@ -240,7 +243,7 @@ int vfwscanf(FILE *f, const wchar_t *fmt, va_list ap)
 
                        int gotmatch = 0;
 
 
                        int gotmatch = 0;
 
-                       for (;;) {
+                       while (width) {
                                if ((c=getwc(f))<0) break;
                                if (in_set(p, c) == invert)
                                        break;
                                if ((c=getwc(f))<0) break;
                                if (in_set(p, c) == invert)
                                        break;
@@ -252,9 +255,10 @@ int vfwscanf(FILE *f, const wchar_t *fmt, va_list ap)
                                        if (s) s+=l;
                                }
                                pos++;
                                        if (s) s+=l;
                                }
                                pos++;
+                               width--;
                                gotmatch=1;
                        }
                                gotmatch=1;
                        }
-                       ungetwc(c, f);
+                       if (width) ungetwc(c, f);
 
                        if (!gotmatch) goto match_fail;
 
 
                        if (!gotmatch) goto match_fail;