getcwd.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Implementation of the GETCWD intrinsic.
  2. Copyright (C) 2004-2015 Free Software Foundation, Inc.
  3. Contributed by Steven G. Kargl <kargls@comcast.net>.
  4. This file is part of the GNU Fortran runtime library (libgfortran).
  5. Libgfortran is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public
  7. License as published by the Free Software Foundation; either
  8. version 3 of the License, or (at your option) any later version.
  9. Libgfortran is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "libgfortran.h"
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #ifdef HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. #ifdef HAVE_GETCWD
  28. extern void getcwd_i4_sub (char *, GFC_INTEGER_4 *, gfc_charlen_type);
  29. iexport_proto(getcwd_i4_sub);
  30. void
  31. getcwd_i4_sub (char *cwd, GFC_INTEGER_4 *status, gfc_charlen_type cwd_len)
  32. {
  33. int err;
  34. if (getcwd (cwd, cwd_len))
  35. {
  36. size_t len = strlen (cwd);
  37. memset (cwd + len, ' ', cwd_len - len);
  38. err = 0;
  39. }
  40. else if (errno == ERANGE)
  41. {
  42. /* There is a possibility that the previous attempt failed due
  43. to not enough space for the terminating null byte. Try again
  44. with a buffer one char longer. */
  45. char *buf = xmalloc (cwd_len + 1);
  46. if (getcwd (buf, cwd_len + 1))
  47. {
  48. memcpy (cwd, buf, cwd_len);
  49. err = 0;
  50. }
  51. else
  52. err = errno;
  53. free (buf);
  54. }
  55. else
  56. err = errno;
  57. if (err)
  58. memset (cwd, ' ', cwd_len);
  59. if (status != NULL)
  60. *status = err;
  61. }
  62. iexport(getcwd_i4_sub);
  63. extern void getcwd_i8_sub (char *, GFC_INTEGER_8 *, gfc_charlen_type);
  64. export_proto(getcwd_i8_sub);
  65. void
  66. getcwd_i8_sub (char *cwd, GFC_INTEGER_8 *status, gfc_charlen_type cwd_len)
  67. {
  68. GFC_INTEGER_4 status4;
  69. getcwd_i4_sub (cwd, &status4, cwd_len);
  70. if (status)
  71. *status = status4;
  72. }
  73. extern GFC_INTEGER_4 PREFIX(getcwd) (char *, gfc_charlen_type);
  74. export_proto_np(PREFIX(getcwd));
  75. GFC_INTEGER_4
  76. PREFIX(getcwd) (char *cwd, gfc_charlen_type cwd_len)
  77. {
  78. GFC_INTEGER_4 status;
  79. getcwd_i4_sub (cwd, &status, cwd_len);
  80. return status;
  81. }
  82. #endif