]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgfortran/intrinsics/access.c
tm.texi.in (SELECT_CC_MODE): Update example.
[thirdparty/gcc.git] / libgfortran / intrinsics / access.c
CommitLineData
a119fc1c 1/* Implementation of the ACCESS intrinsic.
f0bcf628 2 Copyright (C) 2006-2014 Free Software Foundation, Inc.
a119fc1c
FXC
3 Contributed by François-Xavier Coudert <coudert@clipper.ens.fr>
4
5This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
7Libgfortran is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public
9License as published by the Free Software Foundation; either
748086b7 10version 3 of the License, or (at your option) any later version.
a119fc1c
FXC
11
12Libgfortran is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
748086b7
JJ
17Under Section 7 of GPL version 3, you are granted additional
18permissions described in the GCC Runtime Library Exception, version
193.1, as published by the Free Software Foundation.
20
21You should have received a copy of the GNU General Public License and
22a copy of the GCC Runtime Library Exception along with this program;
23see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24<http://www.gnu.org/licenses/>. */
a119fc1c 25
a119fc1c
FXC
26#include "libgfortran.h"
27
28#include <errno.h>
a119fc1c 29#include <string.h>
36ae8a61 30
a119fc1c
FXC
31#ifdef HAVE_UNISTD_H
32#include <unistd.h>
33#endif
34
35/* INTEGER FUNCTION ACCESS(NAME, MODE)
36 CHARACTER(len=*), INTENT(IN) :: NAME, MODE */
37
38#ifdef HAVE_ACCESS
39extern int access_func (char *, char *, gfc_charlen_type, gfc_charlen_type);
40export_proto(access_func);
41
42int
43access_func (char *name, char *mode, gfc_charlen_type name_len,
44 gfc_charlen_type mode_len)
45{
46 char * file;
47 gfc_charlen_type i;
48 int m;
49
50 /* Parse the MODE string. */
51 m = F_OK;
52 for (i = 0; i < mode_len && mode[i]; i++)
53 switch (mode[i])
54 {
55 case ' ':
56 break;
57
58 case 'r':
59 case 'R':
60 m |= R_OK;
61 break;
62
63 case 'w':
64 case 'W':
65 m |= W_OK;
66 break;
67
68 case 'x':
69 case 'X':
70 m |= X_OK;
71 break;
72
73 default:
74 return -1;
75 break;
76 }
77
78 /* Trim trailing spaces from NAME argument. */
79 while (name_len > 0 && name[name_len - 1] == ' ')
80 name_len--;
81
82 /* Make a null terminated copy of the string. */
83 file = gfc_alloca (name_len + 1);
84 memcpy (file, name, name_len);
85 file[name_len] = '\0';
86
87 /* And make the call to access(). */
88 return (access (file, m) == 0 ? 0 : errno);
89}
a119fc1c 90#endif