fs Add tree subcommand

This commit is contained in:
2025-10-04 12:02:03 +02:00
parent 082cb66c7e
commit ef9393e694
8 changed files with 143 additions and 2 deletions

View File

@ -15,6 +15,7 @@ SRCFILES := $(call GRABSRC, \
util \
ubsan \
umalloc \
fs \
)
CFLAGS += -isystem $(ROOT)/share -isystem $(ROOT)/ulib -isystem $(ROOT)/std/include \

60
ulib/fs/path.c Normal file
View File

@ -0,0 +1,60 @@
#include <fs/path.h>
#include <string/string.h>
void path_parse(const char *in, char *mp, char *path) {
if (in == 0 || *in == 0) {
mp[0] = 0;
path[0] = 0;
return;
}
int i = 0, j = 0, colonfound = 0;
while (in[i]) {
if (in[i] == ':') {
if (colonfound) {
mp[0] = 0;
path[0] = 0;
return;
}
colonfound = 1;
mp[i] = 0;
i++;
continue;
}
if (!colonfound) {
mp[i] = in[i];
} else {
path[j++] = in[i];
}
i++;
}
if (!colonfound) {
mp[i] = 0;
path[0] = 0;
} else {
path[j] = 0;
}
}
const char *path_basename(const char *path) {
if (!path) {
return NULL;
}
const char *aftercolon = string_strchr(path, ':');
if (aftercolon) {
path = aftercolon + 1;
}
const char *lastslash = NULL;
for (const char *p = path; *p; p++) {
if (*p == '/') {
lastslash = p;
}
}
return lastslash ? lastslash + 1 : path;
}

7
ulib/fs/path.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef ULIB_FS_PATH_H_
#define ULIB_FS_PATH_H_
void path_parse(const char *in, char *mp, char *path);
const char *path_basename(const char *path);
#endif // ULIB_FS_PATH_H_

View File

@ -172,3 +172,14 @@ char *string_tokenizealloc(char *s, char *delim) {
return w;
}
// https://stackoverflow.com/questions/2488563/strcat-implementation
char *string_combine(char *dest, const char *src) {
size_t i, j;
for(i = 0; dest[i] != '\0'; i++);
for(j = 0; src[j] != '\0'; j++) {
dest[i+j] = src[j];
}
dest[i+j] = '\0';
return dest;
}

View File

@ -17,6 +17,7 @@ char *string_strcpy(char *dest, const char *src);
char *string_strchr(const char *s, int c);
int string_strncmp(const char * s1, const char * s2, size_t n);
char *string_tokenizealloc(char *s, char *delim);
char *string_combine(char *dest, const char *src);
#endif

View File

@ -17,6 +17,7 @@
#include <assert.h>
#include <umalloc/umalloc.h>
#include <dev.h>
#include <fs/path.h>
#include <errors.h>
#include <sysdefs/ioctl.h>