1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2024-09-19 14:55:34 +05:30

C: add extol.modern.c

This commit is contained in:
Intel A80486DX2-66 2024-03-09 16:47:42 +03:00
parent bb3f589cbe
commit 4ea244ca91
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B
2 changed files with 99 additions and 1 deletions

View File

@ -53,4 +53,4 @@
# Custom
**/*.tmp
!**/*.mod.c
!**/*.mod*.c

View File

@ -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 <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
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;
}