Files
epics-base/modules/libcom/src/osi/os/default/osdEnv.c
T
Michael Davidsaver 3eeebb74cd Com: avoid intentional memory leak in epicsEnvSet
Switch default impl. to setenv/unsetenv
Switch WIN32 to use _putenv_s
On vxWorks putenv() is documented to make a copy.

log error, but never halt, if env (un)set not possible.

RTEMS <4.10 compat where unsetenv() returns void.
2021-03-29 10:55:30 -07:00

86 lines
2.3 KiB
C

/*************************************************************************\
* Copyright (c) 2002 The University of Saskatchewan
* SPDX-License-Identifier: EPICS
* 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 <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include "epicsStdio.h"
#include "epicsVersion.h"
#include "errlog.h"
#include "envDefs.h"
#include "osiUnistd.h"
#include "epicsFindSymbol.h"
#include "iocsh.h"
#ifdef __rtems__
# include <rtems.h>
# define RTEMS_VERSION_INT VERSION_INT(__RTEMS_MAJOR__, __RTEMS_MINOR__, 0, 0)
#endif
#if defined(__RTEMS_MAJOR__) && RTEMS_VERSION_INT<VERSION_INT(4,10,0,0)
/* newlib w/ RTEMS <=4.9 returns void */
# define unSetEnv(name) ({unsetenv(name); 0;})
#else
# define unSetEnv(name) unsetenv(name)
#endif
/*
* 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.
*/
LIBCOM_API void epicsStdCall epicsEnvSet (const char *name, const char *value)
{
iocshEnvClear(name);
if(setenv(name, value, 1))
errlogPrintf("setenv(\"%s\", \"%s\") -> %d\n", name, value, errno);
}
/*
* Unset an environment variable
* Using putenv with a an existing name but without "=..." deletes
*/
LIBCOM_API void epicsStdCall epicsEnvUnset (const char *name)
{
iocshEnvClear(name);
if(unSetEnv(name))
errlogPrintf("unsetenv(\"%s\") -> %d\n", name, errno);
}
/*
* Show the value of the specified, or all, environment variables
*/
LIBCOM_API void epicsStdCall 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);
}
}