Merge -r 127928:132243 from trunk
[official-gcc.git] / libgfortran / intrinsics / chmod.c
blob0f44efe66448beff4adf4297c343a6dc61481cc3
1 /* Implementation of the CHMOD intrinsic.
2 Copyright (C) 2006, 2007 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 2 of the License, or (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
19 executable.)
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING. If not,
28 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
31 #include "libgfortran.h"
33 #include <errno.h>
34 #include <string.h>
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39 #ifdef HAVE_SYS_WAIT_H
40 #include <sys/wait.h>
41 #endif
43 /* INTEGER FUNCTION ACCESS(NAME, MODE)
44 CHARACTER(len=*), INTENT(IN) :: NAME, MODE */
46 #if defined(HAVE_FORK) && defined(HAVE_EXECL) && defined(HAVE_WAIT)
48 extern int chmod_func (char *, char *, gfc_charlen_type, gfc_charlen_type);
49 export_proto(chmod_func);
51 int
52 chmod_func (char *name, char *mode, gfc_charlen_type name_len,
53 gfc_charlen_type mode_len)
55 char * file, * m;
56 pid_t pid;
57 int status;
59 /* Trim trailing spaces. */
60 while (name_len > 0 && name[name_len - 1] == ' ')
61 name_len--;
62 while (mode_len > 0 && mode[mode_len - 1] == ' ')
63 mode_len--;
65 /* Make a null terminated copy of the strings. */
66 file = gfc_alloca (name_len + 1);
67 memcpy (file, name, name_len);
68 file[name_len] = '\0';
70 m = gfc_alloca (mode_len + 1);
71 memcpy (m, mode, mode_len);
72 m[mode_len]= '\0';
74 /* Execute /bin/chmod. */
75 if ((pid = fork()) < 0)
76 return errno;
77 if (pid == 0)
79 /* Child process. */
80 execl ("/bin/chmod", "chmod", m, file, (char *) NULL);
81 return errno;
83 else
84 wait (&status);
86 if (WIFEXITED(status))
87 return WEXITSTATUS(status);
88 else
89 return -1;
94 extern void chmod_i4_sub (char *, char *, GFC_INTEGER_4 *,
95 gfc_charlen_type, gfc_charlen_type);
96 export_proto(chmod_i4_sub);
98 void
99 chmod_i4_sub (char *name, char *mode, GFC_INTEGER_4 * status,
100 gfc_charlen_type name_len, gfc_charlen_type mode_len)
102 int val;
104 val = chmod_func (name, mode, name_len, mode_len);
105 if (status)
106 *status = val;
110 extern void chmod_i8_sub (char *, char *, GFC_INTEGER_8 *,
111 gfc_charlen_type, gfc_charlen_type);
112 export_proto(chmod_i8_sub);
114 void
115 chmod_i8_sub (char *name, char *mode, GFC_INTEGER_8 * status,
116 gfc_charlen_type name_len, gfc_charlen_type mode_len)
118 int val;
120 val = chmod_func (name, mode, name_len, mode_len);
121 if (status)
122 *status = val;
125 #endif