summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpgen <p.gen.progs@gmail.com>2023-09-15 23:45:48 +0200
committerpgen <p.gen.progs@gmail.com>2023-09-15 23:49:54 +0200
commitdeb32974e55ac7632ee0de547b646d3a01b68747 (patch)
tree01fb5c1173e1e866aaa575f1953e015cbefd414a
parent36881ac8835674bf5ce0112f1c5bfa7d1b1e3f27 (diff)
Improve the strprint function
-rw-r--r--utils.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/utils.c b/utils.c
index 3ab872b..e828a4a 100644
--- a/utils.c
+++ b/utils.c
@@ -382,12 +382,13 @@ strrep(char *s, const char c1, const char c2)
/* ================================================================== */
/* Allocates and returns a string similar to s but with non printable */
-/* character changed in '_' */
+/* character changed by their ASCII hexadecimal notation. */
/* ================================================================== */
char *
strprint(char *s)
{
- char *new = xcalloc(1, 4 * strlen(s) + 1);
+ size_t l = strlen(s);
+ char *new = xcalloc(1, 4 * l + 1);
char *p = new;
while (*s)
@@ -396,9 +397,13 @@ strprint(char *s)
*(p++) = *s++;
else
{
- sprintf(p, "[%02x]", (unsigned char)*s++);
+ sprintf(p, "\\x%02X", (unsigned char)*s++);
p += 4;
}
}
+
+ if (p - new > l)
+ new = xrealloc(new, p - new + 1);
+
return new;
}