OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / chmod.c
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>
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 "libgfortran.h"
32
33 #include <errno.h>
34 #include <string.h>
35
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39 #ifdef  HAVE_SYS_WAIT_H
40 #include <sys/wait.h>
41 #endif
42
43 /* INTEGER FUNCTION ACCESS(NAME, MODE)
44    CHARACTER(len=*), INTENT(IN) :: NAME, MODE  */
45
46 #if defined(HAVE_FORK) && defined(HAVE_EXECL) && defined(HAVE_WAIT)
47
48 extern int chmod_func (char *, char *, gfc_charlen_type, gfc_charlen_type);
49 export_proto(chmod_func);
50
51 int
52 chmod_func (char *name, char *mode, gfc_charlen_type name_len,
53             gfc_charlen_type mode_len)
54 {
55   char * file, * m;
56   pid_t pid;
57   int status;
58
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--;
64
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';
69
70   m = gfc_alloca (mode_len + 1);
71   memcpy (m, mode, mode_len);
72   m[mode_len]= '\0';
73
74   /* Execute /bin/chmod.  */
75   if ((pid = fork()) < 0)
76     return errno;
77   if (pid == 0)
78     {
79       /* Child process.  */
80       execl ("/bin/chmod", "chmod", m, file, (char *) NULL);
81       return errno;
82     }
83   else
84     wait (&status);
85
86   if (WIFEXITED(status))
87     return WEXITSTATUS(status);
88   else
89     return -1;
90 }
91
92
93
94 extern void chmod_i4_sub (char *, char *, GFC_INTEGER_4 *,
95                           gfc_charlen_type, gfc_charlen_type);
96 export_proto(chmod_i4_sub);
97
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)
101 {
102   int val;
103
104   val = chmod_func (name, mode, name_len, mode_len);
105   if (status)
106     *status = val;
107 }
108
109
110 extern void chmod_i8_sub (char *, char *, GFC_INTEGER_8 *,
111                           gfc_charlen_type, gfc_charlen_type);
112 export_proto(chmod_i8_sub);
113
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)
117 {
118   int val;
119
120   val = chmod_func (name, mode, name_len, mode_len);
121   if (status)
122     *status = val;
123 }
124
125 #endif