]> git.sesse.net Git - vlc/commitdiff
Replacement for setenv() and unsetenv()
authorRémi Denis-Courmont <remi@remlab.net>
Sat, 12 Feb 2011 20:33:01 +0000 (22:33 +0200)
committerRémi Denis-Courmont <remi@remlab.net>
Sat, 12 Feb 2011 20:33:17 +0000 (22:33 +0200)
compat/setenv.c [new file with mode: 0644]
configure.ac
include/vlc_fixups.h

diff --git a/compat/setenv.c b/compat/setenv.c
new file mode 100644 (file)
index 0000000..049effc
--- /dev/null
@@ -0,0 +1,48 @@
+/*****************************************************************************
+ * setenv.c: POSIX setenv() & unsetenv() replacement
+ *****************************************************************************
+ * Copyright © 2010 Rémi Denis-Courmont
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+
+int setenv (const char *name, const char *value, int override)
+{
+    if (override == 0 && getenv (name) != NULL)
+        return 0;
+
+    size_t namelen = strlen (name);
+    size_t valuelen = strlen (value);
+    char *var = malloc (namelen + valuelen + 2);
+
+    if (var == NULL)
+        return -1;
+
+    sprintf (var, "%s=%s", name, value);
+    /* This leaks memory. This is unavoidable. */
+    return putenv (var);
+}
+
+int unsetenv (const char *name)
+{
+    return setenv (name, "", 1);
+}
index 169fdfcbbe741465943bcbfffc2881ef2f0f333a..9cc2f59ad0816c2410a8f9eeabc71b51b5941d23 100644 (file)
@@ -547,8 +547,8 @@ dnl Check for system libs needed
 need_libc=false
 
 dnl Check for usual libc functions
-AC_CHECK_FUNCS([daemon fcntl fdopendir fstatvfs fork getenv getpwuid_r gettimeofday isatty lstat memalign mmap openat pread posix_fadvise posix_madvise posix_memalign setenv setlocale stricmp strnicmp uselocale])
-AC_REPLACE_FUNCS([asprintf atof atoll getcwd getdelim getpid gmtime_r lldiv localtime_r nrand48 rewind strcasecmp strcasestr strdup strlcpy strncasecmp strndup strnlen strsep strtof strtok_r strtoll swab tdestroy vasprintf])
+AC_CHECK_FUNCS([daemon fcntl fdopendir fstatvfs fork getenv getpwuid_r gettimeofday isatty lstat memalign mmap openat pread posix_fadvise posix_madvise posix_memalign setlocale stricmp strnicmp uselocale])
+AC_REPLACE_FUNCS([asprintf atof atoll getcwd getdelim getpid gmtime_r lldiv localtime_r nrand48 rewind setenv strcasecmp strcasestr strdup strlcpy strncasecmp strndup strnlen strsep strtof strtok_r strtoll swab tdestroy vasprintf])
 AC_CHECK_FUNCS(fdatasync,,
   [AC_DEFINE(fdatasync, fsync, [Alias fdatasync() to fsync() if missing.])
 ])
index ef37367774883357208a618dc9c4a5452c9f36f6..f6ca7e88d35da514d411a45d8296f46639883127 100644 (file)
@@ -170,6 +170,11 @@ static inline char *getenv (const char *name)
 }
 #endif
 
+#ifndef HAVE_SETENV
+int setenv (const char *, const char *, int);
+int unsetenv (const char *);
+#endif
+
 /* Alignment of critical static data structures */
 #ifdef ATTRIBUTE_ALIGNED_MAX
 #   define ATTR_ALIGN(align) __attribute__ ((__aligned__ ((ATTRIBUTE_ALIGNED_MAX < align) ? ATTRIBUTE_ALIGNED_MAX : align)))