Adopt use of gnulib for portability.
[pspp-builds.git] / src / permissions.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2004 Free Software Foundation, Inc.
3    Author: John Darrington
4
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.
9
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.
14
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
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include "error.h"
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include "settings.h"
28 #include "command.h"
29 #include "error.h"
30 #include "lexer.h"
31 #include "misc.h"
32 #include "stat-macros.h"
33 #include "str.h"
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37
38 enum PER {PER_RO, PER_RW};
39
40 int change_permissions(const char *filename, enum PER per);
41
42
43 /* Parses the PERMISSIONS command. */
44 int
45 cmd_permissions (void)
46 {
47   char  *fn = 0;
48
49   lex_match ('/');
50
51   if (lex_match_id ("FILE"))
52     lex_match ('=');
53
54   fn = strdup(ds_c_str(&tokstr));
55   lex_force_match(T_STRING);
56
57
58   lex_match ('/');
59   
60   if ( ! lex_match_id ("PERMISSIONS"))
61     goto error;
62
63   lex_match('=');
64
65   if ( lex_match_id("READONLY"))
66     {
67       if ( ! change_permissions(fn, PER_RO ) ) 
68         goto error;
69     }
70   else if ( lex_match_id("WRITEABLE"))
71     {
72       if ( ! change_permissions(fn, PER_RW ) ) 
73         goto error;
74     }
75   else
76     {
77       msg(ME, _("Expecting %s or %s."), "WRITEABLE", "READONLY");
78       goto error;
79     }
80
81   free(fn);
82
83   return CMD_SUCCESS;
84
85  error:
86
87   free(fn);
88
89   return CMD_FAILURE;
90 }
91
92
93
94 int
95 change_permissions(const char *filename, enum PER per)
96 {
97   struct stat buf;
98   mode_t mode;
99
100   if ( safer_mode() )
101     {
102       msg (SE, _("This command not allowed when the SAFER option is set."));
103       return CMD_FAILURE;
104     }
105
106
107   if ( -1 == stat(filename, &buf) ) 
108     {
109       const int errnum = errno;
110       msg(ME,_("Cannot stat %s: %s"), filename, strerror(errnum));
111       return 0;
112     }
113
114   if ( per == PER_RW )
115     mode = buf.st_mode | S_IWUSR ;
116   else
117     mode = buf.st_mode & ~( S_IWOTH | S_IWUSR | S_IWGRP );
118
119   if ( -1 == chmod(filename, mode))
120
121     {
122       const int errnum = errno;
123       msg(ME,_("Cannot change mode of %s: %s"), filename, strerror(errnum));
124       return 0;
125     }
126
127   return 1;
128 }