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 "stat-macros.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)
48 lex_match (lexer, '/');
50 if (lex_match_id (lexer, "FILE"))
51 lex_match (lexer, '=');
53 fn = ds_xstrdup (lex_tokstr (lexer));
54 lex_force_match (lexer, T_STRING);
57 lex_match (lexer, '/');
59 if ( ! lex_match_id (lexer, "PERMISSIONS"))
62 lex_match (lexer, '=');
64 if ( lex_match_id (lexer, "READONLY"))
66 if ( ! change_permissions(fn, PER_RO ) )
69 else if ( lex_match_id (lexer, "WRITEABLE"))
71 if ( ! change_permissions(fn, PER_RW ) )
76 msg (SE, _("Expecting %s or %s."), "WRITEABLE", "READONLY");
94 change_permissions(const char *file_name, enum PER per)
99 if (get_safer_mode ())
101 msg (SE, _("This command not allowed when the SAFER option is set."));
106 if ( -1 == stat(file_name, &buf) )
108 const int errnum = errno;
109 msg (SE, _("Cannot stat %s: %s"), file_name, strerror(errnum));
114 mode = buf.st_mode | S_IWUSR ;
116 mode = buf.st_mode & ~( S_IWOTH | S_IWUSR | S_IWGRP );
118 if ( -1 == chmod(file_name, mode))
121 const int errnum = errno;
122 msg (SE, _("Cannot change mode of %s: %s"), file_name, strerror(errnum));