aboutsummaryrefslogtreecommitdiff
path: root/sys/libc/cenvget.c
blob: 2c812226130f669a17385de1e90e9c398fff1419 (plain) (blame)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
*/

#define import_spp
#define	import_libc
#define	import_xnames
#include <iraf.h>


/*
** ENVGET[SBI] -- Assorted routines for fetching the values of environment
**   		  variables.
**     ENVPUTS -- Set or redefine the value of an environment variable.
**    ENVRESET -- Reset (overwrite) the value of an environment variable.
*/

#define SZ_VALUESTR	SZ_COMMAND

static	XCHAR valuestr[SZ_VALUESTR+1];
static	XINT len_valuestr = SZ_VALUESTR;


/* ENVGET -- (Near) emulation of the UNIX getenv procedure.  This is a handy
** routine for looking up environment variables in C programs.  The value is
** fetched into an internal static array and a pointer to this array is returned
** as the function value, else NULL if the environment variable is not found.
** The caller is responsible for using the value string before it is overwritten
** by the next call.
*/
char *
envget (
  char	*var			/* environment variable name		*/
)
{
	if (ENVFIND (c_sppstr(var), valuestr, &len_valuestr) < 0)
	    return (NULL);
	else
	    return (c_strpak (valuestr, (char *)valuestr, len_valuestr+1));
}


/* C_ENVGETS -- Fetch the string value of an environment variable into the
** output string.  Return the strlen length of the output string as the
** function value.  If the variable is not found and the process is being
** used interactively a query is generated.  If no value is given in response
** to the query or the process is noninteractive and the variable is not
** found, zero is returned.
*/
int
c_envgets (
  char	*var,			/* name of variable to be fetched	*/
  char	*outstr,		/* output string			*/
  int	maxch			/* length including EOS			*/
)
{
	register int	nchars;

	if ((nchars = ENVGETS (c_sppstr(var), valuestr, &len_valuestr)) < 0)
	    return (nchars);
	else {
	    c_strpak (valuestr, outstr, maxch);
	    return (nchars > maxch ? maxch : nchars);
	}
}


/* C_ENVFIND -- Just like ENVGETS, except that a query will not be generated
** even if working interactively.
*/
int
c_envfind (
  char	*var,			/* name of variable to be fetched	*/
  char	*outstr,		/* output string			*/
  int	maxch			/* length including EOS			*/
)
{
	register int	nchars;

	if ((nchars = ENVFIND (c_sppstr(var), valuestr, &len_valuestr)) < 0)
	    return (nchars);
	else {
	    c_strpak (valuestr, outstr, maxch);
	    return (nchars > maxch ? maxch : nchars);
	}
}


/* C_ENVGETB -- Return the boolean value of an environment variable.  An error
** action is taken if the variable is not found or the value string cannot
** be interpreted as a boolean value.
*/
int
c_envgetb (
  char	*var			/* name of variable to be fetched	*/
)
{
	return ((int) BTOI ((XINT) ENVGETB (c_sppstr(var))));
}


/* C_ENVGETI -- Return the integer value of an environment variable.  An error
** action is taken if the variable is not found or the value string cannot
** be interpreted as an integer value.
*/
int
c_envgeti (
  char	*var			/* name of variable to be fetched	*/
)
{
	return ((int) ENVGETI (c_sppstr(var)));
}


/* C_ENVPUTS -- Set or redefine the value of an environment variable.  If the
** variable is not defined a new entry is created.  If the variable is already
** defined but has a different value it is redefined, with the new definition
** having precedence over the former (redefinitions can be undone; see envmark
** and envfree).  If the variable is already defined and the new definition
** has the same value, it is ignored.
**
** Arguments:		set var = value
*/
void
c_envputs (
  char	*var,			/* name of variable to be set		*/
  char	*value			/* value string				*/
)
{
	ENVPUTS (c_sppstr(var), c_strupk (value, valuestr, SZ_VALUESTR));
}


/* C_ENVRESET -- Reset (overwrite) the value of an environment variable.
** If the variable is not defined a new entry is created.
*/
void
c_envreset (
  char	*var,			/* name of variable to be set		*/
  char	*value			/* value string				*/
)
{
	ENVRESET (c_sppstr(var), c_strupk (value, valuestr, SZ_VALUESTR));
}