commit: 875b0a4d6e3750c482341a2bd78e266bf8154947
parent d533d3465fbb24d2574903d0ff674573abb71fb8
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 6 Jun 2025 06:21:27 +0200
lib/strchrnul: new, for lib/offline_realpath and cmd/which
Diffstat:
4 files changed, 28 insertions(+), 25 deletions(-)
diff --git a/cmd/which.c b/cmd/which.c
@@ -4,6 +4,7 @@
#define _POSIX_C_SOURCE 200809L
#include "../lib/getopt_nolong.h"
+#include "../lib/strchrnul.h"
#include <limits.h> // PATH_MAX
#include <stdbool.h>
@@ -14,18 +15,6 @@
const char *argv0 = "which";
-static const char *
-which_strchrnul(const char *s, int c)
-{
- if(!s) return s;
-
- const char *p = s;
- while(*p != c && *p != '\0')
- p++;
-
- return p;
-}
-
int
main(int argc, char *argv[])
{
@@ -73,7 +62,7 @@ main(int argc, char *argv[])
while(true)
{
static char buf[PATH_MAX] = "";
- const char *stop = which_strchrnul(prev, ':');
+ const char *stop = utils_strchrnul(prev, ':');
size_t buflen = stop - prev;
memcpy(buf, prev, buflen);
diff --git a/lib/offline_realpath.c b/lib/offline_realpath.c
@@ -4,6 +4,7 @@
#define _POSIX_C_SOURCE 200809L
#include "fs.h"
+#include "strchrnul.h"
#include <errno.h>
#include <limits.h>
@@ -20,17 +21,6 @@ slash_len(const char *s)
return s - s0;
}
-static char *
-__strchrnul(const char *s, int c)
-{
- c = (unsigned char)c;
- if(!c) return (char *)s + strlen(s);
-
- for(; *s && *(unsigned char *)s != c; s++)
- ;
- return (char *)s;
-}
-
// realpath(3) but without checking for symlinks
char *
offline_realpath(const char *restrict filename, char *restrict resolved)
@@ -76,7 +66,7 @@ offline_realpath(const char *restrict filename, char *restrict resolved)
continue;
}
- char *z = __strchrnul(stack + p, '/');
+ char *z = utils_strchrnul(stack + p, '/');
l0 = l = z - (stack + p);
if(!l && !check_dir) break;
diff --git a/lib/strchrnul.c b/lib/strchrnul.c
@@ -0,0 +1,19 @@
+// Based on the strchrnul(3) function from musl
+// Copyright © 2005-2020 Rich Felker, et al.
+// SPDX-License-Identifier: MIT
+
+#define _POSIX_C_SOURCE 200809L
+#include "strchrnul.h"
+
+#include <string.h>
+
+char *
+utils_strchrnul(const char *s, int c)
+{
+ c = (unsigned char)c;
+ if(!c) return (char *)s + strlen(s);
+
+ for(; *s && *(unsigned char *)s != c; s++)
+ ;
+ return (char *)s;
+}
diff --git a/lib/strchrnul.h b/lib/strchrnul.h
@@ -0,0 +1,5 @@
+// Based on the strchrnul(3) function from musl
+// Copyright © 2005-2020 Rich Felker, et al.
+// SPDX-License-Identifier: MIT
+
+char *utils_strchrnul(const char *s, int c);