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
|
#include "../lib/io/io.h"
#include "../lib/sys/mkdir.h"
#include "../lib/sys/creat.h"
#include "../lib/cstr/cstr.h"
#include "../lib/sys/mknod.h"
#include "../lib/sys/errno.h"
#include "../lib/sys/stat.h"
enum {
FILE,
DIRECTORY,
BLOCK_OR_CHARACTER,
NUMBER_OF_MODES,
};
int(*new_funcs[NUMBER_OF_MODES])(const char *, unsigned int) = {
&creat,
&mkdir
};
int new_block_or_character(int argc, char **argv)
{
if (argc != 4) {
wff(STDERR_FD, "new -n <name> <type> <major> <major>");
return -1;
}
u32 major = cstr_to_u64(argv[2]);
u32 minor = cstr_to_u64(argv[3]);
u32 type = argv[1][0] == 'c' ? S_IFCHR : S_IFBLK;
int err;
if ((err = mknod(argv[0], MODE_USER_READ | MODE_USER_WRITE | type, device(major, minor))) < 0) {
wff(STDERR_FD, "%s\n", errstr[-err]);
return -1;
}
return 0;
}
int main(int argc, char **argv)
{
if (argc < 3) {
wff(STDERR_FD, "new [[-f | -d] <name> ... | -n <name> <type> <major> <minor>]\n");
return -1;
}
int type = FILE;
if (cstr_length(argv[1]) != 2) {
wff(STDERR_FD, "error: unknown entry type.\n");
return -1;
}
switch(argv[1][1]) {
case 'f': type = FILE; break;
case 'd': type = DIRECTORY; break;
case 'n': return new_block_or_character(argc - 2, argv + 2);
default: wff(STDERR_FD, "error: unknown entry type.\n"); return -1;
}
for (int i = 2; i < argc; ++i) {
new_funcs[type](argv[i], MODE_USER_READ | MODE_USER_WRITE);
}
return 0;
}
|