From 5de227689402736235f6bdb932fa9ad5f939fcb8 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Wed, 23 Nov 2016 19:19:20 -0800 Subject: util: put convenience helpers into util.[ch] Also introduces get_ncpus(), in preparation for threaded rendering. --- util.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 util.c (limited to 'util.c') diff --git a/util.c b/util.c new file mode 100644 index 0000000..c07589e --- /dev/null +++ b/util.c @@ -0,0 +1,22 @@ +#include +#include +#include + +#include "util.h" + +#define SYSFS_CPU "/sys/devices/system/cpu/cpu" +#define MAXCPUS 1024 + +unsigned get_ncpus(void) +{ + char path[cstrlen(SYSFS_CPU "1024") + 1]; + unsigned n; + + for (n = 0; n < MAXCPUS; n++) { + snprintf(path, sizeof(path), "%s%u", SYSFS_CPU, n); + if (access(path, F_OK) == -1) + break; + } + + return n == 0 ? 1 : n; +} -- cgit v1.2.1 From 349ae02d9201dffcca98cdba270091d6dfe2d114 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Tue, 13 Dec 2016 07:26:39 -0800 Subject: util: introduce ask_(string,num) helpers quick and dirty stdio dialog helpers --- util.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'util.c') diff --git a/util.c b/util.c index c07589e..0e5825a 100644 --- a/util.c +++ b/util.c @@ -1,5 +1,6 @@ #include #include +#include #include #include "util.h" @@ -20,3 +21,40 @@ unsigned get_ncpus(void) return n == 0 ? 1 : n; } + + +static void query(const char *prompt, const char *def, char *buf, int len) +{ + buf[0] = '\0'; + + printf("%s [%s]: ", prompt, def); + fflush(stdout); + + fgets(buf, len, stdin); + if (buf[0] == '\0' || buf[0] == '\n') { + snprintf(buf, len, "%s", def); + } else if(strchr(buf, '\n')) { + *strchr(buf, '\n') = '\0'; + } +} + + +void ask_string(char *buf, int len, const char *prompt, const char *def) +{ + query(prompt, def, buf, len); +} + + +void ask_num(int *res, int max, const char *prompt, int def) +{ + char buf[21], buf2[256]; + int num; + + snprintf(buf, sizeof(buf), "%i", def); + do { + query(prompt, buf, buf2, sizeof(buf2)); + num = atoi(buf2); /* TODO: errors (strtol)*/ + } while (num > max); + + *res = num; +} -- cgit v1.2.1