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. */
34 void PREFIX(getenv
) (char *, char *, gfc_charlen_type
, gfc_charlen_type
);
35 export_proto_np(PREFIX(getenv
));
38 PREFIX(getenv
) (char * name
, char * value
, gfc_charlen_type name_len
,
39 gfc_charlen_type value_len
)
45 if (name
== NULL
|| value
== NULL
)
46 runtime_error ("Both arguments to getenv are mandatory.");
48 if (value_len
< 1 || name_len
< 1)
49 runtime_error ("Zero length string(s) passed to getenv.");
51 memset (value
, ' ', value_len
); /* Blank the string. */
53 /* Trim trailing spaces from name. */
54 while (name_len
> 0 && name
[name_len
- 1] == ' ')
57 /* Make a null terminated copy of the string. */
58 name_nt
= gfc_alloca (name_len
+ 1);
59 memcpy (name_nt
, name
, name_len
);
60 name_nt
[name_len
] = '\0';
62 res
= getenv(name_nt
);
64 /* If res is NULL, it means that the environment variable didn't
65 exist, so just return. */
69 res_len
= strlen(res
);
70 if (value_len
< res_len
)
71 memcpy (value
, res
, value_len
);
73 memcpy (value
, res
, res_len
);
77 /* GET_ENVIRONMENT_VARIABLE (name, [value, length, status, trim_name])
78 is a F2003 intrinsic for getting an environment variable. */
80 /* Status codes specifyed by the standard. */
82 #define GFC_VALUE_TOO_SHORT -1
83 #define GFC_NAME_DOES_NOT_EXIST 1
85 /* This is also specified by the standard and means that the
86 processor doesn't support environment variables. At the moment,
87 gfortran doesn't use it. */
88 #define GFC_NOT_SUPPORTED 2
90 /* Processor-specific failure code. */
91 #define GFC_FAILURE 42
93 extern void get_environment_variable_i4 (char *, char *, GFC_INTEGER_4
*,
94 GFC_INTEGER_4
*, GFC_LOGICAL_4
*,
95 gfc_charlen_type
, gfc_charlen_type
);
96 iexport_proto(get_environment_variable_i4
);
99 get_environment_variable_i4 (char *name
, char *value
, GFC_INTEGER_4
*length
,
100 GFC_INTEGER_4
*status
, GFC_LOGICAL_4
*trim_name
,
101 gfc_charlen_type name_len
,
102 gfc_charlen_type value_len
)
104 int stat
= GFC_SUCCESS
, res_len
= 0;
109 runtime_error ("Name is required for get_environment_variable.");
111 if (value
== NULL
&& length
== NULL
&& status
== NULL
&& trim_name
== NULL
)
115 runtime_error ("Zero-length string passed as name to "
116 "get_environment_variable.");
121 runtime_error ("Zero-length string passed as value to "
122 "get_environment_variable.");
124 memset (value
, ' ', value_len
); /* Blank the string. */
127 if ((!trim_name
) || *trim_name
)
129 /* Trim trailing spaces from name. */
130 while (name_len
> 0 && name
[name_len
- 1] == ' ')
133 /* Make a null terminated copy of the name. */
134 name_nt
= gfc_alloca (name_len
+ 1);
135 memcpy (name_nt
, name
, name_len
);
136 name_nt
[name_len
] = '\0';
138 res
= getenv(name_nt
);
141 stat
= GFC_NAME_DOES_NOT_EXIST
;
144 res_len
= strlen(res
);
147 if (value_len
< res_len
)
149 memcpy (value
, res
, value_len
);
150 stat
= GFC_VALUE_TOO_SHORT
;
153 memcpy (value
, res
, res_len
);
163 iexport(get_environment_variable_i4
);
166 /* INTEGER*8 wrapper for get_environment_variable. */
168 extern void get_environment_variable_i8 (char *, char *, GFC_INTEGER_8
*,
169 GFC_INTEGER_8
*, GFC_LOGICAL_8
*,
170 gfc_charlen_type
, gfc_charlen_type
);
171 export_proto(get_environment_variable_i8
);
174 get_environment_variable_i8 (char *name
, char *value
, GFC_INTEGER_8
*length
,
175 GFC_INTEGER_8
*status
, GFC_LOGICAL_8
*trim_name
,
176 gfc_charlen_type name_len
,
177 gfc_charlen_type value_len
)
179 GFC_INTEGER_4 length4
, status4
;
180 GFC_LOGICAL_4 trim_name4
;
183 trim_name4
= *trim_name
;
185 get_environment_variable_i4 (name
, value
, &length4
, &status4
,
186 &trim_name4
, name_len
, value_len
);