-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lxfs: stub for parsing directory tree
- Loading branch information
1 parent
0c85003
commit 88926f9
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* luxOS - a unix-like operating system | ||
* Omar Elghoul, 2024 | ||
* | ||
* lxfs: Driver for the lxfs file system | ||
*/ | ||
|
||
#include <lxfs/lxfs.h> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
#include <errno.h> | ||
|
||
/* pathDepth(): helper function to calculate the depth of a path | ||
* params: path - full qualified path | ||
* returns: maximum depth of the path | ||
*/ | ||
|
||
int pathDepth(const char *path) { | ||
if(!path || !strlen(path) || strlen(path) == 1) return 0; | ||
|
||
int c = 0; | ||
for(int i = 0; i < strlen(path); i++) { | ||
if(path[i] == '/') c++; | ||
} | ||
|
||
return c; | ||
} | ||
|
||
/* pathComponent(): helper function to return the nth component of a path | ||
* params: dest - destination buffer | ||
* params: path - full qualified path | ||
* params: n - component number | ||
* returns: pointer to destination on success, NULL on fail | ||
*/ | ||
|
||
char *pathComponent(char *dest, const char *path, int n) { | ||
int depth = pathDepth(path); | ||
if(n > depth) return NULL; | ||
|
||
int c = 0; | ||
int len = 0; | ||
for(int i = 0; i < strlen(path); i++) { | ||
if(path[i] == '/') c++; | ||
if(c > n) { | ||
i++; | ||
while(path[i] && (path[i] != '/')) { | ||
dest[len] = path[i]; | ||
len++; | ||
i++; | ||
} | ||
|
||
dest[len] = 0; | ||
return dest; | ||
} | ||
} | ||
|
||
return NULL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters