File 2117-Rename-argv-0-from-beam-to-invoking-program-name.patch of Package erlang

From fab97e165a79db10b7b560be5aefc7489982bced Mon Sep 17 00:00:00 2001
From: Dan Gudmundsson <dgud@erlang.org>
Date: Fri, 21 Apr 2017 14:49:18 +0200
Subject: [PATCH] Rename argv[0] from beam to invoking program name

Allows ps and htop to display the invoking program/script name
instead of beam[.smp].
---
 erts/etc/common/ct_run.c   | 40 ++++++++++++++++++++++++++++++++++++++++
 erts/etc/common/dialyzer.c | 39 +++++++++++++++++++++++++++++++++++++++
 erts/etc/common/erlc.c     | 37 +++++++++++++++++++++++++++++++++++++
 erts/etc/common/erlexec.c  |  8 ++++++--
 erts/etc/common/escript.c  | 30 +++++++++++++++++++++++++++++-
 erts/etc/common/typer.c    | 38 ++++++++++++++++++++++++++++++++++++++
 6 files changed, 189 insertions(+), 3 deletions(-)

diff --git a/erts/etc/common/ct_run.c b/erts/etc/common/ct_run.c
index acdfa8c8b..898c8ccde 100644
--- a/erts/etc/common/ct_run.c
+++ b/erts/etc/common/ct_run.c
@@ -82,6 +82,9 @@ static int eargc;		/* Number of arguments in eargv. */
 
 static void error(char* format, ...);
 static void* emalloc(size_t size);
+#ifdef HAVE_COPYING_PUTENV
+static void efree(void *p);
+#endif
 static char* strsave(char* string);
 static void push_words(char* src);
 static int run_erlang(char* name, char** argv);
@@ -119,6 +122,30 @@ char *strerror(int errnum)
 }
 #endif /* !HAVE_STRERROR */
 
+
+static void
+set_env(char *key, char *value)
+{
+#ifdef __WIN32__
+    WCHAR wkey[MAXPATHLEN];
+    WCHAR wvalue[MAXPATHLEN];
+    MultiByteToWideChar(CP_UTF8, 0, key, -1, wkey, MAXPATHLEN);
+    MultiByteToWideChar(CP_UTF8, 0, value, -1, wvalue, MAXPATHLEN);
+    if (!SetEnvironmentVariableW(wkey, wvalue))
+        error("SetEnvironmentVariable(\"%s\", \"%s\") failed!", key, value);
+#else
+    size_t size = strlen(key) + 1 + strlen(value) + 1;
+    char *str = emalloc(size);
+    sprintf(str, "%s=%s", key, value);
+    if (putenv(str) != 0)
+        error("putenv(\"%s\") failed!", str);
+#ifdef HAVE_COPYING_PUTENV
+    efree(str);
+#endif
+#endif
+}
+
+
 #ifdef __WIN32__
 int wmain(int argc, wchar_t **wcargv)
 {
@@ -155,6 +182,11 @@ int main(int argc, char** argv)
     emulator = get_default_emulator(argv[0]);
 
     /*
+     * Add scriptname to env
+     */
+    set_env("ESCRIPT_NAME", argv[0]);
+
+    /*
      * Allocate the argv vector to be used for arguments to Erlang.
      * Arrange for starting to pushing information in the middle of
      * the array, to allow easy addition of commands in the beginning.
@@ -458,6 +490,14 @@ erealloc(void *p, size_t size)
 }
 #endif
 
+#ifdef HAVE_COPYING_PUTENV
+static void
+efree(void *p)
+{
+    free(p);
+}
+#endif
+
 static char*
 strsave(char* string)
 {
diff --git a/erts/etc/common/dialyzer.c b/erts/etc/common/dialyzer.c
index 6ba360542..829984ef3 100644
--- a/erts/etc/common/dialyzer.c
+++ b/erts/etc/common/dialyzer.c
@@ -64,6 +64,9 @@ static int eargc;		/* Number of arguments in eargv. */
 
 static void error(char* format, ...);
 static void* emalloc(size_t size);
+#ifdef HAVE_COPYING_PUTENV
+static void efree(void *p);
+#endif
 static char* strsave(char* string);
 static void push_words(char* src);
 static int run_erlang(char* name, char** argv);
@@ -147,6 +150,29 @@ free_env_val(char *value)
 #endif
 }
 
