1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2024-11-08 18:02:23 +05:30

freadln.c: test: compensate lack of %zu fmt. specifier

This commit is contained in:
Intel A80486DX2-66 2024-04-07 18:49:11 +03:00
parent cf291335b7
commit da96289764
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B

View File

@ -4,6 +4,8 @@
* The `freadln` function reads a line from STDIN into a string, allocating
* memory for it.
*
* NOTE: Test: Declare macro NO_SIZE_T_FORMAT when compiling if your compiler
* or glibc do not support `%zu` format specifier.
* NOTE: Test: Declare macro POSIX when compiling if you're using a POSIX
* system.
*
@ -79,6 +81,9 @@ int freadln(FILE* f, char** output, size_t* length_out) {
}
#ifdef TEST
# include <inttypes.h>
# include <stdint.h>
# if POSIX
# include <unistd.h>
# define SLEEP_FN sleep
@ -102,6 +107,21 @@ int freadln(FILE* f, char** output, size_t* length_out) {
# include <time.h>
# endif
# ifndef NO_SIZE_T_FORMAT
# if defined(__TINYC__) || (defined(__STDC_VERSION__) && \
__STDC_VERSION__ < 199901L)
# define NO_SIZE_T_FORMAT
# endif
# endif
# if defined(NO_SIZE_T_FORMAT)
# define PRIuSIZE PRIuMAX
typedef uintmax_t SIZE_T_FORMAT;
# else
# define PRIuSIZE "zu"
typedef size_t SIZE_T_FORMAT;
# endif
int main(void) {
// stdin test
printf("Type something> ");
@ -146,8 +166,8 @@ int main(void) {
"feof? %d)\n", result == freadln_EOF, !!feof(f));
break;
}
printf("File, line #%d: '%s' (%zu characters)\n", i + 1, line,
line_length);
printf("File, line #%d: '%s' (%" PRIuSIZE " characters)\n", i + 1, line,
(SIZE_T_FORMAT) line_length);
SLEEP_FN(1000 - ((long double) (clock() - start) * 1000.l) /
CLOCKS_PER_SEC);