nkiss

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.nihaljere.xyz/nkiss
Log | Files | Refs

commit 042b53a2a18f40c146174333b1e8dc16480684f9
Author: Nihal Jere <nihal@nihaljere.xyz>
Date:   Sun, 14 Feb 2021 20:42:23 -0600

initial commit

Diffstat:
Akiss.c | 16++++++++++++++++
Autil.c | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Autil.h | 17+++++++++++++++++
3 files changed, 84 insertions(+), 0 deletions(-)

diff --git a/kiss.c b/kiss.c @@ -0,0 +1,16 @@ +#include <stdlib.h> +#include <stdio.h> + +#include "util.h" + +char *argv0; + +int main(int argc, char *argv[]) +{ + char *kiss_path; + + argv0 = argv[0]; + if ((kiss_path = getenv("KISS_PATH")) == NULL) + die("KISS_PATH unset."); + puts(kiss_path); +} diff --git a/util.c b/util.c @@ -0,0 +1,51 @@ +/* See LICENSE file for copyright and license details. */ +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <tls.h> + +#ifdef __OpenBSD__ +#include <unistd.h> +#endif /* __OpenBSD__ */ + +#include "util.h" + +static void +verr(const char *fmt, va_list ap) +{ + if (argv0 && strncmp(fmt, "usage", sizeof("usage") - 1)) { + fprintf(stderr, "%s: ", argv0); + } + + vfprintf(stderr, fmt, ap); + + if (fmt[0] && fmt[strlen(fmt) - 1] == ':') { + fputc(' ', stderr); + perror(NULL); + } else { + fputc('\n', stderr); + } +} + +void +warn(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + verr(fmt, ap); + va_end(ap); +} + +void +die(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + verr(fmt, ap); + va_end(ap); + + exit(1); +} diff --git a/util.h b/util.h @@ -0,0 +1,17 @@ +/* See LICENSE file for copyright and license details. */ +#ifndef UTIL_H +#define UTIL_H + +#undef MIN +#define MIN(x,y) ((x) < (y) ? (x) : (y)) +#undef MAX +#define MAX(x,y) ((x) > (y) ? (x) : (y)) +#undef LEN +#define LEN(x) (sizeof (x) / sizeof *(x)) + +extern char *argv0; + +void warn(const char *, ...); +void die(const char *, ...); + +#endif /* UTIL_H */