int parse_until(FILE *src, char *ptr, ssize_t maxlen, const char *s) {
int out = 0;
while (out < maxlen) {
*ptr = fgetc(src);
if (*ptr == EOF) { // hit error while scanning
*ptr = 0;
return ferror(src) ? -out : out;
} else if (strchr(s, *ptr)) {
*ptr = 0;
return out;
}
ptr++; out++;
}
// we only make it here if we hit maxlen
(*--ptr) = 0;
int skipped = parse_skipwhile(src, s);
if (skipped > 0) {
return out + skipped; // errors are negative, eof is ok
}
return ferror(src) ? (skipped - out) : (out - skipped);
}
Hint: it relies on implementation-defined behavior such that it (mostly) works on x86 but not on ARM.(The fact that it silently clips the string if it's overlong is annoying, but it's not what I was thinking).
So EOF is defined as -1. Depending on how the environment chooses to define char, one of two things can happen. On systems like x86 where char is signed, it's impossible to read a byte value of 0xff, as it is confused with EOF. If instead you're on a system like ARM where char is unsigned, then you can never read EOF.