1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2025-01-13 00:32:08 +05:30

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

This commit is contained in:
パチュリー・ノーレッジ 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 * The `freadln` function reads a line from STDIN into a string, allocating
* memory for it. * 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 * NOTE: Test: Declare macro POSIX when compiling if you're using a POSIX
* system. * system.
* *
@ -79,6 +81,9 @@ int freadln(FILE* f, char** output, size_t* length_out) {
} }
#ifdef TEST #ifdef TEST
# include <inttypes.h>
# include <stdint.h>
# if POSIX # if POSIX
# include <unistd.h> # include <unistd.h>
# define SLEEP_FN sleep # define SLEEP_FN sleep
@ -102,6 +107,21 @@ int freadln(FILE* f, char** output, size_t* length_out) {
# include <time.h> # include <time.h>
# endif # 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) { int main(void) {
// stdin test // stdin test
printf("Type something> "); printf("Type something> ");
@ -146,8 +166,8 @@ int main(void) {
"feof? %d)\n", result == freadln_EOF, !!feof(f)); "feof? %d)\n", result == freadln_EOF, !!feof(f));
break; break;
} }
printf("File, line #%d: '%s' (%zu characters)\n", i + 1, line, printf("File, line #%d: '%s' (%" PRIuSIZE " characters)\n", i + 1, line,
line_length); (SIZE_T_FORMAT) line_length);
SLEEP_FN(1000 - ((long double) (clock() - start) * 1000.l) / SLEEP_FN(1000 - ((long double) (clock() - start) * 1000.l) /
CLOCKS_PER_SEC); CLOCKS_PER_SEC);