* POTFILES.in: add gnome/gcustom-layout.c file
[midnight-commander.git] / src / chmod.c
blob74f41bb50b2cfb21c093ff40556dafa390dd34ff
1 /* Chmod command -- for the Midnight Commander
2 Copyright (C) 1994 Radek Doulik
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 2 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, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <config.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <errno.h> /* For errno on SunOS systems */
23 /* Needed for the extern declarations of integer parameters */
24 #include <sys/types.h>
25 #include <sys/param.h>
26 #include <sys/stat.h>
27 #include <grp.h>
28 #include <pwd.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include "tty.h"
33 #include "global.h"
34 #include "win.h"
35 #include "color.h"
36 #include "dlg.h"
37 #include "widget.h"
38 #include "dialog.h" /* For do_refresh() */
40 #include "dir.h"
41 #include "panel.h" /* Needed for the externs */
42 #include "file.h"
43 #include "main.h"
44 #include "chmod.h"
45 #include "achown.h"
46 #include "chown.h"
47 #include "../vfs/vfs.h"
49 #ifdef HAVE_TK
50 # include "tkmain.h"
51 #endif
53 static int single_set;
54 struct Dlg_head *ch_dlg;
56 #define PX 5
57 #define PY 2
59 #define FX 40
60 #define FY 2
62 #define BX 6
63 #define BY 17
65 #define TX 40
66 #define TY 12
68 #define PERMISSIONS 12
69 #define BUTTONS 6
71 #define B_MARKED B_USER
72 #define B_ALL B_USER+1
73 #define B_SETMRK B_USER+2
74 #define B_CLRMRK B_USER+3
76 int mode_change, need_update;
77 int c_file, end_chmod;
79 umode_t and_mask, or_mask, c_stat;
80 char *c_fname, *c_fown, *c_fgrp, *c_fperm;
81 int c_fsize;
83 static WLabel *statl;
84 static int normal_color;
85 static int title_color;
86 static int selection_color;
88 struct {
89 mode_t mode;
90 char *text;
91 int selected;
92 WCheck *check;
93 } check_perm[PERMISSIONS] =
95 { S_IXOTH, N_("execute/search by others"), 0, 0, },
96 { S_IWOTH, N_("write by others"), 0, 0, },
97 { S_IROTH, N_("read by others"), 0, 0, },
98 { S_IXGRP, N_("execute/search by group"), 0, 0, },
99 { S_IWGRP, N_("write by group"), 0, 0, },
100 { S_IRGRP, N_("read by group"), 0, 0, },
101 { S_IXUSR, N_("execute/search by owner"), 0, 0, },
102 { S_IWUSR, N_("write by owner"), 0, 0, },
103 { S_IRUSR, N_("read by owner"), 0, 0, },
104 { S_ISVTX, N_("sticky bit"), 0, 0, },
105 { S_ISGID, N_("set group ID on execution"), 0, 0, },
106 { S_ISUID, N_("set user ID on execution"), 0, 0, },
109 struct {
110 int ret_cmd, flags, y, x;
111 char *text;
112 } chmod_but[BUTTONS] =
114 { B_CANCEL, NORMAL_BUTTON, 2, 33, N_("&Cancel") },
115 { B_ENTER, DEFPUSH_BUTTON, 2, 17, N_("&Set") },
116 { B_CLRMRK, NORMAL_BUTTON, 0, 42, N_("C&lear marked") },
117 { B_SETMRK, NORMAL_BUTTON, 0, 27, N_("S&et marked") },
118 { B_MARKED, NORMAL_BUTTON, 0, 12, N_("&Marked all") },
119 { B_ALL, NORMAL_BUTTON, 0, 0, N_("Set &all") },
122 #ifdef HAVE_X
123 static void chmod_toggle_select (void)
127 #else
128 static void chmod_toggle_select (void)
130 int Id = ch_dlg->current->dlg_id - BUTTONS + single_set * 2;
132 attrset (normal_color);
133 check_perm[Id].selected ^= 1;
135 dlg_move (ch_dlg, PY + PERMISSIONS - Id, PX + 1);
136 addch ((check_perm[Id].selected) ? '*' : ' ');
137 dlg_move (ch_dlg, PY + PERMISSIONS - Id, PX + 3);
140 static void chmod_refresh (void)
142 attrset (COLOR_NORMAL);
143 dlg_erase (ch_dlg);
145 draw_box (ch_dlg, 1, 2, 20 - single_set, 66);
146 draw_box (ch_dlg, PY, PX, PERMISSIONS + 2, 33);
147 draw_box (ch_dlg, FY, FX, 10, 25);
149 dlg_move (ch_dlg, FY + 1, FX + 2);
150 addstr (_("Name"));
151 dlg_move (ch_dlg, FY + 3, FX + 2);
152 addstr (_("Permissions (Octal)"));
153 dlg_move (ch_dlg, FY + 5, FX + 2);
154 addstr (_("Owner name"));
155 dlg_move (ch_dlg, FY + 7, FX + 2);
156 addstr (_("Group name"));
158 attrset (title_color);
159 dlg_move (ch_dlg, 1, 28);
160 addstr (_(" Chmod command "));
161 dlg_move (ch_dlg, PY, PX + 1);
162 addstr (_(" Permission "));
163 dlg_move (ch_dlg, FY, FX + 1);
164 addstr (_(" File "));
166 attrset (selection_color);
168 dlg_move (ch_dlg, TY, TX);
169 addstr (_("Use SPACE to change"));
170 dlg_move (ch_dlg, TY + 1, TX);
171 addstr (_("an option, ARROW KEYS"));
172 dlg_move (ch_dlg, TY + 2, TX);
173 addstr (_("to move between options"));
174 dlg_move (ch_dlg, TY + 3, TX);
175 addstr (_("and T or INS to mark"));
177 #endif
179 static int chmod_callback (Dlg_head *h, int Par, int Msg)
181 char buffer [BUF_TINY];
183 switch (Msg) {
184 case DLG_ACTION:
185 if (Par >= BUTTONS - single_set * 2){
186 c_stat ^= check_perm[Par - BUTTONS + single_set * 2].mode;
187 g_snprintf (buffer, sizeof (buffer), "%o", c_stat);
188 label_set_text (statl, buffer);
189 chmod_toggle_select ();
190 mode_change = 1;
192 break;
194 case DLG_KEY:
195 if ((Par == 'T' || Par == 't' || Par == KEY_IC) &&
196 ch_dlg->current->dlg_id >= BUTTONS - single_set * 2) {
197 chmod_toggle_select ();
198 if (Par == KEY_IC)
199 dlg_one_down (ch_dlg);
200 return 1;
202 break;
203 #ifndef HAVE_X
204 case DLG_DRAW:
205 chmod_refresh ();
206 break;
207 #endif
209 return 0;
212 static void init_chmod (void)
214 int i;
216 do_refresh ();
217 end_chmod = c_file = need_update = 0;
218 single_set = (cpanel->marked < 2) ? 2 : 0;
220 if (use_colors){
221 normal_color = COLOR_NORMAL;
222 title_color = COLOR_HOT_NORMAL;
223 selection_color = COLOR_NORMAL;
224 } else {
225 normal_color = NORMAL_COLOR;
226 title_color = SELECTED_COLOR;
227 selection_color = SELECTED_COLOR;
230 ch_dlg = create_dlg (0, 0, 22 - single_set, 70, dialog_colors,
231 chmod_callback, "[Chmod]", "chmod", DLG_CENTER);
233 x_set_dialog_title (ch_dlg, _("Chmod command"));
235 #define XTRACT(i) BY+chmod_but[i].y-single_set, BX+chmod_but[i].x, \
236 chmod_but[i].ret_cmd, chmod_but[i].flags, _(chmod_but[i].text), 0, 0, NULL
238 for (i = 0; i < BUTTONS; i++) {
239 if (i == 2 && single_set)
240 break;
241 else
242 add_widgetl (ch_dlg, button_new (XTRACT (i)), XV_WLAY_RIGHTOF);
246 #define XTRACT2(i) 0, _(check_perm [i].text), NULL
247 for (i = 0; i < PERMISSIONS; i++) {
248 check_perm[i].check = check_new (PY + (PERMISSIONS - i), PX + 2,
249 XTRACT2 (i));
250 add_widget (ch_dlg, check_perm[i].check);
254 int stat_file (char *filename, struct stat *st)
256 if (mc_stat (filename, st))
257 return 0;
258 if (!(S_ISREG(st->st_mode) || S_ISDIR(st->st_mode) ||
259 S_ISLNK(st->st_mode)))
260 return 0;
262 return 1;
265 static void chmod_done (void)
267 if (need_update)
268 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
269 repaint_screen ();
272 static char *next_file (void)
274 while (!cpanel->dir.list[c_file].f.marked)
275 c_file++;
277 return cpanel->dir.list[c_file].fname;
280 static void do_chmod (struct stat *sf)
282 sf->st_mode &= and_mask;
283 sf->st_mode |= or_mask;
284 if (mc_chmod (cpanel->dir.list [c_file].fname, sf->st_mode) == -1)
285 message (1, MSG_ERROR, _(" Couldn't chmod \"%s\" \n %s "),
286 cpanel->dir.list [c_file].fname, unix_error_string (errno));
288 do_file_mark (cpanel, c_file, 0);
291 static void apply_mask (struct stat *sf)
293 char *fname;
295 need_update = end_chmod = 1;
296 do_chmod (sf);
298 do {
299 fname = next_file ();
300 if (!stat_file (fname, sf))
301 return;
302 c_stat = sf->st_mode;
304 do_chmod (sf);
305 } while (cpanel->marked);
308 void chmod_cmd (void)
310 char buffer [BUF_TINY];
311 char *fname;
312 int i;
313 struct stat sf_stat;
315 #if 0
316 /* Don't do things like this: you do not want to enumerate all
317 filesystems that can not support chmod, here. */
318 if (!vfs_current_is_local ()) {
319 if (vfs_current_is_extfs ()) {
320 message (1, _(" Oops... "),
321 _(" I can't run the Chmod command on an extfs "));
322 return;
323 } else if (vfs_current_is_tarfs ()) {
324 message (1, _(" Oops... "),
325 (" I can't run the Chmod command on a tarfs "));
326 return;
329 #endif
331 do { /* do while any files remaining */
332 init_chmod ();
333 if (cpanel->marked)
334 fname = next_file (); /* next marked file */
335 else
336 fname = selection (cpanel)->fname; /* single file */
338 if (!stat_file (fname, &sf_stat)){ /* get status of file */
339 destroy_dlg (ch_dlg);
340 break;
343 c_stat = sf_stat.st_mode;
344 mode_change = 0; /* clear changes flag */
346 /* set check buttons */
347 for (i = 0; i < PERMISSIONS; i++){
348 check_perm[i].check->state = (c_stat & check_perm[i].mode) ? 1 : 0;
349 check_perm[i].selected = 0;
352 /* Set the labels */
353 c_fname = name_trunc (fname, 21);
354 add_widget (ch_dlg, label_new (FY+2, FX+2, c_fname, NULL));
355 c_fown = name_trunc (get_owner (sf_stat.st_uid), 21);
356 add_widget (ch_dlg, label_new (FY+6, FX+2, c_fown, NULL));
357 c_fgrp = name_trunc (get_group (sf_stat.st_gid), 21);
358 add_widget (ch_dlg, label_new (FY+8, FX+2, c_fgrp, NULL));
359 g_snprintf (buffer, sizeof (buffer), "%o", c_stat);
360 statl = label_new (FY+4, FX+2, buffer, NULL);
361 add_widget (ch_dlg, statl);
363 run_dlg (ch_dlg); /* retrieve an action */
365 /* do action */
366 switch (ch_dlg->ret_value){
367 case B_ENTER:
368 if (mode_change)
369 if (mc_chmod (fname, c_stat) == -1)
370 message (1, MSG_ERROR, _(" Couldn't chmod \"%s\" \n %s "),
371 fname, unix_error_string (errno));
372 need_update = 1;
373 break;
375 case B_CANCEL:
376 end_chmod = 1;
377 break;
379 case B_ALL:
380 case B_MARKED:
381 and_mask = or_mask = 0;
382 and_mask = ~and_mask;
384 for (i = 0; i < PERMISSIONS; i++) {
385 if (check_perm[i].selected || ch_dlg->ret_value == B_ALL)
386 if (check_perm[i].check->state & C_BOOL)
387 or_mask |= check_perm[i].mode;
388 else
389 and_mask &= ~check_perm[i].mode;
392 apply_mask (&sf_stat);
393 break;
395 case B_SETMRK:
396 and_mask = or_mask = 0;
397 and_mask = ~and_mask;
399 for (i = 0; i < PERMISSIONS; i++) {
400 if (check_perm[i].selected)
401 or_mask |= check_perm[i].mode;
404 apply_mask (&sf_stat);
405 break;
406 case B_CLRMRK:
407 and_mask = or_mask = 0;
408 and_mask = ~and_mask;
410 for (i = 0; i < PERMISSIONS; i++) {
411 if (check_perm[i].selected)
412 and_mask &= ~check_perm[i].mode;
415 apply_mask (&sf_stat);
416 break;
419 if (cpanel->marked && ch_dlg->ret_value!=B_CANCEL) {
420 do_file_mark (cpanel, c_file, 0);
421 need_update = 1;
423 destroy_dlg (ch_dlg);
424 } while (cpanel->marked && !end_chmod);
425 chmod_done ();
428 void ch1_cmd (int id)
430 if (advanced_chfns)
431 chown_advanced_cmd ();
432 else
433 chmod_cmd ();
436 void ch2_cmd (int id)
438 if (advanced_chfns)
439 chown_advanced_cmd ();
440 else
441 chown_cmd ();