access.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Implementation of the ACCESS intrinsic.
  2. Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. Contributed by François-Xavier Coudert <coudert@clipper.ens.fr>
  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 <errno.h>
  22. #ifdef HAVE_UNISTD_H
  23. #include <unistd.h>
  24. #endif
  25. /* INTEGER FUNCTION ACCESS(NAME, MODE)
  26. CHARACTER(len=*), INTENT(IN) :: NAME, MODE */
  27. #ifdef HAVE_ACCESS
  28. extern int access_func (char *, char *, gfc_charlen_type, gfc_charlen_type);
  29. export_proto(access_func);
  30. int
  31. access_func (char *name, char *mode, gfc_charlen_type name_len,
  32. gfc_charlen_type mode_len)
  33. {
  34. gfc_charlen_type i;
  35. int m;
  36. /* Parse the MODE string. */
  37. m = F_OK;
  38. for (i = 0; i < mode_len && mode[i]; i++)
  39. switch (mode[i])
  40. {
  41. case ' ':
  42. break;
  43. case 'r':
  44. case 'R':
  45. m |= R_OK;
  46. break;
  47. case 'w':
  48. case 'W':
  49. m |= W_OK;
  50. break;
  51. case 'x':
  52. case 'X':
  53. m |= X_OK;
  54. break;
  55. default:
  56. return -1;
  57. break;
  58. }
  59. char *path = fc_strdup (name, name_len);
  60. /* And make the call to access(). */
  61. int res = (access (path, m) == 0 ? 0 : errno);
  62. free (path);
  63. return res;
  64. }
  65. #endif