env.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Implementation of the GETENV g77, and
  2. GET_ENVIRONMENT_VARIABLE F2003, intrinsics.
  3. Copyright (C) 2004-2015 Free Software Foundation, Inc.
  4. Contributed by Janne Blomqvist.
  5. This file is part of the GNU Fortran 95 runtime library (libgfortran).
  6. Libgfortran is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public
  8. License as published by the Free Software Foundation; either
  9. version 3 of the License, or (at your option) any later version.
  10. Libgfortran is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. Under Section 7 of GPL version 3, you are granted additional
  15. permissions described in the GCC Runtime Library Exception, version
  16. 3.1, as published by the Free Software Foundation.
  17. You should have received a copy of the GNU General Public License and
  18. a copy of the GCC Runtime Library Exception along with this program;
  19. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. <http://www.gnu.org/licenses/>. */
  21. #include "libgfortran.h"
  22. #include <stdlib.h>
  23. #include <string.h>
  24. /* GETENV (NAME, VALUE), g77 intrinsic for retrieving the value of
  25. an environment variable. The name of the variable is specified in
  26. NAME, and the result is stored into VALUE. */
  27. void PREFIX(getenv) (char *, char *, gfc_charlen_type, gfc_charlen_type);
  28. export_proto_np(PREFIX(getenv));
  29. void
  30. PREFIX(getenv) (char * name, char * value, gfc_charlen_type name_len,
  31. gfc_charlen_type value_len)
  32. {
  33. char *name_nt;
  34. char *res = NULL;
  35. if (name == NULL || value == NULL)
  36. runtime_error ("Both arguments to getenv are mandatory.");
  37. if (value_len < 1 || name_len < 1)
  38. runtime_error ("Zero length string(s) passed to getenv.");
  39. else
  40. memset (value, ' ', value_len); /* Blank the string. */
  41. /* Make a null terminated copy of the string. */
  42. name_nt = fc_strdup (name, name_len);
  43. res = getenv(name_nt);
  44. free (name_nt);
  45. /* If res is NULL, it means that the environment variable didn't
  46. exist, so just return. */
  47. if (res == NULL)
  48. return;
  49. cf_strcpy (value, value_len, res);
  50. }
  51. /* GET_ENVIRONMENT_VARIABLE (name, [value, length, status, trim_name])
  52. is a F2003 intrinsic for getting an environment variable. */
  53. /* Status codes specifyed by the standard. */
  54. #define GFC_SUCCESS 0
  55. #define GFC_VALUE_TOO_SHORT -1
  56. #define GFC_NAME_DOES_NOT_EXIST 1
  57. /* This is also specified by the standard and means that the
  58. processor doesn't support environment variables. At the moment,
  59. gfortran doesn't use it. */
  60. #define GFC_NOT_SUPPORTED 2
  61. /* Processor-specific failure code. */
  62. #define GFC_FAILURE 42
  63. extern void get_environment_variable_i4 (char *, char *, GFC_INTEGER_4 *,
  64. GFC_INTEGER_4 *, GFC_LOGICAL_4 *,
  65. gfc_charlen_type, gfc_charlen_type);
  66. iexport_proto(get_environment_variable_i4);
  67. void
  68. get_environment_variable_i4 (char *name, char *value, GFC_INTEGER_4 *length,
  69. GFC_INTEGER_4 *status, GFC_LOGICAL_4 *trim_name,
  70. gfc_charlen_type name_len,
  71. gfc_charlen_type value_len)
  72. {
  73. int stat = GFC_SUCCESS, res_len = 0;
  74. char *name_nt;
  75. char *res;
  76. if (name == NULL)
  77. runtime_error ("Name is required for get_environment_variable.");
  78. if (value == NULL && length == NULL && status == NULL && trim_name == NULL)
  79. return;
  80. if (name_len < 1)
  81. runtime_error ("Zero-length string passed as name to "
  82. "get_environment_variable.");
  83. if (value != NULL)
  84. {
  85. if (value_len < 1)
  86. runtime_error ("Zero-length string passed as value to "
  87. "get_environment_variable.");
  88. else
  89. memset (value, ' ', value_len); /* Blank the string. */
  90. }
  91. if ((!trim_name) || *trim_name)
  92. name_nt = fc_strdup (name, name_len);
  93. else
  94. name_nt = fc_strdup_notrim (name, name_len);
  95. res = getenv(name_nt);
  96. free (name_nt);
  97. if (res == NULL)
  98. stat = GFC_NAME_DOES_NOT_EXIST;
  99. else
  100. {
  101. res_len = strlen(res);
  102. if (value != NULL)
  103. {
  104. if (value_len < res_len)
  105. {
  106. memcpy (value, res, value_len);
  107. stat = GFC_VALUE_TOO_SHORT;
  108. }
  109. else
  110. memcpy (value, res, res_len);
  111. }
  112. }
  113. if (status != NULL)
  114. *status = stat;
  115. if (length != NULL)
  116. *length = res_len;
  117. }
  118. iexport(get_environment_variable_i4);
  119. /* INTEGER*8 wrapper for get_environment_variable. */
  120. extern void get_environment_variable_i8 (char *, char *, GFC_INTEGER_8 *,
  121. GFC_INTEGER_8 *, GFC_LOGICAL_8 *,
  122. gfc_charlen_type, gfc_charlen_type);
  123. export_proto(get_environment_variable_i8);
  124. void
  125. get_environment_variable_i8 (char *name, char *value, GFC_INTEGER_8 *length,
  126. GFC_INTEGER_8 *status, GFC_LOGICAL_8 *trim_name,
  127. gfc_charlen_type name_len,
  128. gfc_charlen_type value_len)
  129. {
  130. GFC_INTEGER_4 length4, status4;
  131. GFC_LOGICAL_4 trim_name4;
  132. if (trim_name)
  133. trim_name4 = *trim_name;
  134. get_environment_variable_i4 (name, value, &length4, &status4,
  135. trim_name ? &trim_name4 : NULL,
  136. name_len, value_len);
  137. if (length)
  138. *length = length4;
  139. if (status)
  140. *status = status4;
  141. }