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 General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combine
22 Libgfortran is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public
28 License along with libgfortran; see the file COPYING. If not,
29 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
30 Boston, MA 02110-1301, USA. */
35 #include "libgfortran.h"
38 /* GETENV (NAME, VALUE), g77 intrinsic for retrieving the value of
39 an environment variable. The name of the variable is specified in
40 NAME, and the result is stored into VALUE. */
42 void PREFIX(getenv
) (char *, char *, gfc_charlen_type
, gfc_charlen_type
);
43 export_proto_np(PREFIX(getenv
));
46 PREFIX(getenv
) (char * name
, char * value
, gfc_charlen_type name_len
,
47 gfc_charlen_type value_len
)
53 if (name
== NULL
|| value
== NULL
)
54 runtime_error ("Both arguments to getenv are mandatory.");
56 if (value_len
< 1 || name_len
< 1)
57 runtime_error ("Zero length string(s) passed to getenv.");
59 memset (value
, ' ', value_len
); /* Blank the string. */
61 /* Trim trailing spaces from name. */
62 while (name_len
> 0 && name
[name_len
- 1] == ' ')
65 /* Make a null terminated copy of the string. */
66 name_nt
= gfc_alloca (name_len
+ 1);
67 memcpy (name_nt
, name
, name_len
);
68 name_nt
[name_len
] = '\0';
70 res
= getenv(name_nt
);
72 /* If res is NULL, it means that the environment variable didn't
73 exist, so just return. */
77 res_len
= strlen(res
);
78 if (value_len
< res_len
)
79 memcpy (value
, res
, value_len
);
81 memcpy (value
, res
, res_len
);
85 /* GET_ENVIRONMENT_VARIABLE (name, [value, length, status, trim_name])
86 is a F2003 intrinsic for getting an environment variable. */
88 /* Status codes specifyed by the standard. */
90 #define GFC_VALUE_TOO_SHORT -1
91 #define GFC_NAME_DOES_NOT_EXIST 1
93 /* This is also specified by the standard and means that the
94 processor doesn't support environment variables. At the moment,
95 gfortran doesn't use it. */
96 #define GFC_NOT_SUPPORTED 2
98 /* Processor-specific failure code. */
99 #define GFC_FAILURE 42
101 extern void get_environment_variable_i4 (char *, char *, GFC_INTEGER_4
*,
102 GFC_INTEGER_4
*, GFC_LOGICAL_4
*,
103 gfc_charlen_type
, gfc_charlen_type
);
104 iexport_proto(get_environment_variable_i4
);
107 get_environment_variable_i4 (char *name
, char *value
, GFC_INTEGER_4
*length
,
108 GFC_INTEGER_4
*status
, GFC_LOGICAL_4
*trim_name
,
109 gfc_charlen_type name_len
,
110 gfc_charlen_type value_len
)
112 int stat
= GFC_SUCCESS
, res_len
= 0;
117 runtime_error ("Name is required for get_environment_variable.");
119 if (value
== NULL
&& length
== NULL
&& status
== NULL
&& trim_name
== NULL
)
123 runtime_error ("Zero-length string passed as name to "
124 "get_environment_variable.");
129 runtime_error ("Zero-length string passed as value to "
130 "get_environment_variable.");
132 memset (value
, ' ', value_len
); /* Blank the string. */
135 if ((!trim_name
) || *trim_name
)
137 /* Trim trailing spaces from name. */
138 while (name_len
> 0 && name
[name_len
- 1] == ' ')
141 /* Make a null terminated copy of the name. */
142 name_nt
= gfc_alloca (name_len
+ 1);
143 memcpy (name_nt
, name
, name_len
);
144 name_nt
[name_len
] = '\0';
146 res
= getenv(name_nt
);
149 stat
= GFC_NAME_DOES_NOT_EXIST
;
152 res_len
= strlen(res
);
155 if (value_len
< res_len
)
157 memcpy (value
, res
, value_len
);
158 stat
= GFC_VALUE_TOO_SHORT
;
161 memcpy (value
, res
, res_len
);
171 iexport(get_environment_variable_i4
);
174 /* INTEGER*8 wrapper for get_environment_variable. */
176 extern void get_environment_variable_i8 (char *, char *, GFC_INTEGER_8
*,
177 GFC_INTEGER_8
*, GFC_LOGICAL_8
*,
178 gfc_charlen_type
, gfc_charlen_type
);
179 export_proto(get_environment_variable_i8
);
182 get_environment_variable_i8 (char *name
, char *value
, GFC_INTEGER_8
*length
,
183 GFC_INTEGER_8
*status
, GFC_LOGICAL_8
*trim_name
,
184 gfc_charlen_type name_len
,
185 gfc_charlen_type value_len
)
187 GFC_INTEGER_4 length4
, status4
;
188 GFC_LOGICAL_4 trim_name4
;
191 trim_name4
= *trim_name
;
193 get_environment_variable_i4 (name
, value
, &length4
, &status4
,
194 &trim_name4
, name_len
, value_len
);