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.
21 * \brief Source: chmod command
30 #include <sys/types.h>
34 #include "lib/global.h"
36 #include "lib/tty/tty.h"
38 #include "lib/vfs/mc-vfs/vfs.h"
39 #include "lib/strutil.h"
41 #include "lib/widget.h"
43 #include "panel.h" /* do_file_mark() */
44 #include "main.h" /* update_panels() */
45 #include "layout.h" /* repaint_screen() */
48 /*** global variables ****************************************************************************/
50 /*** file scope macro definitions ****************************************************************/
64 #define PERMISSIONS 12
67 #define B_MARKED B_USER
68 #define B_ALL (B_USER+1)
69 #define B_SETMRK (B_USER+2)
70 #define B_CLRMRK (B_USER+3)
72 /*** file scope type declarations ****************************************************************/
74 /*** file scope variables ************************************************************************/
76 static int single_set
;
78 static int mode_change
, need_update
;
79 static int c_file
, end_chmod
;
81 static mode_t and_mask
, or_mask
, c_stat
;
83 /* FIXME: these variables are superfluous, aren't they? (hint: str_trunc
84 * returns a pointer to a static buffer, and label_new creates its own copy
86 * --rillig, 2004-08-29 */
87 static const char *c_fname
, *c_fown
, *c_fgrp
;
97 } check_perm
[PERMISSIONS
] =
100 { S_IXOTH
, N_("execute/search by others"), 0, 0 },
101 { S_IWOTH
, N_("write by others"), 0, 0 },
102 { S_IROTH
, N_("read by others"), 0, 0 },
103 { S_IXGRP
, N_("execute/search by group"), 0, 0 },
104 { S_IWGRP
, N_("write by group"), 0, 0 },
105 { S_IRGRP
, N_("read by group"), 0, 0 },
106 { S_IXUSR
, N_("execute/search by owner"), 0, 0 },
107 { S_IWUSR
, N_("write by owner"), 0, 0 },
108 { S_IRUSR
, N_("read by owner"), 0, 0 },
109 { S_ISVTX
, N_("sticky bit"), 0, 0 },
110 { S_ISGID
, N_("set group ID on execution"), 0, 0 },
111 { S_ISUID
, N_("set user ID on execution"), 0, 0 }
117 int ret_cmd
, flags
, y
, x
;
119 } chmod_but
[BUTTONS
] =
122 { B_CANCEL
, NORMAL_BUTTON
, 2, 33, N_("&Cancel") },
123 { B_ENTER
, DEFPUSH_BUTTON
, 2, 17, N_("&Set") },
124 { B_CLRMRK
, NORMAL_BUTTON
, 0, 42, N_("C&lear marked") },
125 { B_SETMRK
, NORMAL_BUTTON
, 0, 27, N_("S&et marked") },
126 { B_MARKED
, NORMAL_BUTTON
, 0, 12, N_("&Marked all") },
127 { B_ALL
, NORMAL_BUTTON
, 0, 0, N_("Set &all") }
131 /*** file scope functions ************************************************************************/
132 /* --------------------------------------------------------------------------------------------- */
135 chmod_toggle_select (Dlg_head
* h
, int Id
)
137 tty_setcolor (COLOR_NORMAL
);
138 check_perm
[Id
].selected
^= 1;
140 dlg_move (h
, PY
+ PERMISSIONS
- Id
, PX
+ 1);
141 tty_print_char ((check_perm
[Id
].selected
) ? '*' : ' ');
142 dlg_move (h
, PY
+ PERMISSIONS
- Id
, PX
+ 3);
145 /* --------------------------------------------------------------------------------------------- */
148 chmod_refresh (Dlg_head
* h
)
150 common_dialog_repaint (h
);
152 tty_setcolor (COLOR_NORMAL
);
154 dlg_move (h
, FY
+ 1, FX
+ 2);
155 tty_print_string (_("Name"));
156 dlg_move (h
, FY
+ 3, FX
+ 2);
157 tty_print_string (_("Permissions (Octal)"));
158 dlg_move (h
, FY
+ 5, FX
+ 2);
159 tty_print_string (_("Owner name"));
160 dlg_move (h
, FY
+ 7, FX
+ 2);
161 tty_print_string (_("Group name"));
163 dlg_move (h
, TY
, TX
);
164 tty_print_string (_("Use SPACE to change"));
165 dlg_move (h
, TY
+ 1, TX
);
166 tty_print_string (_("an option, ARROW KEYS"));
167 dlg_move (h
, TY
+ 2, TX
);
168 tty_print_string (_("to move between options"));
169 dlg_move (h
, TY
+ 3, TX
);
170 tty_print_string (_("and T or INS to mark"));
173 /* --------------------------------------------------------------------------------------------- */
176 chmod_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
178 char buffer
[BUF_TINY
];
179 int id
= dlg_get_current_widget_id (h
) - BUTTONS
+ single_set
* 2 - 1;
186 c_stat
^= check_perm
[id
].mode
;
187 g_snprintf (buffer
, sizeof (buffer
), "%o", c_stat
);
188 label_set_text (statl
, buffer
);
189 chmod_toggle_select (h
, id
);
195 if ((parm
== 'T' || parm
== 't' || parm
== KEY_IC
) && id
> 0)
197 chmod_toggle_select (h
, id
);
202 return MSG_NOT_HANDLED
;
209 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
213 /* --------------------------------------------------------------------------------------------- */
222 end_chmod
= c_file
= need_update
= 0;
223 single_set
= (current_panel
->marked
< 2) ? 2 : 0;
226 create_dlg (TRUE
, 0, 0, 22 - single_set
, 70, dialog_colors
,
227 chmod_callback
, "[Chmod]", _("Chmod command"), DLG_CENTER
| DLG_REVERSE
);
229 for (i
= 0; i
< BUTTONS
; i
++)
231 if (i
== 2 && single_set
)
235 button_new (BY
+ chmod_but
[i
].y
- single_set
,
237 chmod_but
[i
].ret_cmd
,
238 chmod_but
[i
].flags
, _(chmod_but
[i
].text
), 0));
241 add_widget (ch_dlg
, groupbox_new (FY
, FX
, 10, 25, _("File")));
243 for (i
= 0; i
< PERMISSIONS
; i
++)
245 check_perm
[i
].check
= check_new (PY
+ (PERMISSIONS
- i
), PX
+ 2, 0, _(check_perm
[i
].text
));
246 add_widget (ch_dlg
, check_perm
[i
].check
);
249 add_widget (ch_dlg
, groupbox_new (PY
, PX
, PERMISSIONS
+ 2, 33, _("Permission")));
254 /* --------------------------------------------------------------------------------------------- */
260 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
264 /* --------------------------------------------------------------------------------------------- */
269 while (!current_panel
->dir
.list
[c_file
].f
.marked
)
272 return current_panel
->dir
.list
[c_file
].fname
;
275 /* --------------------------------------------------------------------------------------------- */
278 do_chmod (struct stat
*sf
)
280 sf
->st_mode
&= and_mask
;
281 sf
->st_mode
|= or_mask
;
282 if (mc_chmod (current_panel
->dir
.list
[c_file
].fname
, sf
->st_mode
) == -1)
283 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
284 current_panel
->dir
.list
[c_file
].fname
, unix_error_string (errno
));
286 do_file_mark (current_panel
, c_file
, 0);
289 /* --------------------------------------------------------------------------------------------- */
292 apply_mask (struct stat
*sf
)
296 need_update
= end_chmod
= 1;
301 fname
= next_file ();
302 if (mc_stat (fname
, sf
) != 0)
304 c_stat
= sf
->st_mode
;
308 while (current_panel
->marked
);
311 /* --------------------------------------------------------------------------------------------- */
312 /*** public functions ****************************************************************************/
313 /* --------------------------------------------------------------------------------------------- */
318 char buffer
[BUF_TINY
];
325 { /* do while any files remaining */
326 ch_dlg
= init_chmod ();
327 if (current_panel
->marked
)
328 fname
= next_file (); /* next marked file */
330 fname
= selection (current_panel
)->fname
; /* single file */
332 if (mc_stat (fname
, &sf_stat
) != 0)
333 { /* get status of file */
334 destroy_dlg (ch_dlg
);
338 c_stat
= sf_stat
.st_mode
;
339 mode_change
= 0; /* clear changes flag */
341 /* set check buttons */
342 for (i
= 0; i
< PERMISSIONS
; i
++)
344 check_perm
[i
].check
->state
= (c_stat
& check_perm
[i
].mode
) ? 1 : 0;
345 check_perm
[i
].selected
= 0;
349 c_fname
= str_trunc (fname
, 21);
350 add_widget (ch_dlg
, label_new (FY
+ 2, FX
+ 2, c_fname
));
351 c_fown
= str_trunc (get_owner (sf_stat
.st_uid
), 21);
352 add_widget (ch_dlg
, label_new (FY
+ 6, FX
+ 2, c_fown
));
353 c_fgrp
= str_trunc (get_group (sf_stat
.st_gid
), 21);
354 add_widget (ch_dlg
, label_new (FY
+ 8, FX
+ 2, c_fgrp
));
355 g_snprintf (buffer
, sizeof (buffer
), "%o", c_stat
);
356 statl
= label_new (FY
+ 4, FX
+ 2, buffer
);
357 add_widget (ch_dlg
, statl
);
359 run_dlg (ch_dlg
); /* retrieve an action */
362 switch (ch_dlg
->ret_value
)
366 if (mc_chmod (fname
, c_stat
) == -1)
367 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
368 fname
, unix_error_string (errno
));
378 and_mask
= or_mask
= 0;
379 and_mask
= ~and_mask
;
381 for (i
= 0; i
< PERMISSIONS
; i
++)
383 if (check_perm
[i
].selected
|| ch_dlg
->ret_value
== B_ALL
)
385 if (check_perm
[i
].check
->state
& C_BOOL
)
386 or_mask
|= check_perm
[i
].mode
;
388 and_mask
&= ~check_perm
[i
].mode
;
392 apply_mask (&sf_stat
);
396 and_mask
= or_mask
= 0;
397 and_mask
= ~and_mask
;
399 for (i
= 0; i
< PERMISSIONS
; i
++)
401 if (check_perm
[i
].selected
)
402 or_mask
|= check_perm
[i
].mode
;
405 apply_mask (&sf_stat
);
409 and_mask
= or_mask
= 0;
410 and_mask
= ~and_mask
;
412 for (i
= 0; i
< PERMISSIONS
; i
++)
414 if (check_perm
[i
].selected
)
415 and_mask
&= ~check_perm
[i
].mode
;
418 apply_mask (&sf_stat
);
422 if (current_panel
->marked
&& ch_dlg
->ret_value
!= B_CANCEL
)
424 do_file_mark (current_panel
, c_file
, 0);
428 destroy_dlg (ch_dlg
);
430 while (current_panel
->marked
&& !end_chmod
);
435 /* --------------------------------------------------------------------------------------------- */