xref: /netbsd-src/external/gpl3/gcc/dist/libgfortran/intrinsics/access.c (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1181254a7Smrg /* Implementation of the ACCESS intrinsic.
2*b1e83836Smrg    Copyright (C) 2006-2022 Free Software Foundation, Inc.
3181254a7Smrg    Contributed by François-Xavier Coudert <coudert@clipper.ens.fr>
4181254a7Smrg 
5181254a7Smrg This file is part of the GNU Fortran runtime library (libgfortran).
6181254a7Smrg 
7181254a7Smrg Libgfortran is free software; you can redistribute it and/or
8181254a7Smrg modify it under the terms of the GNU General Public
9181254a7Smrg License as published by the Free Software Foundation; either
10181254a7Smrg version 3 of the License, or (at your option) any later version.
11181254a7Smrg 
12181254a7Smrg Libgfortran is distributed in the hope that it will be useful,
13181254a7Smrg but WITHOUT ANY WARRANTY; without even the implied warranty of
14181254a7Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15181254a7Smrg GNU General Public License for more details.
16181254a7Smrg 
17181254a7Smrg Under Section 7 of GPL version 3, you are granted additional
18181254a7Smrg permissions described in the GCC Runtime Library Exception, version
19181254a7Smrg 3.1, as published by the Free Software Foundation.
20181254a7Smrg 
21181254a7Smrg You should have received a copy of the GNU General Public License and
22181254a7Smrg a copy of the GCC Runtime Library Exception along with this program;
23181254a7Smrg see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24181254a7Smrg <http://www.gnu.org/licenses/>.  */
25181254a7Smrg 
26181254a7Smrg #include "libgfortran.h"
27181254a7Smrg 
28181254a7Smrg #include <errno.h>
29181254a7Smrg 
30181254a7Smrg #ifdef HAVE_UNISTD_H
31181254a7Smrg #include <unistd.h>
32181254a7Smrg #endif
33181254a7Smrg 
34181254a7Smrg /* INTEGER FUNCTION ACCESS(NAME, MODE)
35181254a7Smrg    CHARACTER(len=*), INTENT(IN) :: NAME, MODE  */
36181254a7Smrg 
37181254a7Smrg #ifdef HAVE_ACCESS
38181254a7Smrg extern int access_func (char *, char *, gfc_charlen_type, gfc_charlen_type);
39181254a7Smrg export_proto(access_func);
40181254a7Smrg 
41181254a7Smrg int
access_func(char * name,char * mode,gfc_charlen_type name_len,gfc_charlen_type mode_len)42181254a7Smrg access_func (char *name, char *mode, gfc_charlen_type name_len,
43181254a7Smrg 	     gfc_charlen_type mode_len)
44181254a7Smrg {
45181254a7Smrg   gfc_charlen_type i;
46181254a7Smrg   int m;
47181254a7Smrg 
48181254a7Smrg   /* Parse the MODE string.  */
49181254a7Smrg   m = F_OK;
50181254a7Smrg   for (i = 0; i < mode_len && mode[i]; i++)
51181254a7Smrg     switch (mode[i])
52181254a7Smrg       {
53181254a7Smrg 	case ' ':
54181254a7Smrg 	  break;
55181254a7Smrg 
56181254a7Smrg 	case 'r':
57181254a7Smrg 	case 'R':
58181254a7Smrg 	  m |= R_OK;
59181254a7Smrg 	  break;
60181254a7Smrg 
61181254a7Smrg 	case 'w':
62181254a7Smrg 	case 'W':
63181254a7Smrg 	  m |= W_OK;
64181254a7Smrg 	  break;
65181254a7Smrg 
66181254a7Smrg 	case 'x':
67181254a7Smrg 	case 'X':
68181254a7Smrg 	  m |= X_OK;
69181254a7Smrg 	  break;
70181254a7Smrg 
71181254a7Smrg 	default:
72181254a7Smrg 	  return -1;
73181254a7Smrg 	  break;
74181254a7Smrg       }
75181254a7Smrg 
76181254a7Smrg   char *path = fc_strdup (name, name_len);
77181254a7Smrg 
78181254a7Smrg   /* And make the call to access().  */
79181254a7Smrg   int res = (access (path, m) == 0 ? 0 : errno);
80181254a7Smrg 
81181254a7Smrg   free (path);
82181254a7Smrg   return res;
83181254a7Smrg }
84181254a7Smrg #endif
85