From 9fe644f0d99375ffd3011d8828f7dbd0fb103af0 Mon Sep 17 00:00:00 2001 From: Louis Burda Date: Sat, 4 Dec 2021 13:48:01 +0100 Subject: Added more gui interaction --- util.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'util.c') diff --git a/util.c b/util.c index b9f37de..675dc3c 100644 --- a/util.c +++ b/util.c @@ -2,11 +2,15 @@ #include "util.h" +#include #include #include #include #include +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; +} -- cgit v1.2.3-71-gd317