OSDN Git Service

2010-03-29 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / chmod.c
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>
4
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
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.
11
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.
16
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.
20
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/>.  */
25
26 #include "libgfortran.h"
27
28 #include <errno.h>
29 #include <string.h>
30
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #ifdef  HAVE_SYS_WAIT_H
35 #include <sys/wait.h>
36 #endif
37
38 /* INTEGER FUNCTION ACCESS(NAME, MODE)
39    CHARACTER(len=*), INTENT(IN) :: NAME, MODE  */
40
41 #if defined(HAVE_FORK) && defined(HAVE_EXECL) && defined(HAVE_WAIT)
42
43 extern int chmod_func (char *, char *, gfc_charlen_type, gfc_charlen_type);
44 export_proto(chmod_func);
45
46 int
47 chmod_func (char *name, char *mode, gfc_charlen_type name_len,
48             gfc_charlen_type mode_len)
49 {
50   char * file, * m;
51   pid_t pid;
52   int status;
53
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--;
59
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';
64
65   m = gfc_alloca (mode_len + 1);
66   memcpy (m, mode, mode_len);
67   m[mode_len]= '\0';
68
69   /* Execute /bin/chmod.  */
70   if ((pid = fork()) < 0)
71     return errno;
72   if (pid == 0)
73     {
74       /* Child process.  */
75       execl ("/bin/chmod", "chmod", m, file, (char *) NULL);
76       return errno;
77     }
78   else
79     wait (&status);
80
81   if (WIFEXITED(status))
82     return WEXITSTATUS(status);
83   else
84     return -1;
85 }
86
87
88
89 extern void chmod_i4_sub (char *, char *, GFC_INTEGER_4 *,
90                           gfc_charlen_type, gfc_charlen_type);
91 export_proto(chmod_i4_sub);
92
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)
96 {
97   int val;
98
99   val = chmod_func (name, mode, name_len, mode_len);
100   if (status)
101     *status = val;
102 }
103
104
105 extern void chmod_i8_sub (char *, char *, GFC_INTEGER_8 *,
106                           gfc_charlen_type, gfc_charlen_type);
107 export_proto(chmod_i8_sub);
108
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)
112 {
113   int val;
114
115   val = chmod_func (name, mode, name_len, mode_len);
116   if (status)
117     *status = val;
118 }
119
120 #endif