re PR libfortran/16137 (Fortran compiler unable to produce executables as libfortran...
[gcc.git] / libgfortran / intrinsics / env.c
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.
5
6 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7
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.
12
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.
17
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. */
22
23 #include "config.h"
24 #include <sys/types.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "libgfortran.h"
28
29
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. */
33
34 void
35 prefix(getenv) (char * name,
36 char * value,
37 gfc_charlen_type name_len,
38 gfc_charlen_type value_len)
39 {
40 char *name_nt;
41 char *res = NULL;
42 int res_len;
43
44 if (name == NULL || value == NULL)
45 runtime_error ("Both arguments to getenv are mandatory.");
46
47 if (value_len < 1 || name_len < 1)
48 runtime_error ("Zero length string(s) passed to getenv.");
49 else
50 memset (value, ' ', value_len); /* Blank the string. */
51
52 /* Trim trailing spaces from name. */
53 while (name_len > 0 && name[name_len - 1] == ' ')
54 name_len--;
55
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';
60
61 res = getenv(name_nt);
62
63 /* If res is NULL, it means that the environment variable didn't
64 exist, so just return. */
65 if (res == NULL)
66 return;
67
68 res_len = strlen(res);
69 if (value_len < res_len)
70 memcpy (value, res, value_len);
71 else
72 memcpy (value, res, res_len);
73 }
74
75
76 /* GET_ENVIRONMENT_VARIABLE (name, [value, length, status, trim_name])
77 is a F2003 intrinsic for getting an environment variable. */
78
79 /* Status codes specifyed by the standard. */
80 #define GFC_SUCCESS 0
81 #define GFC_VALUE_TOO_SHORT -1
82 #define GFC_NAME_DOES_NOT_EXIST 1
83
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
88
89 /* Processor-specific failure code. */
90 #define GFC_FAILURE 42
91
92 void
93 prefix(get_environment_variable_i4)
94 (
95 char *name,
96 char *value,
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)
102 {
103 int stat = GFC_SUCCESS, res_len = 0;
104 char *name_nt;
105 char *res;
106
107 if (name == NULL)
108 runtime_error ("Name is required for get_environment_variable.");
109
110 if (value == NULL && length == NULL && status == NULL && trim_name == NULL)
111 return;
112
113 if (name_len < 1)
114 runtime_error ("Zero-length string passed as name to "
115 "get_environment_variable.");
116
117 if (value != NULL)
118 {
119 if (value_len < 1)
120 runtime_error ("Zero-length string passed as value to "
121 "get_environment_variable.");
122 else
123 memset (value, ' ', value_len); /* Blank the string. */
124 }
125
126 if ((!trim_name) || *trim_name)
127 {
128 /* Trim trailing spaces from name. */
129 while (name_len > 0 && name[name_len - 1] == ' ')
130 name_len--;
131 }
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';
136
137 res = getenv(name_nt);
138
139 if (res == NULL)
140 stat = GFC_NAME_DOES_NOT_EXIST;
141 else
142 {
143 res_len = strlen(res);
144 if (value != NULL)
145 {
146 if (value_len < res_len)
147 {
148 memcpy (value, res, value_len);
149 stat = GFC_VALUE_TOO_SHORT;
150 }
151 else
152 memcpy (value, res, res_len);
153 }
154 }
155
156 if (status != NULL)
157 *status = stat;
158
159 if (length != NULL)
160 *length = res_len;
161 }
162
163
164 /* INTEGER*8 wrapper for get_environment_variable. */
165
166 void
167 prefix(get_environment_variable_i8)
168 (
169 char *name,
170 char *value,
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)
176 {
177 GFC_INTEGER_4 length4, status4;
178 GFC_LOGICAL_4 trim_name4;
179
180 if (trim_name)
181 trim_name4 = *trim_name;
182
183 prefix (get_environment_variable_i4) (name, value, &length4, &status4,
184 &trim_name4, name_len, value_len);
185
186 if (length)
187 *length = length4;
188
189 if (status)
190 *status = status4;
191 }