string: reword strtok
This commit is contained in:
parent
25a828437a
commit
08b54c22f5
6 changed files with 24 additions and 24 deletions
|
|
@ -136,9 +136,8 @@ int ext2_open_file(void *extfs, struct file *file, const char *path)
|
||||||
|
|
||||||
strcpy(path_copy, path);
|
strcpy(path_copy, path);
|
||||||
|
|
||||||
spinlock_acquire(&strtok_lock);
|
char *strtok_last = NULL;
|
||||||
|
char *token = strtok(path_copy, "/", &strtok_last);
|
||||||
char *token = strtok(path_copy, "/");
|
|
||||||
uint32_t inode_no = 2;
|
uint32_t inode_no = 2;
|
||||||
while (token) {
|
while (token) {
|
||||||
struct ext2_dir_entry *entry = kmalloc(fs->block_size, ALLOC_DMA);
|
struct ext2_dir_entry *entry = kmalloc(fs->block_size, ALLOC_DMA);
|
||||||
|
|
@ -182,10 +181,9 @@ int ext2_open_file(void *extfs, struct file *file, const char *path)
|
||||||
|
|
||||||
ext2_read_inode(fs, inode, entry->inode);
|
ext2_read_inode(fs, inode, entry->inode);
|
||||||
|
|
||||||
token = strtok(NULL, "/");
|
token = strtok(NULL, "/", &strtok_last);
|
||||||
}
|
}
|
||||||
|
|
||||||
spinlock_release(&strtok_lock);
|
|
||||||
|
|
||||||
kfree(path_copy);
|
kfree(path_copy);
|
||||||
|
|
||||||
|
|
|
||||||
20
fs/vfs.c
20
fs/vfs.c
|
|
@ -7,31 +7,37 @@
|
||||||
|
|
||||||
#include <fs/vfs.h>
|
#include <fs/vfs.h>
|
||||||
|
|
||||||
struct rbtree *kfd;
|
static struct rbtree *kfd;
|
||||||
|
static struct fs *rootfs;
|
||||||
|
|
||||||
typedef struct fs *(*vfs_init_t)(struct block_device *);
|
typedef struct fs *(*vfs_init_t)(struct block_device *);
|
||||||
|
|
||||||
void vfs_init(const char *rootfs)
|
void vfs_init(const char *rootdev_name)
|
||||||
{
|
{
|
||||||
kfd = kzalloc(sizeof(struct rbtree), ALLOC_DMA);
|
kfd = kzalloc(sizeof(struct rbtree), ALLOC_DMA);
|
||||||
|
|
||||||
/* mount root */
|
/* mount root */
|
||||||
struct block_device *rootdev = block_get_device(rootfs);
|
struct block_device *rootdev = block_get_device(rootdev_name);
|
||||||
if (!rootdev) {
|
if (!rootdev) {
|
||||||
kprintf(LOG_ERROR "Failed to find root device %s\n", rootfs);
|
kprintf(LOG_ERROR "Failed to find root device %s\n", rootdev_name);
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
kprintf(LOG_DEBUG "Found root device %s\n", rootfs);
|
kprintf(LOG_DEBUG "Found root device %s\n", rootdev_name);
|
||||||
|
|
||||||
/* TODO: detect filesystem type */
|
/* TODO: detect filesystem type */
|
||||||
/* for now, assume ext2 */
|
/* for now, assume ext2 */
|
||||||
struct fs *fs = ext2_init_fs(rootdev);
|
struct fs *fs = ext2_init_fs(rootdev);
|
||||||
if (!fs) {
|
if (!fs) {
|
||||||
kprintf(LOG_ERROR "Failed to mount root device %s\n", rootfs);
|
kprintf(LOG_ERROR "Failed to mount root device %s\n", rootdev_name);
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
kprintf(LOG_DEBUG "Mounted root device %s\n", rootfs);
|
kprintf(LOG_DEBUG "Mounted root device %s\n", rootdev_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fs->mount_point = kzalloc(2, ALLOC_KERN);
|
||||||
|
fs->mount_point[0] = '/';
|
||||||
|
|
||||||
|
rootfs = fs;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,8 @@ struct fs {
|
||||||
void *fs;
|
void *fs;
|
||||||
uint32_t type;
|
uint32_t type;
|
||||||
struct fs_ops ops;
|
struct fs_ops ops;
|
||||||
|
|
||||||
|
char *mount_point;
|
||||||
};
|
};
|
||||||
|
|
||||||
void vfs_init(const char *rootfs);
|
void vfs_init(const char *rootfs);
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ int strncmp(const char *a, const char *b, size_t n);
|
||||||
void *memset(void *str, int c, size_t n);
|
void *memset(void *str, int c, size_t n);
|
||||||
char *strcpy(char *dest, const char *src);
|
char *strcpy(char *dest, const char *src);
|
||||||
char *strncpy(char *dest, const char *src, size_t n);
|
char *strncpy(char *dest, const char *src, size_t n);
|
||||||
char *strtok(char *str, const char *delim);
|
char *strtok(char *str, const char *delim, char **llast);
|
||||||
char **strsplit(const char *str, char delim, int *num);
|
char **strsplit(const char *str, char delim, int *num);
|
||||||
size_t strlen(const char *c);
|
size_t strlen(const char *c);
|
||||||
int atoi(const char *a);
|
int atoi(const char *a);
|
||||||
|
|
|
||||||
10
init/main.c
10
init/main.c
|
|
@ -64,37 +64,33 @@ char *kcmdline_get_symbol(const char *sym)
|
||||||
size_t sym_len = strlen(sym);
|
size_t sym_len = strlen(sym);
|
||||||
size_t cmd_len = strlen(kcmdline);
|
size_t cmd_len = strlen(kcmdline);
|
||||||
|
|
||||||
spinlock_acquire(&strtok_lock);
|
|
||||||
|
|
||||||
char *cmdline_copy = kmalloc(cmd_len + 1, ALLOC_KERN);
|
char *cmdline_copy = kmalloc(cmd_len + 1, ALLOC_KERN);
|
||||||
if (!cmdline_copy) {
|
if (!cmdline_copy) {
|
||||||
spinlock_release(&strtok_lock);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
strcpy(cmdline_copy, kcmdline);
|
strcpy(cmdline_copy, kcmdline);
|
||||||
|
|
||||||
char *token = strtok(cmdline_copy, " ");
|
char *strtok_last = NULL;
|
||||||
|
char *token = strtok(cmdline_copy, " ", &strtok_last);
|
||||||
while (token) {
|
while (token) {
|
||||||
if (!strncmp(token, sym, sym_len)) {
|
if (!strncmp(token, sym, sym_len)) {
|
||||||
size_t ret_len = strlen(token + sym_len + 1);
|
size_t ret_len = strlen(token + sym_len + 1);
|
||||||
char *ret = kzalloc(ret_len + 1, ALLOC_KERN);
|
char *ret = kzalloc(ret_len + 1, ALLOC_KERN);
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
kfree(cmdline_copy);
|
kfree(cmdline_copy);
|
||||||
spinlock_release(&strtok_lock);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
strcpy(ret, token + sym_len + 1);
|
strcpy(ret, token + sym_len + 1);
|
||||||
|
|
||||||
kfree(cmdline_copy);
|
kfree(cmdline_copy);
|
||||||
spinlock_release(&strtok_lock);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
token = strtok(NULL, " ");
|
token = strtok(NULL, " ", &strtok_last);
|
||||||
}
|
}
|
||||||
|
|
||||||
spinlock_release(&strtok_lock);
|
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,6 @@
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
spinlock_t strtok_lock = 0;
|
|
||||||
|
|
||||||
void *memcpy(void *dest, const void *src, size_t num)
|
void *memcpy(void *dest, const void *src, size_t num)
|
||||||
{
|
{
|
||||||
char *d = (char *)dest;
|
char *d = (char *)dest;
|
||||||
|
|
@ -122,9 +120,9 @@ char *strchr(const char *s, int c)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *strtok(char *str, const char *delim)
|
char *strtok(char *str, const char *delim, char **llast)
|
||||||
{
|
{
|
||||||
static char *last = NULL;
|
char *last = *llast;
|
||||||
if (str)
|
if (str)
|
||||||
last = str;
|
last = str;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue