summaryrefslogtreecommitdiff
path: root/libgfortran
diff options
context:
space:
mode:
authorpbrook <pbrook@138bc75d-0d04-0410-961f-82ee72b054a4>2004-08-06 21:47:03 +0000
committerpbrook <pbrook@138bc75d-0d04-0410-961f-82ee72b054a4>2004-08-06 21:47:03 +0000
commit96901ab7a8210e7384658485cb0ecbf4def87847 (patch)
treec74733622978a45c69d005d29049be3d1ac7b4da /libgfortran
parent4007e2c54decc22beeef862535d0a619b0247ecf (diff)
downloadgcc-96901ab7a8210e7384658485cb0ecbf4def87847.tar.gz
2004-08-06 Janne Blomqvist <jblomqvi@cc.hut.fi>
* intrinsic.c (add_subroutines): Add getenv and get_environment_variable. (add_sym_5s): New function. * intrinsic.h (gfc_resolve_get_environment_variable): Add prototype. * iresolve.c (gfc_resolve_get_environment_variable): New function. libgfortran/ * intrinsics/env.c: New file. * Makefile.am: Add env.c to build. * Makefile.in: Regenerate. testsuite/ * gfortran.dg/getenv_1.f90: New test. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@85656 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libgfortran')
-rw-r--r--libgfortran/ChangeLog6
-rw-r--r--libgfortran/Makefile.am1
-rw-r--r--libgfortran/Makefile.in12
-rw-r--r--libgfortran/intrinsics/env.c181
4 files changed, 199 insertions, 1 deletions
diff --git a/libgfortran/ChangeLog b/libgfortran/ChangeLog
index 4a06ac5e8fe..fc7f6654c2c 100644
--- a/libgfortran/ChangeLog
+++ b/libgfortran/ChangeLog
@@ -1,3 +1,9 @@
+2004-08-06 Janne Blomqvist <jblomqvi@cc.hut.fi>
+
+ * intrinsics/env.c: New file.
+ * Makefile.am: Add env.c to build.
+ * Makefile.in: Regenerate.
+
2004-08-05 Victor Leikehman <lei@il.ibm.com>
PR libgfortran/16704
diff --git a/libgfortran/Makefile.am b/libgfortran/Makefile.am
index 450a9a4e868..35332dc4c07 100644
--- a/libgfortran/Makefile.am
+++ b/libgfortran/Makefile.am
@@ -43,6 +43,7 @@ intrinsics/c99_functions.c \
intrinsics/cpu_time.c \
intrinsics/cshift0.c \
intrinsics/date_and_time.c \
+intrinsics/env.c \
intrinsics/eoshift0.c \
intrinsics/eoshift2.c \
intrinsics/etime.c \
diff --git a/libgfortran/Makefile.in b/libgfortran/Makefile.in
index 754c3ea4027..b0fa2ec7bba 100644
--- a/libgfortran/Makefile.in
+++ b/libgfortran/Makefile.in
@@ -119,7 +119,7 @@ am__objects_32 = backspace.lo close.lo endfile.lo format.lo inquire.lo \
list_read.lo lock.lo open.lo read.lo rewind.lo transfer.lo \
unit.lo unix.lo write.lo
am__objects_33 = associated.lo abort.lo args.lo c99_functions.lo \
- cpu_time.lo cshift0.lo date_and_time.lo eoshift0.lo \
+ cpu_time.lo cshift0.lo date_and_time.lo env.lo eoshift0.lo \
eoshift2.lo etime.lo ishftc.lo pack_generic.lo size.lo \
spread_generic.lo string_intrinsics.lo rand.lo random.lo \
reshape_generic.lo reshape_packed.lo selected_kind.lo \
@@ -314,6 +314,7 @@ intrinsics/c99_functions.c \
intrinsics/cpu_time.c \
intrinsics/cshift0.c \
intrinsics/date_and_time.c \
+intrinsics/env.c \
intrinsics/eoshift0.c \
intrinsics/eoshift2.c \
intrinsics/etime.c \
@@ -2025,6 +2026,15 @@ date_and_time.obj: intrinsics/date_and_time.c
date_and_time.lo: intrinsics/date_and_time.c
$(LIBTOOL) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o date_and_time.lo `test -f 'intrinsics/date_and_time.c' || echo '$(srcdir)/'`intrinsics/date_and_time.c
+env.o: intrinsics/env.c
+ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o env.o `test -f 'intrinsics/env.c' || echo '$(srcdir)/'`intrinsics/env.c
+
+env.obj: intrinsics/env.c
+ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o env.obj `if test -f 'intrinsics/env.c'; then $(CYGPATH_W) 'intrinsics/env.c'; else $(CYGPATH_W) '$(srcdir)/intrinsics/env.c'; fi`
+
+env.lo: intrinsics/env.c
+ $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o env.lo `test -f 'intrinsics/env.c' || echo '$(srcdir)/'`intrinsics/env.c
+
eoshift0.o: intrinsics/eoshift0.c
$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o eoshift0.o `test -f 'intrinsics/eoshift0.c' || echo '$(srcdir)/'`intrinsics/eoshift0.c
diff --git a/libgfortran/intrinsics/env.c b/libgfortran/intrinsics/env.c
new file mode 100644
index 00000000000..9898471844a
--- /dev/null
+++ b/libgfortran/intrinsics/env.c
@@ -0,0 +1,181 @@
+/* Implementation of the GETENV g77, and
+ GET_ENVIRONMENT_VARIABLE F2003, intrinsics.
+ Copyright (C) 2004 Free Software Foundation, Inc.
+ Contributed by Janne Blomqvist.
+
+This file is part of the GNU Fortran 95 runtime library (libgfortran).
+
+Libgfortran is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+Libgfortran is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with libgfor; see the file COPYING.LIB. If not,
+write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA. */
+
+#include "config.h"
+#include <sys/types.h>
+#include <stdlib.h>
+#include <string.h>
+#include "libgfortran.h"
+
+
+/* GETENV (NAME, VALUE), g77 intrinsic for retrieving the value of
+ an environment variable. The name of the variable is specified in
+ NAME, and the result is stored into VALUE. */
+
+void
+prefix(getenv) (char * name,
+ char * value,
+ gfc_strlen_type name_len,
+ gfc_strlen_type value_len)
+{
+
+ /* Make a null-terminated copy of the name string so that c library
+ functions work correctly. This is a C99 VLA, which ought to be
+ faster than malloc and free. */
+
+ char name_nt[name_len+1];
+
+ char *res = NULL;
+ int res_len;
+
+ if (name == NULL || value == NULL)
+ runtime_error ("Both arguments to getenv are mandatory.");
+
+ if (value_len < 1 || name_len < 1)
+ runtime_error ("Zero length string(s) passed to getenv.");
+ else
+ memset (value, ' ', value_len); /* Blank the string. */
+
+ memcpy (name_nt, name, name_len);
+ memset (&name_nt[name_len], '\0', 1);
+
+ res = getenv(name_nt);
+
+ /* If res is NULL, it means that the environment variable didn't
+ exist, so just return. */
+ if (res == NULL)
+ return;
+
+ res_len = strlen(res);
+ if (value_len < res_len)
+ memcpy (value, res, value_len);
+ else
+ memcpy (value, res, res_len);
+}
+
+
+/* GET_ENVIRONMENT_VARIABLE (name, [value, length, status, trim_name])
+ is a F2003 intrinsic for getting an environment variable. Note that as
+ Un*x doesn't consider trailing blanks in environment variables to be
+ significant, the trim_name argument has no meaning. */
+
+/* Status codes specifyed by the standard. */
+#define GFC_SUCCESS 0
+#define GFC_VALUE_TOO_SHORT -1
+#define GFC_NAME_DOES_NOT_EXIST 1
+
+/* This is also specified by the standard and means that the
+ processor doesn't support environment variables. At the moment,
+ gfortran doesn't use it. */
+#define GFC_NOT_SUPPORTED 2
+
+/* Processor-specific failure code. */
+#define GFC_FAILURE 42
+
+void
+prefix(get_environment_variable_i4)
+ (
+ char *name,
+ char *value,
+ GFC_INTEGER_4 *length,
+ GFC_INTEGER_4 *status,
+ GFC_LOGICAL_4 *trim_name,
+ gfc_strlen_type name_len,
+ gfc_strlen_type value_len)
+{
+ int stat = GFC_SUCCESS, res_len = 0;
+ char name_nt[name_len+1], *res;
+
+ if (name == NULL)
+ runtime_error ("Name is required for get_environment_variable.");
+
+ if (value == NULL && length == NULL && status == NULL && trim_name == NULL)
+ return;
+
+ if (name_len < 1)
+ runtime_error ("Zero-length string passed as name to "
+ "get_environment_variable.");
+
+ if (value != NULL)
+ {
+ if (value_len < 1)
+ runtime_error ("Zero-length string passed as value to "
+ "get_environment_variable.");
+ else
+ memset (value, ' ', value_len); /* Blank the string. */
+ }
+
+ memcpy (name_nt, name, name_len);
+ memset (&name_nt[name_len], '\0', 1);
+
+ res = getenv(name_nt);
+
+ if (res == NULL)
+ stat = GFC_NAME_DOES_NOT_EXIST;
+ else
+ {
+ res_len = strlen(res);
+ if (value != NULL)
+ {
+ if (value_len < res_len)
+ {
+ memcpy (value, res, value_len);
+ stat = GFC_VALUE_TOO_SHORT;
+ }
+ else
+ memcpy (value, res, res_len);
+ }
+ }
+
+ if (status != NULL)
+ *status = stat;
+
+ if (length != NULL)
+ *length = res_len;
+}
+
+
+/* INTEGER*8 wrapper for get_environment_variable. */
+
+void
+prefix(get_environment_variable_i8)
+ (
+ char *name,
+ char *value,
+ GFC_INTEGER_8 *length,
+ GFC_INTEGER_8 *status,
+ GFC_LOGICAL_8 *trim_name,
+ gfc_strlen_type name_len,
+ gfc_strlen_type value_len)
+{
+ GFC_INTEGER_4 length4, status4;
+ GFC_LOGICAL_4 trim_name4;
+
+ prefix (get_environment_variable_i4) (name, value, &length4, &status4,
+ &trim_name4, name_len, value_len);
+
+ if (length)
+ *length = length4;
+
+ if (status)
+ *status = status4;
+}