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 "lib/widget.h"
42 #include "lib/keybind.h" /* CK_DialogCancel */
44 #include "midnight.h" /* current_panel */
45 #include "layout.h" /* repaint_screen() */
48 /*** global variables ****************************************************************************/
50 /*** file scope macro definitions ****************************************************************/
64 #define PERMISSIONS 12
67 #define B_MARKED B_USER
68 #define B_ALL (B_USER+1)
69 #define B_SETMRK (B_USER+2)
70 #define B_CLRMRK (B_USER+3)
72 /*** file scope type declarations ****************************************************************/
74 /*** file scope variables ************************************************************************/
76 static int single_set
;
78 static gboolean mode_change
, need_update
, end_chmod
;
81 static mode_t and_mask
, or_mask
, c_stat
;
83 /* FIXME: these variables are superfluous, aren't they? (hint: str_trunc
84 * returns a pointer to a static buffer, and label_new creates its own copy
86 * --rillig, 2004-08-29 */
87 static const char *c_fname
, *c_fown
, *c_fgrp
;
97 } check_perm
[PERMISSIONS
] =
100 { S_IXOTH
, N_("execute/search by others"), FALSE
, NULL
},
101 { S_IWOTH
, N_("write by others"), FALSE
, NULL
},
102 { S_IROTH
, N_("read by others"), FALSE
, NULL
},
103 { S_IXGRP
, N_("execute/search by group"), FALSE
, NULL
},
104 { S_IWGRP
, N_("write by group"), FALSE
, NULL
},
105 { S_IRGRP
, N_("read by group"), FALSE
, NULL
},
106 { S_IXUSR
, N_("execute/search by owner"), FALSE
, NULL
},
107 { S_IWUSR
, N_("write by owner"), FALSE
, NULL
},
108 { S_IRUSR
, N_("read by owner"), FALSE
, NULL
},
109 { S_ISVTX
, N_("sticky bit"), FALSE
, NULL
},
110 { S_ISGID
, N_("set group ID on execution"), FALSE
, NULL
},
111 { S_ISUID
, N_("set user ID on execution"), FALSE
, NULL
}
117 int ret_cmd
, flags
, y
, x
;
119 } chmod_but
[BUTTONS
] =
122 { B_CANCEL
, NORMAL_BUTTON
, 2, 33, N_("&Cancel") },
123 { B_ENTER
, DEFPUSH_BUTTON
, 2, 17, N_("&Set") },
124 { B_CLRMRK
, NORMAL_BUTTON
, 0, 42, N_("C&lear marked") },
125 { B_SETMRK
, NORMAL_BUTTON
, 0, 27, N_("S&et marked") },
126 { B_MARKED
, NORMAL_BUTTON
, 0, 12, N_("&Marked all") },
127 { B_ALL
, NORMAL_BUTTON
, 0, 0, N_("Set &all") }
131 /*** file scope functions ************************************************************************/
132 /* --------------------------------------------------------------------------------------------- */
135 chmod_toggle_select (Dlg_head
* h
, int Id
)
137 tty_setcolor (COLOR_NORMAL
);
138 check_perm
[Id
].selected
= !check_perm
[Id
].selected
;
140 dlg_move (h
, PY
+ PERMISSIONS
- Id
, PX
+ 1);
141 tty_print_char ((check_perm
[Id
].selected
) ? '*' : ' ');
142 dlg_move (h
, PY
+ PERMISSIONS
- Id
, PX
+ 3);
145 /* --------------------------------------------------------------------------------------------- */
148 chmod_refresh (Dlg_head
* h
)
150 common_dialog_repaint (h
);
152 tty_setcolor (COLOR_NORMAL
);
154 dlg_move (h
, FY
+ 1, FX
+ 2);
155 tty_print_string (_("Name"));
156 dlg_move (h
, FY
+ 3, FX
+ 2);
157 tty_print_string (_("Permissions (Octal)"));
158 dlg_move (h
, FY
+ 5, FX
+ 2);
159 tty_print_string (_("Owner name"));
160 dlg_move (h
, FY
+ 7, FX
+ 2);
161 tty_print_string (_("Group name"));
163 dlg_move (h
, TY
, TX
);
164 tty_print_string (_("Use SPACE to change"));
165 dlg_move (h
, TY
+ 1, TX
);
166 tty_print_string (_("an option, ARROW KEYS"));
167 dlg_move (h
, TY
+ 2, TX
);
168 tty_print_string (_("to move between options"));
169 dlg_move (h
, TY
+ 3, TX
);
170 tty_print_string (_("and T or INS to mark"));
173 /* --------------------------------------------------------------------------------------------- */
176 chmod_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
178 char buffer
[BUF_TINY
];
179 int id
= dlg_get_current_widget_id (h
) - BUTTONS
+ single_set
* 2 - 1;
184 /* close dialog due to SIGINT (ctrl-g) */
185 if (sender
== NULL
&& parm
== CK_DialogCancel
)
186 return MSG_NOT_HANDLED
;
188 /* handle checkboxes */
191 c_stat
^= check_perm
[id
].mode
;
192 g_snprintf (buffer
, sizeof (buffer
), "%o", (unsigned int) c_stat
);
193 label_set_text (statl
, buffer
);
194 chmod_toggle_select (h
, id
);
199 return MSG_NOT_HANDLED
;
202 if ((parm
== 'T' || parm
== 't' || parm
== KEY_IC
) && id
> 0)
204 chmod_toggle_select (h
, id
);
209 return MSG_NOT_HANDLED
;
216 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
220 /* --------------------------------------------------------------------------------------------- */
233 single_set
= (current_panel
->marked
< 2) ? 2 : 0;
236 create_dlg (TRUE
, 0, 0, 22 - single_set
, 70, dialog_colors
,
237 chmod_callback
, "[Chmod]", _("Chmod command"), DLG_CENTER
| DLG_REVERSE
);
239 for (i
= 0; i
< BUTTONS
; i
++)
241 if (i
== 2 && single_set
!= 0)
245 button_new (BY
+ chmod_but
[i
].y
- single_set
,
247 chmod_but
[i
].ret_cmd
,
248 chmod_but
[i
].flags
, _(chmod_but
[i
].text
), 0));
251 add_widget (ch_dlg
, groupbox_new (FY
, FX
, 10, 25, _("File")));
253 for (i
= 0; i
< PERMISSIONS
; i
++)
255 check_perm
[i
].check
= check_new (PY
+ (PERMISSIONS
- i
), PX
+ 2, 0, _(check_perm
[i
].text
));
256 add_widget (ch_dlg
, check_perm
[i
].check
);
259 add_widget (ch_dlg
, groupbox_new (PY
, PX
, PERMISSIONS
+ 2, 33, _("Permission")));
264 /* --------------------------------------------------------------------------------------------- */
270 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
274 /* --------------------------------------------------------------------------------------------- */
279 while (!current_panel
->dir
.list
[c_file
].f
.marked
)
282 return current_panel
->dir
.list
[c_file
].fname
;
285 /* --------------------------------------------------------------------------------------------- */
288 do_chmod (struct stat
*sf
)
290 sf
->st_mode
&= and_mask
;
291 sf
->st_mode
|= or_mask
;
292 if (mc_chmod (current_panel
->dir
.list
[c_file
].fname
, sf
->st_mode
) == -1)
293 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
294 current_panel
->dir
.list
[c_file
].fname
, unix_error_string (errno
));
296 do_file_mark (current_panel
, c_file
, 0);
299 /* --------------------------------------------------------------------------------------------- */
302 apply_mask (struct stat
*sf
)
313 fname
= next_file ();
314 if (mc_stat (fname
, sf
) != 0)
316 c_stat
= sf
->st_mode
;
320 while (current_panel
->marked
!= 0);
323 /* --------------------------------------------------------------------------------------------- */
324 /*** public functions ****************************************************************************/
325 /* --------------------------------------------------------------------------------------------- */
330 char buffer
[BUF_TINY
];
337 { /* do while any files remaining */
338 ch_dlg
= init_chmod ();
340 if (current_panel
->marked
!= 0)
341 fname
= next_file (); /* next marked file */
343 fname
= selection (current_panel
)->fname
; /* single file */
345 if (mc_stat (fname
, &sf_stat
) != 0)
346 { /* get status of file */
347 destroy_dlg (ch_dlg
);
351 c_stat
= sf_stat
.st_mode
;
352 mode_change
= FALSE
; /* clear changes flag */
354 /* set check buttons */
355 for (i
= 0; i
< PERMISSIONS
; i
++)
357 check_perm
[i
].check
->state
= (c_stat
& check_perm
[i
].mode
) != 0 ? 1 : 0;
358 check_perm
[i
].selected
= FALSE
;
362 c_fname
= str_trunc (fname
, 21);
363 add_widget (ch_dlg
, label_new (FY
+ 2, FX
+ 2, c_fname
));
364 c_fown
= str_trunc (get_owner (sf_stat
.st_uid
), 21);
365 add_widget (ch_dlg
, label_new (FY
+ 6, FX
+ 2, c_fown
));
366 c_fgrp
= str_trunc (get_group (sf_stat
.st_gid
), 21);
367 add_widget (ch_dlg
, label_new (FY
+ 8, FX
+ 2, c_fgrp
));
368 g_snprintf (buffer
, sizeof (buffer
), "%o", (unsigned int) c_stat
);
369 statl
= label_new (FY
+ 4, FX
+ 2, buffer
);
370 add_widget (ch_dlg
, statl
);
372 run_dlg (ch_dlg
); /* retrieve an action */
375 switch (ch_dlg
->ret_value
)
379 if (mc_chmod (fname
, c_stat
) == -1)
380 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
381 fname
, unix_error_string (errno
));
391 and_mask
= or_mask
= 0;
392 and_mask
= ~and_mask
;
394 for (i
= 0; i
< PERMISSIONS
; i
++)
396 if (check_perm
[i
].selected
|| ch_dlg
->ret_value
== B_ALL
)
398 if (check_perm
[i
].check
->state
& C_BOOL
)
399 or_mask
|= check_perm
[i
].mode
;
401 and_mask
&= ~check_perm
[i
].mode
;
405 apply_mask (&sf_stat
);
409 and_mask
= or_mask
= 0;
410 and_mask
= ~and_mask
;
412 for (i
= 0; i
< PERMISSIONS
; i
++)
414 if (check_perm
[i
].selected
)
415 or_mask
|= check_perm
[i
].mode
;
418 apply_mask (&sf_stat
);
422 and_mask
= or_mask
= 0;
423 and_mask
= ~and_mask
;
425 for (i
= 0; i
< PERMISSIONS
; i
++)
427 if (check_perm
[i
].selected
)
428 and_mask
&= ~check_perm
[i
].mode
;
431 apply_mask (&sf_stat
);
435 if (current_panel
->marked
!= 0 && ch_dlg
->ret_value
!= B_CANCEL
)
437 do_file_mark (current_panel
, c_file
, 0);
441 destroy_dlg (ch_dlg
);
443 while (current_panel
->marked
!= 0 && !end_chmod
);
448 /* --------------------------------------------------------------------------------------------- */