summaryrefslogtreecommitdiff
path: root/src/Xw/Xw_get_env.cxx
blob: bda7e58849228234b98fde335ff0932d4024d861 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

#include <Xw_Extension.h>

	/* ifdef then trace on */
#ifdef TRACE
#define TRACE_GET_ENV
#define TRACE_PUT_ENV
#endif

/*
   STATUS Xw_get_env (symbol,value,length):
   char *symbol			Symbol string to read
   char *value			return Symbol value to fill
   int  length			Value max length in chars

	Get external Symbol value (Set by setenv UNIX)

	Returns ERROR if No symbol exist
		      or Length is too small for value
	Returns SUCCESS if Successful      

   STATUS Xw_put_env (symbol,value):
   char *symbol			Symbol string to write
   char *value			Symbol value to write

	Put external Symbol value (Set by putenv UNIX)

	Returns ERROR if symbol is empty 
	Returns SUCCESS if Successful      
*/

#ifdef XW_PROTOTYPE
XW_STATUS Xw_get_env (const char* symbol,char* value,int length)
#else
XW_STATUS Xw_get_env (symbol,value,length)
const char *symbol;
char *value ;
int length ;
#endif /*XW_PROTOTYPE*/
{
char *string ;

    	if( !(string = getenv (symbol)) ) {
	  if( Xw_get_trace() > 1 )
		printf("*UNDEFINED SYMBOL*Xw_get_env('%s',...)\n",symbol);
	  return (XW_ERROR);
	}

	if( strlen(string) >= (unsigned int ) length ) return (XW_ERROR) ;

	strcpy(value,string) ;

#ifdef  TRACE_GET_ENV
if( Xw_get_trace() > 1 ) {
    printf (" '%s' = Xw_get_env('%s','%s',%d)\n",value,symbol,string,length) ;
}
#endif

	return (XW_SUCCESS);
}

#ifdef XW_PROTOTYPE
XW_STATUS Xw_put_env (char* symbol,char* value)
#else
XW_STATUS Xw_put_env (symbol,value)
char *symbol,*value ;
#endif /*XW_PROTOTYPE*/
{
char *string ;

	if( !strlen(symbol) ) return (XW_ERROR) ;

	string = (char*) malloc(strlen(symbol) + strlen(value) + 4) ;

	sprintf(string,"%s=%s",symbol,value) ;

	if( putenv(string) ) return (XW_ERROR) ;

#ifdef  TRACE_PUT_ENV
if( Xw_get_trace() > 1 ) {
    printf (" Xw_put_env('%s','%s')\n",symbol,value) ;
}
#endif

	return (XW_SUCCESS);
}