Compare commits
No commits in common. "355dd6c32b775af09d9c4a2170b32cbc32de9d6b" and "00672c4b221995cc0125df5ec5a4d705fd9955aa" have entirely different histories.
355dd6c32b
...
00672c4b22
@ -7,4 +7,4 @@ function(luna_app SOURCE_FILE APP_NAME)
|
|||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
luna_app(init.c init)
|
luna_app(init.c init)
|
||||||
luna_app(sh.c sh)
|
luna_app(hello.c hello)
|
||||||
|
12
apps/hello.c
Normal file
12
apps/hello.c
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
printf("Hello world! argc=%d, argv[0]=%s, argv[1]=%s, argv[2]=%s\n", argc, argv[0], argv[1], argv[2]);
|
||||||
|
|
||||||
|
char buf[1024];
|
||||||
|
char* rc = fgets(buf, sizeof(buf), stdin);
|
||||||
|
if (!rc) perror("fgets");
|
||||||
|
|
||||||
|
fputs(buf, stdout);
|
||||||
|
}
|
12
apps/init.c
12
apps/init.c
@ -36,16 +36,14 @@ int main()
|
|||||||
stdout = fopen("/dev/console", "w");
|
stdout = fopen("/dev/console", "w");
|
||||||
stderr = fopen("/dev/console", "w");
|
stderr = fopen("/dev/console", "w");
|
||||||
|
|
||||||
|
printf("init is running as PID %d\n", getpid());
|
||||||
|
|
||||||
pid_t ret = fork();
|
pid_t ret = fork();
|
||||||
|
|
||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
{
|
{
|
||||||
char* argv[] = { "/bin/sh", NULL };
|
char* argv[] = { "/bin/hello", "--help", NULL };
|
||||||
execv(argv[0], argv);
|
execv("/bin/hello", argv);
|
||||||
perror("execv");
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
else { printf("my child is PID %d!\n", ret); }
|
||||||
while (1)
|
|
||||||
;
|
|
||||||
}
|
}
|
||||||
|
100
apps/sh.c
100
apps/sh.c
@ -1,100 +0,0 @@
|
|||||||
#include <errno.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
static char** split_command_into_argv(const char* cmd)
|
|
||||||
{
|
|
||||||
size_t argc = 1;
|
|
||||||
char* str = strdup(cmd);
|
|
||||||
char** arr = calloc(sizeof(char*), argc);
|
|
||||||
|
|
||||||
char* segment = strtok(str, " \n");
|
|
||||||
arr[argc - 1] = segment;
|
|
||||||
if (segment == NULL) return arr;
|
|
||||||
|
|
||||||
argc++;
|
|
||||||
arr = realloc(arr, sizeof(char*) * argc);
|
|
||||||
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
char* segment = strtok(NULL, " \n");
|
|
||||||
arr[argc - 1] = segment;
|
|
||||||
|
|
||||||
if (segment == NULL) break;
|
|
||||||
|
|
||||||
argc++;
|
|
||||||
arr = realloc(arr, sizeof(char*) * argc);
|
|
||||||
}
|
|
||||||
|
|
||||||
return arr;
|
|
||||||
}
|
|
||||||
|
|
||||||
static char* join_path(const char* str1, const char* str2)
|
|
||||||
{
|
|
||||||
char* buf = malloc(strlen(str1) + strlen(str2) + 1);
|
|
||||||
strlcpy(buf, str1, strlen(str1) + 1);
|
|
||||||
strncat(buf, str2, strlen(str2));
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int execute_in_path(const char* dir, char** argv)
|
|
||||||
{
|
|
||||||
char* path = join_path(dir, argv[0]);
|
|
||||||
int err = errno;
|
|
||||||
|
|
||||||
int status = execv(path, argv);
|
|
||||||
free(path);
|
|
||||||
|
|
||||||
if (errno == ENOENT)
|
|
||||||
{
|
|
||||||
errno = err;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int sh_execvp(char** argv)
|
|
||||||
{
|
|
||||||
if (strchr(argv[0], '/'))
|
|
||||||
{
|
|
||||||
// Relative paths do not need path resolution.
|
|
||||||
return execv(argv[0], argv);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (execute_in_path("/bin/", argv) != 0) return -1;
|
|
||||||
if (execute_in_path("/sbin/", argv) != 0) return -1;
|
|
||||||
if (execute_in_path("/usr/bin/", argv) != 0) return -1;
|
|
||||||
if (execute_in_path("/usr/local/bin/", argv) != 0) return -1;
|
|
||||||
|
|
||||||
errno = ENOENT;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
fputs("sh$ ", stdout);
|
|
||||||
|
|
||||||
char command[4096];
|
|
||||||
fgets(command, sizeof(command), stdin);
|
|
||||||
|
|
||||||
pid_t child = fork();
|
|
||||||
if (child < 0) { perror("fork"); }
|
|
||||||
|
|
||||||
if (child == 0)
|
|
||||||
{
|
|
||||||
char** argv = split_command_into_argv(command);
|
|
||||||
sh_execvp(argv);
|
|
||||||
perror(argv[0]);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Don't have sched_yield() or waitpid() yet...
|
|
||||||
sleep(1);
|
|
||||||
}
|
|
||||||
}
|
|
@ -164,7 +164,6 @@ template <typename T> class Vector
|
|||||||
{
|
{
|
||||||
m_size = m_capacity = 0;
|
m_size = m_capacity = 0;
|
||||||
free_impl(m_data);
|
free_impl(m_data);
|
||||||
m_data = nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Loading…
Reference in New Issue
Block a user