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/libutil/strnsubst.c | 61 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 util/sbase/libutil/strnsubst.c (limited to 'util/sbase/libutil/strnsubst.c') diff --git a/util/sbase/libutil/strnsubst.c b/util/sbase/libutil/strnsubst.c new file mode 100644 index 00000000..2da54aba --- /dev/null +++ b/util/sbase/libutil/strnsubst.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2002 J. Mallett. All rights reserved. + * You may do whatever you want with this file as long as + * the above copyright and this notice remain intact, along + * with the following statement: + * For the man who taught me vi, and who got too old, too young. + */ + +#include +#include +#include + +#include "../util.h" + +/* + * Replaces str with a string consisting of str with match replaced with + * replstr as many times as can be done before the constructed string is + * maxsize bytes large. It does not free the string pointed to by str, it + * is up to the calling program to be sure that the original contents of + * str as well as the new contents are handled in an appropriate manner. + * If replstr is NULL, then that internally is changed to a nil-string, so + * that we can still pretend to do somewhat meaningful substitution. + * No value is returned. + */ +void +strnsubst(char **str, const char *match, const char *replstr, size_t maxsize) +{ + char *s1, *s2, *this; + size_t matchlen, s2len; + int n; + + if ((s1 = *str) == NULL) + return; + s2 = emalloc(maxsize); + + if (replstr == NULL) + replstr = ""; + + if (match == NULL || *match == '\0' || strlen(s1) >= maxsize) { + strlcpy(s2, s1, maxsize); + goto done; + } + + *s2 = '\0'; + s2len = 0; + matchlen = strlen(match); + for (;;) { + if ((this = strstr(s1, match)) == NULL) + break; + n = snprintf(s2 + s2len, maxsize - s2len, "%.*s%s", + (int)(this - s1), s1, replstr); + if (n == -1 || n + s2len + strlen(this + matchlen) >= maxsize) + break; /* out of room */ + s2len += n; + s1 = this + matchlen; + } + strlcpy(s2 + s2len, s1, maxsize - s2len); +done: + *str = s2; + return; +} -- cgit v1.2.1