donstranding

Don Stranding
git clone git://git.kocotian.pl/donstranding.git
Log | Files | Refs | Submodules | README | LICENSE

commit 47a694db0f1811a4b117ffe3384945bf56669a5b
parent 1a70811539b736f05f50e58573aed7caffd63bee
Author: kocotian <kocotian@kocotian.pl>
Date:   Sun,  2 May 2021 12:06:54 +0200

libsl

Diffstat:
MMakefile | 3+++
Mconfig.mk | 2+-
Ainclude/arg.h | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Ainclude/util.h | 13+++++++++++++
Alicenses/LIBSL | 24++++++++++++++++++++++++
Autil.c | 36++++++++++++++++++++++++++++++++++++
6 files changed, 127 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile @@ -4,6 +4,9 @@ VERSION = 0.1 # avoid editions of this file. include config.mk +LIB = util.c +OBJ = ${LIB:.c=.o} + all: options donstranding options: diff --git a/config.mk b/config.mk @@ -1,5 +1,5 @@ # includes -INC = -Itermbox/src/ +INC = -Itermbox/src/ -Iinclude/ # flags CFLAGS = -Wall -Wextra -Wconversion -std=c99 -pedantic ${INC} diff --git a/include/arg.h b/include/arg.h @@ -0,0 +1,50 @@ +/* See licenses/LIBSL file for copyright and license details. */ +/* + * Copy me if you can. + * by 20h + */ + +#ifndef ARG_H__ +#define ARG_H__ + +extern char *argv0; + +/* use main(int argc, char *argv[]) */ +#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ + argv[0] && argv[0][0] == '-'\ + && argv[0][1];\ + argc--, argv++) {\ + char argc_;\ + char **argv_;\ + int brk_;\ + if (argv[0][1] == '-' && argv[0][2] == '\0') {\ + argv++;\ + argc--;\ + break;\ + }\ + for (brk_ = 0, argv[0]++, argv_ = argv;\ + argv[0][0] && !brk_;\ + argv[0]++) {\ + if (argv_ != argv)\ + break;\ + argc_ = argv[0][0];\ + switch (argc_) + +#define ARGEND }\ + } + +#define ARGC() argc_ + +#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\ + ((x), abort(), (char *)0) :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\ + (char *)0 :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#endif diff --git a/include/util.h b/include/util.h @@ -0,0 +1,13 @@ +/* See licenses/LIBSL file for copyright and license details. */ + +#ifndef _UTIL_H +#define _UTIL_H + +#define MAX(A, B) ((A) > (B) ? (A) : (B)) +#define MIN(A, B) ((A) < (B) ? (A) : (B)) +#define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B)) + +void *ecalloc(size_t nmemb, size_t size); +void die(const char *fmt, ...); + +#endif diff --git a/licenses/LIBSL b/licenses/LIBSL @@ -0,0 +1,24 @@ +MIT/X Consortium License + +© 2013-2019 Anselm R Garbe <anselm@garbe.ca> +© 2015-2019 Hiltjo Posthuma <hiltjo@codemadness.org> +© 2016 Markus Teich <markus.teich@stusta.mhn.de> +© 2015 Eric Pruitt <eric.pruitt@gmail.com> + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/util.c b/util.c @@ -0,0 +1,36 @@ +/* See licenses/LIBSL file for copyright and license details. */ +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "util.h" + +void * +ecalloc(size_t nmemb, size_t size) +{ + void *p; + + if (!(p = calloc(nmemb, size))) + die("calloc:"); + return p; +} + +void +die(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + if (fmt[0] && fmt[strlen(fmt) - 1] == ':') { + fputc(' ', stderr); + perror(NULL); + } else { + fputc('\n', stderr); + } + + exit(1); +}