From 8eb986df639f9ba7b3127619eabdb59e17612de4 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 26 Oct 2022 19:17:05 +0200 Subject: [PATCH] libc: Add program_invocation_name This is a GNU extension, but I'm fine with adding it to libc. It's guarded by the _GNU_SOURCE feature test macro anyways. --- libs/libc/include/errno.h | 5 +++++ libs/libc/src/errno.cpp | 3 ++- libs/libc/src/init.cpp | 7 ++++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/libs/libc/include/errno.h b/libs/libc/include/errno.h index dca6d19e..239b0c42 100644 --- a/libs/libc/include/errno.h +++ b/libs/libc/include/errno.h @@ -28,4 +28,9 @@ extern int errno; #define ENOTSUP 95 // Operation not supported #define EOPNOTSUPP 95 // Operation not supported +#ifdef _GNU_SOURCE // Give it only to programs that ask for it. +/* Name used to invoke calling program. Same value as argv[0] in main(), but can be used globally. */ +extern char* program_invocation_name; +#endif + #endif \ No newline at end of file diff --git a/libs/libc/src/errno.cpp b/libs/libc/src/errno.cpp index 434ba1ab..ccd33dd5 100644 --- a/libs/libc/src/errno.cpp +++ b/libs/libc/src/errno.cpp @@ -1,3 +1,4 @@ #include -int errno; \ No newline at end of file +int errno; +char* program_invocation_name; \ No newline at end of file diff --git a/libs/libc/src/init.cpp b/libs/libc/src/init.cpp index 330ca048..b2a92c25 100644 --- a/libs/libc/src/init.cpp +++ b/libs/libc/src/init.cpp @@ -51,12 +51,17 @@ static void check_for_file(int fd, FILE** target_stream, const char* path, const else { *target_stream = fdopen(fd, mode); } } -extern "C" void initialize_libc() +extern char* program_invocation_name; + +extern "C" void initialize_libc(int argc, char** argv) { check_for_file(STDIN_FILENO, &stdin, "/dev/kbd", "r"); check_for_file(STDOUT_FILENO, &stdout, "/dev/console", "rw"); check_for_file(STDERR_FILENO, &stderr, "/dev/console", "rw"); + program_invocation_name = + argv[0]; // argv[0] should only be nullptr when being init, which shouldn't use program_invocation_name anyways. + initialize_random(); atexit(terminate_libc); } \ No newline at end of file