1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2024-12-26 11:30:03 +05:30
gists/c-programming/jokes/extol.c

104 lines
1.9 KiB
C

/*
* extol.c [joke!]
*
* Author: Intel A80486DX2-66
* License: Creative Commons Zero 1.0 Universal
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
enum
{
EXTOL_NORMAL,
EXTOL_QUIET
} EXTOL_MODE;
#define EXTOL_PROTOTYPE "int extol(FILE* f)"
#define EXTOL_FILE_NAME "extol.txt"
#define EXTOL_FILE_LIMIT 128
int extol (FILE *f);
int extol_interface (signed char type);
void extol_quiet (void);
int
main (void)
{
int i;
for (i = 0; i < 2; i++)
printf (EXTOL_PROTOTYPE ": %d\n", extol_interface (EXTOL_NORMAL));
atexit (&extol_quiet);
return 0;
}
int
extol (FILE *f)
{
static bool spec_run = true;
int result;
off_t size;
if (spec_run == false)
{
printf ("\n");
return !(spec_run = true) - spec_run;
}
if (f == NULL)
{
FILE *fs = fopen (EXTOL_FILE_NAME, "a+");
if (fs == NULL)
{
perror ("fopen");
exit (EXIT_FAILURE);
}
fseek (fs, 0L, SEEK_END);
size = ftello (fs);
if (size == -1)
{
perror ("ftello");
exit (EXIT_FAILURE);
}
else if (size > EXTOL_FILE_LIMIT)
{
FILE *new_fs = freopen (EXTOL_FILE_NAME, "w", fs);
if (new_fs == NULL)
{
perror ("freopen");
exit (EXIT_FAILURE);
}
fs = new_fs;
}
result = extol (fs);
for (result <<= 4, result &= ~15;
(result & 15) != 15 || (result >>= 4, 0); result++)
fputc ('=', fs);
fprintf (fs, "\n");
fclose (fs);
}
else
{
result = fprintf (f, "PRAISE THE USER!\n");
}
spec_run = false;
return result;
}
int
extol_interface (signed char type)
{
FILE *f;
f = type == EXTOL_NORMAL ? stdout : NULL;
return printf (EXTOL_PROTOTYPE " returns %d\n", extol (f));
}
void
extol_quiet (void)
{
extol_interface (EXTOL_QUIET);
}