OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgfortran/] [intrinsics/] [env.c] - Blame information for rev 796

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.