]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: tools: add an unsetenv() implementation
authorWilly Tarreau <w@1wt.eu>
Fri, 29 Mar 2019 17:49:09 +0000 (18:49 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 29 Mar 2019 20:05:37 +0000 (21:05 +0100)
Older Solaris and AIX versions do not have unsetenv(). This adds a
fairly simple implementation which scans the environment, for use
with those systems. It will simply require to pass the define in
the "DEFINE" macro at build time like this :

      DEFINE="-Dunsetenv=my_unsetenv"

include/common/standard.h
src/standard.c

index 7536a6491d6def62963c5ae5f9710b73334b8998..0900798d4885cd36e8c574d015c0de795d262e8a 100644 (file)
@@ -1224,6 +1224,16 @@ char *memprintf(char **out, const char *format, ...)
  */
 char *indent_msg(char **out, int level);
 
+/* removes environment variable <name> from the environment as found in
+ * environ. This is only provided as an alternative for systems without
+ * unsetenv() (old Solaris and AIX versions). THIS IS NOT THREAD SAFE.
+ * The principle is to scan environ for each occurence of variable name
+ * <name> and to replace the matching pointers with the last pointer of
+ * the array (since variables are not ordered).
+ * It always returns 0 (success).
+ */
+int my_unsetenv(const char *name);
+
 /* Convert occurrences of environment variables in the input string to their
  * corresponding value. A variable is identified as a series of alphanumeric
  * characters or underscores following a '$' sign. The <in> string must be
index 09bc155136d11b3734b33ccb71ed8aa41d011108..1035e202720d156c5aa70b6efe43c1b2ced6909e 100644 (file)
@@ -3699,6 +3699,38 @@ char *indent_msg(char **out, int level)
        return ret;
 }
 
+/* removes environment variable <name> from the environment as found in
+ * environ. This is only provided as an alternative for systems without
+ * unsetenv() (old Solaris and AIX versions). THIS IS NOT THREAD SAFE.
+ * The principle is to scan environ for each occurence of variable name
+ * <name> and to replace the matching pointers with the last pointer of
+ * the array (since variables are not ordered).
+ * It always returns 0 (success).
+ */
+int my_unsetenv(const char *name)
+{
+       extern char **environ;
+       char **p = environ;
+       int vars;
+       int next;
+       int len;
+
+       len = strlen(name);
+       for (vars = 0; p[vars]; vars++)
+               ;
+       next = 0;
+       while (next < vars) {
+               if (strncmp(p[next], name, len) != 0 || p[next][len] != '=') {
+                       next++;
+                       continue;
+               }
+               if (next < vars - 1)
+                       p[next] = p[vars - 1];
+               p[--vars] = NULL;
+       }
+       return 0;
+}
+
 /* Convert occurrences of environment variables in the input string to their
  * corresponding value. A variable is identified as a series of alphanumeric
  * characters or underscores following a '$' sign. The <in> string must be