[vfs] Working directories, I hope

This commit is contained in:
Kevin Lange 2011-11-17 15:55:59 -06:00
parent 65087e8a4e
commit 20d89355dc
6 changed files with 71 additions and 17 deletions

View File

@ -107,6 +107,52 @@ kprintf(
}
}
int
sprintf(
char * buf,
const char *fmt,
...
) {
int i = 0;
char *s;
va_list args;
va_start(args, fmt);
ptr = 0;
for ( ; fmt[i]; ++i) {
if (fmt[i] != '%') {
buf[ptr++] = fmt[i];
continue;
}
/* fmt[i] == '%' */
switch (fmt[++i]) {
case 's': /* String pointer -> String */
s = (char *)va_arg(args, char *);
while (*s) {
buf[ptr++] = *s++;
}
break;
case 'c': /* Single character */
buf[ptr++] = (char)va_arg(args, int);
break;
case 'x': /* Hexadecimal number */
parse_hex((unsigned long)va_arg(args, unsigned long));
break;
case 'd': /* Decimal number */
parse_num((unsigned long)va_arg(args, unsigned long), 10);
break;
case '%': /* Escape */
buf[ptr++] = '%';
break;
default: /* Nothing at all, just dump it */
buf[ptr++] = fmt[i];
break;
}
}
/* Ensure the buffer ends in a null */
buf[ptr] = '\0';
return ptr;
}
/*
* gets() implementation for the kernel
*/

View File

@ -111,10 +111,11 @@ start_shell() {
kprintf("cd: could not change directory\n");
}
}
for (uint32_t i = 0; i <= strlen(path); ++i) {
current_task->wd[i] = path[i];
}
}
}
} else if (!strcmp(cmd, "pwd")) {
kprintf("%d %s\n", strlen(path), path);
} else if (!strcmp(cmd, "ls")) {
/*
* List the files in the current working directory
@ -187,18 +188,11 @@ start_shell() {
} else {
/* Alright, here we go */
char * filename = malloc(sizeof(char) * 1024);
fs_node_t * chd = NULL;
if (argv[0][0] == '/') {
memcpy(filename, argv[0], strlen(argv[0]) + 1);
} else {
memcpy(filename, path, strlen(path));
if (!strcmp(path,"/")) {
memcpy((void *)((uintptr_t)filename + strlen(path)),argv[0],strlen(argv[0])+1);
} else {
filename[strlen(path)] = '/';
memcpy((void *)((uintptr_t)filename + strlen(path) + 1),argv[0],strlen(argv[0])+1);
}
chd = kopen(filename, 0);
}
fs_node_t * chd = kopen(filename, 0);
if (!chd) {
/* Alright, let's try this... */
char * search_path = "/bin/";

View File

@ -113,7 +113,7 @@ memsetw(
* strlen
* Returns the length of a given `str`.
*/
int
uint32_t
strlen(
const char *str
) {

View File

@ -75,6 +75,8 @@ tasking_install() {
current_task->descriptors = (fs_node_t **)kmalloc(sizeof(fs_node_t *) * 1024);
current_task->next_fd = 0;
current_task->wd[0] = '/';
current_task->wd[1] = 0;
//switch_page_directory(current_task->page_directory);
@ -115,6 +117,9 @@ fork() {
new_task->heap = current_task->heap;
new_task->heap_a = current_task->heap_a;
new_task->image_size = current_task->image_size;
for (uint32_t i = 0; i <= strlen(current_task->wd); ++i) {
new_task->wd[i] = current_task->wd[i];
}
task_t * tmp_task = (task_t *)ready_queue;
new_task->parent = parent;

View File

@ -61,18 +61,25 @@ kopen(
const char *filename,
uint32_t flags
) {
char * cwd = (char *)&(current_task->wd);
/* Some sanity checks */
if (!fs_root || !filename || filename[0] != '/') {
if (!fs_root || !filename) { //|| filename[0] != '/') {
return NULL;
}
size_t path_len = strlen(filename);
char npath[1024];
if (filename[0] != '/') {
sprintf(npath, "%s/%s", cwd, filename);
} else {
sprintf(npath, "%s", filename);
}
size_t path_len = strlen(npath);
if (path_len == 1) {
fs_node_t * root_clone = malloc(sizeof(fs_node_t));
memcpy(root_clone, fs_root, sizeof(fs_node_t));
return root_clone;
}
char * path = (char *)malloc(sizeof(char) * (path_len + 1));
memcpy(path, filename, path_len);
memcpy(path, npath, path_len);
char * path_offset = path;
uint32_t path_depth = 0;
while (path_offset < path + path_len) {

View File

@ -33,7 +33,7 @@ extern void *memcpy(void *restrict dest, const void *restrict src, size_t count)
extern void *memmove(void *restrict dest, const void *restrict src, size_t count);
extern void *memset(void *dest, int val, size_t count);
extern unsigned short *memsetw(unsigned short *dest, unsigned short val, int count);
extern int strlen(const char *str);
extern uint32_t strlen(const char *str);
extern int atoi(const char *str);
extern unsigned char inportb(unsigned short _port);
extern void outportb(unsigned short _port, unsigned char _data);
@ -123,7 +123,8 @@ extern void mouse_install();
/* kprintf */
extern void kprintf(const char *fmt, ...);
extern int kgets(char *buf, int size);
extern int sprintf(char *buf, const char *fmt, ...);
extern int kgets(char *buf, int size);
/* Memory Management */
extern uintptr_t placement_pointer;
@ -203,6 +204,7 @@ typedef struct task {
uintptr_t entry;
uintptr_t heap;
uintptr_t heap_a;
char wd[1024];
} task_t;
extern __volatile__ task_t * current_task;