1 /* PSPP - computes sample statistics.
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 #include <libpspp/message.h>
22 #include <sys/types.h>
26 #include <data/settings.h>
27 #include <language/command.h>
28 #include <libpspp/message.h>
29 #include <language/lexer/lexer.h>
30 #include <libpspp/misc.h>
31 #include <libpspp/str.h>
34 #define _(msgid) gettext (msgid)
36 enum PER {PER_RO, PER_RW};
38 int change_permissions(const char *file_name, enum PER per);
41 /* Parses the PERMISSIONS command. */
43 cmd_permissions (struct lexer *lexer, struct dataset *ds UNUSED)
47 lex_match (lexer, '/');
49 if (lex_match_id (lexer, "FILE"))
50 lex_match (lexer, '=');
52 fn = ds_xstrdup (lex_tokstr (lexer));
53 lex_force_match (lexer, T_STRING);
56 lex_match (lexer, '/');
58 if ( ! lex_match_id (lexer, "PERMISSIONS"))
61 lex_match (lexer, '=');
63 if ( lex_match_id (lexer, "READONLY"))
65 if ( ! change_permissions(fn, PER_RO ) )
68 else if ( lex_match_id (lexer, "WRITEABLE"))
70 if ( ! change_permissions(fn, PER_RW ) )
75 msg (SE, _("Expecting %s or %s."), "WRITEABLE", "READONLY");
93 change_permissions(const char *file_name, enum PER per)
98 if (get_safer_mode ())
100 msg (SE, _("This command not allowed when the SAFER option is set."));
105 if ( -1 == stat(file_name, &buf) )
107 const int errnum = errno;
108 msg (SE, _("Cannot stat %s: %s"), file_name, strerror(errnum));
113 mode = buf.st_mode | S_IWUSR ;
115 mode = buf.st_mode & ~( S_IWOTH | S_IWUSR | S_IWGRP );
117 if ( -1 == chmod(file_name, mode))
120 const int errnum = errno;
121 msg (SE, _("Cannot change mode of %s: %s"), file_name, strerror(errnum));