1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2004 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/>. */
18 #include <libpspp/message.h>
20 #include <sys/types.h>
24 #include <data/settings.h>
25 #include <language/command.h>
26 #include <libpspp/message.h>
27 #include <language/lexer/lexer.h>
28 #include <libpspp/misc.h>
29 #include <libpspp/str.h>
32 #define _(msgid) gettext (msgid)
34 enum PER {PER_RO, PER_RW};
36 int change_permissions(const char *file_name, enum PER per);
39 /* Parses the PERMISSIONS command. */
41 cmd_permissions (struct lexer *lexer, struct dataset *ds UNUSED)
45 lex_match (lexer, '/');
47 if (lex_match_id (lexer, "FILE"))
48 lex_match (lexer, '=');
50 fn = ds_xstrdup (lex_tokstr (lexer));
51 lex_force_match (lexer, T_STRING);
54 lex_match (lexer, '/');
56 if ( ! lex_match_id (lexer, "PERMISSIONS"))
59 lex_match (lexer, '=');
61 if ( lex_match_id (lexer, "READONLY"))
63 if ( ! change_permissions(fn, PER_RO ) )
66 else if ( lex_match_id (lexer, "WRITEABLE"))
68 if ( ! change_permissions(fn, PER_RW ) )
73 msg (SE, _("Expecting %s or %s."), "WRITEABLE", "READONLY");
91 change_permissions (const char *file_name, enum PER per)
96 if (settings_get_safer_mode ())
98 msg (SE, _("This command not allowed when the SAFER option is set."));
103 if ( -1 == stat(file_name, &buf) )
105 const int errnum = errno;
106 msg (SE, _("Cannot stat %s: %s"), file_name, strerror(errnum));
111 mode = buf.st_mode | S_IWUSR ;
113 mode = buf.st_mode & ~( S_IWOTH | S_IWUSR | S_IWGRP );
115 if ( -1 == chmod(file_name, mode))
118 const int errnum = errno;
119 msg (SE, _("Cannot change mode of %s: %s"), file_name, strerror(errnum));