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 /* This is needed for vxWorks 6.8 to prevent an obnoxious compiler warning */
16 #define _VSB_CONFIG_FILE <../lib/h/config/vsbConfig.h>
17 
18 #include <string.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <ctype.h>
22 #include <envLib.h>
23 
24 #include "cantProceed.h"
25 #include "epicsFindSymbol.h"
26 #include "epicsStdio.h"
27 #include "errlog.h"
28 #include "iocsh.h"
29 
30 /*
31  * Set the value of an environment variable
32  * Leaks memory, but the assumption is that this routine won't be
33  * called often enough for the leak to be a problem.
34  */
35 LIBCOM_API void epicsStdCall epicsEnvSet (const char *name, const char *value)
36 {
37  char *cp;
38 
39  if (!name) {
40  printf ("Usage: epicsEnvSet \"name\", \"value\"\n");
41  return;
42  }
43 
44  iocshEnvClear(name);
45 
46  cp = mallocMustSucceed (strlen (name) + strlen (value) + 2, "epicsEnvSet");
47  strcpy (cp, name);
48  strcat (cp, "=");
49  strcat (cp, value);
50  if (putenv (cp) < 0) {
51  errPrintf(-1L, __FILE__, __LINE__,
52  "Failed to set environment parameter \"%s\" to \"%s\": %s\n",
53  name, value, strerror (errno));
54  free (cp);
55  }
56 }
57 
58 /*
59  * Unset an environment variable
60  * Basically destroy the name of that variable because vxWorks does not
61  * support to really unset an environment variable.
62  */
63 
64 LIBCOM_API void epicsStdCall epicsEnvUnset (const char *name)
65 {
66  char* var;
67 
68  if (!name) return;
69  iocshEnvClear(name);
70  var = getenv(name);
71  if (!var) return;
72  var -= strlen(name);
73  var --;
74  *var = 0;
75 }
76 
77 /*
78  * Show the value of the specified, or all, environment variables
79  */
80 LIBCOM_API void epicsStdCall epicsEnvShow (const char *name)
81 {
82  if (name == NULL) {
83  envShow (0);
84  }
85  else {
86  const char *cp = getenv (name);
87  if (cp == NULL)
88  printf ("%s is not an environment variable.\n", name);
89  else
90  printf ("%s=%s\n", name, cp);
91  }
92 }
Definition: link.h:174
void epicsStdCall iocshEnvClear(const char *name)
Definition: iocsh.cpp:1054
#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
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