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  if (!name) return;
39  iocshEnvClear(name);
40 
41  cp = mallocMustSucceed (strlen (name) + strlen (value) + 2, "epicsEnvSet");
42  strcpy (cp, name);
43  strcat (cp, "=");
44  strcat (cp, value);
45  if (putenv (cp) < 0) {
46  errPrintf(
47  -1L,
48  __FILE__,
49  __LINE__,
50  "Failed to set environment parameter \"%s\" to \"%s\": %s\n",
51  name,
52  value,
53  strerror (errno));
54  free (cp);
55  }
56 }
57 
58 /*
59  * Unset an environment variable
60  * Using putenv with a an existing name but without "=..." deletes
61  */
62 
63 LIBCOM_API void epicsStdCall epicsEnvUnset (const char *name)
64 {
65  iocshEnvClear(name);
66  if (getenv(name) != NULL)
67  putenv((char*)name);
68 }
69 
70 /*
71  * Show the value of the specified, or all, environment variables
72  */
73 LIBCOM_API void epicsStdCall epicsEnvShow (const char *name)
74 {
75  if (name == NULL) {
76  extern char **environ;
77  char **sp;
78 
79  for (sp = environ ; (sp != NULL) && (*sp != NULL) ; sp++)
80  printf ("%s\n", *sp);
81  }
82  else {
83  const char *cp = getenv (name);
84  if (cp == NULL)
85  printf ("%s is not an environment variable.\n", name);
86  else
87  printf ("%s=%s\n", name, cp);
88  }
89 }
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