1 /* Implementation of the GETENV g77, and
2 GET_ENVIRONMENT_VARIABLE F2003, intrinsics.
3 Copyright (C) 2004-2015 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 3 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 General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
27 #include "libgfortran.h"
32 /* GETENV (NAME, VALUE), g77 intrinsic for retrieving the value of
33 an environment variable. The name of the variable is specified in
34 NAME, and the result is stored into VALUE. */
36 void PREFIX(getenv
) (char *, char *, gfc_charlen_type
, gfc_charlen_type
);
37 export_proto_np(PREFIX(getenv
));
40 PREFIX(getenv
) (char * name
, char * value
, gfc_charlen_type name_len
,
41 gfc_charlen_type value_len
)
46 if (name
== NULL
|| value
== NULL
)
47 runtime_error ("Both arguments to getenv are mandatory.");
49 if (value_len
< 1 || name_len
< 1)
50 runtime_error ("Zero length string(s) passed to getenv.");
52 memset (value
, ' ', value_len
); /* Blank the string. */
54 /* Make a null terminated copy of the string. */
55 name_nt
= fc_strdup (name
, name_len
);
57 res
= getenv(name_nt
);
61 /* If res is NULL, it means that the environment variable didn't
62 exist, so just return. */
66 cf_strcpy (value
, value_len
, res
);
70 /* GET_ENVIRONMENT_VARIABLE (name, [value, length, status, trim_name])
71 is a F2003 intrinsic for getting an environment variable. */
73 /* Status codes specifyed by the standard. */
75 #define GFC_VALUE_TOO_SHORT -1
76 #define GFC_NAME_DOES_NOT_EXIST 1
78 /* This is also specified by the standard and means that the
79 processor doesn't support environment variables. At the moment,
80 gfortran doesn't use it. */
81 #define GFC_NOT_SUPPORTED 2
83 /* Processor-specific failure code. */
84 #define GFC_FAILURE 42
86 extern void get_environment_variable_i4 (char *, char *, GFC_INTEGER_4
*,
87 GFC_INTEGER_4
*, GFC_LOGICAL_4
*,
88 gfc_charlen_type
, gfc_charlen_type
);
89 iexport_proto(get_environment_variable_i4
);
92 get_environment_variable_i4 (char *name
, char *value
, GFC_INTEGER_4
*length
,
93 GFC_INTEGER_4
*status
, GFC_LOGICAL_4
*trim_name
,
94 gfc_charlen_type name_len
,
95 gfc_charlen_type value_len
)
97 int stat
= GFC_SUCCESS
, res_len
= 0;
102 runtime_error ("Name is required for get_environment_variable.");
104 if (value
== NULL
&& length
== NULL
&& status
== NULL
&& trim_name
== NULL
)
108 runtime_error ("Zero-length string passed as name to "
109 "get_environment_variable.");
114 runtime_error ("Zero-length string passed as value to "
115 "get_environment_variable.");
117 memset (value
, ' ', value_len
); /* Blank the string. */
120 if ((!trim_name
) || *trim_name
)
121 name_nt
= fc_strdup (name
, name_len
);
123 name_nt
= fc_strdup_notrim (name
, name_len
);
125 res
= getenv(name_nt
);
130 stat
= GFC_NAME_DOES_NOT_EXIST
;
133 res_len
= strlen(res
);
136 if (value_len
< res_len
)
138 memcpy (value
, res
, value_len
);
139 stat
= GFC_VALUE_TOO_SHORT
;
142 memcpy (value
, res
, res_len
);
152 iexport(get_environment_variable_i4
);
155 /* INTEGER*8 wrapper for get_environment_variable. */
157 extern void get_environment_variable_i8 (char *, char *, GFC_INTEGER_8
*,
158 GFC_INTEGER_8
*, GFC_LOGICAL_8
*,
159 gfc_charlen_type
, gfc_charlen_type
);
160 export_proto(get_environment_variable_i8
);
163 get_environment_variable_i8 (char *name
, char *value
, GFC_INTEGER_8
*length
,
164 GFC_INTEGER_8
*status
, GFC_LOGICAL_8
*trim_name
,
165 gfc_charlen_type name_len
,
166 gfc_charlen_type value_len
)
168 GFC_INTEGER_4 length4
, status4
;
169 GFC_LOGICAL_4 trim_name4
;
172 trim_name4
= *trim_name
;
174 get_environment_variable_i4 (name
, value
, &length4
, &status4
,
175 trim_name
? &trim_name4
: NULL
,
176 name_len
, value_len
);