#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.
*/
/* 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;
}
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;
}
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");
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");
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;
}
}
}
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;
}
}
}
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;
}
}
}
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;
}
}
printf("ERROR (iconv_close)\n");
}
else {
- printf("FAIL (errno=%d)\n", saved_errno);
+ printf("FAIL (%s)\n", ERRNO_STR(saved_errno));
}
+
return FAILURE;
}