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 "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() */
49 static int single_set
;
63 #define PERMISSIONS 12
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
79 * --rillig, 2004-08-29 */
80 static const char *c_fname
, *c_fown
, *c_fgrp
;
90 } check_perm
[PERMISSIONS
] =
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 }
110 int ret_cmd
, flags
, y
, x
;
112 } chmod_but
[BUTTONS
] =
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") }
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);
136 chmod_refresh (Dlg_head
* h
)
138 common_dialog_repaint (h
);
140 tty_setcolor (COLOR_NORMAL
);
142 dlg_move (h
, FY
+ 1, FX
+ 2);
143 tty_print_string (_("Name"));
144 dlg_move (h
, FY
+ 3, FX
+ 2);
145 tty_print_string (_("Permissions (Octal)"));
146 dlg_move (h
, FY
+ 5, FX
+ 2);
147 tty_print_string (_("Owner name"));
148 dlg_move (h
, FY
+ 7, FX
+ 2);
149 tty_print_string (_("Group name"));
151 dlg_move (h
, TY
, TX
);
152 tty_print_string (_("Use SPACE to change"));
153 dlg_move (h
, TY
+ 1, TX
);
154 tty_print_string (_("an option, ARROW KEYS"));
155 dlg_move (h
, TY
+ 2, TX
);
156 tty_print_string (_("to move between options"));
157 dlg_move (h
, TY
+ 3, TX
);
158 tty_print_string (_("and T or INS to mark"));
162 chmod_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
164 char buffer
[BUF_TINY
];
165 int id
= dlg_get_current_widget_id (h
) - BUTTONS
+ single_set
* 2 - 1;
172 c_stat
^= check_perm
[id
].mode
;
173 g_snprintf (buffer
, sizeof (buffer
), "%o", c_stat
);
174 label_set_text (statl
, buffer
);
175 chmod_toggle_select (h
, id
);
181 if ((parm
== 'T' || parm
== 't' || parm
== KEY_IC
) && id
> 0)
183 chmod_toggle_select (h
, id
);
188 return MSG_NOT_HANDLED
;
195 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
206 end_chmod
= c_file
= need_update
= 0;
207 single_set
= (current_panel
->marked
< 2) ? 2 : 0;
210 create_dlg (TRUE
, 0, 0, 22 - single_set
, 70, dialog_colors
,
211 chmod_callback
, "[Chmod]", _("Chmod command"), DLG_CENTER
| DLG_REVERSE
);
213 for (i
= 0; i
< BUTTONS
; i
++)
215 if (i
== 2 && single_set
)
219 button_new (BY
+ chmod_but
[i
].y
- single_set
,
221 chmod_but
[i
].ret_cmd
,
222 chmod_but
[i
].flags
, _(chmod_but
[i
].text
), 0));
225 add_widget (ch_dlg
, groupbox_new (FY
, FX
, 10, 25, _("File")));
227 for (i
= 0; i
< PERMISSIONS
; i
++)
229 check_perm
[i
].check
= check_new (PY
+ (PERMISSIONS
- i
), PX
+ 2, 0, _(check_perm
[i
].text
));
230 add_widget (ch_dlg
, check_perm
[i
].check
);
233 add_widget (ch_dlg
, groupbox_new ( PY
, PX
, PERMISSIONS
+ 2, 33, _("Permission")));
242 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
249 while (!current_panel
->dir
.list
[c_file
].f
.marked
)
252 return current_panel
->dir
.list
[c_file
].fname
;
256 do_chmod (struct stat
*sf
)
258 sf
->st_mode
&= and_mask
;
259 sf
->st_mode
|= or_mask
;
260 if (mc_chmod (current_panel
->dir
.list
[c_file
].fname
, sf
->st_mode
) == -1)
261 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
262 current_panel
->dir
.list
[c_file
].fname
, unix_error_string (errno
));
264 do_file_mark (current_panel
, c_file
, 0);
268 apply_mask (struct stat
*sf
)
272 need_update
= end_chmod
= 1;
277 fname
= next_file ();
278 if (mc_stat (fname
, sf
) != 0)
280 c_stat
= sf
->st_mode
;
284 while (current_panel
->marked
);
290 char buffer
[BUF_TINY
];
297 { /* do while any files remaining */
298 ch_dlg
= init_chmod ();
299 if (current_panel
->marked
)
300 fname
= next_file (); /* next marked file */
302 fname
= selection (current_panel
)->fname
; /* single file */
304 if (mc_stat (fname
, &sf_stat
) != 0)
305 { /* get status of file */
306 destroy_dlg (ch_dlg
);
310 c_stat
= sf_stat
.st_mode
;
311 mode_change
= 0; /* clear changes flag */
313 /* set check buttons */
314 for (i
= 0; i
< PERMISSIONS
; i
++)
316 check_perm
[i
].check
->state
= (c_stat
& check_perm
[i
].mode
) ? 1 : 0;
317 check_perm
[i
].selected
= 0;
321 c_fname
= str_trunc (fname
, 21);
322 add_widget (ch_dlg
, label_new (FY
+ 2, FX
+ 2, c_fname
));
323 c_fown
= str_trunc (get_owner (sf_stat
.st_uid
), 21);
324 add_widget (ch_dlg
, label_new (FY
+ 6, FX
+ 2, c_fown
));
325 c_fgrp
= str_trunc (get_group (sf_stat
.st_gid
), 21);
326 add_widget (ch_dlg
, label_new (FY
+ 8, FX
+ 2, c_fgrp
));
327 g_snprintf (buffer
, sizeof (buffer
), "%o", c_stat
);
328 statl
= label_new (FY
+ 4, FX
+ 2, buffer
);
329 add_widget (ch_dlg
, statl
);
331 run_dlg (ch_dlg
); /* retrieve an action */
334 switch (ch_dlg
->ret_value
)
338 if (mc_chmod (fname
, c_stat
) == -1)
339 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
340 fname
, unix_error_string (errno
));
350 and_mask
= or_mask
= 0;
351 and_mask
= ~and_mask
;
353 for (i
= 0; i
< PERMISSIONS
; i
++)
355 if (check_perm
[i
].selected
|| ch_dlg
->ret_value
== B_ALL
)
357 if (check_perm
[i
].check
->state
& C_BOOL
)
358 or_mask
|= check_perm
[i
].mode
;
360 and_mask
&= ~check_perm
[i
].mode
;
364 apply_mask (&sf_stat
);
368 and_mask
= or_mask
= 0;
369 and_mask
= ~and_mask
;
371 for (i
= 0; i
< PERMISSIONS
; i
++)
373 if (check_perm
[i
].selected
)
374 or_mask
|= check_perm
[i
].mode
;
377 apply_mask (&sf_stat
);
381 and_mask
= or_mask
= 0;
382 and_mask
= ~and_mask
;
384 for (i
= 0; i
< PERMISSIONS
; i
++)
386 if (check_perm
[i
].selected
)
387 and_mask
&= ~check_perm
[i
].mode
;
390 apply_mask (&sf_stat
);
394 if (current_panel
->marked
&& ch_dlg
->ret_value
!= B_CANCEL
)
396 do_file_mark (current_panel
, c_file
, 0);
400 destroy_dlg (ch_dlg
);
402 while (current_panel
->marked
&& !end_chmod
);