/* Author: Ruud Harmsen, August 2021. Explanation: https://rudhar.com/sfreview/siworin/siworin09.htm Meant to illustrate this bug: https://sourceware.org/bugzilla/show_bug.cgi?id=28277 */ #include #include #include #include #include #include void timeit (FILE *fp); int main (void) { FILE *fp; int c; wint_t wc; char *testfile = "../siworin09.htm"; const int numblks = 4; fprintf(stdout, "Default block is %d\n", (int)BUFSIZ); setlocale(LC_ALL, ""); if (!(fp = fopen(testfile, "r"))) {perror(NULL); exit(1);} if (setvbuf(fp, NULL, _IOFBF, 1024 * numblks)) perror(NULL); fprintf(stdout, "File read as character stream\n"); while (c = getc(fp), c != EOF) { fprintf(stdout, "c=%c, pos=%4ld ", isprint(c) ? c : '?', ftell(fp)); timeit(fp); } fclose(fp); if (!(fp = fopen(testfile, "r"))) {perror(NULL); exit(2);} if (setvbuf(fp, NULL, _IOFBF, 1024 * numblks)) perror(NULL); fprintf(stdout, "\nFile read as wide character stream\n"); while (wc = getwc(fp), wc != WEOF) { fprintf(stdout, "c=%lc, pos=%4ld ", iswprint(wc) ? wc : L'?', ftell(fp)); timeit(fp); } fclose(fp); return 0; } #include #define HOW_OFTEN 1000 void timeit (FILE *fp) { struct timeval befor, after; int i; double duration; /* in nanoseconds */ gettimeofday(&befor, NULL); for (i = 0; i < HOW_OFTEN; i++) { ftell(fp); } gettimeofday(&after, NULL); duration = 1000000.0 * 1000.0 * (after.tv_sec - befor.tv_sec); duration += (after.tv_usec - befor.tv_usec) * 1000.0; fprintf(stdout, "dura=%5.0f ns\n", duration / HOW_OFTEN); }