diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-01-18 09:49:10 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-01-18 09:49:10 +0100 |
commit | ba2b3c7e440f35573641d89b32d47764f791f51a (patch) | |
tree | a7a57a37664b3c8e302f5b0c3618ed505724f4c7 | |
parent | 5f832714d383497618a9305a70d47a0da0922c6c (diff) |
Simplify majorly
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()
-rw-r--r-- | make.c | 60 |
1 files changed, 10 insertions, 50 deletions
@@ -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); -} |