+static void
+set_env(char *key, char *value)
+{
+#ifdef __WIN32__
+    WCHAR wkey[MAXPATHLEN];
+    WCHAR wvalue[MAXPATHLEN];
+    MultiByteToWideChar(CP_UTF8, 0, key, -1, wkey, MAXPATHLEN);
+    MultiByteToWideChar(CP_UTF8, 0, value, -1, wvalue, MAXPATHLEN);
+    if (!SetEnvironmentVariableW(wkey, wvalue))
+        error("SetEnvironmentVariable(\"%s\", \"%s\") failed!", key, value);
+#else
+    size_t size = strlen(key) + 1 + strlen(value) + 1;
+    char *str = emalloc(size);
+    sprintf(str, "%s=%s", key, value);
+    if (putenv(str) != 0)
+        error("putenv(\"%s\") failed!", str);
+#ifdef HAVE_COPYING_PUTENV
+    efree(str);
+#endif
+#endif
+}
+
+
 #ifdef __WIN32__
 int wmain(int argc, wchar_t **wcargv)
 {
@@ -181,6 +207,11 @@ int main(int argc, char** argv)
         error("Value of environment variable DIALYZER_EMULATOR is too large");
 
     /*
+     * Add scriptname to env
+     */
+    set_env("ESCRIPT_NAME", argv[0]);
+
+    /*
      * Allocate the argv vector to be used for arguments to Erlang.
      * Arrange for starting to pushing information in the middle of
      * the array, to allow easy addition of commands in the beginning.
@@ -434,6 +465,14 @@ erealloc(void *p, size_t size)
 }
 #endif
 
+#ifdef HAVE_COPYING_PUTENV
+static void
+efree(void *p)
+{
+    free(p);
+}
+#endif
+
 static char*
 strsave(char* string)
 {
diff --git a/erts/etc/common/erlc.c b/erts/etc/common/erlc.c
index b54cb31be..2abeff33a 100644
--- a/erts/etc/common/erlc.c
+++ b/erts/etc/common/erlc.c
@@ -72,6 +72,9 @@ static int pause_after_execution = 0;
 static char* process_opt(int* pArgc, char*** pArgv, int offset);
 static void error(char* format, ...);
 static void* emalloc(size_t size);
+#ifdef HAVE_COPYING_PUTENV
+static void efree(void *p);
+#endif
 static char* strsave(char* string);
 static void push_words(char* src);
 static int run_erlang(char* name, char** argv);
@@ -147,6 +150,28 @@ get_env(char *key)
 }
 
 static void
+set_env(char *key, char *value)
+{
+#ifdef __WIN32__
+    WCHAR wkey[MAXPATHLEN];
+    WCHAR wvalue[MAXPATHLEN];
+    MultiByteToWideChar(CP_UTF8, 0, key, -1, wkey, MAXPATHLEN);
+    MultiByteToWideChar(CP_UTF8, 0, value, -1, wvalue, MAXPATHLEN);
+    if (!SetEnvironmentVariableW(wkey, wvalue))
+        error("SetEnvironmentVariable(\"%s\", \"%s\") failed!", key, value);
+#else
+    size_t size = strlen(key) + 1 + strlen(value) + 1;
+    char *str = emalloc(size);
+    sprintf(str, "%s=%s", key, value);
+    if (putenv(str) != 0)
+        error("putenv(\"%s\") failed!", str);
+#ifdef HAVE_COPYING_PUTENV
+    efree(str);
+#endif
+#endif
+}
+
+static void
 free_env_val(char *value)
 {
 #ifdef __WIN32__
@@ -188,6 +213,11 @@ int main(int argc, char** argv)
         error("Value of environment variable ERLC_EMULATOR is too large");
 
     /*
+     * Add scriptname to env
+     */
+    set_env("ESCRIPT_NAME", argv[0]);
+
+    /*
      * Allocate the argv vector to be used for arguments to Erlang.
      * Arrange for starting to pushing information in the middle of
      * the array, to allow easy adding of emulator options (like -pa)
@@ -499,6 +529,13 @@ erealloc(void *p, size_t size)
 }
 #endif
 
+#ifdef HAVE_COPYING_PUTENV
+static void
+efree(void *p)
+{
+    free(p);
+}
+#endif
 static char*
 strsave(char* string)
 {
diff --git a/erts/etc/common/erlexec.c b/erts/etc/common/erlexec.c
index ee5975994..a1c6bb223 100644
--- a/erts/etc/common/erlexec.c
+++ b/erts/etc/common/erlexec.c
@@ -583,8 +583,12 @@ int main(int argc, char **argv)
     erts_snprintf(tmpStr, sizeof(tmpStr), "%s" DIRSEP "%s" BINARY_EXT, bindir, emu);
     emu = strsave(tmpStr);
 
-    add_Eargs(emu);		/* Will be argv[0] -- necessary! */
-
+    s = get_env("ESCRIPT_NAME");
+    if(s) {
+        add_Eargs(s);         /* argv[0] = scriptname*/
+    } else {
+        add_Eargs(progname);  /* argv[0] = erl or cerl */
+    }
     /*
      * Add the bindir to the path (unless it is there already).
      */
diff --git a/erts/etc/common/escript.c b/erts/etc/common/escript.c
index 4134a3ff3..c28e45a04 100644
--- a/erts/etc/common/escript.c
+++ b/erts/etc/common/escript.c
@@ -155,6 +155,29 @@ free_env_val(char *value)
 	efree(value);
 #endif
 }
