summaryrefslogtreecommitdiff
path: root/src/sys/os/vfs_subr.c
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-09-19 00:42:14 -0400
committerIan Moffett <ian@osmora.org>2025-09-19 00:42:14 -0400
commit6a0e4e3da5cf17635d094008c7cc5562b1f081e1 (patch)
tree95dfc7821f205d1658d63b769457018431904160 /src/sys/os/vfs_subr.c
parent70dc7ad8924cf3e30e06b02698b1294fe9553538 (diff)
kern: vfs: Add path component counter
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'src/sys/os/vfs_subr.c')
-rw-r--r--src/sys/os/vfs_subr.c66
1 files changed, 66 insertions, 0 deletions
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,9 +31,34 @@
#include <sys/errno.h>
#include <os/vnode.h>
#include <os/kalloc.h>
+#include <os/vfs.h>
#include <string.h>
/*
+ * 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
*/
int
@@ -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;
+}