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
;
61 #define PERMISSIONS 12
64 #define B_MARKED B_USER
65 #define B_ALL B_USER+1
66 #define B_SETMRK B_USER+2
67 #define B_CLRMRK B_USER+3
69 static int mode_change
, need_update
;
70 static int c_file
, end_chmod
;
72 static umode_t and_mask
, or_mask
, c_stat
;
73 static char *c_fname
, *c_fown
, *c_fgrp
;
82 } check_perm
[PERMISSIONS
] =
84 { S_IXOTH
, N_("execute/search by others"), 0, 0, },
85 { S_IWOTH
, N_("write by others"), 0, 0, },
86 { S_IROTH
, N_("read by others"), 0, 0, },
87 { S_IXGRP
, N_("execute/search by group"), 0, 0, },
88 { S_IWGRP
, N_("write by group"), 0, 0, },
89 { S_IRGRP
, N_("read by group"), 0, 0, },
90 { S_IXUSR
, N_("execute/search by owner"), 0, 0, },
91 { S_IWUSR
, N_("write by owner"), 0, 0, },
92 { S_IRUSR
, N_("read by owner"), 0, 0, },
93 { S_ISVTX
, N_("sticky bit"), 0, 0, },
94 { S_ISGID
, N_("set group ID on execution"), 0, 0, },
95 { S_ISUID
, N_("set user ID on execution"), 0, 0, },
99 int ret_cmd
, flags
, y
, x
;
101 } chmod_but
[BUTTONS
] =
103 { B_CANCEL
, NORMAL_BUTTON
, 2, 33, N_("&Cancel") },
104 { B_ENTER
, DEFPUSH_BUTTON
, 2, 17, N_("&Set") },
105 { B_CLRMRK
, NORMAL_BUTTON
, 0, 42, N_("C&lear marked") },
106 { B_SETMRK
, NORMAL_BUTTON
, 0, 27, N_("S&et marked") },
107 { B_MARKED
, NORMAL_BUTTON
, 0, 12, N_("&Marked all") },
108 { B_ALL
, NORMAL_BUTTON
, 0, 0, N_("Set &all") },
111 static void chmod_toggle_select (Dlg_head
*h
)
113 int Id
= h
->current
->dlg_id
- BUTTONS
+ single_set
* 2;
115 attrset (COLOR_NORMAL
);
116 check_perm
[Id
].selected
^= 1;
118 dlg_move (h
, PY
+ PERMISSIONS
- Id
, PX
+ 1);
119 addch ((check_perm
[Id
].selected
) ? '*' : ' ');
120 dlg_move (h
, PY
+ PERMISSIONS
- Id
, PX
+ 3);
123 static void chmod_refresh (Dlg_head
*h
)
125 common_dialog_repaint (h
);
127 attrset (COLOR_NORMAL
);
129 draw_box (h
, PY
, PX
, PERMISSIONS
+ 2, 33);
130 draw_box (h
, FY
, FX
, 10, 25);
132 dlg_move (h
, FY
+ 1, FX
+ 2);
134 dlg_move (h
, FY
+ 3, FX
+ 2);
135 addstr (_("Permissions (Octal)"));
136 dlg_move (h
, FY
+ 5, FX
+ 2);
137 addstr (_("Owner name"));
138 dlg_move (h
, FY
+ 7, FX
+ 2);
139 addstr (_("Group name"));
141 dlg_move (h
, TY
, TX
);
142 addstr (_("Use SPACE to change"));
143 dlg_move (h
, TY
+ 1, TX
);
144 addstr (_("an option, ARROW KEYS"));
145 dlg_move (h
, TY
+ 2, TX
);
146 addstr (_("to move between options"));
147 dlg_move (h
, TY
+ 3, TX
);
148 addstr (_("and T or INS to mark"));
150 attrset (COLOR_HOT_NORMAL
);
152 dlg_move (h
, PY
, PX
+ 1);
153 addstr (_(" Permission "));
154 dlg_move (h
, FY
, FX
+ 1);
155 addstr (_(" File "));
158 static int chmod_callback (Dlg_head
*h
, int Par
, int Msg
)
160 char buffer
[BUF_TINY
];
164 if (Par
>= BUTTONS
- single_set
* 2){
165 c_stat
^= check_perm
[Par
- BUTTONS
+ single_set
* 2].mode
;
166 g_snprintf (buffer
, sizeof (buffer
), "%o", c_stat
);
167 label_set_text (statl
, buffer
);
168 chmod_toggle_select (h
);
174 if ((Par
== 'T' || Par
== 't' || Par
== KEY_IC
) &&
175 h
->current
->dlg_id
>= BUTTONS
- single_set
* 2) {
176 chmod_toggle_select (h
);
197 end_chmod
= c_file
= need_update
= 0;
198 single_set
= (cpanel
->marked
< 2) ? 2 : 0;
201 create_dlg (0, 0, 22 - single_set
, 70, dialog_colors
,
202 chmod_callback
, "[Chmod]", _("Chmod command"),
205 for (i
= 0; i
< BUTTONS
; i
++) {
206 if (i
== 2 && single_set
)
210 button_new (BY
+ chmod_but
[i
].y
- single_set
,
212 chmod_but
[i
].ret_cmd
,
214 _(chmod_but
[i
].text
), 0, 0, NULL
));
217 for (i
= 0; i
< PERMISSIONS
; i
++) {
218 check_perm
[i
].check
=
219 check_new (PY
+ (PERMISSIONS
- i
), PX
+ 2, 0,
220 _(check_perm
[i
].text
), NULL
);
221 add_widget (ch_dlg
, check_perm
[i
].check
);
227 int stat_file (char *filename
, struct stat
*st
)
229 if (mc_stat (filename
, st
))
235 static void chmod_done (void)
238 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
242 static char *next_file (void)
244 while (!cpanel
->dir
.list
[c_file
].f
.marked
)
247 return cpanel
->dir
.list
[c_file
].fname
;
250 static void do_chmod (struct stat
*sf
)
252 sf
->st_mode
&= and_mask
;
253 sf
->st_mode
|= or_mask
;
254 if (mc_chmod (cpanel
->dir
.list
[c_file
].fname
, sf
->st_mode
) == -1)
255 message (1, MSG_ERROR
, _(" Couldn't chmod \"%s\" \n %s "),
256 cpanel
->dir
.list
[c_file
].fname
, unix_error_string (errno
));
258 do_file_mark (cpanel
, c_file
, 0);
261 static void apply_mask (struct stat
*sf
)
265 need_update
= end_chmod
= 1;
269 fname
= next_file ();
270 if (!stat_file (fname
, sf
))
272 c_stat
= sf
->st_mode
;
275 } while (cpanel
->marked
);
278 void chmod_cmd (void)
280 char buffer
[BUF_TINY
];
286 do { /* do while any files remaining */
287 ch_dlg
= init_chmod ();
289 fname
= next_file (); /* next marked file */
291 fname
= selection (cpanel
)->fname
; /* single file */
293 if (!stat_file (fname
, &sf_stat
)){ /* get status of file */
294 destroy_dlg (ch_dlg
);
298 c_stat
= sf_stat
.st_mode
;
299 mode_change
= 0; /* clear changes flag */
301 /* set check buttons */
302 for (i
= 0; i
< PERMISSIONS
; i
++){
303 check_perm
[i
].check
->state
= (c_stat
& check_perm
[i
].mode
) ? 1 : 0;
304 check_perm
[i
].selected
= 0;
308 c_fname
= name_trunc (fname
, 21);
309 add_widget (ch_dlg
, label_new (FY
+2, FX
+2, c_fname
, NULL
));
310 c_fown
= name_trunc (get_owner (sf_stat
.st_uid
), 21);
311 add_widget (ch_dlg
, label_new (FY
+6, FX
+2, c_fown
, NULL
));
312 c_fgrp
= name_trunc (get_group (sf_stat
.st_gid
), 21);
313 add_widget (ch_dlg
, label_new (FY
+8, FX
+2, c_fgrp
, NULL
));
314 g_snprintf (buffer
, sizeof (buffer
), "%o", c_stat
);
315 statl
= label_new (FY
+4, FX
+2, buffer
, NULL
);
316 add_widget (ch_dlg
, statl
);
318 run_dlg (ch_dlg
); /* retrieve an action */
321 switch (ch_dlg
->ret_value
){
324 if (mc_chmod (fname
, c_stat
) == -1)
325 message (1, MSG_ERROR
, _(" Couldn't chmod \"%s\" \n %s "),
326 fname
, unix_error_string (errno
));
336 and_mask
= or_mask
= 0;
337 and_mask
= ~and_mask
;
339 for (i
= 0; i
< PERMISSIONS
; i
++) {
340 if (check_perm
[i
].selected
|| ch_dlg
->ret_value
== B_ALL
) {
341 if (check_perm
[i
].check
->state
& C_BOOL
)
342 or_mask
|= check_perm
[i
].mode
;
344 and_mask
&= ~check_perm
[i
].mode
;
348 apply_mask (&sf_stat
);
352 and_mask
= or_mask
= 0;
353 and_mask
= ~and_mask
;
355 for (i
= 0; i
< PERMISSIONS
; i
++) {
356 if (check_perm
[i
].selected
)
357 or_mask
|= check_perm
[i
].mode
;
360 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 and_mask
&= ~check_perm
[i
].mode
;
371 apply_mask (&sf_stat
);
375 if (cpanel
->marked
&& ch_dlg
->ret_value
!=B_CANCEL
) {
376 do_file_mark (cpanel
, c_file
, 0);
379 destroy_dlg (ch_dlg
);
380 } while (cpanel
->marked
&& !end_chmod
);