1 /* Implementation of the GETENV g77, and
2 GET_ENVIRONMENT_VARIABLE F2003, intrinsics.
3 Copyright (C) 2004 Free Software Foundation, Inc.
4 Contributed by Janne Blomqvist.
6 This file is part of the GNU Fortran 95 runtime library (libgfortran).
8 Libgfortran is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 Libgfortran is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with libgfor; see the file COPYING.LIB. If not,
20 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 #include <sys/types.h>
27 #include "libgfortran.h"
30 /* GETENV (NAME, VALUE), g77 intrinsic for retrieving the value of
31 an environment variable. The name of the variable is specified in
32 NAME, and the result is stored into VALUE. */
35 prefix(getenv
) (char * name
,
37 gfc_charlen_type name_len
,
38 gfc_charlen_type value_len
)
44 if (name
== NULL
|| value
== NULL
)
45 runtime_error ("Both arguments to getenv are mandatory.");
47 if (value_len
< 1 || name_len
< 1)
48 runtime_error ("Zero length string(s) passed to getenv.");
50 memset (value
, ' ', value_len
); /* Blank the string. */
52 /* Trim trailing spaces from name. */
53 while (name_len
> 0 && name
[name_len
- 1] == ' ')
56 /* Make a null terminated copy of the string. */
57 name_nt
= gfc_alloca (name_len
+ 1);
58 memcpy (name_nt
, name
, name_len
);
59 name_nt
[name_len
] = '\0';
61 res
= getenv(name_nt
);
63 /* If res is NULL, it means that the environment variable didn't
64 exist, so just return. */
68 res_len
= strlen(res
);
69 if (value_len
< res_len
)
70 memcpy (value
, res
, value_len
);
72 memcpy (value
, res
, res_len
);
76 /* GET_ENVIRONMENT_VARIABLE (name, [value, length, status, trim_name])
77 is a F2003 intrinsic for getting an environment variable. */
79 /* Status codes specifyed by the standard. */
81 #define GFC_VALUE_TOO_SHORT -1
82 #define GFC_NAME_DOES_NOT_EXIST 1
84 /* This is also specified by the standard and means that the
85 processor doesn't support environment variables. At the moment,
86 gfortran doesn't use it. */
87 #define GFC_NOT_SUPPORTED 2
89 /* Processor-specific failure code. */
90 #define GFC_FAILURE 42
93 prefix(get_environment_variable_i4
)
97 GFC_INTEGER_4
*length
,
98 GFC_INTEGER_4
*status
,
99 GFC_LOGICAL_4
*trim_name
,
100 gfc_charlen_type name_len
,
101 gfc_charlen_type value_len
)
103 int stat
= GFC_SUCCESS
, res_len
= 0;
108 runtime_error ("Name is required for get_environment_variable.");
110 if (value
== NULL
&& length
== NULL
&& status
== NULL
&& trim_name
== NULL
)
114 runtime_error ("Zero-length string passed as name to "
115 "get_environment_variable.");
120 runtime_error ("Zero-length string passed as value to "
121 "get_environment_variable.");
123 memset (value
, ' ', value_len
); /* Blank the string. */
126 if ((!trim_name
) || *trim_name
)
128 /* Trim trailing spaces from name. */
129 while (name_len
> 0 && name
[name_len
- 1] == ' ')
132 /* Make a null terminated copy of the name. */
133 name_nt
= gfc_alloca (name_len
+ 1);
134 memcpy (name_nt
, name
, name_len
);
135 name_nt
[name_len
] = '\0';
137 res
= getenv(name_nt
);
140 stat
= GFC_NAME_DOES_NOT_EXIST
;
143 res_len
= strlen(res
);
146 if (value_len
< res_len
)
148 memcpy (value
, res
, value_len
);
149 stat
= GFC_VALUE_TOO_SHORT
;
152 memcpy (value
, res
, res_len
);
164 /* INTEGER*8 wrapper for get_environment_variable. */
167 prefix(get_environment_variable_i8
)
171 GFC_INTEGER_8
*length
,
172 GFC_INTEGER_8
*status
,
173 GFC_LOGICAL_8
*trim_name
,
174 gfc_charlen_type name_len
,
175 gfc_charlen_type value_len
)
177 GFC_INTEGER_4 length4
, status4
;
178 GFC_LOGICAL_4 trim_name4
;
181 trim_name4
= *trim_name
;
183 prefix (get_environment_variable_i4
) (name
, value
, &length4
, &status4
,
184 &trim_name4
, name_len
, value_len
);