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.
This commit is contained in:
apio 2022-10-26 19:17:05 +02:00
parent 7d20c507b1
commit 8eb986df63
3 changed files with 13 additions and 2 deletions

View File

@ -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

View File

@ -1,3 +1,4 @@
#include <errno.h>
int errno;
int errno;
char* program_invocation_name;

View File

@ -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);
}