OSDN Git Service

* intrinsics/cpu_time.c: Don't include headers already included
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / chmod.c
1 /* Implementation of the CHMOD intrinsic.
2    Copyright (C) 2006 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 2 of the License, or (at your option) any later version.
11
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.)
20
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.
25
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.  */
30
31 #include "config.h"
32 #include "libgfortran.h"
33
34 #include <errno.h>
35
36 #ifdef HAVE_STRING_H
37 #include <string.h>
38 #endif
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42 #ifdef  HAVE_SYS_WAIT_H
43 #include <sys/wait.h>
44 #endif
45
46 /* INTEGER FUNCTION ACCESS(NAME, MODE)
47    CHARACTER(len=*), INTENT(IN) :: NAME, MODE  */
48
49 #if defined(HAVE_FORK) && defined(HAVE_EXECL) && defined(HAVE_WAIT)
50
51 extern int chmod_func (char *, char *, gfc_charlen_type, gfc_charlen_type);
52 export_proto(chmod_func);
53
54 int
55 chmod_func (char *name, char *mode, gfc_charlen_type name_len,
56             gfc_charlen_type mode_len)
57 {
58   char * file, * m;
59   pid_t pid;
60   int status;
61
62   /* Trim trailing spaces.  */
63   while (name_len > 0 && name[name_len - 1] == ' ')
64     name_len--;
65   while (mode_len > 0 && mode[mode_len - 1] == ' ')
66     mode_len--;
67
68   /* Make a null terminated copy of the strings.  */
69   file = gfc_alloca (name_len + 1);
70   memcpy (file, name, name_len);
71   file[name_len] = '\0';
72
73   m = gfc_alloca (mode_len + 1);
74   memcpy (m, mode, mode_len);
75   m[mode_len]= '\0';
76
77   /* Execute /bin/chmod.  */
78   if ((pid = fork()) < 0)
79     return errno;
80   if (pid == 0)
81     {
82       /* Child process.  */
83       execl ("/bin/chmod", "chmod", m, file, (char *) NULL);
84       return errno;
85     }
86   else
87     wait (&status);
88
89   if (WIFEXITED(status))
90     return WEXITSTATUS(status);
91   else
92     return -1;
93 }
94
95
96
97 extern void chmod_i4_sub (char *, char *, GFC_INTEGER_4 *,
98                           gfc_charlen_type, gfc_charlen_type);
99 export_proto(chmod_i4_sub);
100
101 void
102 chmod_i4_sub (char *name, char *mode, GFC_INTEGER_4 * status,
103               gfc_charlen_type name_len, gfc_charlen_type mode_len)
104 {
105   int val;
106
107   val = chmod_func (name, mode, name_len, mode_len);
108   if (status)
109     *status = val;
110 }
111
112
113 extern void chmod_i8_sub (char *, char *, GFC_INTEGER_8 *,
114                           gfc_charlen_type, gfc_charlen_type);
115 export_proto(chmod_i8_sub);
116
117 void
118 chmod_i8_sub (char *name, char *mode, GFC_INTEGER_8 * status,
119               gfc_charlen_type name_len, gfc_charlen_type mode_len)
120 {
121   int val;
122
123   val = chmod_func (name, mode, name_len, mode_len);
124   if (status)
125     *status = val;
126 }
127
128 #endif