From 2c813f5901329d2fabaed52a774cbf94fcc5a561 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 19 Oct 2022 21:11:38 +0200 Subject: [PATCH] apps: Add more simple apps Now that we can start them at will from the command line, bring them on!! --- apps/Makefile | 2 +- apps/src/crash.c | 5 +++++ apps/src/init.c | 41 +---------------------------------------- apps/src/uname.c | 26 ++++++++++++++++++++++++++ apps/src/uptime.c | 29 +++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 41 deletions(-) create mode 100644 apps/src/crash.c create mode 100644 apps/src/uname.c create mode 100644 apps/src/uptime.c diff --git a/apps/Makefile b/apps/Makefile index f3910471..384ce943 100644 --- a/apps/Makefile +++ b/apps/Makefile @@ -1,4 +1,4 @@ -APPS := init sym sh +APPS := init sym sh crash uname uptime APPS_DIR := $(LUNA_ROOT)/apps APPS_SRC := $(APPS_DIR)/src diff --git a/apps/src/crash.c b/apps/src/crash.c new file mode 100644 index 00000000..27e5ea9b --- /dev/null +++ b/apps/src/crash.c @@ -0,0 +1,5 @@ +int main() +{ + int* ptr = (int*)0xdeadbeef; + *ptr = 6; +} \ No newline at end of file diff --git a/apps/src/init.c b/apps/src/init.c index 3d94f2dc..41ee2020 100644 --- a/apps/src/init.c +++ b/apps/src/init.c @@ -1,43 +1,8 @@ #include -#include #include -#include -#include -#include #include #include -int print_version() -{ - char version[4096]; - - FILE* fp = fopen("/dev/version", "r"); - if (!fp) - { - perror("fopen"); - return 1; - } - - size_t nread = fread(version, 4096, 1, fp); - if (ferror(fp)) - { - perror("fread"); - return 1; - } - - version[nread] = 0; - - if (fclose(fp) < 0) - { - perror("fclose"); - return 1; - } - - printf("Your kernel version is %s\n\n", version); - - return 0; -} - int main() { if (getpid() != 1) @@ -46,11 +11,7 @@ int main() return 1; } - printf("Welcome to Luna!\n"); - - printf("Running as PID %ld\n", getpid()); - - if (print_version()) return 1; + printf("Welcome to Luna!\n\n"); msleep(200); diff --git a/apps/src/uname.c b/apps/src/uname.c new file mode 100644 index 00000000..c8a13386 --- /dev/null +++ b/apps/src/uname.c @@ -0,0 +1,26 @@ +#include + +int main() +{ + FILE* fp = fopen("/dev/version", "r"); + if (!fp) + { + perror("fopen"); + return 1; + } + + char buf[4096]; + size_t nread = fread(buf, sizeof(buf) - 1, 1, fp); + + if (ferror(fp)) + { + perror("fread"); + return 1; + } + + buf[nread] = 0; // null terminate it :) + + printf("%s\n", buf); + + fclose(fp); +} \ No newline at end of file diff --git a/apps/src/uptime.c b/apps/src/uptime.c new file mode 100644 index 00000000..4ee27daa --- /dev/null +++ b/apps/src/uptime.c @@ -0,0 +1,29 @@ +#include +#include + +int main() +{ + FILE* fp = fopen("/dev/uptime", "r"); + if (!fp) + { + perror("fopen"); + return 1; + } + + char buf[4096]; + size_t nread = fread(buf, sizeof(buf) - 1, 1, fp); + + if (ferror(fp)) + { + perror("fread"); + return 1; + } + + buf[nread] = 0; // null terminate it :) + + long ms_uptime = atol(buf); + + printf("up for %ld seconds\n", ms_uptime / 1000); + + fclose(fp); +} \ No newline at end of file