|
| 1 | +#if HAVE_CONFIG_H |
| 2 | +# include <config.h> |
| 3 | +#endif |
| 4 | + |
| 5 | +#include <stdlib.h> |
| 6 | +#include <stdio.h> |
| 7 | + |
| 8 | +#include "dat1.h" |
| 9 | + |
| 10 | + |
| 11 | +/* |
| 12 | +*+ |
| 13 | +* Name: |
| 14 | +* dat1Getenv |
| 15 | +
|
| 16 | +* Purpose: |
| 17 | +* Obtain an integer value from an environment variable. |
| 18 | +
|
| 19 | +* Invocation: |
| 20 | +* dat1Getenv( varname, def, val ); |
| 21 | +
|
| 22 | +* Description: |
| 23 | +* This function obtains the value of a specified environment variable |
| 24 | +* as an integer. If the variable is not defined, or its value does not |
| 25 | +* make sense as an integer, or any other error occurs, then a default |
| 26 | +* value is returned instead. |
| 27 | +
|
| 28 | +* Parameters: |
| 29 | +* const char *varname |
| 30 | +* Pointer to a null-terminated character string giving the name of |
| 31 | +* the environment variable. The use of upper case is recommended. |
| 32 | +* int def |
| 33 | +* The default value to be used if an integer value cannot be |
| 34 | +* obtained from the environment variable. |
| 35 | +* int *val |
| 36 | +* Pointer to an integer in which the result will be returned. |
| 37 | +
|
| 38 | +* Notes: |
| 39 | +* This routine does not perform error checking. |
| 40 | +
|
| 41 | +* Authors: |
| 42 | +* RFWS: R.F. Warren-Smith (STARLINK) |
| 43 | +* {@enter_new_authors_here@} |
| 44 | +
|
| 45 | +* History: |
| 46 | +* 25-FEB-1992 (RFWS): |
| 47 | +* Original version. |
| 48 | +* {@enter_changes_here@} |
| 49 | +
|
| 50 | +* Licence: |
| 51 | +* This program is free software; you can redistribute it and/or |
| 52 | +* modify it under the terms of the GNU General Public License as |
| 53 | +* published by the Free Software Foundation; either version 3 of |
| 54 | +* the License, or (at your option) any later version. |
| 55 | +* |
| 56 | +* This program is distributed in the hope that it will be |
| 57 | +* useful, but WITHOUT ANY WARRANTY; without even the implied |
| 58 | +* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 59 | +* PURPOSE. See the GNU General Public License for more details. |
| 60 | +* |
| 61 | +* You should have received a copy of the GNU General Public License |
| 62 | +* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 63 | +
|
| 64 | +*- |
| 65 | +*/ |
| 66 | +void dat1Getenv( const char *varname, int def, int *val ) { |
| 67 | + |
| 68 | + /* Local Variables: */ |
| 69 | + char *txt; /* Pointer to translation value */ |
| 70 | + |
| 71 | + /* Obtain a pointer to the environment variable's value. */ |
| 72 | + txt = getenv( varname ); |
| 73 | + |
| 74 | + /* If no value was obtained, then use the default. */ |
| 75 | + if ( txt == NULL ) { |
| 76 | + |
| 77 | + *val = def; |
| 78 | + |
| 79 | + /* Otherwise, try to read an integer value from it. If unsuccessful, then */ |
| 80 | + /* use the default. */ |
| 81 | + } else if ( sscanf( txt, "%d", val ) != 1 ) { |
| 82 | + *val = def; |
| 83 | + } |
| 84 | + |
| 85 | + return; |
| 86 | +} |
0 commit comments