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 draw_box (h
, PY
, PX
, PERMISSIONS
+ 2, 33, TRUE
);
143 draw_box (h
, FY
, FX
, 10, 25, TRUE
);
145 dlg_move (h
, FY
+ 1, FX
+ 2);
146 tty_print_string (_("Name"));
147 dlg_move (h
, FY
+ 3, FX
+ 2);
148 tty_print_string (_("Permissions (Octal)"));
149 dlg_move (h
, FY
+ 5, FX
+ 2);
150 tty_print_string (_("Owner name"));
151 dlg_move (h
, FY
+ 7, FX
+ 2);
152 tty_print_string (_("Group name"));
154 dlg_move (h
, TY
, TX
);
155 tty_print_string (_("Use SPACE to change"));
156 dlg_move (h
, TY
+ 1, TX
);
157 tty_print_string (_("an option, ARROW KEYS"));
158 dlg_move (h
, TY
+ 2, TX
);
159 tty_print_string (_("to move between options"));
160 dlg_move (h
, TY
+ 3, TX
);
161 tty_print_string (_("and T or INS to mark"));
163 tty_setcolor (COLOR_HOT_NORMAL
);
165 dlg_move (h
, PY
, PX
+ 1);
166 tty_print_string (_(" Permission "));
167 dlg_move (h
, FY
, FX
+ 1);
168 tty_print_string (_(" File "));
172 chmod_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
174 char buffer
[BUF_TINY
];
175 int id
= h
->current
->dlg_id
- BUTTONS
+ single_set
* 2;
182 c_stat
^= check_perm
[id
].mode
;
183 g_snprintf (buffer
, sizeof (buffer
), "%o", c_stat
);
184 label_set_text (statl
, buffer
);
185 chmod_toggle_select (h
, id
);
191 if ((parm
== 'T' || parm
== 't' || parm
== KEY_IC
) && id
> 0)
193 chmod_toggle_select (h
, id
);
198 return MSG_NOT_HANDLED
;
205 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
216 end_chmod
= c_file
= need_update
= 0;
217 single_set
= (current_panel
->marked
< 2) ? 2 : 0;
220 create_dlg (0, 0, 22 - single_set
, 70, dialog_colors
,
221 chmod_callback
, "[Chmod]", _("Chmod command"), DLG_CENTER
| DLG_REVERSE
);
223 for (i
= 0; i
< BUTTONS
; i
++)
225 if (i
== 2 && single_set
)
229 button_new (BY
+ chmod_but
[i
].y
- single_set
,
231 chmod_but
[i
].ret_cmd
,
232 chmod_but
[i
].flags
, _(chmod_but
[i
].text
), 0));
235 for (i
= 0; i
< PERMISSIONS
; i
++)
237 check_perm
[i
].check
= check_new (PY
+ (PERMISSIONS
- i
), PX
+ 2, 0, _(check_perm
[i
].text
));
238 add_widget (ch_dlg
, check_perm
[i
].check
);
248 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
255 while (!current_panel
->dir
.list
[c_file
].f
.marked
)
258 return current_panel
->dir
.list
[c_file
].fname
;
262 do_chmod (struct stat
*sf
)
264 sf
->st_mode
&= and_mask
;
265 sf
->st_mode
|= or_mask
;
266 if (mc_chmod (current_panel
->dir
.list
[c_file
].fname
, sf
->st_mode
) == -1)
267 message (D_ERROR
, MSG_ERROR
, _(" Cannot chmod \"%s\" \n %s "),
268 current_panel
->dir
.list
[c_file
].fname
, unix_error_string (errno
));
270 do_file_mark (current_panel
, c_file
, 0);
274 apply_mask (struct stat
*sf
)
278 need_update
= end_chmod
= 1;
283 fname
= next_file ();
284 if (mc_stat (fname
, sf
) != 0)
286 c_stat
= sf
->st_mode
;
290 while (current_panel
->marked
);
296 char buffer
[BUF_TINY
];
303 { /* do while any files remaining */
304 ch_dlg
= init_chmod ();
305 if (current_panel
->marked
)
306 fname
= next_file (); /* next marked file */
308 fname
= selection (current_panel
)->fname
; /* single file */
310 if (mc_stat (fname
, &sf_stat
) != 0)
311 { /* get status of file */
312 destroy_dlg (ch_dlg
);
316 c_stat
= sf_stat
.st_mode
;
317 mode_change
= 0; /* clear changes flag */
319 /* set check buttons */
320 for (i
= 0; i
< PERMISSIONS
; i
++)
322 check_perm
[i
].check
->state
= (c_stat
& check_perm
[i
].mode
) ? 1 : 0;
323 check_perm
[i
].selected
= 0;
327 c_fname
= str_trunc (fname
, 21);
328 add_widget (ch_dlg
, label_new (FY
+ 2, FX
+ 2, c_fname
));
329 c_fown
= str_trunc (get_owner (sf_stat
.st_uid
), 21);
330 add_widget (ch_dlg
, label_new (FY
+ 6, FX
+ 2, c_fown
));
331 c_fgrp
= str_trunc (get_group (sf_stat
.st_gid
), 21);
332 add_widget (ch_dlg
, label_new (FY
+ 8, FX
+ 2, c_fgrp
));
333 g_snprintf (buffer
, sizeof (buffer
), "%o", c_stat
);
334 statl
= label_new (FY
+ 4, FX
+ 2, buffer
);
335 add_widget (ch_dlg
, statl
);
337 run_dlg (ch_dlg
); /* retrieve an action */
340 switch (ch_dlg
->ret_value
)
344 if (mc_chmod (fname
, c_stat
) == -1)
345 message (D_ERROR
, MSG_ERROR
, _(" Cannot chmod \"%s\" \n %s "),
346 fname
, unix_error_string (errno
));
356 and_mask
= or_mask
= 0;
357 and_mask
= ~and_mask
;
359 for (i
= 0; i
< PERMISSIONS
; i
++)
361 if (check_perm
[i
].selected
|| ch_dlg
->ret_value
== B_ALL
)
363 if (check_perm
[i
].check
->state
& C_BOOL
)
364 or_mask
|= check_perm
[i
].mode
;
366 and_mask
&= ~check_perm
[i
].mode
;
370 apply_mask (&sf_stat
);
374 and_mask
= or_mask
= 0;
375 and_mask
= ~and_mask
;
377 for (i
= 0; i
< PERMISSIONS
; i
++)
379 if (check_perm
[i
].selected
)
380 or_mask
|= check_perm
[i
].mode
;
383 apply_mask (&sf_stat
);
386 and_mask
= or_mask
= 0;
387 and_mask
= ~and_mask
;
389 for (i
= 0; i
< PERMISSIONS
; i
++)
391 if (check_perm
[i
].selected
)
392 and_mask
&= ~check_perm
[i
].mode
;
395 apply_mask (&sf_stat
);
399 if (current_panel
->marked
&& ch_dlg
->ret_value
!= B_CANCEL
)
401 do_file_mark (current_panel
, c_file
, 0);
404 destroy_dlg (ch_dlg
);
406 while (current_panel
->marked
&& !end_chmod
);