]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgfortran/intrinsics/env.c
minloc1.m4: Update copyright year and ajust headers order.
[thirdparty/gcc.git] / libgfortran / intrinsics / env.c
1 /* Implementation of the GETENV g77, and
2 GET_ENVIRONMENT_VARIABLE F2003, intrinsics.
3 Copyright (C) 2004, 2007 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 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.
12
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
20 executable.)
21
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.
26
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. */
31
32 #include "libgfortran.h"
33 #include <stdlib.h>
34 #include <string.h>
35
36
37 /* GETENV (NAME, VALUE), g77 intrinsic for retrieving the value of
38 an environment variable. The name of the variable is specified in
39 NAME, and the result is stored into VALUE. */
40
41 void PREFIX(getenv) (char *, char *, gfc_charlen_type, gfc_charlen_type);
42 export_proto_np(PREFIX(getenv));
43
44 void
45 PREFIX(getenv) (char * name, char * value, gfc_charlen_type name_len,
46 gfc_charlen_type value_len)
47 {
48 char *name_nt;
49 char *res = NULL;
50 int res_len;
51
52 if (name == NULL || value == NULL)
53 runtime_error ("Both arguments to getenv are mandatory.");
54
55 if (value_len < 1 || name_len < 1)
56 runtime_error ("Zero length string(s) passed to getenv.");
57 else
58 memset (value, ' ', value_len); /* Blank the string. */
59
60 /* Trim trailing spaces from name. */
61 while (name_len > 0 && name[name_len - 1] == ' ')
62 name_len--;
63
64 /* Make a null terminated copy of the string. */
65 name_nt = gfc_alloca (name_len + 1);
66 memcpy (name_nt, name, name_len);
67 name_nt[name_len] = '\0';
68
69 res = getenv(name_nt);
70
71 /* If res is NULL, it means that the environment variable didn't
72 exist, so just return. */
73 if (res == NULL)
74 return;
75
76 res_len = strlen(res);
77 if (value_len < res_len)
78 memcpy (value, res, value_len);
79 else
80 memcpy (value, res, res_len);
81 }
82
83
84 /* GET_ENVIRONMENT_VARIABLE (name, [value, length, status, trim_name])
85 is a F2003 intrinsic for getting an environment variable. */
86
87 /* Status codes specifyed by the standard. */
88 #define GFC_SUCCESS 0
89 #define GFC_VALUE_TOO_SHORT -1
90 #define GFC_NAME_DOES_NOT_EXIST 1
91
92 /* This is also specified by the standard and means that the
93 processor doesn't support environment variables. At the moment,
94 gfortran doesn't use it. */
95 #define GFC_NOT_SUPPORTED 2
96
97 /* Processor-specific failure code. */
98 #define GFC_FAILURE 42
99
100 extern void get_environment_variable_i4 (char *, char *, GFC_INTEGER_4 *,
101 GFC_INTEGER_4 *, GFC_LOGICAL_4 *,
102 gfc_charlen_type, gfc_charlen_type);
103 iexport_proto(get_environment_variable_i4);
104
105 void
106 get_environment_variable_i4 (char *name, char *value, GFC_INTEGER_4 *length,
107 GFC_INTEGER_4 *status, GFC_LOGICAL_4 *trim_name,
108 gfc_charlen_type name_len,
109 gfc_charlen_type value_len)
110 {
111 int stat = GFC_SUCCESS, res_len = 0;
112 char *name_nt;
113 char *res;
114
115 if (name == NULL)
116 runtime_error ("Name is required for get_environment_variable.");
117
118 if (value == NULL && length == NULL && status == NULL && trim_name == NULL)
119 return;
120
121 if (name_len < 1)
122 runtime_error ("Zero-length string passed as name to "
123 "get_environment_variable.");
124
125 if (value != NULL)
126 {
127 if (value_len < 1)
128 runtime_error ("Zero-length string passed as value to "
129 "get_environment_variable.");
130 else
131 memset (value, ' ', value_len); /* Blank the string. */
132 }
133
134 if ((!trim_name) || *trim_name)
135 {
136 /* Trim trailing spaces from name. */
137 while (name_len > 0 && name[name_len - 1] == ' ')
138 name_len--;
139 }
140 /* Make a null terminated copy of the name. */
141 name_nt = gfc_alloca (name_len + 1);
142 memcpy (name_nt, name, name_len);
143 name_nt[name_len] = '\0';
144
145 res = getenv(name_nt);
146
147 if (res == NULL)
148 stat = GFC_NAME_DOES_NOT_EXIST;
149 else
150 {
151 res_len = strlen(res);
152 if (value != NULL)
153 {
154 if (value_len < res_len)
155 {
156 memcpy (value, res, value_len);
157 stat = GFC_VALUE_TOO_SHORT;
158 }
159 else
160 memcpy (value, res, res_len);
161 }
162 }
163
164 if (status != NULL)
165 *status = stat;
166
167 if (length != NULL)
168 *length = res_len;
169 }
170 iexport(get_environment_variable_i4);
171
172
173 /* INTEGER*8 wrapper for get_environment_variable. */
174
175 extern void get_environment_variable_i8 (char *, char *, GFC_INTEGER_8 *,
176 GFC_INTEGER_8 *, GFC_LOGICAL_8 *,
177 gfc_charlen_type, gfc_charlen_type);
178 export_proto(get_environment_variable_i8);
179
180 void
181 get_environment_variable_i8 (char *name, char *value, GFC_INTEGER_8 *length,
182 GFC_INTEGER_8 *status, GFC_LOGICAL_8 *trim_name,
183 gfc_charlen_type name_len,
184 gfc_charlen_type value_len)
185 {
186 GFC_INTEGER_4 length4, status4;
187 GFC_LOGICAL_4 trim_name4;
188
189 if (trim_name)
190 trim_name4 = *trim_name;
191
192 get_environment_variable_i4 (name, value, &length4, &status4,
193 &trim_name4, name_len, value_len);
194
195 if (length)
196 *length = length4;
197
198 if (status)
199 *status = status4;
200 }