From: Michael Orlitzky Date: Sun, 5 Jul 2026 20:45:10 +0000 (-0400) Subject: main.c: add macro for errno->string conversion X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=de03af88b008c760cdac558721f43d21f0572c71;p=iconv-quirks.git main.c: add macro for errno->string conversion --- diff --git a/main.c b/main.c index cb02297..e63e138 100644 --- a/main.c +++ b/main.c @@ -36,6 +36,17 @@ #define FAILURE 1 #define UNSUPPORTED 2 /* also success if the standard allows it */ +#define ERRNO_STR(e) \ + ((e) == E2BIG) ? "E2BIG" : \ + ((e) == EBADF) ? "EBADF" : \ + ((e) == EMFILE) ? "EMFILE" : \ + ((e) == ENFILE) ? "ENFILE" : \ + ((e) == ENOMEM) ? "ENOMEM" : \ + ((e) == EILSEQ) ? "EILSEQ" : \ + ((e) == EINVAL) ? "EINVAL" : \ + "UNKNOWN" + + /* Open a conversion descriptor by reference. Wraps the boilerplate * error reporting when iconv_open() fails. */ @@ -45,26 +56,10 @@ int _my_iconv_open(iconv_t* cd, const char *tocode, const char *fromcode) { /* The errno values are standard, but we have no way of knowing * if the implementation follows the standard! */ fprintf(stderr, - "ERROR: iconv_open(\"%s\", \"%s\") returned ", + "ERROR: iconv_open(\"%s\", \"%s\") returned %s\n", tocode, - fromcode); - switch (errno) { - case EMFILE: - fprintf(stderr, "EMFILE"); - break; - case ENFILE: - fprintf(stderr, "ENFILE"); - break; - case ENOMEM: - fprintf(stderr, "ENOMEM"); - break; - case EINVAL: - fprintf(stderr, "EINVAL"); - break; - default: - fprintf(stderr, "UNKNOWN (%d)", errno); - } - fprintf(stderr, "\n"); + fromcode, + ERRNO_STR(errno)); return FAILURE; } @@ -78,15 +73,7 @@ int _my_iconv_close(iconv_t* cd) { if (iconv_close(*cd) == -1) { /* The underlying type of iconv_t is unspecified, so we can't * easily print the argument here. */ - fprintf(stderr, "ERROR: iconv_close() returned "); - switch (errno) { - case EBADF: - fprintf(stderr, "EBADF"); - break; - default: - fprintf(stderr, "UNKNOWN (%d)", errno); - } - fprintf(stderr, "\n"); + fprintf(stderr, "ERROR: iconv_close() returned %s\n", ERRNO_STR(errno)); return FAILURE; } @@ -135,13 +122,13 @@ int test_iconv_open_does_not_support_invalid_suffix() { printf("%-60s", "iconv_open() does not support //INVALID..."); iconv_t cd = iconv_open(tocode, fromcode); if (cd == (iconv_t)(-1)) { - switch (errno) { - case EINVAL: - printf("OK (EINVAL, expected)\n"); + if (errno == EINVAL) { + printf("OK (EINVAL)\n"); return SUCCESS; - default: - printf("FAIL (errno=%d, unexpected)\n", errno); - return FAILURE; + } + else { + printf("FAIL (%s)\n", ERRNO_STR(errno)); + return FAILURE; } } printf("FAIL (invalid suffix was accepted)\n"); @@ -157,13 +144,13 @@ int test_iconv_open_does_not_support_invalid_to_codeset() { printf("%-60s", "iconv_open() does not support invalid \"to\" codeset..."); iconv_t cd = iconv_open(tocode, fromcode); if (cd == (iconv_t)(-1)) { - switch (errno) { - case EINVAL: - printf("OK (EINVAL, expected)\n"); - return SUCCESS; - default: - printf("FAIL (errno=%d, unexpected)\n", errno); - return FAILURE; + if (errno == EINVAL) { + printf("OK (EINVAL)\n"); + return SUCCESS; + } + else { + printf("FAIL (%s)\n", ERRNO_STR(errno)); + return FAILURE; } } printf("FAIL (invalid codeset was accepted)\n"); @@ -179,11 +166,11 @@ int test_iconv_open_does_not_support_invalid_from_codeset() { iconv_t cd = iconv_open(tocode, fromcode); if (cd == (iconv_t)(-1)) { if (errno == EINVAL) { - printf("OK (EINVAL, expected)\n"); + printf("OK (EINVAL)\n"); return SUCCESS; } else { - printf("FAIL (errno=%d, unexpected)\n", errno); + printf("FAIL (%s)\n", ERRNO_STR(errno)); return FAILURE; } } @@ -229,11 +216,11 @@ int test_iconv_invalid_char_is_eilseq() { } if (saved_errno == EILSEQ) { - printf("OK (EILSEQ, expected)\n"); + printf("OK\n"); return SUCCESS; } else { - printf("FAIL (errno=%d, unexpected)\n", saved_errno); + printf("FAIL (%s)\n", ERRNO_STR(saved_errno)); return FAILURE; } } @@ -280,11 +267,11 @@ int test_iconv_incomplete_multibyte_is_einval() { } if (saved_errno == EINVAL) { - printf("OK (EINVAL, expected)\n"); + printf("OK)\n"); return SUCCESS; } else { - printf("FAIL (errno=%d, unexpected)\n", saved_errno); + printf("FAIL (%s)\n", ERRNO_STR(saved_errno)); return FAILURE; } } @@ -324,11 +311,11 @@ int test_iconv_too_big_is_e2big() { } if (saved_errno == E2BIG) { - printf("OK (E2BIG, expected)\n"); + printf("OK\n"); return SUCCESS; } else { - printf("FAIL (errno=%d)\n", saved_errno); + printf("FAIL (%s)\n", ERRNO_STR(saved_errno)); return FAILURE; } } @@ -380,8 +367,9 @@ int test_iconv_ignore_ignores_invalid() { printf("ERROR (iconv_close)\n"); } else { - printf("FAIL (errno=%d)\n", saved_errno); + printf("FAIL (%s)\n", ERRNO_STR(saved_errno)); } + return FAILURE; }