From 6a0e4e3da5cf17635d094008c7cc5562b1f081e1 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Fri, 19 Sep 2025 00:42:14 -0400 Subject: kern: vfs: Add path component counter Signed-off-by: Ian Moffett --- src/sys/os/vfs_subr.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'src/sys/os') diff --git a/src/sys/os/vfs_subr.c b/src/sys/os/vfs_subr.c index d950b53..f9f4a60 100644 --- a/src/sys/os/vfs_subr.c +++ b/src/sys/os/vfs_subr.c @@ -31,8 +31,33 @@ #include #include #include +#include #include +/* + * Returns a value of zero if a character value + * within a path is valid, otherwise a less than + * zero value on error. + */ +static int +vfs_pathc_valid(char c) +{ + /* Handle [A-Za-z] */ + if (c >= 'a' && c <= 'z') + return 0; + if (c >= 'A' && c <= 'Z') + return 0; + + /* Handle [0-9] and '/' */ + if (c >= '0' && c <= '9') + return 0; + if (c == '/') + return 0; + + return -1; + +} + /* * Allocate a new vnode */ @@ -67,3 +92,44 @@ vfs_vrel(struct vnode *vp, int flags) kfree(vp); return 0; } + + +/* + * Count number of components + */ +int +vfs_cmp_cnt(const char *path) +{ + const char *p; + int error, cnt = -1; + + if (path == NULL) { + return -EINVAL; + } + + if (*path != '/') { + return -ENOENT; + } + + p = path; + cnt = 0; + + while (*p != '\0') { + while (*p == '/') + ++p; + while (*p != '/' && *p != '\0') + error = vfs_pathc_valid(*p++); + + /* Invalid character? */ + if (error < 0) + return -EINVAL; + + /* Break if we got NUL */ + if (*p == '\0') + break; + + ++p, ++cnt; + } + + return cnt + 1; +} -- cgit v1.2.3