-
Notifications
You must be signed in to change notification settings - Fork 10
/
devfs.c
95 lines (73 loc) · 1.89 KB
/
devfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <kernel.h>
#include <list.h>
static list_t* dev_nodes = NULL;
static uint32_t dev_files = 0;
static fs_node_t* devfs_root_node = NULL;
static uint8_t
_devfs_find(list_t* entry, void* udata) {
if( !strcmp(((fs_node_t*)(entry->data))->name, (uint8_t*)udata) )
return FALSE;
else
return TRUE;
}
fs_node_t*
devfs_finddir(fs_node_t *node, char *name) {
uint32_t i;
list_t* entry;
fs_node_t* dev_node = NULL;
if( name == NULL )
return NULL;
if( node == devfs_root_node ) {
entry = list_foreach(dev_nodes, _devfs_find, name);
if( entry ) {
dev_node = (fs_node_t*)entry->data;
}
}
return dev_node;
}
struct dirent*
devfs_readdir(fs_node_t *node, uint32_t index) {
struct dirent* d = NULL;
fs_node_t* dev_node;
list_t* entry;
if( index >= dev_files)
return NULL;
entry = list_get(dev_nodes, index);
dev_node = (fs_node_t*)entry->data;
if( dev_node ) {
d = (struct dirent*)malloc(sizeof(struct dirent));
if( d == NULL )
return NULL;
strcpy(d->name,dev_node->name);
d->inode = (uint32_t)dev_node;
d->offset = index;
d->flags = dev_node->flags;
}
return d;
}
void
devfs_register(fs_node_t* node) {
//TODO: check if node->name ahead exists
dev_nodes = list_add(dev_nodes, node);
printf("devfs: '/dev/%s' created.\n", node->name);
dev_files++;
}
fs_node_t*
devfs() {
devfs_root_node = (fs_node_t*)malloc(sizeof(fs_node_t));
strcpy(devfs_root_node->name, "dev");
devfs_root_node->flags = FS_DIRECTORY;
devfs_root_node->read = 0;
devfs_root_node->write = 0;
devfs_root_node->open = 0;
devfs_root_node->close = 0;
devfs_root_node->readdir = &devfs_readdir;
devfs_root_node->finddir = &devfs_finddir;
dev_one();
dev_zero();
dev_rand();
//dev_tty();
printf("/dev initialized.\n");
fs_register(devfs_root_node);
return devfs_root_node;
}