Added SILC Environment manipulation API.
authorPekka Riikonen <priikone@silcnet.org>
Tue, 3 Jul 2007 19:44:57 +0000 (19:44 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Tue, 3 Jul 2007 19:44:57 +0000 (19:44 +0000)
lib/silcutil/silcenv.c [new file with mode: 0644]
lib/silcutil/silcenv.h [new file with mode: 0644]
lib/silcutil/tests/test_silcenv.c [new file with mode: 0644]

diff --git a/lib/silcutil/silcenv.c b/lib/silcutil/silcenv.c
new file mode 100644 (file)
index 0000000..fda4849
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+
+  silcenv.c
+
+  Author: Pekka Riikonen <priikone@silcnet.org>
+
+  Copyright (C) 2007 Pekka Riikonen
+
+  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; version 2 of the License.
+
+  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.
+
+*/
+
+#include "silc.h"
+
+/* Set environment variable with value */
+
+SilcBool silc_setenv(const char *variable, const char *value)
+{
+  SILC_LOG_DEBUG(("Set %s=%s", variable, value));
+#if defined(HAVE_SETENV)
+  return setenv(variable, value, TRUE) == 0;
+#elif defined (HAVE_PUTENV)
+  char tmp[1024];
+  silc_snprintf(tmp, sizeof(tmp), "%s=%s", variable, value);
+  return putenv(tmp) == 0;
+#endif /* HAVE_SETENV */
+  return FALSE;
+}
+
+/* Get environment variable value */
+
+const char *silc_getenv(const char *variable)
+{
+  SILC_LOG_DEBUG(("Get %s value", variable));
+#if defined(HAVE_GETENV)
+  return (const char *)getenv(variable);
+#endif /* HAVE_GETENV */
+  return NULL;
+}
+
+/* Unset environment variable */
+
+SilcBool silc_unsetenv(const char *variable)
+{
+  SILC_LOG_DEBUG(("Unset %s value", variable));
+#if defined(HAVE_UNSETENV)
+  return unsetenv(variable) == 0;
+#endif /* HAVE_GETENV */
+  return FALSE;
+}
+
+/* Clear environment */
+
+SilcBool silc_clearenv(void)
+{
+  SILC_LOG_DEBUG(("Clear allenvironment variables"));
+#if defined(HAVE_CLEARENV)
+  return clearenv() == 0;
+#endif /* HAVE_GETENV */
+  return FALSE;
+}
diff --git a/lib/silcutil/silcenv.h b/lib/silcutil/silcenv.h
new file mode 100644 (file)
index 0000000..925caa3
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+
+  silcenv.h
+
+  Author: Pekka Riikonen <priikone@silcnet.org>
+
+  Copyright (C) 2007 Pekka Riikonen
+
+  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; version 2 of the License.
+
+  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.
+
+*/
+
+/****h* silcutil/Environment Manipulation Interface
+ *
+ * DESCRIPTION
+ *
+ * This interface provides utility functions for manipulating environment
+ * variables.  On platforms that do not support environment variables this
+ * interfaces does nothing.
+ *
+ ***/
+
+#ifndef SILCENV_H
+#define SILCENV_H
+
+/****f* silcutil/SilcEnvAPI/silc_setenv
+ *
+ * SYNOPSIS
+ *
+ *    SilcBool silc_setenv(const char *variable, const char *value);
+ *
+ * DESCRIPTION
+ *
+ *    Sets the environment variable named `variable' with value `value'
+ *    to the environment.  If the `variable' already exists in the
+ *    environment its value is changed to `value'.  Returns FALSE if the
+ *    value could not be set or if environment variable are not supported.
+ *
+ ***/
+SilcBool silc_setenv(const char *variable, const char *value);
+
+/****f* silcutil/SilcEnvAPI/silc_getenv
+ *
+ * SYNOPSIS
+ *
+ *    const char *silc_getenv(const char *variable);
+ *
+ * DESCRIPTION
+ *
+ *    Returns the value of the environment variable `variable' or NULL if
+ *    such variable does not exist in the environment.
+ *
+ ***/
+const char *silc_getenv(const char *variable);
+
+/****f* silcutil/SilcEnvAPI/silc_unsetenv
+ *
+ * SYNOPSIS
+ *
+ *    SilcBool silc_unsetenv(const char *variable);
+ *
+ * DESCRIPTION
+ *
+ *    Clears the value of the environment variable `variable'.  Returns FALSE
+ *    if the value could not be cleared or if environment variables are not
+ *    supported.
+ *
+ ***/
+SilcBool silc_unsetenv(const char *variable);
+
+/****f* silcutil/SilcEnvAPI/silc_clearenv
+ *
+ * SYNOPSIS
+ *
+ *    SilcBool silc_clearenv(void);
+ *
+ * DESCRIPTION
+ *
+ *    Clears the environment of all environment variables.  Returns FALSE
+ *    if the environment could not be cleared or if environment variables are
+ *    not supported.
+ *
+ ***/
+SilcBool silc_clearenv(void);
+
+
+#endif /* SILCENV_H */
diff --git a/lib/silcutil/tests/test_silcenv.c b/lib/silcutil/tests/test_silcenv.c
new file mode 100644 (file)
index 0000000..40c01f8
--- /dev/null
@@ -0,0 +1,28 @@
+/* environment tests */
+
+#include "silc.h"
+
+int main(int argc, char **argv)
+{
+  SilcBool success = FALSE;
+
+  if (argc > 1 && !strcmp(argv[1], "-d")) {
+    silc_log_debug(TRUE);
+    silc_log_quick(TRUE);
+    silc_log_debug_hexdump(TRUE);
+    silc_log_set_debug_string("*env*");
+  }
+
+  silc_setenv("FOO", "BAR");
+  SILC_LOG_DEBUG(("%s", silc_getenv("FOO")));
+  silc_unsetenv("FOO");
+  if (silc_getenv("FOO") != NULL)
+    goto err;
+  success = TRUE;
+
+ err:
+  SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
+  fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
+
+  return success;
+}