-
Notifications
You must be signed in to change notification settings - Fork 1
/
dat1Getenv.c
86 lines (67 loc) · 2.33 KB
/
dat1Getenv.c
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
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include "dat1.h"
/*
*+
* Name:
* dat1Getenv
* Purpose:
* Obtain an integer value from an environment variable.
* Invocation:
* dat1Getenv( varname, def, val );
* Description:
* This function obtains the value of a specified environment variable
* as an integer. If the variable is not defined, or its value does not
* make sense as an integer, or any other error occurs, then a default
* value is returned instead.
* Parameters:
* const char *varname
* Pointer to a null-terminated character string giving the name of
* the environment variable. The use of upper case is recommended.
* int def
* The default value to be used if an integer value cannot be
* obtained from the environment variable.
* int *val
* Pointer to an integer in which the result will be returned.
* Notes:
* This routine does not perform error checking.
* Authors:
* RFWS: R.F. Warren-Smith (STARLINK)
* {@enter_new_authors_here@}
* History:
* 25-FEB-1992 (RFWS):
* Original version.
* {@enter_changes_here@}
* Licence:
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*-
*/
void dat1Getenv( const char *varname, int def, int *val ) {
/* Local Variables: */
char *txt; /* Pointer to translation value */
/* Obtain a pointer to the environment variable's value. */
txt = getenv( varname );
/* If no value was obtained, then use the default. */
if ( txt == NULL ) {
*val = def;
/* Otherwise, try to read an integer value from it. If unsuccessful, then */
/* use the default. */
} else if ( sscanf( txt, "%d", val ) != 1 ) {
*val = def;
}
return;
}