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>
36 #include "dialog.h" /* For do_refresh() */
39 #include "panel.h" /* Needed for the externs */
45 #include "../vfs/vfs.h"
47 static int single_set
;
48 static struct Dlg_head
*ch_dlg
;
62 #define PERMISSIONS 12
65 #define B_MARKED B_USER
66 #define B_ALL B_USER+1
67 #define B_SETMRK B_USER+2
68 #define B_CLRMRK B_USER+3
70 static int mode_change
, need_update
;
71 static int c_file
, end_chmod
;
73 static umode_t and_mask
, or_mask
, c_stat
;
74 static char *c_fname
, *c_fown
, *c_fgrp
;
77 static int normal_color
;
78 static int title_color
;
79 static int selection_color
;
86 } check_perm
[PERMISSIONS
] =
88 { S_IXOTH
, N_("execute/search by others"), 0, 0, },
89 { S_IWOTH
, N_("write by others"), 0, 0, },
90 { S_IROTH
, N_("read by others"), 0, 0, },
91 { S_IXGRP
, N_("execute/search by group"), 0, 0, },
92 { S_IWGRP
, N_("write by group"), 0, 0, },
93 { S_IRGRP
, N_("read by group"), 0, 0, },
94 { S_IXUSR
, N_("execute/search by owner"), 0, 0, },
95 { S_IWUSR
, N_("write by owner"), 0, 0, },
96 { S_IRUSR
, N_("read by owner"), 0, 0, },
97 { S_ISVTX
, N_("sticky bit"), 0, 0, },
98 { S_ISGID
, N_("set group ID on execution"), 0, 0, },
99 { S_ISUID
, N_("set user ID on execution"), 0, 0, },
103 int ret_cmd
, flags
, y
, x
;
105 } chmod_but
[BUTTONS
] =
107 { B_CANCEL
, NORMAL_BUTTON
, 2, 33, N_("&Cancel") },
108 { B_ENTER
, DEFPUSH_BUTTON
, 2, 17, N_("&Set") },
109 { B_CLRMRK
, NORMAL_BUTTON
, 0, 42, N_("C&lear marked") },
110 { B_SETMRK
, NORMAL_BUTTON
, 0, 27, N_("S&et marked") },
111 { B_MARKED
, NORMAL_BUTTON
, 0, 12, N_("&Marked all") },
112 { B_ALL
, NORMAL_BUTTON
, 0, 0, N_("Set &all") },
115 static void chmod_toggle_select (void)
117 int Id
= ch_dlg
->current
->dlg_id
- BUTTONS
+ single_set
* 2;
119 attrset (normal_color
);
120 check_perm
[Id
].selected
^= 1;
122 dlg_move (ch_dlg
, PY
+ PERMISSIONS
- Id
, PX
+ 1);
123 addch ((check_perm
[Id
].selected
) ? '*' : ' ');
124 dlg_move (ch_dlg
, PY
+ PERMISSIONS
- Id
, PX
+ 3);
127 static void chmod_refresh (void)
129 attrset (COLOR_NORMAL
);
132 draw_box (ch_dlg
, 1, 2, 20 - single_set
, 66);
133 draw_box (ch_dlg
, PY
, PX
, PERMISSIONS
+ 2, 33);
134 draw_box (ch_dlg
, FY
, FX
, 10, 25);
136 dlg_move (ch_dlg
, FY
+ 1, FX
+ 2);
138 dlg_move (ch_dlg
, FY
+ 3, FX
+ 2);
139 addstr (_("Permissions (Octal)"));
140 dlg_move (ch_dlg
, FY
+ 5, FX
+ 2);
141 addstr (_("Owner name"));
142 dlg_move (ch_dlg
, FY
+ 7, FX
+ 2);
143 addstr (_("Group name"));
145 attrset (title_color
);
146 dlg_move (ch_dlg
, 1, 28);
147 addstr (_(" Chmod command "));
148 dlg_move (ch_dlg
, PY
, PX
+ 1);
149 addstr (_(" Permission "));
150 dlg_move (ch_dlg
, FY
, FX
+ 1);
151 addstr (_(" File "));
153 attrset (selection_color
);
155 dlg_move (ch_dlg
, TY
, TX
);
156 addstr (_("Use SPACE to change"));
157 dlg_move (ch_dlg
, TY
+ 1, TX
);
158 addstr (_("an option, ARROW KEYS"));
159 dlg_move (ch_dlg
, TY
+ 2, TX
);
160 addstr (_("to move between options"));
161 dlg_move (ch_dlg
, TY
+ 3, TX
);
162 addstr (_("and T or INS to mark"));
165 static int chmod_callback (Dlg_head
*h
, int Par
, int Msg
)
167 char buffer
[BUF_TINY
];
171 if (Par
>= BUTTONS
- single_set
* 2){
172 c_stat
^= check_perm
[Par
- BUTTONS
+ single_set
* 2].mode
;
173 g_snprintf (buffer
, sizeof (buffer
), "%o", c_stat
);
174 label_set_text (statl
, buffer
);
175 chmod_toggle_select ();
181 if ((Par
== 'T' || Par
== 't' || Par
== KEY_IC
) &&
182 ch_dlg
->current
->dlg_id
>= BUTTONS
- single_set
* 2) {
183 chmod_toggle_select ();
185 dlg_one_down (ch_dlg
);
197 static void init_chmod (void)
202 end_chmod
= c_file
= need_update
= 0;
203 single_set
= (cpanel
->marked
< 2) ? 2 : 0;
206 normal_color
= COLOR_NORMAL
;
207 title_color
= COLOR_HOT_NORMAL
;
208 selection_color
= COLOR_NORMAL
;
210 normal_color
= NORMAL_COLOR
;
211 title_color
= SELECTED_COLOR
;
212 selection_color
= SELECTED_COLOR
;
215 ch_dlg
= create_dlg (0, 0, 22 - single_set
, 70, dialog_colors
,
216 chmod_callback
, "[Chmod]", "chmod", DLG_CENTER
);
218 x_set_dialog_title (ch_dlg
, _("Chmod command"));
220 #define XTRACT(i) BY+chmod_but[i].y-single_set, BX+chmod_but[i].x, \
221 chmod_but[i].ret_cmd, chmod_but[i].flags, _(chmod_but[i].text), 0, 0, NULL
223 for (i
= 0; i
< BUTTONS
; i
++) {
224 if (i
== 2 && single_set
)
227 add_widget (ch_dlg
, button_new (XTRACT (i
)));
231 #define XTRACT2(i) 0, _(check_perm [i].text), NULL
232 for (i
= 0; i
< PERMISSIONS
; i
++) {
233 check_perm
[i
].check
= check_new (PY
+ (PERMISSIONS
- i
), PX
+ 2,
235 add_widget (ch_dlg
, check_perm
[i
].check
);
239 int stat_file (char *filename
, struct stat
*st
)
241 if (mc_stat (filename
, st
))
247 static void chmod_done (void)
250 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
254 static char *next_file (void)
256 while (!cpanel
->dir
.list
[c_file
].f
.marked
)
259 return cpanel
->dir
.list
[c_file
].fname
;
262 static void do_chmod (struct stat
*sf
)
264 sf
->st_mode
&= and_mask
;
265 sf
->st_mode
|= or_mask
;
266 if (mc_chmod (cpanel
->dir
.list
[c_file
].fname
, sf
->st_mode
) == -1)
267 message (1, MSG_ERROR
, _(" Couldn't chmod \"%s\" \n %s "),
268 cpanel
->dir
.list
[c_file
].fname
, unix_error_string (errno
));
270 do_file_mark (cpanel
, c_file
, 0);
273 static void apply_mask (struct stat
*sf
)
277 need_update
= end_chmod
= 1;
281 fname
= next_file ();
282 if (!stat_file (fname
, sf
))
284 c_stat
= sf
->st_mode
;
287 } while (cpanel
->marked
);
290 void chmod_cmd (void)
292 char buffer
[BUF_TINY
];
297 do { /* do while any files remaining */
300 fname
= next_file (); /* next marked file */
302 fname
= selection (cpanel
)->fname
; /* single file */
304 if (!stat_file (fname
, &sf_stat
)){ /* get status of file */
305 destroy_dlg (ch_dlg
);
309 c_stat
= sf_stat
.st_mode
;
310 mode_change
= 0; /* clear changes flag */
312 /* set check buttons */
313 for (i
= 0; i
< PERMISSIONS
; i
++){
314 check_perm
[i
].check
->state
= (c_stat
& check_perm
[i
].mode
) ? 1 : 0;
315 check_perm
[i
].selected
= 0;
319 c_fname
= name_trunc (fname
, 21);
320 add_widget (ch_dlg
, label_new (FY
+2, FX
+2, c_fname
, NULL
));
321 c_fown
= name_trunc (get_owner (sf_stat
.st_uid
), 21);
322 add_widget (ch_dlg
, label_new (FY
+6, FX
+2, c_fown
, NULL
));
323 c_fgrp
= name_trunc (get_group (sf_stat
.st_gid
), 21);
324 add_widget (ch_dlg
, label_new (FY
+8, FX
+2, c_fgrp
, NULL
));
325 g_snprintf (buffer
, sizeof (buffer
), "%o", c_stat
);
326 statl
= label_new (FY
+4, FX
+2, buffer
, NULL
);
327 add_widget (ch_dlg
, statl
);
329 run_dlg (ch_dlg
); /* retrieve an action */
332 switch (ch_dlg
->ret_value
){
335 if (mc_chmod (fname
, c_stat
) == -1)
336 message (1, MSG_ERROR
, _(" Couldn't chmod \"%s\" \n %s "),
337 fname
, unix_error_string (errno
));
347 and_mask
= or_mask
= 0;
348 and_mask
= ~and_mask
;
350 for (i
= 0; i
< PERMISSIONS
; i
++) {
351 if (check_perm
[i
].selected
|| ch_dlg
->ret_value
== B_ALL
) {
352 if (check_perm
[i
].check
->state
& C_BOOL
)
353 or_mask
|= check_perm
[i
].mode
;
355 and_mask
&= ~check_perm
[i
].mode
;
359 apply_mask (&sf_stat
);
363 and_mask
= or_mask
= 0;
364 and_mask
= ~and_mask
;
366 for (i
= 0; i
< PERMISSIONS
; i
++) {
367 if (check_perm
[i
].selected
)
368 or_mask
|= check_perm
[i
].mode
;
371 apply_mask (&sf_stat
);
374 and_mask
= or_mask
= 0;
375 and_mask
= ~and_mask
;
377 for (i
= 0; i
< PERMISSIONS
; i
++) {
378 if (check_perm
[i
].selected
)
379 and_mask
&= ~check_perm
[i
].mode
;
382 apply_mask (&sf_stat
);
386 if (cpanel
->marked
&& ch_dlg
->ret_value
!=B_CANCEL
) {
387 do_file_mark (cpanel
, c_file
, 0);
390 destroy_dlg (ch_dlg
);
391 } while (cpanel
->marked
&& !end_chmod
);
395 void ch1_cmd (int id
)
398 chown_advanced_cmd ();
403 void ch2_cmd (int id
)
406 chown_advanced_cmd ();