From 4859c222e759c6899c26290e8923eead804d1311 Mon Sep 17 00:00:00 2001
From: Steve McKay <steve@b.abbies.us>
Date: Sat, 17 Aug 2019 12:35:43 -0400
Subject: [PATCH] Make program_invocation_name modifiable

libiconv expects program_invocation_name to be an lvalue
---
 include/bits/errno.h        |  2 +-
 src/header/errno/mod.rs     |  4 ++--
 tests/Makefile              |  1 +
 tests/errno.c               | 13 +++++++++++++
 tests/expected/errno.stderr |  0
 tests/expected/errno.stdout |  4 ++++
 6 files changed, 21 insertions(+), 3 deletions(-)
 create mode 100644 tests/errno.c
 create mode 100644 tests/expected/errno.stderr
 create mode 100644 tests/expected/errno.stdout

diff --git a/include/bits/errno.h b/include/bits/errno.h
index 15d7d66c4..3d6409a2e 100644
--- a/include/bits/errno.h
+++ b/include/bits/errno.h
@@ -8,7 +8,7 @@ extern "C" {
 #define ENOTSUP EOPNOTSUPP
 
 #define errno (*__errno_location())
-#define program_invocation_name (__program_invocation_name())
+#define program_invocation_name (*__program_invocation_name())
 
 #ifdef __cplusplus
 } // extern "C"
diff --git a/src/header/errno/mod.rs b/src/header/errno/mod.rs
index 82dea1c06..0530f74ee 100644
--- a/src/header/errno/mod.rs
+++ b/src/header/errno/mod.rs
@@ -14,8 +14,8 @@ pub unsafe extern "C" fn __errno_location() -> *mut c_int {
 }
 
 #[no_mangle]
-pub unsafe extern "C" fn __program_invocation_name() -> *mut c_char {
-    platform::inner_argv[0]
+pub unsafe extern "C" fn __program_invocation_name() -> *mut *mut c_char {
+    &mut platform::inner_argv[0]
 }
 
 pub const EPERM: c_int = 1; /* Operation not permitted */
diff --git a/tests/Makefile b/tests/Makefile
index 4f5e83f67..106923349 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -8,6 +8,7 @@ EXPECT_NAMES=\
 	ctype \
 	destructor \
 	dirent/scandir \
+	errno \
 	error \
 	fcntl/create \
 	fcntl/fcntl \
diff --git a/tests/errno.c b/tests/errno.c
new file mode 100644
index 000000000..699bfa96a
--- /dev/null
+++ b/tests/errno.c
@@ -0,0 +1,13 @@
+#include <errno.h>
+#include <stdio.h>
+#include "test_helpers.h"
+
+int main(int argc, char **argv) {
+    puts(argv[0]);
+    puts(program_invocation_name);
+
+    program_invocation_name = "yes, you can change this";
+
+    puts(argv[0]);
+    puts(program_invocation_name);
+}
diff --git a/tests/expected/errno.stderr b/tests/expected/errno.stderr
new file mode 100644
index 000000000..e69de29bb
diff --git a/tests/expected/errno.stdout b/tests/expected/errno.stdout
new file mode 100644
index 000000000..62770a791
--- /dev/null
+++ b/tests/expected/errno.stdout
@@ -0,0 +1,4 @@
+bins/errno
+bins/errno
+yes, you can change this
+yes, you can change this
-- 
GitLab