config

OpenBSD system configuration
git clone git://jacobedwards.org/config
Log | Files | Refs | README

commit 9b3a8572c11fd93cd1b842490f0f5b765ac37a49
parent d6705b250e2e72e54e612f9eca4ef58afedff6d1
Author: Jacob R. Edwards <n/a>
Date:   Tue, 11 Jan 2022 14:11:15 -0800

Add src module for compiled programs

Add urlencode, urldecode, and datediff programs. The url encoding
and decoding programs were written months ago, but I only decided
on how to add them now.

Diffstat:
Mlocal/Makefile | 1+
Alocal/src/src/single/Makefile | 24++++++++++++++++++++++++
Alocal/src/src/single/datediff.c | 125+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Alocal/src/src/single/urldecode.c | 42++++++++++++++++++++++++++++++++++++++++++
Alocal/src/src/single/urlencode.c | 31+++++++++++++++++++++++++++++++
5 files changed, 223 insertions(+), 0 deletions(-)

diff --git a/local/Makefile b/local/Makefile @@ -8,6 +8,7 @@ MODULES =\ links\ netquery\ rc\ + src\ ssh\ sxiv\ xenodm\ diff --git a/local/src/src/single/Makefile b/local/src/src/single/Makefile @@ -0,0 +1,24 @@ +names =\ + datediff\ + urldecode\ + urlencode\ + +src = ${names:.c=} + +.c: + ${CC} ${CFLAGS} -o $@ $< ${LDFLAGS} + +all: ${src:.c=} + +clean: + rm -f ${names} + +install: + cp -f ${names} ${HOME}/bin + +uninstall: + cd ${HOME}/bin + rm -f ${names} + +.PHONY: clean install uninstall +.SUFFIXES: .c diff --git a/local/src/src/single/datediff.c b/local/src/src/single/datediff.c @@ -0,0 +1,125 @@ +/* + * Copyright 2022 Jacob R. Edwards + * Show the difference between two dates + */ + +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <unistd.h> + +#define LENGTH(X) (sizeof(X) / sizeof(*X)) + +struct scale { + char *name; + unsigned int scale; + unsigned int value; +}; + +static char *usage = "usage: [-s] [-j fmt] date1 date2"; +static char *fmt = "%F"; +static int inseconds; + +void +die(char *s) +{ + if (errno) + perror(s); + else + fprintf(stderr, "%s\n", s); + exit(1); +} + +void +printvalue(struct scale *scale) +{ + printf("%u %s%c", scale->value, scale->name, (scale->value != 1 ? 's' : 0)); +} + +void +printelapsed(time_t time) +{ + static struct scale scales[] = { + { "second", 1 }, + { "minuite", 60 }, + { "hour", 60 }, + { "day", 24 }, + { "week", 7 }, + { "year", 52 }, + { "decade", 10 }, + { "century", 10 } + }; + int i, j, nonzero; + + scales[0].value = time; + for (i = 1; i < LENGTH(scales) && scales[i - 1].value >= scales[i].scale; ++i) { + scales[i].value = scales[i - 1].value / scales[i].scale; + scales[i - 1].value = scales[i - 1].value % scales[i].scale; + } + + for (j = 0; j < LENGTH(scales) && !scales[j].value; ++j) + ; + + nonzero = 0; + for (--i; i > j; --i) { + if (scales[i].value) { + ++nonzero; + printvalue(&scales[i]); + putchar(' '); + } + } + if (nonzero) + printf("and "); + printvalue(&scales[i]); + putchar('\n'); +} + +int +main(int argc, char *argv[]) +{ + int c, i; + char *ret; + struct tm tms[2]; + time_t times[2]; + time_t diff; + + while ((c = getopt(argc, argv, "j:s")) >= 0) { + switch (c) { + case 's': + inseconds = 1; + break; + case 'j': + fmt = optarg; + break; + default: + die(usage); + } + } + + if ((argc -= optind) != 2) + die(usage); + argv += optind; + + memset(tms, 0, sizeof(tms)); + for (i = 0; i < 2; ++i) { + ret = strptime(argv[i], fmt, &tms[i]); + if (ret == NULL || *ret != '\0') + die("Invalid date"); + tms[i].tm_isdst = -1; + times[i] = tms[i].tm_gmtoff; + times[i] = mktime(&tms[i]) - times[i]; + } + + if (times[0] > times[1]) + diff = times[0] - times[1]; + else + diff = times[1] - times[0]; + + if (inseconds) + printf("%lld\n", diff); + else + printelapsed(diff); + return 0; +} diff --git a/local/src/src/single/urldecode.c b/local/src/src/single/urldecode.c @@ -0,0 +1,42 @@ +/* + * Copyright 2021 Jacob R. Edwards + * Decode URI Percent Encoding (See RFC 2396, Section 2.4.1., "Escaped Encoding") + */ + +#include <stdio.h> +#include <stdlib.h> + +int +main(void) +{ + char *b; + size_t l; + size_t s; + char o[3]; + int p; + unsigned char c; + char *e; + + o[2] = 0; + b = NULL; + s = 0; + while ((l = getdelim(&b, &s, '%', stdin)) != -1) { + if (b[l - 1] == '%') { + --l; + if (fread(o, 1, 2, stdin) != 2) + goto badescape; + c = strtoul(o, &e, 16); + if (*e) { +badescape: + perror("bad escape"); + return 1; + } + p = 1; + } else + p = 0; + if (fwrite(b, 1, l, stdout) != l || (p && fputc(c, stdout) < 0)) + return 1; + } + + return 0; +} diff --git a/local/src/src/single/urlencode.c b/local/src/src/single/urlencode.c @@ -0,0 +1,31 @@ +/* + * Copyright 2021 Jacob R. Edwards + * Percent Encode URI (See RFC 2396, Sections 2., "Characters") + */ + +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int +main(void) +{ + int i, il, ol; + unsigned char ib[1024], ob[sizeof(ib) * 3 + 1]; + + while ((il = read(0, ib, sizeof(ib))) > 0) { + ol = 0; + for (i = 0; i < il; ++i) { + if (ib[i] <= 0x20 || strchr(":/?#[]@!$&'()*+,;=" "+%\x7F", ib[i])) { + if (sprintf(ob + ol, "%%%.2X", ib[i]) != 3) + return 1; + ol += 3; + } else + ob[ol++] = ib[i]; + } + if (write(1, ob, ol) != ol) + return 1; + } + + return il == -1; +}