From 4ea244ca911bc0cafedf21503feced1560ed7d54 Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Sat, 9 Mar 2024 16:47:42 +0300 Subject: [PATCH] C: add extol.modern.c --- c-programming/.gitignore | 2 +- c-programming/jokes/extol.modern.c | 98 ++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 c-programming/jokes/extol.modern.c diff --git a/c-programming/.gitignore b/c-programming/.gitignore index 522dab0..1f652ee 100644 --- a/c-programming/.gitignore +++ b/c-programming/.gitignore @@ -53,4 +53,4 @@ # Custom **/*.tmp -!**/*.mod.c +!**/*.mod*.c diff --git a/c-programming/jokes/extol.modern.c b/c-programming/jokes/extol.modern.c new file mode 100644 index 0000000..e99041f --- /dev/null +++ b/c-programming/jokes/extol.modern.c @@ -0,0 +1,98 @@ +/* + * extol.modern.c [joke!] + * + * A modern C version of extol.c + * + * Author: Intel A80486DX2-66 + * License: Creative Commons Zero 1.0 Universal + */ + +#include +#include +#include +#include + +enum +{ + EXTOL_NORMAL, + EXTOL_QUIET +} EXTOL_MODE; + +#define EXTOL_PROTOTYPE "int extol(FILE* file)" +#define EXTOL_FILE_NAME "extol.txt" +#define EXTOL_FILE_LIMIT 128 + +int extol(FILE *file); +int extol_interface(int type); +void extol_quiet(void); + +int extol(FILE *file) +{ + static bool spec_run = true; + int result; + off_t size; + + if (spec_run == false) + { + printf("\n"); + return !(spec_run = true) - spec_run; + } + + if (file == NULL) + { + FILE *file_handler = fopen(EXTOL_FILE_NAME, "a"); + if (file_handler == NULL) + { + perror("fopen"); + exit(EXIT_FAILURE); + } + fseek(file_handler, 0L, SEEK_END); + size = ftello(file_handler); + if (size == -1) + { + perror("ftello"); + exit(EXIT_FAILURE); + } + else if (size > EXTOL_FILE_LIMIT) + { + FILE *new_file_handler = + freopen(EXTOL_FILE_NAME, "w", file_handler); + if (new_file_handler == NULL) + { + perror("freopen"); + exit(EXIT_FAILURE); + } + file_handler = new_file_handler; + } + result = extol(file_handler); + for (uint_fast8_t i = 0; i < 16; i++) + fputc('=', file_handler); + fprintf(file_handler, "\n"); + fclose(file_handler); + } + else + { + result = fprintf(file, "PRAISE THE USER!\n"); + } + spec_run = false; + return result; +} + +int extol_interface(int type) +{ + return printf(EXTOL_PROTOTYPE " returns %d\n", + extol(type == EXTOL_NORMAL ? stdout : NULL)); +} + +void extol_quiet(void) +{ + extol_interface(EXTOL_QUIET); +} + +int main(void) +{ + for (uint_fast8_t i = 0; i < 2; i++) + printf(EXTOL_PROTOTYPE ": %d\n", extol_interface(EXTOL_NORMAL)); + atexit(&extol_quiet); + return 0; +}