From 7980a5d2a8e3c3db8ce3cd9b7a2151936faeb325 Mon Sep 17 00:00:00 2001 From: Omar Polo Date: Wed, 18 Oct 2023 17:13:51 +0000 Subject: gg: print the response header for non-2x replies to standard error --- gg.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'gg.c') diff --git a/gg.c b/gg.c index 6f2ee68..623d2ea 100644 --- a/gg.c +++ b/gg.c @@ -23,6 +23,7 @@ #include #include #include +#include enum debug { DEBUG_NONE, @@ -181,6 +182,26 @@ dorep(struct tls *ctx, uint8_t *buf, size_t len) return tot; } +static void +safeprint(FILE *fp, const char *str) +{ + int len; + wchar_t wc; + + for (; *str != '\0'; str += len) { + if ((len = mbtowc(&wc, str, MB_CUR_MAX)) == -1) { + mbtowc(NULL, NULL, MB_CUR_MAX); + fputc('?', fp); + len = 1; + } else if (wcwidth(wc) == -1) { + fputc('?', fp); + } else if (wc != L'\n') + putwc(wc, fp); + } + + fputc('\n', fp); +} + static int get(const char *r) { @@ -285,6 +306,10 @@ get(const char *r) /* skip the header */ t = memmem(buf, len, "\r\n", 2); assert(t != NULL); + if (code < 20 || code >= 30) { + *t = '\0'; + safeprint(stderr, buf + 3); /* skip return code */ + } t += 2; /* skip \r\n */ len -= t - buf; write(1, t, len); -- cgit v1.2.3