From 06c5330a672970974fc77c9f7c1c8bfbb17cff5c Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Sun, 10 Mar 2024 15:40:51 +0300 Subject: [PATCH] freadln.*: add testing against unexpected STDOUT flushing --- c-programming/io/freadln.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/c-programming/io/freadln.c b/c-programming/io/freadln.c index 79e98cb..a4460ce 100644 --- a/c-programming/io/freadln.c +++ b/c-programming/io/freadln.c @@ -77,6 +77,24 @@ int freadln(FILE* f, char** output, size_t* length_out) { } #ifdef TEST +# if defined(_WIN32) || defined(WIN32) +# include +# define SLEEP_FN Sleep +# define DO_SLEEP 1 +# elif defined(__CYGWIN__) || defined(__unix__) || defined(__APPLE__) && \ + defined(__MACH__) || defined(__linux__) || defined(__FreeBSD__) || \ + defined(__NetBSD__) || defined(__OpenBSD__) || defined(__bsdi__) || \ + defined(__DragonFly__) || defined(__MINGW32__) || defined(__MINGW64__) +# include +# define SLEEP_FN usleep +# define DO_SLEEP 1 +# else +# define DO_SLEEP 0 +# endif +# if DO_SLEEP +# include +# endif + int main(void) { // stdin test printf("Type something> "); @@ -101,7 +119,16 @@ int main(void) { exit(EXIT_FAILURE); } f = new_f; + + printf("Waiting for 4 seconds...\n"); + fflush(stdout); + + clock_t start; for (int i = 0; i < 4; i++) { +#if DO_SLEEP + start = clock(); +#endif + size_t line_length; int result = freadln(f, &line, &line_length); if (result == freadln_ERROR) { @@ -114,8 +141,12 @@ int main(void) { } printf("File, line #%d: '%s' (%zu characters)\n", i + 1, line, line_length); + + SLEEP_FN(1000 - ((long double) (clock() - start) * 1000.l) / + CLOCKS_PER_SEC); } fclose(f); + fflush(stdout); return 0; }