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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <errno.h> /* For errno on SunOS systems */
23 /* Needed for the extern declarations of integer parameters */
24 #include <sys/types.h>
31 #include "tty.h" /* A_REVERSE */
32 #include "color.h" /* dialog_colors */
33 #include "dialog.h" /* add_widget() */
34 #include "widget.h" /* NORMAL_BUTTON */
35 #include "wtools.h" /* message() */
36 #include "panel.h" /* do_file_mark() */
37 #include "main.h" /* update_panels() */
40 static int single_set
;
54 #define PERMISSIONS 12
57 #define B_MARKED B_USER
58 #define B_ALL (B_USER+1)
59 #define B_SETMRK (B_USER+2)
60 #define B_CLRMRK (B_USER+3)
62 static int mode_change
, need_update
;
63 static int c_file
, end_chmod
;
65 static umode_t and_mask
, or_mask
, c_stat
;
67 /* FIXME: these variables are superfluous, aren't they? (hint: name_trunc
68 * returns a pointer to a static buffer, and label_new creates its own copy
70 * --rillig, 2004-08-29 */
71 static const char *c_fname
, *c_fown
, *c_fgrp
;
80 } check_perm
[PERMISSIONS
] =
82 { S_IXOTH
, N_("execute/search by others"), 0, 0, },
83 { S_IWOTH
, N_("write by others"), 0, 0, },
84 { S_IROTH
, N_("read by others"), 0, 0, },
85 { S_IXGRP
, N_("execute/search by group"), 0, 0, },
86 { S_IWGRP
, N_("write by group"), 0, 0, },
87 { S_IRGRP
, N_("read by group"), 0, 0, },
88 { S_IXUSR
, N_("execute/search by owner"), 0, 0, },
89 { S_IWUSR
, N_("write by owner"), 0, 0, },
90 { S_IRUSR
, N_("read by owner"), 0, 0, },
91 { S_ISVTX
, N_("sticky bit"), 0, 0, },
92 { S_ISGID
, N_("set group ID on execution"), 0, 0, },
93 { S_ISUID
, N_("set user ID on execution"), 0, 0, },
97 int ret_cmd
, flags
, y
, x
;
99 } chmod_but
[BUTTONS
] =
101 { B_CANCEL
, NORMAL_BUTTON
, 2, 33, N_("&Cancel") },
102 { B_ENTER
, DEFPUSH_BUTTON
, 2, 17, N_("&Set") },
103 { B_CLRMRK
, NORMAL_BUTTON
, 0, 42, N_("C&lear marked") },
104 { B_SETMRK
, NORMAL_BUTTON
, 0, 27, N_("S&et marked") },
105 { B_MARKED
, NORMAL_BUTTON
, 0, 12, N_("&Marked all") },
106 { B_ALL
, NORMAL_BUTTON
, 0, 0, N_("Set &all") },
109 static void chmod_toggle_select (Dlg_head
*h
, int Id
)
111 attrset (COLOR_NORMAL
);
112 check_perm
[Id
].selected
^= 1;
114 dlg_move (h
, PY
+ PERMISSIONS
- Id
, PX
+ 1);
115 addch ((check_perm
[Id
].selected
) ? '*' : ' ');
116 dlg_move (h
, PY
+ PERMISSIONS
- Id
, PX
+ 3);
119 static void chmod_refresh (Dlg_head
*h
)
121 common_dialog_repaint (h
);
123 attrset (COLOR_NORMAL
);
125 draw_box (h
, PY
, PX
, PERMISSIONS
+ 2, 33);
126 draw_box (h
, FY
, FX
, 10, 25);
128 dlg_move (h
, FY
+ 1, FX
+ 2);
130 dlg_move (h
, FY
+ 3, FX
+ 2);
131 addstr (_("Permissions (Octal)"));
132 dlg_move (h
, FY
+ 5, FX
+ 2);
133 addstr (_("Owner name"));
134 dlg_move (h
, FY
+ 7, FX
+ 2);
135 addstr (_("Group name"));
137 dlg_move (h
, TY
, TX
);
138 addstr (_("Use SPACE to change"));
139 dlg_move (h
, TY
+ 1, TX
);
140 addstr (_("an option, ARROW KEYS"));
141 dlg_move (h
, TY
+ 2, TX
);
142 addstr (_("to move between options"));
143 dlg_move (h
, TY
+ 3, TX
);
144 addstr (_("and T or INS to mark"));
146 attrset (COLOR_HOT_NORMAL
);
148 dlg_move (h
, PY
, PX
+ 1);
149 addstr (_(" Permission "));
150 dlg_move (h
, FY
, FX
+ 1);
151 addstr (_(" File "));
155 chmod_callback (Dlg_head
*h
, dlg_msg_t msg
, int parm
)
157 char buffer
[BUF_TINY
];
158 int id
= h
->current
->dlg_id
- BUTTONS
+ single_set
* 2;
163 c_stat
^= check_perm
[id
].mode
;
164 g_snprintf (buffer
, sizeof (buffer
), "%o", c_stat
);
165 label_set_text (statl
, buffer
);
166 chmod_toggle_select (h
, id
);
172 if ((parm
== 'T' || parm
== 't' || parm
== KEY_IC
) && id
> 0) {
173 chmod_toggle_select (h
, id
);
178 return MSG_NOT_HANDLED
;
185 return default_dlg_callback (h
, msg
, parm
);
196 end_chmod
= c_file
= need_update
= 0;
197 single_set
= (current_panel
->marked
< 2) ? 2 : 0;
200 create_dlg (0, 0, 22 - single_set
, 70, dialog_colors
,
201 chmod_callback
, "[Chmod]", _("Chmod command"),
202 DLG_CENTER
| DLG_REVERSE
);
204 for (i
= 0; i
< BUTTONS
; i
++) {
205 if (i
== 2 && single_set
)
209 button_new (BY
+ chmod_but
[i
].y
- single_set
,
211 chmod_but
[i
].ret_cmd
,
213 _(chmod_but
[i
].text
), 0));
216 for (i
= 0; i
< PERMISSIONS
; i
++) {
217 check_perm
[i
].check
=
218 check_new (PY
+ (PERMISSIONS
- i
), PX
+ 2, 0,
219 _(check_perm
[i
].text
));
220 add_widget (ch_dlg
, check_perm
[i
].check
);
226 static void chmod_done (void)
229 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
233 static char *next_file (void)
235 while (!current_panel
->dir
.list
[c_file
].f
.marked
)
238 return current_panel
->dir
.list
[c_file
].fname
;
241 static void do_chmod (struct stat
*sf
)
243 sf
->st_mode
&= and_mask
;
244 sf
->st_mode
|= or_mask
;
245 if (mc_chmod (current_panel
->dir
.list
[c_file
].fname
, sf
->st_mode
) == -1)
246 message (1, MSG_ERROR
, _(" Cannot chmod \"%s\" \n %s "),
247 current_panel
->dir
.list
[c_file
].fname
, unix_error_string (errno
));
249 do_file_mark (current_panel
, c_file
, 0);
252 static void apply_mask (struct stat
*sf
)
256 need_update
= end_chmod
= 1;
260 fname
= next_file ();
261 if (mc_stat (fname
, sf
) != 0)
263 c_stat
= sf
->st_mode
;
266 } while (current_panel
->marked
);
269 void chmod_cmd (void)
271 char buffer
[BUF_TINY
];
277 do { /* do while any files remaining */
278 ch_dlg
= init_chmod ();
279 if (current_panel
->marked
)
280 fname
= next_file (); /* next marked file */
282 fname
= selection (current_panel
)->fname
; /* single file */
284 if (mc_stat (fname
, &sf_stat
) != 0) { /* get status of file */
285 destroy_dlg (ch_dlg
);
289 c_stat
= sf_stat
.st_mode
;
290 mode_change
= 0; /* clear changes flag */
292 /* set check buttons */
293 for (i
= 0; i
< PERMISSIONS
; i
++){
294 check_perm
[i
].check
->state
= (c_stat
& check_perm
[i
].mode
) ? 1 : 0;
295 check_perm
[i
].selected
= 0;
299 c_fname
= name_trunc (fname
, 21);
300 add_widget (ch_dlg
, label_new (FY
+2, FX
+2, c_fname
));
301 c_fown
= name_trunc (get_owner (sf_stat
.st_uid
), 21);
302 add_widget (ch_dlg
, label_new (FY
+6, FX
+2, c_fown
));
303 c_fgrp
= name_trunc (get_group (sf_stat
.st_gid
), 21);
304 add_widget (ch_dlg
, label_new (FY
+8, FX
+2, c_fgrp
));
305 g_snprintf (buffer
, sizeof (buffer
), "%o", c_stat
);
306 statl
= label_new (FY
+4, FX
+2, buffer
);
307 add_widget (ch_dlg
, statl
);
309 run_dlg (ch_dlg
); /* retrieve an action */
312 switch (ch_dlg
->ret_value
){
315 if (mc_chmod (fname
, c_stat
) == -1)
316 message (1, MSG_ERROR
, _(" Cannot chmod \"%s\" \n %s "),
317 fname
, unix_error_string (errno
));
327 and_mask
= or_mask
= 0;
328 and_mask
= ~and_mask
;
330 for (i
= 0; i
< PERMISSIONS
; i
++) {
331 if (check_perm
[i
].selected
|| ch_dlg
->ret_value
== B_ALL
) {
332 if (check_perm
[i
].check
->state
& C_BOOL
)
333 or_mask
|= check_perm
[i
].mode
;
335 and_mask
&= ~check_perm
[i
].mode
;
339 apply_mask (&sf_stat
);
343 and_mask
= or_mask
= 0;
344 and_mask
= ~and_mask
;
346 for (i
= 0; i
< PERMISSIONS
; i
++) {
347 if (check_perm
[i
].selected
)
348 or_mask
|= check_perm
[i
].mode
;
351 apply_mask (&sf_stat
);
354 and_mask
= or_mask
= 0;
355 and_mask
= ~and_mask
;
357 for (i
= 0; i
< PERMISSIONS
; i
++) {
358 if (check_perm
[i
].selected
)
359 and_mask
&= ~check_perm
[i
].mode
;
362 apply_mask (&sf_stat
);
366 if (current_panel
->marked
&& ch_dlg
->ret_value
!=B_CANCEL
) {
367 do_file_mark (current_panel
, c_file
, 0);
370 destroy_dlg (ch_dlg
);
371 } while (current_panel
->marked
&& !end_chmod
);