This is Unofficial EPICS BASE Doxygen Site
osdEnv.c
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2002 The University of Saskatchewan
3 * EPICS BASE is distributed subject to a Software License Agreement found
4 * in file LICENSE that is included with this distribution.
5 \*************************************************************************/
6 /* osdEnv.c */
7 /*
8  * Author: Eric Norum
9  * Date: May 7, 2001
10  *
11  * Routines to modify/display environment variables and EPICS parameters
12  *
13  */
14 
15 #include <string.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <ctype.h>
19 #include <errno.h>
20 
21 #include "epicsStdio.h"
22 #include "errlog.h"
23 #include "cantProceed.h"
24 #include "envDefs.h"
25 #include "osiUnistd.h"
26 #include "epicsFindSymbol.h"
27 #include "iocsh.h"
28 
29 /*
30  * Set the value of an environment variable
31  * Leaks memory, but the assumption is that this routine won't be
32  * called often enough for the leak to be a problem.
33  */
34 LIBCOM_API void epicsStdCall epicsEnvSet (const char *name, const char *value)
35 {
36  char *cp;
37 
38  iocshEnvClear(name);
39 
40  cp = mallocMustSucceed (strlen (name) + strlen (value) + 2, "epicsEnvSet");
41  strcpy (cp, name);
42  strcat (cp, "=");
43  strcat (cp, value);
44  if (putenv (cp) < 0) {
45  errPrintf(
46  -1L,
47  __FILE__,
48  __LINE__,
49  "Failed to set environment parameter \"%s\" to \"%s\": %s\n",
50  name,
51  value,
52  strerror (errno));
53  free (cp);
54  }
55 }
56 
57 /*
58  * Unset an environment variable
59  * Using putenv with a an existing name plus "=" (without value) deletes
60  */
61 
62 LIBCOM_API void epicsStdCall epicsEnvUnset (const char *name)
63 {
64  iocshEnvClear(name);
65  if (getenv(name) != NULL)
66  epicsEnvSet((char*)name, "");
67 }
68 
69 /*
70  * Show the value of the specified, or all, environment variables
71  */
72 LIBCOM_API void epicsStdCall epicsEnvShow (const char *name)
73 {
74  if (name == NULL) {
75  extern char **environ;
76  char **sp;
77 
78  for (sp = environ ; (sp != NULL) && (*sp != NULL) ; sp++)
79  printf ("%s\n", *sp);
80  }
81  else {
82  const char *cp = getenv (name);
83  if (cp == NULL)
84  printf ("%s is not an environment variable.\n", name);
85  else
86  printf ("%s=%s\n", name, cp);
87  }
88 }
Definition: link.h:174
void epicsStdCall iocshEnvClear(const char *name)
Definition: iocsh.cpp:1054
Routines to get and set EPICS environment parameters.
#define printf
Definition: epicsStdio.h:41
#define NULL
Definition: catime.c:38
LIBCOM_API void epicsStdCall epicsEnvShow(const char *name)
Print value of an environment variable, or all variables.
Definition: osdEnv.c:55
void errPrintf(long status, const char *pFileName, int lineno, const char *pformat,...)
Definition: errlog.c:383
int putenv(char *)
LIBCOM_API void * mallocMustSucceed(size_t size, const char *msg)
A malloc() that never returns NULL.
Definition: cantProceed.c:38
LIBCOM_API void epicsStdCall epicsEnvSet(const char *name, const char *value)
Set an environment variable&#39;s value.
Definition: osdEnv.c:35
#define environ
Definition: osdEnv.c:26
Routines for code that can&#39;t continue or return after an error.
LIBCOM_API void epicsStdCall epicsEnvUnset(const char *name)
Clear the value of an environment variable.
Definition: osdEnv.c:46