1 /* PSPP - computes sample statistics.
2 Copyright (C) 2004 Free Software Foundation, Inc.
3 Author: John Darrington
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include <libpspp/message.h>
23 #include <sys/types.h>
27 #include <data/settings.h>
28 #include <language/command.h>
29 #include <libpspp/message.h>
30 #include <language/lexer/lexer.h>
31 #include <libpspp/misc.h>
32 #include "stat-macros.h"
33 #include <libpspp/str.h>
36 #define _(msgid) gettext (msgid)
38 enum PER {PER_RO, PER_RW};
40 int change_permissions(const char *file_name, enum PER per);
43 /* Parses the PERMISSIONS command. */
45 cmd_permissions (void)
51 if (lex_match_id ("FILE"))
54 fn = strdup(ds_c_str(&tokstr));
55 lex_force_match(T_STRING);
60 if ( ! lex_match_id ("PERMISSIONS"))
65 if ( lex_match_id("READONLY"))
67 if ( ! change_permissions(fn, PER_RO ) )
70 else if ( lex_match_id("WRITEABLE"))
72 if ( ! change_permissions(fn, PER_RW ) )
77 msg (SE, _("Expecting %s or %s."), "WRITEABLE", "READONLY");
95 change_permissions(const char *file_name, enum PER per)
100 if (get_safer_mode ())
102 msg (SE, _("This command not allowed when the SAFER option is set."));
107 if ( -1 == stat(file_name, &buf) )
109 const int errnum = errno;
110 msg (SE, _("Cannot stat %s: %s"), file_name, strerror(errnum));
115 mode = buf.st_mode | S_IWUSR ;
117 mode = buf.st_mode & ~( S_IWOTH | S_IWUSR | S_IWGRP );
119 if ( -1 == chmod(file_name, mode))
122 const int errnum = errno;
123 msg (SE, _("Cannot change mode of %s: %s"), file_name, strerror(errnum));