From c027d9dc9096c92f87949b6306fe1596f4d2272c Mon Sep 17 00:00:00 2001 From: Dylan Lom Date: Sat, 20 Feb 2021 12:17:49 +1100 Subject: [PATCH] Extend util --- build.sh | 3 +- src/line.c | 10 ++++-- src/util.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/util.h | 10 ++++++ 4 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 src/util.c diff --git a/build.sh b/build.sh index e376154..9c9c9ac 100755 --- a/build.sh +++ b/build.sh @@ -18,7 +18,7 @@ cbuild() { target="$1" targetdest="bin/$(prettyname "$target")" shift - (set -x; cc $CFLAGS -o "$targetdest" "$target" "$@") + (set -x; cc $CFLAGS -o "$targetdest" "$target" src/util.c "$@") } fbuild() { @@ -31,6 +31,7 @@ bin() { [ ! -d bin ] && mkdir bin for f in src/*.c; do + [ "$f" = "src/util.c" ] && continue cbuild "$f" done diff --git a/src/line.c b/src/line.c index 61c3538..c25660a 100644 --- a/src/line.c +++ b/src/line.c @@ -21,14 +21,18 @@ #include "util.h" -const char* argv0; +const char *argv0; -void usage() { +void +usage() +{ fprintf(stderr, "usage: %s ln\n", argv0); exit(EXIT_FAILURE); } -int main(int argc, char *argv[]) { +int +main(int argc, char *argv[]) +{ SET_ARGV0(); if (argc < 1) usage(); diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..1e086c3 --- /dev/null +++ b/src/util.c @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include +#include + +#include "util.h" + +void +die(const char *fmt, ...) +{ + fmt = str_concat(2, fmt, "\n"); + va_list ap; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + exit(1); +} + +void +edie(const char *fmt, ...) +{ + fmt = str_concat(2, fmt, strerror(errno)); + va_list ap; + die(fmt, ap); +} + +void * +ecalloc(size_t nmemb, size_t size) +{ + void *ptr = calloc(nmemb, size); + if (ptr == NULL) edie("calloc: "); + return ptr; +} + +size_t +str_pushc(char *s, char c, size_t s_size, size_t realloc_amount) +{ + /* Realloc s if needed */ + size_t s_len = strlen(s); + if (s_len + 1 > s_size) { + s_size += realloc_amount; + s = realloc(s, s_size); + if (s == NULL) edie("realloc: "); + } + + s[s_len] = c; + s[s_len + 1] = '\0'; + return s_size; +} + +int +str_trimr(char *s, char c, int max_num) +{ + int removed = 0; + int i = strlen(s)-1; + while (s[i] == c && removed <= max_num) { + s[i] = '\0'; + i--; + removed++; + } + return removed; +} + +char * +str_concat(int count, ...) +{ + va_list ap; + int new_len = 1; + + /* Total length of all strings */ + va_start(ap, count); + for (int i = 0; i < count; i++) + new_len += strlen(va_arg(ap, char*)); + va_end(ap); + + char *new_str = calloc(new_len, sizeof(char)); + if (new_str == NULL) edie("calloc: "); + + /* Concat strings into newstr */ + va_start(ap, count); + for (int i = 0; i < count; i++) { + char *s = va_arg(ap, char*); + strcat(new_str, s); + } + va_end(ap); + + return new_str; +} diff --git a/src/util.h b/src/util.h index a121f83..e05f503 100644 --- a/src/util.h +++ b/src/util.h @@ -1,3 +1,13 @@ #define SHIFT_ARGS() argv++; argc--; #define SET_ARGV0() argv0 = argv[0]; SHIFT_ARGS(); +void die(const char *fmt, ...); +void edie(const char *fmt, ...); + +void *ecalloc(size_t nmemb, size_t size); + +size_t str_pushc(char *s, char c, size_t size, size_t realloc_amount); +int str_trimr(char *s, char c, int max_num); +char *str_concat(int count, ...); +#define STR_EALLOC(s) ecalloc((s), sizeof(char)) +#define STR_MALLOC(s) calloc((s), sizeof(char))