From ba2b3c7e440f35573641d89b32d47764f791f51a Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Thu, 18 Jan 2024 09:49:10 +0100 Subject: Simplify majorly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit mkoutpath() is only ever called with one argument so it doesn’t need to be variadic. Additionally I only care about supporting MacOS, Linux, and the BSDs, and all of them support strlcat()*. * Linux doesn’t ship a libc, but both Glibc and Musl ship strlcat() --- make.c | 60 ++++++++++-------------------------------------------------- 1 file changed, 10 insertions(+), 50 deletions(-) (limited to 'make.c') diff --git a/make.c b/make.c index 7214fc2..f4e94d8 100644 --- a/make.c +++ b/make.c @@ -28,10 +28,7 @@ cmdclr(&c); \ } while (0) -static size_t _strlcat(char *, const char *, size_t); -static char *_mkoutpath(const char **, size_t); -#define mkoutpath(...) \ - _mkoutpath((const char **)_vtoa(__VA_ARGS__), lengthof(_vtoa(__VA_ARGS__))) +static char *mkoutpath(const char *); static bool pflag; @@ -101,27 +98,23 @@ main(int argc, char **argv) } char * -_mkoutpath(const char **xs, size_t n) +mkoutpath(const char *s) { -#define trycat(s) \ - do { \ - if (_strlcat(buf, (s), PATH_MAX) >= PATH_MAX) \ - goto toolong; \ - } while (0) - char *p, *buf; buf = bufalloc(NULL, PATH_MAX, sizeof(char)); buf[0] = 0; - if (p = getenv("DESTDIR"), p && *p) - trycat(p); + if (p = getenv("DESTDIR"), p && *p) { + if (strlcat(buf, p, PATH_MAX) >= PATH_MAX) + goto toolong; + } p = getenv("PREFIX"); - trycat(p && *p ? p : PREFIX); - - for (size_t i = 0; i < n; i++) - trycat(xs[i]); + if (strlcat(buf, p && *p ? p : PREFIX, PATH_MAX) >= PATH_MAX) + goto toolong; + if (strlcat(buf, s, PATH_MAX) >= PATH_MAX) + goto toolong; return buf; @@ -129,36 +122,3 @@ toolong: errno = ENAMETOOLONG; die(__func__); } - -size_t -_strlcat(char *dst, const char *src, size_t size) -{ - char *d = dst; - const char *s = src; - size_t n = size; - size_t dlen; - - while (n-- != 0 && *d != '\0') - d++; - - dlen = d - dst; - n = size - dlen; - - if (n == 0) { - while (*s++) - ; - return dlen + (s - src - 1); - } - - while (*s != '\0') { - if (n != 1) { - *d++ = *s; - n--; - } - s++; - } - - *d = '\0'; - - return dlen + (s - src); -} -- cgit v1.2.3