+
+static void
+set_env(char *key, char *value)
+{
+#ifdef __WIN32__
+    WCHAR wkey[MAXPATHLEN];
+    WCHAR wvalue[MAXPATHLEN];
+    MultiByteToWideChar(CP_UTF8, 0, key, -1, wkey, MAXPATHLEN);
+    MultiByteToWideChar(CP_UTF8, 0, value, -1, wvalue, MAXPATHLEN);
+    if (!SetEnvironmentVariableW(wkey, wvalue))
+        error("SetEnvironmentVariable(\"%s\", \"%s\") failed!", key, value);
+#else
+    size_t size = strlen(key) + 1 + strlen(value) + 1;
+    char *str = emalloc(size);
+    sprintf(str, "%s=%s", key, value);
+    if (putenv(str) != 0)
+        error("putenv(\"%s\") failed!", str);
+#ifdef HAVE_COPYING_PUTENV
+    efree(str);
+#endif
+#endif
+}
+
 /*
  * Find absolute path to this program
  */
@@ -548,7 +571,12 @@ main(int argc, char** argv)
     while (--eargc_base >= 0) {
 	UNSHIFT(eargv_base[eargc_base]);
     }
-    
+
+    /*
+     * Add scriptname to env
+     */
+    set_env("ESCRIPT_NAME", scriptname);
+
     /*
      * Invoke Erlang with the collected options.
      */
diff --git a/erts/etc/common/typer.c b/erts/etc/common/typer.c
index 77a95ccde..644c90a79 100644
--- a/erts/etc/common/typer.c
+++ b/erts/etc/common/typer.c
@@ -64,6 +64,9 @@ static int eargc;		/* Number of arguments in eargv. */
 
 static void error(char* format, ...);
 static void* emalloc(size_t size);
+#ifdef HAVE_COPYING_PUTENV
+static void efree(void *p);
+#endif
 static char* strsave(char* string);
 static void push_words(char* src);
 static int run_erlang(char* name, char** argv);
@@ -101,6 +104,29 @@ char *strerror(int errnum)
 }
 #endif /* !HAVE_STRERROR */
 
+static void
+set_env(char *key, char *value)
+{
+#ifdef __WIN32__
+    WCHAR wkey[MAXPATHLEN];
+    WCHAR wvalue[MAXPATHLEN];
+    MultiByteToWideChar(CP_UTF8, 0, key, -1, wkey, MAXPATHLEN);
+    MultiByteToWideChar(CP_UTF8, 0, value, -1, wvalue, MAXPATHLEN);
+    if (!SetEnvironmentVariableW(wkey, wvalue))
+        error("SetEnvironmentVariable(\"%s\", \"%s\") failed!", key, value);
+#else
+    size_t size = strlen(key) + 1 + strlen(value) + 1;
+    char *str = emalloc(size);
+    sprintf(str, "%s=%s", key, value);
+    if (putenv(str) != 0)
+        error("putenv(\"%s\") failed!", str);
+#ifdef HAVE_COPYING_PUTENV
+    efree(str);
+#endif
+#endif
+}
+
+
 #ifdef __WIN32__
 int wmain(int argc, wchar_t **wcargv)
 {
@@ -129,6 +155,10 @@ main(int argc, char** argv)
 #endif
 
     emulator = get_default_emulator(argv[0]);
+    /*
+     * Add scriptname to env
+     */
+    set_env("ESCRIPT_NAME", argv[0]);
 
     /*
      * Allocate the argv vector to be used for arguments to Erlang.
@@ -353,6 +383,14 @@ erealloc(void *p, size_t size)
 }
 #endif
 
+#ifdef HAVE_COPYING_PUTENV
+static void
+efree(void *p)
+{
+    free(p);
+}
+#endif
+
 static char*
 strsave(char* string)
 {
-- 
2.12.2

openSUSE Build Service is sponsored by