diff --git a/apps/cat.cpp b/apps/cat.cpp index ee506ddc..e4799b5c 100644 --- a/apps/cat.cpp +++ b/apps/cat.cpp @@ -31,7 +31,7 @@ static void do_cat(StringView path) if (f != stdin) fclose(f); } -int main(int argc, char** argv) +Result luna_main(int argc, char** argv) { StringView filename; @@ -42,4 +42,6 @@ int main(int argc, char** argv) do_cat(filename); for (auto file : extra_files) { do_cat(file); } + + return 0; } diff --git a/apps/edit.cpp b/apps/edit.cpp index 0871047f..5c7ff17a 100644 --- a/apps/edit.cpp +++ b/apps/edit.cpp @@ -5,7 +5,7 @@ #include #include -int main(int argc, char** argv) +Result luna_main(int argc, char** argv) { StringView pathname; @@ -13,7 +13,7 @@ int main(int argc, char** argv) parser.add_positional_argument(pathname, "path"_sv, true); parser.parse(argc, argv); - auto file = os::File::open_or_create(pathname, os::File::WriteOnly).release_value(); + auto file = TRY(os::File::open_or_create(pathname, os::File::WriteOnly)); char buffer[4096]; @@ -21,7 +21,7 @@ int main(int argc, char** argv) { char* rc = fgets(buffer, sizeof(buffer), stdin); if (rc == 0) break; - file->write(StringView { buffer }); + TRY(file->write(StringView { buffer })); } return 0; diff --git a/apps/ls.cpp b/apps/ls.cpp index 72facc9d..2b848022 100644 --- a/apps/ls.cpp +++ b/apps/ls.cpp @@ -5,7 +5,7 @@ #include #include -int main(int argc, char** argv) +Result luna_main(int argc, char** argv) { StringView pathname; bool show_all { false }; diff --git a/apps/rm.cpp b/apps/rm.cpp index 2fb33283..c122cbbe 100644 --- a/apps/rm.cpp +++ b/apps/rm.cpp @@ -1,7 +1,7 @@ #include #include -int main(int argc, char** argv) +Result luna_main(int argc, char** argv) { StringView path; @@ -14,4 +14,6 @@ int main(int argc, char** argv) perror("rm"); return 1; } + + return 0; } diff --git a/apps/sh.cpp b/apps/sh.cpp index 09fcf06c..f3043d98 100644 --- a/apps/sh.cpp +++ b/apps/sh.cpp @@ -47,7 +47,7 @@ static Result> split_command_into_argv(const char* cmd) exit(1); } -int main(int argc, char** argv) +Result luna_main(int argc, char** argv) { StringView file; StringView command; diff --git a/apps/su.cpp b/apps/su.cpp index 8f518801..f699dc0e 100644 --- a/apps/su.cpp +++ b/apps/su.cpp @@ -51,7 +51,7 @@ char* getpass() return buf; } -int main(int argc, char** argv) +Result luna_main(int argc, char** argv) { StringView name; @@ -92,4 +92,6 @@ int main(int argc, char** argv) chdir(entry->pw_dir); execl(entry->pw_shell, entry->pw_shell, NULL); + + return 1; } diff --git a/libos/CMakeLists.txt b/libos/CMakeLists.txt index 00d2f22a..504de652 100644 --- a/libos/CMakeLists.txt +++ b/libos/CMakeLists.txt @@ -6,6 +6,7 @@ set(SOURCES ${HEADERS} src/ArgumentParser.cpp src/File.cpp + src/Main.cpp ) add_library(os ${SOURCES}) diff --git a/libos/include/os/Main.h b/libos/include/os/Main.h new file mode 100644 index 00000000..a53f9941 --- /dev/null +++ b/libos/include/os/Main.h @@ -0,0 +1,4 @@ +#pragma once +#include + +Result luna_main(int argc, char** argv); diff --git a/libos/src/Main.cpp b/libos/src/Main.cpp new file mode 100644 index 00000000..c14548ed --- /dev/null +++ b/libos/src/Main.cpp @@ -0,0 +1,23 @@ +#include + +#include +#include + +int g_argc; +char** g_argv; + +__attribute__((weak)) int main(int argc, char** argv) +{ + g_argc = argc; + g_argv = argv; + + int result; + bool ok = luna_main(argc, argv).try_set_value_or_error(result, errno); + if (!ok) + { + perror(g_argv[0]); + return 1; + } + + return result; +}