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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include <sys/types.h>
30 #include "tty.h" /* A_REVERSE */
31 #include "color.h" /* dialog_colors */
32 #include "dialog.h" /* add_widget() */
33 #include "widget.h" /* NORMAL_BUTTON */
34 #include "wtools.h" /* message() */
35 #include "panel.h" /* do_file_mark() */
36 #include "main.h" /* update_panels() */
39 static int single_set
;
53 #define PERMISSIONS 12
56 #define B_MARKED B_USER
57 #define B_ALL (B_USER+1)
58 #define B_SETMRK (B_USER+2)
59 #define B_CLRMRK (B_USER+3)
61 static int mode_change
, need_update
;
62 static int c_file
, end_chmod
;
64 static mode_t and_mask
, or_mask
, c_stat
;
66 /* FIXME: these variables are superfluous, aren't they? (hint: name_trunc
67 * returns a pointer to a static buffer, and label_new creates its own copy
69 * --rillig, 2004-08-29 */
70 static const char *c_fname
, *c_fown
, *c_fgrp
;
79 } check_perm
[PERMISSIONS
] =
81 { S_IXOTH
, N_("execute/search by others"), 0, 0, },
82 { S_IWOTH
, N_("write by others"), 0, 0, },
83 { S_IROTH
, N_("read by others"), 0, 0, },
84 { S_IXGRP
, N_("execute/search by group"), 0, 0, },
85 { S_IWGRP
, N_("write by group"), 0, 0, },
86 { S_IRGRP
, N_("read by group"), 0, 0, },
87 { S_IXUSR
, N_("execute/search by owner"), 0, 0, },
88 { S_IWUSR
, N_("write by owner"), 0, 0, },
89 { S_IRUSR
, N_("read by owner"), 0, 0, },
90 { S_ISVTX
, N_("sticky bit"), 0, 0, },
91 { S_ISGID
, N_("set group ID on execution"), 0, 0, },
92 { S_ISUID
, N_("set user ID on execution"), 0, 0, },
96 int ret_cmd
, flags
, y
, x
;
98 } chmod_but
[BUTTONS
] =
100 { B_CANCEL
, NORMAL_BUTTON
, 2, 33, N_("&Cancel") },
101 { B_ENTER
, DEFPUSH_BUTTON
, 2, 17, N_("&Set") },
102 { B_CLRMRK
, NORMAL_BUTTON
, 0, 42, N_("C&lear marked") },
103 { B_SETMRK
, NORMAL_BUTTON
, 0, 27, N_("S&et marked") },
104 { B_MARKED
, NORMAL_BUTTON
, 0, 12, N_("&Marked all") },
105 { B_ALL
, NORMAL_BUTTON
, 0, 0, N_("Set &all") },
108 static void chmod_toggle_select (Dlg_head
*h
, int Id
)
110 attrset (COLOR_NORMAL
);
111 check_perm
[Id
].selected
^= 1;
113 dlg_move (h
, PY
+ PERMISSIONS
- Id
, PX
+ 1);
114 addch ((check_perm
[Id
].selected
) ? '*' : ' ');
115 dlg_move (h
, PY
+ PERMISSIONS
- Id
, PX
+ 3);
118 static void chmod_refresh (Dlg_head
*h
)
120 common_dialog_repaint (h
);
122 attrset (COLOR_NORMAL
);
124 draw_box (h
, PY
, PX
, PERMISSIONS
+ 2, 33);
125 draw_box (h
, FY
, FX
, 10, 25);
127 dlg_move (h
, FY
+ 1, FX
+ 2);
129 dlg_move (h
, FY
+ 3, FX
+ 2);
130 addstr (_("Permissions (Octal)"));
131 dlg_move (h
, FY
+ 5, FX
+ 2);
132 addstr (_("Owner name"));
133 dlg_move (h
, FY
+ 7, FX
+ 2);
134 addstr (_("Group name"));
136 dlg_move (h
, TY
, TX
);
137 addstr (_("Use SPACE to change"));
138 dlg_move (h
, TY
+ 1, TX
);
139 addstr (_("an option, ARROW KEYS"));
140 dlg_move (h
, TY
+ 2, TX
);
141 addstr (_("to move between options"));
142 dlg_move (h
, TY
+ 3, TX
);
143 addstr (_("and T or INS to mark"));
145 attrset (COLOR_HOT_NORMAL
);
147 dlg_move (h
, PY
, PX
+ 1);
148 addstr (_(" Permission "));
149 dlg_move (h
, FY
, FX
+ 1);
150 addstr (_(" File "));
154 chmod_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
)
156 char buffer
[BUF_TINY
];
157 int id
= h
->current
->dlg_id
- BUTTONS
+ single_set
* 2;
162 c_stat
^= check_perm
[id
].mode
;
163 g_snprintf (buffer
, sizeof (buffer
), "%o", c_stat
);
164 label_set_text (statl
, buffer
);
165 chmod_toggle_select (h
, id
);
171 if ((parm
== 'T' || parm
== 't' || parm
== KEY_IC
) && id
> 0) {
172 chmod_toggle_select (h
, id
);
177 return MSG_NOT_HANDLED
;
184 return default_dlg_callback (h
, msg
, parm
);
195 end_chmod
= c_file
= need_update
= 0;
196 single_set
= (current_panel
->marked
< 2) ? 2 : 0;
199 create_dlg (0, 0, 22 - single_set
, 70, dialog_colors
,
200 chmod_callback
, "[Chmod]", _("Chmod command"),
201 DLG_CENTER
| DLG_REVERSE
);
203 for (i
= 0; i
< BUTTONS
; i
++) {
204 if (i
== 2 && single_set
)
208 button_new (BY
+ chmod_but
[i
].y
- single_set
,
210 chmod_but
[i
].ret_cmd
,
212 _(chmod_but
[i
].text
), 0));
215 for (i
= 0; i
< PERMISSIONS
; i
++) {
216 check_perm
[i
].check
=
217 check_new (PY
+ (PERMISSIONS
- i
), PX
+ 2, 0,
218 _(check_perm
[i
].text
));
219 add_widget (ch_dlg
, check_perm
[i
].check
);
225 static void chmod_done (void)
228 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
232 static char *next_file (void)
234 while (!current_panel
->dir
.list
[c_file
].f
.marked
)
237 return current_panel
->dir
.list
[c_file
].fname
;
240 static void do_chmod (struct stat
*sf
)
242 sf
->st_mode
&= and_mask
;
243 sf
->st_mode
|= or_mask
;
244 if (mc_chmod (current_panel
->dir
.list
[c_file
].fname
, sf
->st_mode
) == -1)
245 message (1, MSG_ERROR
, _(" Cannot chmod \"%s\" \n %s "),
246 current_panel
->dir
.list
[c_file
].fname
, unix_error_string (errno
));
248 do_file_mark (current_panel
, c_file
, 0);
251 static void apply_mask (struct stat
*sf
)
255 need_update
= end_chmod
= 1;
259 fname
= next_file ();
260 if (mc_stat (fname
, sf
) != 0)
262 c_stat
= sf
->st_mode
;
265 } while (current_panel
->marked
);
268 void chmod_cmd (void)
270 char buffer
[BUF_TINY
];
276 do { /* do while any files remaining */
277 ch_dlg
= init_chmod ();
278 if (current_panel
->marked
)
279 fname
= next_file (); /* next marked file */
281 fname
= selection (current_panel
)->fname
; /* single file */
283 if (mc_stat (fname
, &sf_stat
) != 0) { /* get status of file */
284 destroy_dlg (ch_dlg
);
288 c_stat
= sf_stat
.st_mode
;
289 mode_change
= 0; /* clear changes flag */
291 /* set check buttons */
292 for (i
= 0; i
< PERMISSIONS
; i
++){
293 check_perm
[i
].check
->state
= (c_stat
& check_perm
[i
].mode
) ? 1 : 0;
294 check_perm
[i
].selected
= 0;
298 c_fname
= name_trunc (fname
, 21);
299 add_widget (ch_dlg
, label_new (FY
+2, FX
+2, c_fname
));
300 c_fown
= name_trunc (get_owner (sf_stat
.st_uid
), 21);
301 add_widget (ch_dlg
, label_new (FY
+6, FX
+2, c_fown
));
302 c_fgrp
= name_trunc (get_group (sf_stat
.st_gid
), 21);
303 add_widget (ch_dlg
, label_new (FY
+8, FX
+2, c_fgrp
));
304 g_snprintf (buffer
, sizeof (buffer
), "%o", c_stat
);
305 statl
= label_new (FY
+4, FX
+2, buffer
);
306 add_widget (ch_dlg
, statl
);
308 run_dlg (ch_dlg
); /* retrieve an action */
311 switch (ch_dlg
->ret_value
){
314 if (mc_chmod (fname
, c_stat
) == -1)
315 message (1, MSG_ERROR
, _(" Cannot chmod \"%s\" \n %s "),
316 fname
, unix_error_string (errno
));
326 and_mask
= or_mask
= 0;
327 and_mask
= ~and_mask
;
329 for (i
= 0; i
< PERMISSIONS
; i
++) {
330 if (check_perm
[i
].selected
|| ch_dlg
->ret_value
== B_ALL
) {
331 if (check_perm
[i
].check
->state
& C_BOOL
)
332 or_mask
|= check_perm
[i
].mode
;
334 and_mask
&= ~check_perm
[i
].mode
;
338 apply_mask (&sf_stat
);
342 and_mask
= or_mask
= 0;
343 and_mask
= ~and_mask
;
345 for (i
= 0; i
< PERMISSIONS
; i
++) {
346 if (check_perm
[i
].selected
)
347 or_mask
|= check_perm
[i
].mode
;
350 apply_mask (&sf_stat
);
353 and_mask
= or_mask
= 0;
354 and_mask
= ~and_mask
;
356 for (i
= 0; i
< PERMISSIONS
; i
++) {
357 if (check_perm
[i
].selected
)
358 and_mask
&= ~check_perm
[i
].mode
;
361 apply_mask (&sf_stat
);
365 if (current_panel
->marked
&& ch_dlg
->ret_value
!=B_CANCEL
) {
366 do_file_mark (current_panel
, c_file
, 0);
369 destroy_dlg (ch_dlg
);
370 } while (current_panel
->marked
&& !end_chmod
);