/* Author: Ruud Harmsen, August 2021. Explanation: https://rudhar.com/sfreview/siworin/siworin15.htm Meant to illustrate this bug: https://sourceware.org/bugzilla/show_bug.cgi?id=28258 */ #include #include #include #include #define REPERR(LINE, ERRNO) { \ fprintf(stderr, "Line %3d, error %d ", (LINE), (ERRNO)); \ perror(NULL);} int main (int argc, char **argv) { static char bytes[] = u8"รก"; FILE *fp; int i; wint_t cw; int retval; if (!setlocale(LC_ALL, "en_US.UTF-8")) REPERR(__LINE__, errno); /* Write the test string to disk. */ fp = fopen("in", "w"); fwrite(bytes, 1, sizeof bytes - 1, fp); fclose(fp); /* Reopen for reading. */ fp = fopen("in", "r"); for (i = argc > 1 ? 0 : 1; i < sizeof bytes && bytes[i]; i++) { fprintf(stderr, "Line %3d, i = %d\n", __LINE__, i); if ((retval = fseek(fp, i, SEEK_SET)) < 0) REPERR(__LINE__, errno); fprintf(stderr, "Line %3d, i = %d\n", __LINE__, i); cw = fgetwc(fp); if (ferror(fp)) {REPERR(__LINE__, errno); clearerr(fp);} } fclose(fp); return 0; }