diff options
author | Matthias Andree <matthias.andree@gmx.de> | 2009-08-05 16:27:47 +0000 |
---|---|---|
committer | Matthias Andree <matthias.andree@gmx.de> | 2009-08-05 16:27:47 +0000 |
commit | c8f608f4c4c6d2eb6cb7b12cb60de04cadcb3750 (patch) | |
tree | 99b131162e6c7b1d87ea295ed09b1fbeb6ffd66f /sdump.c | |
parent | 62acd57d67fff935e1c8a1796853e911869ee9f8 (diff) | |
download | fetchmail-c8f608f4c4c6d2eb6cb7b12cb60de04cadcb3750.tar.gz fetchmail-c8f608f4c4c6d2eb6cb7b12cb60de04cadcb3750.tar.bz2 fetchmail-c8f608f4c4c6d2eb6cb7b12cb60de04cadcb3750.zip |
Add sdump(), split xmalloc.h out of fetchmail.h
svn path=/branches/BRANCH_6-3/; revision=5390
Diffstat (limited to 'sdump.c')
-rw-r--r-- | sdump.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/sdump.c b/sdump.c new file mode 100644 index 00000000..5782f134 --- /dev/null +++ b/sdump.c @@ -0,0 +1,37 @@ +/* sdump.c -- library to allocate a printable version of a string */ +/** \file sdump.c + * \author Matthias Andree + * \date 2009 + */ + +#include <ctype.h> /* for isprint() */ +#include <stdio.h> /* for sprintf() */ +#include <stdlib.h> /* for size_t */ +#include "xmalloc.h" /* for xmalloc() */ + +#include "sdump.h" /* for prototype */ + +/** sdump converts a byte string \a in of size \a len into a printable + * string and returns a pointer to the memory region. + * \returns a pointer to a xmalloc()ed string that the caller must + * free() after use. This function causes program abort on failure. */ +char *sdump(const char *in, size_t len) +{ + size_t outlen = 0, i; + char *out, *oi; + + for (i = 0; i < len; i++) { + outlen += isprint((unsigned char)in[i]) ? 1 : 4; + } + + oi = out = (char *)xmalloc(outlen + 1); + for (i = 0; i < len; i++) { + if (isprint((unsigned char)in[i])) { + *(oi++) = in[i]; + } else { + oi += sprintf(oi, "\\x%02X", in[i]); + } + } + *oi = '\0'; + return out; +} |