From 4e865a03d8382f3c8f8441cd77e9d5b607fcb09b Mon Sep 17 00:00:00 2001 From: Ralph Lange Date: Thu, 11 Oct 2018 15:12:10 +0200 Subject: [PATCH] libCom/osi: fix epicsEnvUnset for WIN32 and solaris --- src/libCom/osi/os/WIN32/osdEnv.c | 89 ++++++++++++++++++++++++++++++ src/libCom/osi/os/solaris/osdEnv.c | 62 +++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 src/libCom/osi/os/WIN32/osdEnv.c create mode 100644 src/libCom/osi/os/solaris/osdEnv.c diff --git a/src/libCom/osi/os/WIN32/osdEnv.c b/src/libCom/osi/os/WIN32/osdEnv.c new file mode 100644 index 000000000..4c5587bb2 --- /dev/null +++ b/src/libCom/osi/os/WIN32/osdEnv.c @@ -0,0 +1,89 @@ +/*************************************************************************\ +* Copyright (c) 2002 The University of Saskatchewan +* EPICS BASE is distributed subject to a Software License Agreement found +* in file LICENSE that is included with this distribution. +\*************************************************************************/ +/* osdEnv.c */ +/* + * Author: Eric Norum + * Date: May 7, 2001 + * + * Routines to modify/display environment variables and EPICS parameters + * + */ + +#include +#include +#include +#include +#include + +#define epicsExportSharedSymbols +#include "epicsStdio.h" +#include "errlog.h" +#include "cantProceed.h" +#include "envDefs.h" +#include "osiUnistd.h" +#include "epicsFindSymbol.h" +#include "iocsh.h" + +/* + * Set the value of an environment variable + * Leaks memory, but the assumption is that this routine won't be + * called often enough for the leak to be a problem. + */ +epicsShareFunc void epicsShareAPI epicsEnvSet (const char *name, const char *value) +{ + char *cp; + + iocshEnvClear(name); + + cp = mallocMustSucceed (strlen (name) + strlen (value) + 2, "epicsEnvSet"); + strcpy (cp, name); + strcat (cp, "="); + strcat (cp, value); + if (putenv (cp) < 0) { + errPrintf( + -1L, + __FILE__, + __LINE__, + "Failed to set environment parameter \"%s\" to \"%s\": %s\n", + name, + value, + strerror (errno)); + free (cp); + } +} + +/* + * Unset an environment variable + * Using putenv with a an existing name plus "=" (without value) deletes + */ + +epicsShareFunc void epicsShareAPI epicsEnvUnset (const char *name) +{ + iocshEnvClear(name); + if (getenv(name) != NULL) + epicsEnvSet((char*)name, ""); +} + +/* + * Show the value of the specified, or all, environment variables + */ +epicsShareFunc void epicsShareAPI epicsEnvShow (const char *name) +{ + if (name == NULL) { + extern char **environ; + char **sp; + + for (sp = environ ; (sp != NULL) && (*sp != NULL) ; sp++) + printf ("%s\n", *sp); + } + else { + const char *cp = getenv (name); + if (cp == NULL) + printf ("%s is not an environment variable.\n", name); + else + printf ("%s=%s\n", name, cp); + } +} diff --git a/src/libCom/osi/os/solaris/osdEnv.c b/src/libCom/osi/os/solaris/osdEnv.c new file mode 100644 index 000000000..c356a0a13 --- /dev/null +++ b/src/libCom/osi/os/solaris/osdEnv.c @@ -0,0 +1,62 @@ +/*************************************************************************\ +* Copyright (c) 2002 The University of Saskatchewan +* EPICS BASE is distributed subject to a Software License Agreement found +* in file LICENSE that is included with this distribution. +\*************************************************************************/ +/* osdEnv.c */ +/* + * Author: Eric Norum + * Date: May 7, 2001 + * + * Routines to modify/display environment variables and EPICS parameters + * + */ + +#include +#include + +#define epicsExportSharedSymbols +#include "epicsStdio.h" +#include "envDefs.h" +#include "osiUnistd.h" +#include "iocsh.h" + +/* + * Set the value of an environment variable + */ +epicsShareFunc void epicsShareAPI epicsEnvSet (const char *name, const char *value) +{ + iocshEnvClear(name); + setenv(name, value, 1); +} + +/* + * Unset an environment variable + */ + +epicsShareFunc void epicsShareAPI epicsEnvUnset (const char *name) +{ + iocshEnvClear(name); + unsetenv(name); +} + +/* + * Show the value of the specified, or all, environment variables + */ +epicsShareFunc void epicsShareAPI epicsEnvShow (const char *name) +{ + if (name == NULL) { + extern char **environ; + char **sp; + + for (sp = environ ; (sp != NULL) && (*sp != NULL) ; sp++) + printf ("%s\n", *sp); + } + else { + const char *cp = getenv (name); + if (cp == NULL) + printf ("%s is not an environment variable.\n", name); + else + printf ("%s=%s\n", name, cp); + } +}