access.c 2.1 KB

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