lexer: Reimplement for better testability and internationalization.
[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/i18n.h"
29 #include "libpspp/message.h"
30 #include "libpspp/misc.h"
31 #include "libpspp/str.h"
32
33 #include "gettext.h"
34 #define _(msgid) gettext (msgid)
35
36 enum PER {PER_RO, PER_RW};
37
38 int change_permissions(const char *file_name, enum PER per);
39
40
41 /* Parses the PERMISSIONS command. */
42 int
43 cmd_permissions (struct lexer *lexer, struct dataset *ds UNUSED)
44 {
45   char  *fn = 0;
46
47   lex_match (lexer, T_SLASH);
48
49   if (lex_match_id (lexer, "FILE"))
50     lex_match (lexer, T_EQUALS);
51
52   if (!lex_force_string (lexer))
53     return CMD_FAILURE;
54
55   fn = ss_xstrdup (lex_tokss (lexer));
56   lex_force_match (lexer, T_STRING);
57
58
59   lex_match (lexer, T_SLASH);
60
61   if ( ! lex_match_id (lexer, "PERMISSIONS"))
62     goto error;
63
64   lex_match (lexer, T_EQUALS);
65
66   if ( lex_match_id (lexer, "READONLY"))
67     {
68       if ( ! change_permissions(fn, PER_RO ) )
69         goto error;
70     }
71   else if ( lex_match_id (lexer, "WRITEABLE"))
72     {
73       if ( ! change_permissions(fn, PER_RW ) )
74         goto error;
75     }
76   else
77     {
78       msg (SE, _("Expecting %s or %s."), "WRITEABLE", "READONLY");
79       goto error;
80     }
81
82   free(fn);
83
84   return CMD_SUCCESS;
85
86  error:
87
88   free(fn);
89
90   return CMD_FAILURE;
91 }
92
93
94
95 int
96 change_permissions (const char *file_name, enum PER per)
97 {
98   char *locale_file_name;
99   struct stat buf;
100   mode_t mode;
101
102   if (settings_get_safer_mode ())
103     {
104       msg (SE, _("This command not allowed when the SAFER option is set."));
105       return 0;
106     }
107
108
109   locale_file_name = utf8_to_filename (file_name);
110   if ( -1 == stat(locale_file_name, &buf) )
111     {
112       const int errnum = errno;
113       msg (SE, _("Cannot stat %s: %s"), file_name, strerror(errnum));
114       free (locale_file_name);
115       return 0;
116     }
117
118   if ( per == PER_RW )
119     mode = buf.st_mode | 0200;
120   else
121     mode = buf.st_mode & ~0222;
122
123   if ( -1 == chmod(locale_file_name, mode))
124
125     {
126       const int errnum = errno;
127       msg (SE, _("Cannot change mode of %s: %s"), file_name, strerror(errnum));
128       free (locale_file_name);
129       return 0;
130     }
131
132   free (locale_file_name);
133
134   return 1;
135 }