X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Finternal%2Fshgetc.h;h=9435381a61d1ee45c373c87bf7bcb5b43b370676;hb=593caa456309714402ca4cb77c3770f4c24da9da;hp=7beb8ce62182b64bb6f7349640b6f2d833b113c3;hpb=f007bb854b0b2d2d12cd45a8feb674fa9abe70b2;p=musl diff --git a/src/internal/shgetc.h b/src/internal/shgetc.h index 7beb8ce6..9435381a 100644 --- a/src/internal/shgetc.h +++ b/src/internal/shgetc.h @@ -1,9 +1,32 @@ #include "stdio_impl.h" -void __shlim(FILE *, off_t); -int __shgetc(FILE *); +/* Scan helper "stdio" functions for use by scanf-family and strto*-family + * functions. These accept either a valid stdio FILE, or a minimal pseudo + * FILE whose buffer pointers point into a null-terminated string. In the + * latter case, the sh_fromstring macro should be used to setup the FILE; + * the rest of the structure can be left uninitialized. + * + * To begin using these functions, shlim must first be called on the FILE + * to set a field width limit, or 0 for no limit. For string pseudo-FILEs, + * a nonzero limit is not valid and produces undefined behavior. After that, + * shgetc, shunget, and shcnt are valid as long as no other stdio functions + * are called on the stream. + * + * When used with a real FILE object, shunget has only one byte of pushback + * available. Further shunget (up to a limit of the stdio UNGET buffer size) + * will adjust the position but will not restore the data to be read again. + * This functionality is needed for the wcsto*-family functions, where it's + * okay because the FILE will be discarded immediately anyway. When used + * with string pseudo-FILEs, shunget has unlimited pushback, back to the + * beginning of the string. */ -#define shcnt(f) ((f)->shcnt + ((f)->rpos - (f)->rend)) +hidden void __shlim(FILE *, off_t); +hidden int __shgetc(FILE *); + +#define shcnt(f) ((f)->shcnt + ((f)->rpos - (f)->buf)) #define shlim(f, lim) __shlim((f), (lim)) -#define shgetc(f) (((f)->rpos < (f)->shend) ? *(f)->rpos++ : __shgetc(f)) -#define shunget(f) ((f)->shend ? (void)(f)->rpos-- : (void)0) +#define shgetc(f) (((f)->rpos != (f)->shend) ? *(f)->rpos++ : __shgetc(f)) +#define shunget(f) ((f)->shlim>=0 ? (void)(f)->rpos-- : (void)0) + +#define sh_fromstring(f, s) \ + ((f)->buf = (f)->rpos = (void *)(s), (f)->rend = (void*)-1)