• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "stdio_impl.h"
2 #include "../include/features.h"
3 
4 /* Scan helper "stdio" functions for use by scanf-family and strto*-family
5  * functions. These accept either a valid stdio FILE, or a minimal pseudo
6  * FILE whose buffer pointers point into a null-terminated string. In the
7  * latter case, the sh_fromstring macro should be used to setup the FILE;
8  * the rest of the structure can be left uninitialized.
9  *
10  * To begin using these functions, shlim must first be called on the FILE
11  * to set a field width limit, or 0 for no limit. For string pseudo-FILEs,
12  * a nonzero limit is not valid and produces undefined behavior. After that,
13  * shgetc, shunget, and shcnt are valid as long as no other stdio functions
14  * are called on the stream.
15  *
16  * When used with a real FILE object, shunget has only one byte of pushback
17  * available. Further shunget (up to a limit of the stdio UNGET buffer size)
18  * will adjust the position but will not restore the data to be read again.
19  * This functionality is needed for the wcsto*-family functions, where it's
20  * okay because the FILE will be discarded immediately anyway. When used
21  * with string pseudo-FILEs, shunget has unlimited pushback, back to the
22  * beginning of the string. */
23 
24 hidden void __shlim(FILE *, off_t);
25 hidden int __shgetc(FILE *);
26 
27 #define shcnt(f) ((f)->shcnt + ((f)->rpos - (f)->buf))
28 #define shlim(f, lim) __shlim((f), (lim))
29 #define shgetc(f) (((f)->rpos != (f)->shend) ? *(f)->rpos++ : __shgetc(f))
30 #define shunget(f) ((f)->shlim>=0 ? (void)(f)->rpos-- : (void)0)
31 
32 #define sh_fromstring(f, s) \
33 	((f)->buf = (f)->rpos = (void *)(s), (f)->rend = (void*)-1)
34