2011-01-08 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libgfortran / intrinsics / chmod.c
blobcf768ff002a1422de61f5f1e62c691e8eab0101e
1 /* Implementation of the CHMOD intrinsic.
2 Copyright (C) 2006, 2007, 2009 Free Software Foundation, Inc.
3 Contributed by François-Xavier Coudert <coudert@clipper.ens.fr>
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) any later version.
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "libgfortran.h"
28 #include <errno.h>
29 #include <string.h>
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #ifdef HAVE_SYS_WAIT_H
35 #include <sys/wait.h>
36 #endif
38 /* INTEGER FUNCTION ACCESS(NAME, MODE)
39 CHARACTER(len=*), INTENT(IN) :: NAME, MODE */
41 #if defined(HAVE_FORK) && defined(HAVE_EXECL) && defined(HAVE_WAIT)
43 extern int chmod_func (char *, char *, gfc_charlen_type, gfc_charlen_type);
44 export_proto(chmod_func);
46 int
47 chmod_func (char *name, char *mode, gfc_charlen_type name_len,
48 gfc_charlen_type mode_len)
50 char * file, * m;
51 pid_t pid;
52 int status;
54 /* Trim trailing spaces. */
55 while (name_len > 0 && name[name_len - 1] == ' ')
56 name_len--;
57 while (mode_len > 0 && mode[mode_len - 1] == ' ')
58 mode_len--;
60 /* Make a null terminated copy of the strings. */
61 file = gfc_alloca (name_len + 1);
62 memcpy (file, name, name_len);
63 file[name_len] = '\0';
65 m = gfc_alloca (mode_len + 1);
66 memcpy (m, mode, mode_len);
67 m[mode_len]= '\0';
69 /* Execute /bin/chmod. */
70 if ((pid = fork()) < 0)
71 return errno;
72 if (pid == 0)
74 /* Child process. */
75 execl ("/bin/chmod", "chmod", m, file, (char *) NULL);
76 return errno;
78 else
79 wait (&status);
81 if (WIFEXITED(status))
82 return WEXITSTATUS(status);
83 else
84 return -1;
89 extern void chmod_i4_sub (char *, char *, GFC_INTEGER_4 *,
90 gfc_charlen_type, gfc_charlen_type);
91 export_proto(chmod_i4_sub);
93 void
94 chmod_i4_sub (char *name, char *mode, GFC_INTEGER_4 * status,
95 gfc_charlen_type name_len, gfc_charlen_type mode_len)
97 int val;
99 val = chmod_func (name, mode, name_len, mode_len);
100 if (status)
101 *status = val;
105 extern void chmod_i8_sub (char *, char *, GFC_INTEGER_8 *,
106 gfc_charlen_type, gfc_charlen_type);
107 export_proto(chmod_i8_sub);
109 void
110 chmod_i8_sub (char *name, char *mode, GFC_INTEGER_8 * status,
111 gfc_charlen_type name_len, gfc_charlen_type mode_len)
113 int val;
115 val = chmod_func (name, mode, name_len, mode_len);
116 if (status)
117 *status = val;
120 #endif