[vlc-commits] Replacement for setenv() and unsetenv()
Rémi Denis-Courmont
git at videolan.org
Sat Feb 12 21:33:25 CET 2011
vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sat Feb 12 22:33:01 2011 +0200| [c7681744dec5cd48a9c9de9a5405d648081b0bd9] | committer: Rémi Denis-Courmont
Replacement for setenv() and unsetenv()
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=c7681744dec5cd48a9c9de9a5405d648081b0bd9
---
compat/setenv.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
configure.ac | 4 ++--
include/vlc_fixups.h | 5 +++++
3 files changed, 55 insertions(+), 2 deletions(-)
diff --git a/compat/setenv.c b/compat/setenv.c
new file mode 100644
index 0000000..049effc
--- /dev/null
+++ b/compat/setenv.c
@@ -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);
+}
diff --git a/configure.ac b/configure.ac
index 169fdfc..9cc2f59 100644
--- a/configure.ac
+++ b/configure.ac
@@ -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.])
])
diff --git a/include/vlc_fixups.h b/include/vlc_fixups.h
index ef37367..f6ca7e8 100644
--- a/include/vlc_fixups.h
+++ b/include/vlc_fixups.h
@@ -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)))
More information about the vlc-commits
mailing list