From e9a910b33c7837b4b868e3abda18eb4810df7f02 Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Sat, 4 Oct 2025 09:14:33 +0100 Subject: config/git: import suckless sbase i currently use the output of sha512sum in several places of xbmk, which is a bit unreliable in case output changes. other cases where i use util outputs in variables are probably reliable, because i'm using mostly posix utilities in those. to mitigate this, i now import suckless sbase, which has a reasonable sha512sum implementation. *every* binary it builds is being placed in build.list, because i'll probably start using more of them. for example, i may start modifying the "date" implementation, adding the GNU-specific options that i need as mentioned on init.sh i'm importing it in util/ because the sha512sum util is needed for verifying project sources, so if sbase itself is a "project source", that means we can into a chicken and egg bootstrapping problem. this is sbase at revision: 055cc1ae1b3a13c3d8f25af0a4a3316590efcd48 Signed-off-by: Leah Rowe --- util/sbase/paste.c | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 util/sbase/paste.c (limited to 'util/sbase/paste.c') diff --git a/util/sbase/paste.c b/util/sbase/paste.c new file mode 100644 index 00000000..4fa9fc5a --- /dev/null +++ b/util/sbase/paste.c @@ -0,0 +1,144 @@ +/* See LICENSE file for copyright and license details. */ +#include +#include + +#include "utf.h" +#include "util.h" + +struct fdescr { + FILE *fp; + const char *name; +}; + +static void +sequential(struct fdescr *dsc, int fdescrlen, Rune *delim, size_t delimlen) +{ + Rune c, last; + size_t i, d; + + for (i = 0; i < fdescrlen; i++) { + d = 0; + last = 0; + + while (efgetrune(&c, dsc[i].fp, dsc[i].name)) { + if (last == '\n') { + if (delim[d] != '\0') + efputrune(&delim[d], stdout, ""); + d = (d + 1) % delimlen; + } + + if (c != '\n') + efputrune(&c, stdout, ""); + last = c; + } + + if (last == '\n') + efputrune(&last, stdout, ""); + } +} + +static void +parallel(struct fdescr *dsc, int fdescrlen, Rune *delim, size_t delimlen) +{ + Rune c, d; + size_t i, m; + ssize_t last; + +nextline: + last = -1; + + for (i = 0; i < fdescrlen; i++) { + d = delim[i % delimlen]; + c = 0; + + while (efgetrune(&c, dsc[i].fp, dsc[i].name)) { + for (m = last + 1; m < i; m++) { + if (delim[m % delimlen] != '\0') + efputrune(&delim[m % delimlen], stdout, ""); + } + last = i; + if (c == '\n') { + if (i != fdescrlen - 1) + c = d; + efputrune(&c, stdout, ""); + break; + } + efputrune(&c, stdout, ""); + } + + if (c == 0 && last != -1) { + if (i == fdescrlen - 1) + putchar('\n'); + else if (d != '\0') + efputrune(&d, stdout, ""); + last++; + } + } + if (last != -1) + goto nextline; +} + +static void +usage(void) +{ + eprintf("usage: %s [-s] [-d list] file ...\n", argv0); +} + +int +main(int argc, char *argv[]) +{ + struct fdescr *dsc; + Rune *delim_rune = NULL; + size_t delim_runelen, i, delim_bytelen = 1; + int seq = 0, ret = 0; + char *delim = "\t"; + + ARGBEGIN { + case 's': + seq = 1; + break; + case 'd': + delim = EARGF(usage()); + delim_bytelen = unescape(delim); + break; + default: + usage(); + } ARGEND + + if (!argc) + usage(); + + /* populate delimiters */ + delim_rune = ereallocarray(NULL, + utfmemlen(delim, delim_bytelen) + 1, sizeof(*delim_rune)); + if (!(delim_runelen = utfntorunestr(delim, delim_bytelen, delim_rune))) { + usage(); + } + + /* populate file list */ + dsc = ereallocarray(NULL, argc, sizeof(*dsc)); + + for (i = 0; i < argc; i++) { + if (!strcmp(argv[i], "-")) { + argv[i] = ""; + dsc[i].fp = stdin; + } else if (!(dsc[i].fp = fopen(argv[i], "r"))) { + eprintf("fopen %s:", argv[i]); + } + dsc[i].name = argv[i]; + } + + if (seq) { + sequential(dsc, argc, delim_rune, delim_runelen); + } else { + parallel(dsc, argc, delim_rune, delim_runelen); + } + + for (i = 0; i < argc; i++) + if (dsc[i].fp != stdin && fshut(dsc[i].fp, argv[i])) + ret |= fshut(dsc[i].fp, argv[i]); + + ret |= fshut(stdin, "") | fshut(stdout, ""); + + return ret; +} -- cgit v1.2.1