Update all #include directives to the currently preferred style.
[pspp-builds.git] / src / language / utilities / permissions.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 2010, 2011 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24
25 #include "data/settings.h"
26 #include "language/command.h"
27 #include "language/lexer/lexer.h"
28 #include "libpspp/message.h"
29 #include "libpspp/misc.h"
30 #include "libpspp/str.h"
31
32 #include "gettext.h"
33 #define _(msgid) gettext (msgid)
34
35 enum PER {PER_RO, PER_RW};
36
37 int change_permissions(const char *file_name, enum PER per);
38
39
40 /* Parses the PERMISSIONS command. */
41 int
42 cmd_permissions (struct lexer *lexer, struct dataset *ds UNUSED)
43 {
44   char  *fn = 0;
45
46   lex_match (lexer, T_SLASH);
47
48   if (lex_match_id (lexer, "FILE"))
49     lex_match (lexer, T_EQUALS);
50
51   if (!lex_force_string (lexer))
52     return CMD_FAILURE;
53
54   fn = ss_xstrdup (lex_tokss (lexer));
55   lex_force_match (lexer, T_STRING);
56
57
58   lex_match (lexer, T_SLASH);
59
60   if ( ! lex_match_id (lexer, "PERMISSIONS"))
61     goto error;
62
63   lex_match (lexer, T_EQUALS);
64
65   if ( lex_match_id (lexer, "READONLY"))
66     {
67       if ( ! change_permissions(fn, PER_RO ) )
68         goto error;
69     }
70   else if ( lex_match_id (lexer, "WRITEABLE"))
71     {
72       if ( ! change_permissions(fn, PER_RW ) )
73         goto error;
74     }
75   else
76     {
77       msg (SE, _("Expecting %s or %s."), "WRITEABLE", "READONLY");
78       goto error;
79     }
80
81   free(fn);
82
83   return CMD_SUCCESS;
84
85  error:
86
87   free(fn);
88
89   return CMD_FAILURE;
90 }
91
92
93
94 int
95 change_permissions (const char *file_name, enum PER per)
96 {
97   struct stat buf;
98   mode_t mode;
99
100   if (settings_get_safer_mode ())
101     {
102       msg (SE, _("This command not allowed when the SAFER option is set."));
103       return CMD_FAILURE;
104     }
105
106
107   if ( -1 == stat(file_name, &buf) )
108     {
109       const int errnum = errno;
110       msg (SE, _("Cannot stat %s: %s"), file_name, strerror(errnum));
111       return 0;
112     }
113
114   if ( per == PER_RW )
115     mode = buf.st_mode | 0200;
116   else
117     mode = buf.st_mode & ~0222;
118
119   if ( -1 == chmod(file_name, mode))
120
121     {
122       const int errnum = errno;
123       msg (SE, _("Cannot change mode of %s: %s"), file_name, strerror(errnum));
124       return 0;
125     }
126
127   return 1;
128 }