Refresh PO files
[midnight-commander.git] / src / chmod.c
blob6ee8c88d0fa24375bbf026346ae935b2144b7dc7
1 /* Chmod command -- for the Midnight Commander
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
3 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU 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 02110-1301, USA.
20 /** \file chmod.c
21 * \brief Source: chmod command
24 #include <config.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
34 #include "lib/global.h"
36 #include "lib/tty/tty.h"
37 #include "lib/skin.h"
38 #include "lib/vfs/mc-vfs/vfs.h"
39 #include "lib/strutil.h"
41 #include "dialog.h" /* add_widget() */
42 #include "widget.h" /* NORMAL_BUTTON */
43 #include "wtools.h" /* message() */
44 #include "panel.h" /* do_file_mark() */
45 #include "main.h" /* update_panels() */
46 #include "layout.h" /* repaint_screen() */
47 #include "chmod.h"
49 static int single_set;
51 #define PX 5
52 #define PY 2
54 #define FX 40
55 #define FY 2
57 #define BX 6
58 #define BY 17
60 #define TX 40
61 #define TY 12
63 #define PERMISSIONS 12
64 #define BUTTONS 6
66 #define B_MARKED B_USER
67 #define B_ALL (B_USER+1)
68 #define B_SETMRK (B_USER+2)
69 #define B_CLRMRK (B_USER+3)
71 static int mode_change, need_update;
72 static int c_file, end_chmod;
74 static mode_t and_mask, or_mask, c_stat;
76 /* FIXME: these variables are superfluous, aren't they? (hint: name_trunc
77 * returns a pointer to a static buffer, and label_new creates its own copy
78 * of its argument)
79 * --rillig, 2004-08-29 */
80 static const char *c_fname, *c_fown, *c_fgrp;
82 static WLabel *statl;
84 static struct
86 mode_t mode;
87 const char *text;
88 int selected;
89 WCheck *check;
90 } check_perm[PERMISSIONS] =
92 /* *INDENT-OFF* */
93 { S_IXOTH, N_("execute/search by others"), 0, 0 },
94 { S_IWOTH, N_("write by others"), 0, 0 },
95 { S_IROTH, N_("read by others"), 0, 0 },
96 { S_IXGRP, N_("execute/search by group"), 0, 0 },
97 { S_IWGRP, N_("write by group"), 0, 0 },
98 { S_IRGRP, N_("read by group"), 0, 0 },
99 { S_IXUSR, N_("execute/search by owner"), 0, 0 },
100 { S_IWUSR, N_("write by owner"), 0, 0 },
101 { S_IRUSR, N_("read by owner"), 0, 0 },
102 { S_ISVTX, N_("sticky bit"), 0, 0 },
103 { S_ISGID, N_("set group ID on execution"), 0, 0 },
104 { S_ISUID, N_("set user ID on execution"), 0, 0 }
105 /* *INDENT-ON* */
108 static struct
110 int ret_cmd, flags, y, x;
111 const char *text;
112 } chmod_but[BUTTONS] =
114 /* *INDENT-OFF* */
115 { B_CANCEL, NORMAL_BUTTON, 2, 33, N_("&Cancel") },
116 { B_ENTER, DEFPUSH_BUTTON, 2, 17, N_("&Set") },
117 { B_CLRMRK, NORMAL_BUTTON, 0, 42, N_("C&lear marked") },
118 { B_SETMRK, NORMAL_BUTTON, 0, 27, N_("S&et marked") },
119 { B_MARKED, NORMAL_BUTTON, 0, 12, N_("&Marked all") },
120 { B_ALL, NORMAL_BUTTON, 0, 0, N_("Set &all") }
121 /* *INDENT-ON* */
124 static void
125 chmod_toggle_select (Dlg_head * h, int Id)
127 tty_setcolor (COLOR_NORMAL);
128 check_perm[Id].selected ^= 1;
130 dlg_move (h, PY + PERMISSIONS - Id, PX + 1);
131 tty_print_char ((check_perm[Id].selected) ? '*' : ' ');
132 dlg_move (h, PY + PERMISSIONS - Id, PX + 3);
135 static void
136 chmod_refresh (Dlg_head * h)
138 common_dialog_repaint (h);
140 tty_setcolor (COLOR_NORMAL);
142 draw_box (h, PY, PX, PERMISSIONS + 2, 33, TRUE);
143 draw_box (h, FY, FX, 10, 25, TRUE);
145 dlg_move (h, FY + 1, FX + 2);
146 tty_print_string (_("Name"));
147 dlg_move (h, FY + 3, FX + 2);
148 tty_print_string (_("Permissions (Octal)"));
149 dlg_move (h, FY + 5, FX + 2);
150 tty_print_string (_("Owner name"));
151 dlg_move (h, FY + 7, FX + 2);
152 tty_print_string (_("Group name"));
154 dlg_move (h, TY, TX);
155 tty_print_string (_("Use SPACE to change"));
156 dlg_move (h, TY + 1, TX);
157 tty_print_string (_("an option, ARROW KEYS"));
158 dlg_move (h, TY + 2, TX);
159 tty_print_string (_("to move between options"));
160 dlg_move (h, TY + 3, TX);
161 tty_print_string (_("and T or INS to mark"));
163 tty_setcolor (COLOR_HOT_NORMAL);
165 dlg_move (h, PY, PX + 1);
166 tty_print_string (_(" Permission "));
167 dlg_move (h, FY, FX + 1);
168 tty_print_string (_(" File "));
171 static cb_ret_t
172 chmod_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
174 char buffer[BUF_TINY];
175 int id = h->current->dlg_id - BUTTONS + single_set * 2;
177 switch (msg)
179 case DLG_ACTION:
180 if (id >= 0)
182 c_stat ^= check_perm[id].mode;
183 g_snprintf (buffer, sizeof (buffer), "%o", c_stat);
184 label_set_text (statl, buffer);
185 chmod_toggle_select (h, id);
186 mode_change = 1;
188 return MSG_HANDLED;
190 case DLG_KEY:
191 if ((parm == 'T' || parm == 't' || parm == KEY_IC) && id > 0)
193 chmod_toggle_select (h, id);
194 if (parm == KEY_IC)
195 dlg_one_down (h);
196 return MSG_HANDLED;
198 return MSG_NOT_HANDLED;
200 case DLG_DRAW:
201 chmod_refresh (h);
202 return MSG_HANDLED;
204 default:
205 return default_dlg_callback (h, sender, msg, parm, data);
209 static Dlg_head *
210 init_chmod (void)
212 int i;
213 Dlg_head *ch_dlg;
215 do_refresh ();
216 end_chmod = c_file = need_update = 0;
217 single_set = (current_panel->marked < 2) ? 2 : 0;
219 ch_dlg =
220 create_dlg (0, 0, 22 - single_set, 70, dialog_colors,
221 chmod_callback, "[Chmod]", _("Chmod command"), DLG_CENTER | DLG_REVERSE);
223 for (i = 0; i < BUTTONS; i++)
225 if (i == 2 && single_set)
226 break;
227 else
228 add_widget (ch_dlg,
229 button_new (BY + chmod_but[i].y - single_set,
230 BX + chmod_but[i].x,
231 chmod_but[i].ret_cmd,
232 chmod_but[i].flags, _(chmod_but[i].text), 0));
235 for (i = 0; i < PERMISSIONS; i++)
237 check_perm[i].check = check_new (PY + (PERMISSIONS - i), PX + 2, 0, _(check_perm[i].text));
238 add_widget (ch_dlg, check_perm[i].check);
241 return ch_dlg;
244 static void
245 chmod_done (void)
247 if (need_update)
248 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
249 repaint_screen ();
252 static char *
253 next_file (void)
255 while (!current_panel->dir.list[c_file].f.marked)
256 c_file++;
258 return current_panel->dir.list[c_file].fname;
261 static void
262 do_chmod (struct stat *sf)
264 sf->st_mode &= and_mask;
265 sf->st_mode |= or_mask;
266 if (mc_chmod (current_panel->dir.list[c_file].fname, sf->st_mode) == -1)
267 message (D_ERROR, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
268 current_panel->dir.list[c_file].fname, unix_error_string (errno));
270 do_file_mark (current_panel, c_file, 0);
273 static void
274 apply_mask (struct stat *sf)
276 char *fname;
278 need_update = end_chmod = 1;
279 do_chmod (sf);
283 fname = next_file ();
284 if (mc_stat (fname, sf) != 0)
285 return;
286 c_stat = sf->st_mode;
288 do_chmod (sf);
290 while (current_panel->marked);
293 void
294 chmod_cmd (void)
296 char buffer[BUF_TINY];
297 char *fname;
298 int i;
299 struct stat sf_stat;
300 Dlg_head *ch_dlg;
303 { /* do while any files remaining */
304 ch_dlg = init_chmod ();
305 if (current_panel->marked)
306 fname = next_file (); /* next marked file */
307 else
308 fname = selection (current_panel)->fname; /* single file */
310 if (mc_stat (fname, &sf_stat) != 0)
311 { /* get status of file */
312 destroy_dlg (ch_dlg);
313 break;
316 c_stat = sf_stat.st_mode;
317 mode_change = 0; /* clear changes flag */
319 /* set check buttons */
320 for (i = 0; i < PERMISSIONS; i++)
322 check_perm[i].check->state = (c_stat & check_perm[i].mode) ? 1 : 0;
323 check_perm[i].selected = 0;
326 /* Set the labels */
327 c_fname = str_trunc (fname, 21);
328 add_widget (ch_dlg, label_new (FY + 2, FX + 2, c_fname));
329 c_fown = str_trunc (get_owner (sf_stat.st_uid), 21);
330 add_widget (ch_dlg, label_new (FY + 6, FX + 2, c_fown));
331 c_fgrp = str_trunc (get_group (sf_stat.st_gid), 21);
332 add_widget (ch_dlg, label_new (FY + 8, FX + 2, c_fgrp));
333 g_snprintf (buffer, sizeof (buffer), "%o", c_stat);
334 statl = label_new (FY + 4, FX + 2, buffer);
335 add_widget (ch_dlg, statl);
337 run_dlg (ch_dlg); /* retrieve an action */
339 /* do action */
340 switch (ch_dlg->ret_value)
342 case B_ENTER:
343 if (mode_change)
344 if (mc_chmod (fname, c_stat) == -1)
345 message (D_ERROR, MSG_ERROR, _(" Cannot chmod \"%s\" \n %s "),
346 fname, unix_error_string (errno));
347 need_update = 1;
348 break;
350 case B_CANCEL:
351 end_chmod = 1;
352 break;
354 case B_ALL:
355 case B_MARKED:
356 and_mask = or_mask = 0;
357 and_mask = ~and_mask;
359 for (i = 0; i < PERMISSIONS; i++)
361 if (check_perm[i].selected || ch_dlg->ret_value == B_ALL)
363 if (check_perm[i].check->state & C_BOOL)
364 or_mask |= check_perm[i].mode;
365 else
366 and_mask &= ~check_perm[i].mode;
370 apply_mask (&sf_stat);
371 break;
373 case B_SETMRK:
374 and_mask = or_mask = 0;
375 and_mask = ~and_mask;
377 for (i = 0; i < PERMISSIONS; i++)
379 if (check_perm[i].selected)
380 or_mask |= check_perm[i].mode;
383 apply_mask (&sf_stat);
384 break;
385 case B_CLRMRK:
386 and_mask = or_mask = 0;
387 and_mask = ~and_mask;
389 for (i = 0; i < PERMISSIONS; i++)
391 if (check_perm[i].selected)
392 and_mask &= ~check_perm[i].mode;
395 apply_mask (&sf_stat);
396 break;
399 if (current_panel->marked && ch_dlg->ret_value != B_CANCEL)
401 do_file_mark (current_panel, c_file, 0);
402 need_update = 1;
404 destroy_dlg (ch_dlg);
406 while (current_panel->marked && !end_chmod);
407 chmod_done ();