about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2013-04-06 17:50:37 -0400
committerRich Felker <dalias@aerifal.cx>2013-04-06 17:50:37 -0400
commitb4ea63856a6af3d1bcc2db12537785371ac2024c (patch)
tree689c9acbdd5e5339261988fb4f6ba007eb345d6b
parent5c5ac810c31a77dd17b95bb93e13487a3c1c1433 (diff)
downloadmusl-b4ea63856a6af3d1bcc2db12537785371ac2024c.tar.gz
musl-b4ea63856a6af3d1bcc2db12537785371ac2024c.tar.xz
musl-b4ea63856a6af3d1bcc2db12537785371ac2024c.zip
add support for program_invocation[_short]_name
this is a bit ugly, and the motivation for supporting it is
questionable. however the main factors were:
1. it will be useful to have this for certain internal purposes
anyway -- things like syslog.
2. applications can just save argv[0] in main, but it's hard to fix
non-portable library code that's depending on being able to get the
invocation name without the main application's help.
-rw-r--r--include/errno.h6
-rw-r--r--src/env/__libc_start_main.c10
-rw-r--r--src/internal/libc.c4
3 files changed, 18 insertions, 2 deletions
diff --git a/include/errno.h b/include/errno.h
index 9e1e3202..0361b33a 100644
--- a/include/errno.h
+++ b/include/errno.h
@@ -5,6 +5,8 @@
 extern "C" {
 #endif
 
+#include <features.h>
+
 #include <bits/errno.h>
 
 #ifdef __GNUC__
@@ -13,6 +15,10 @@ __attribute__((const))
 int *__errno_location(void);
 #define errno (*__errno_location())
 
+#ifdef _GNU_SOURCE
+extern char *program_invocation_short_name, *program_invocation_name;
+#endif
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/src/env/__libc_start_main.c b/src/env/__libc_start_main.c
index 04a454e8..aef9f9ec 100644
--- a/src/env/__libc_start_main.c
+++ b/src/env/__libc_start_main.c
@@ -8,8 +8,9 @@ void __init_ldso_ctors(void);
 #define AUX_CNT 38
 
 extern size_t __hwcap, __sysinfo;
+extern char *__progname, *__progname_full;
 
-void __init_libc(char **envp)
+void __init_libc(char **envp, char *pn)
 {
 	size_t i, *auxv, aux[AUX_CNT] = { 0 };
 	__environ = envp;
@@ -19,6 +20,11 @@ void __init_libc(char **envp)
 	__hwcap = aux[AT_HWCAP];
 	__sysinfo = aux[AT_SYSINFO];
 
+	if (pn) {
+		__progname = __progname_full = pn;
+		for (i=0; pn[i]; i++) if (pn[i]=='/') __progname = pn+i+1;
+	}
+
 	__init_tls(aux);
 	__init_security(aux);
 }
@@ -30,7 +36,7 @@ int __libc_start_main(
 {
 	char **envp = argv+argc+1;
 
-	__init_libc(envp);
+	__init_libc(envp, argv[0]);
 
 	libc.ldso_fini = ldso_fini;
 	libc.fini = fini;
diff --git a/src/internal/libc.c b/src/internal/libc.c
index d22d2747..942f6b44 100644
--- a/src/internal/libc.c
+++ b/src/internal/libc.c
@@ -16,3 +16,7 @@ __asm__(".hidden __libc");
 
 size_t __hwcap;
 size_t __sysinfo;
+char *__progname=0, *__progname_full=0;
+
+weak_alias(__progname, program_invocation_short_name);
+weak_alias(__progname_full, program_invocation_name);