diff options
Diffstat (limited to 'util.c')
| -rw-r--r-- | util.c | 46 |
1 files changed, 46 insertions, 0 deletions
@@ -2,11 +2,15 @@ #include "util.h" +#include <stdarg.h> #include <stdlib.h> #include <string.h> #include <wchar.h> #include <wctype.h> +static const char *allowed = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:,;-_(){}[]"; + int strnwidth(const char *s, int n) { @@ -41,3 +45,45 @@ assert(int cond, const char *file, int line, const char *condstr) fprintf(stderr, "Assertion failed %s:%i (%s)", file, line, condstr); exit(1); } + +char * +sanitized(const char *instr) +{ + const char *p; + char *clean; + int i; + + clean = strdup(instr); + ASSERT(clean != NULL); + for (i = 0, p = instr; *p; p++) { + if (strchr(allowed, *p)) + clean[i++] = *p; + } + ASSERT(i != 0); + clean[i] = '\0'; + + return clean; +} + +char * +aprintf(const char *fmtstr, ...) +{ + va_list ap, cpy; + size_t size; + char *str; + + va_copy(cpy, ap); + + va_start(ap, fmtstr); + size = vsnprintf(NULL, 0, fmtstr, ap); + va_end(ap); + + str = malloc(size + 1); + ASSERT(str != NULL); + + va_start(cpy, fmtstr); + vsnprintf(str, size + 1, fmtstr, cpy); + va_end(cpy); + + return str; +} |
