aboutsummaryrefslogtreecommitdiff
path: root/gg.c
diff options
context:
space:
mode:
authorOmar Polo <op@omarpolo.com>2023-10-18 17:13:51 +0000
committerOmar Polo <op@omarpolo.com>2023-10-18 17:13:51 +0000
commit7980a5d2a8e3c3db8ce3cd9b7a2151936faeb325 (patch)
tree21ad773e6e28f4a26f1002c46b0d5464157de4b6 /gg.c
parentc7bcd4a8a9b22a4b28136e24069279cb93b1e43d (diff)
gg: print the response header for non-2x replies to standard error
Diffstat (limited to 'gg.c')
-rw-r--r--gg.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/gg.c b/gg.c
index 6f2ee68..623d2ea 100644
--- a/gg.c
+++ b/gg.c
@@ -23,6 +23,7 @@
#include <errno.h>
#include <locale.h>
#include <string.h>
+#include <wchar.h>
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);