1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2004, 2010, 2011 Free Software Foundation, Inc.
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.
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.
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/>. */
22 #include <sys/types.h>
25 #include "data/settings.h"
26 #include "language/command.h"
27 #include "language/lexer/lexer.h"
28 #include "libpspp/cast.h"
29 #include "libpspp/i18n.h"
30 #include "libpspp/message.h"
31 #include "libpspp/misc.h"
32 #include "libpspp/str.h"
35 #define _(msgid) gettext (msgid)
37 enum PER {PER_RO, PER_RW};
39 int change_permissions(const char *file_name, enum PER per);
42 /* Parses the PERMISSIONS command. */
44 cmd_permissions (struct lexer *lexer, struct dataset *ds UNUSED)
47 const char *str = NULL;
48 lex_match (lexer, T_SLASH);
50 if (lex_match_id (lexer, "FILE"))
51 lex_match (lexer, T_EQUALS);
53 str = lex_tokcstr (lexer);
57 if (!lex_force_match (lexer, T_STRING) || str == NULL)
60 lex_match (lexer, T_SLASH);
62 if ( ! lex_match_id (lexer, "PERMISSIONS"))
65 lex_match (lexer, T_EQUALS);
67 if ( lex_match_id (lexer, "READONLY"))
69 if (! change_permissions (fn, PER_RO))
72 else if (lex_match_id (lexer, "WRITEABLE"))
74 if (! change_permissions (fn, PER_RW ))
79 lex_error_expecting (lexer, "WRITEABLE", "READONLY", NULL_SENTINEL);
97 change_permissions (const char *file_name, enum PER per)
99 char *locale_file_name;
103 if (settings_get_safer_mode ())
105 msg (SE, _("This command not allowed when the %s option is set."), "SAFER");
109 locale_file_name = utf8_to_filename (file_name);
110 if ( -1 == stat(locale_file_name, &buf) )
112 const int errnum = errno;
113 msg (SE, _("Cannot stat %s: %s"), file_name, strerror(errnum));
114 free (locale_file_name);
119 mode = buf.st_mode | 0200;
121 mode = buf.st_mode & ~0222;
123 if ( -1 == chmod(locale_file_name, mode))
126 const int errnum = errno;
127 msg (SE, _("Cannot change mode of %s: %s"), file_name, strerror(errnum));
128 free (locale_file_name);
132 free (locale_file_name);