forked from ish-app/ish
-
Notifications
You must be signed in to change notification settings - Fork 12
/
xX_main_Xx.h
63 lines (58 loc) · 1.64 KB
/
xX_main_Xx.h
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
#include <signal.h>
#include <unistd.h>
#include "kernel/init.h"
#include "kernel/fs.h"
static void exit_handler(int code) {
if (code & 0xff)
raise(code & 0xff);
else
exit(code >> 8);
}
// this function parses command line arguments and initializes global
// data structures. thanks programming discussions discord server for the name.
// https://discord.gg/9zT7NHP
static inline int xX_main_Xx(int argc, char *const argv[], char *const *envp) {
// parse cli options
int opt;
const char *root = "";
bool has_root = false;
const struct fs_ops *fs = &realfs;
while ((opt = getopt(argc, argv, "+r:f:")) != -1) {
switch (opt) {
case 'r':
case 'f':
root = optarg;
has_root = true;
if (opt == 'f')
fs = &fakefs;
break;
}
}
char root_realpath[MAX_PATH + 1] = "/";
if (has_root && realpath(root, root_realpath) == NULL) {
perror(root);
exit(1);
}
if (fs == &fakefs)
strcat(root_realpath, "/data");
int err = mount_root(fs, root_realpath);
if (err < 0)
return err;
create_first_process();
if (!has_root) {
char cwd[MAX_PATH + 1];
getcwd(cwd, sizeof(cwd));
struct fd *pwd = generic_open(cwd, O_RDONLY_, 0);
fs_chdir(current->fs, pwd);
}
if (envp == NULL)
envp = (char *[]) {NULL};
err = sys_execve(argv[optind], argv + optind, envp);
if (err < 0)
return err;
err = create_stdio(real_tty_driver);
if (err < 0)
return err;
exit_hook = exit_handler;
return 0;
}