1 /* Chmod command -- for the Midnight Commander
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007,
3 2008, 2009, 2010, 2011 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
27 #include <sys/types.h>
31 #include "lib/global.h"
33 #include "lib/tty/tty.h"
35 #include "lib/vfs/mc-vfs/vfs.h"
36 #include "lib/strutil.h"
38 #include "lib/widget.h"
39 #include "lib/keybind.h" /* CK_DialogCancel */
41 #include "midnight.h" /* current_panel */
42 #include "layout.h" /* repaint_screen() */
45 /*** global variables ****************************************************************************/
47 /*** file scope macro definitions ****************************************************************/
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 /*** file scope type declarations ****************************************************************/
71 /*** file scope variables ************************************************************************/
73 static int single_set
;
75 static gboolean mode_change
, need_update
, end_chmod
;
78 static mode_t and_mask
, or_mask
, c_stat
;
88 } check_perm
[PERMISSIONS
] =
91 { S_IXOTH
, N_("execute/search by others"), FALSE
, NULL
},
92 { S_IWOTH
, N_("write by others"), FALSE
, NULL
},
93 { S_IROTH
, N_("read by others"), FALSE
, NULL
},
94 { S_IXGRP
, N_("execute/search by group"), FALSE
, NULL
},
95 { S_IWGRP
, N_("write by group"), FALSE
, NULL
},
96 { S_IRGRP
, N_("read by group"), FALSE
, NULL
},
97 { S_IXUSR
, N_("execute/search by owner"), FALSE
, NULL
},
98 { S_IWUSR
, N_("write by owner"), FALSE
, NULL
},
99 { S_IRUSR
, N_("read by owner"), FALSE
, NULL
},
100 { S_ISVTX
, N_("sticky bit"), FALSE
, NULL
},
101 { S_ISGID
, N_("set group ID on execution"), FALSE
, NULL
},
102 { S_ISUID
, N_("set user ID on execution"), FALSE
, NULL
}
113 } chmod_but
[BUTTONS
] =
116 { B_CANCEL
, NORMAL_BUTTON
, 2, 33, N_("&Cancel") },
117 { B_ENTER
, DEFPUSH_BUTTON
, 2, 17, N_("&Set") },
118 { B_CLRMRK
, NORMAL_BUTTON
, 0, 42, N_("C&lear marked") },
119 { B_SETMRK
, NORMAL_BUTTON
, 0, 27, N_("S&et marked") },
120 { B_MARKED
, NORMAL_BUTTON
, 0, 12, N_("&Marked all") },
121 { B_ALL
, NORMAL_BUTTON
, 0, 0, N_("Set &all") }
125 /*** file scope functions ************************************************************************/
126 /* --------------------------------------------------------------------------------------------- */
132 static gboolean i18n
= FALSE
;
140 for (i
= 0; i
< PERMISSIONS
; i
++)
141 check_perm
[i
].text
= _(check_perm
[i
].text
);
143 for (i
= 0; i
< BUTTONS
; i
++)
144 chmod_but
[i
].text
= _(chmod_but
[i
].text
);
145 #endif /* ENABLE_NLS */
148 /* --------------------------------------------------------------------------------------------- */
151 chmod_toggle_select (Dlg_head
* h
, int Id
)
153 tty_setcolor (COLOR_NORMAL
);
154 check_perm
[Id
].selected
= !check_perm
[Id
].selected
;
156 dlg_move (h
, PY
+ PERMISSIONS
- Id
, PX
+ 1);
157 tty_print_char (check_perm
[Id
].selected
? '*' : ' ');
158 dlg_move (h
, PY
+ PERMISSIONS
- Id
, PX
+ 3);
161 /* --------------------------------------------------------------------------------------------- */
164 chmod_refresh (Dlg_head
* h
)
166 common_dialog_repaint (h
);
168 tty_setcolor (COLOR_NORMAL
);
170 dlg_move (h
, FY
+ 1, FX
+ 2);
171 tty_print_string (_("Name"));
172 dlg_move (h
, FY
+ 3, FX
+ 2);
173 tty_print_string (_("Permissions (Octal)"));
174 dlg_move (h
, FY
+ 5, FX
+ 2);
175 tty_print_string (_("Owner name"));
176 dlg_move (h
, FY
+ 7, FX
+ 2);
177 tty_print_string (_("Group name"));
180 /* --------------------------------------------------------------------------------------------- */
183 chmod_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
185 char buffer
[BUF_TINY
];
188 id
= dlg_get_current_widget_id (h
) - BUTTONS
+ single_set
* 2 - 1;
193 /* close dialog due to SIGINT (ctrl-g) */
194 if (sender
== NULL
&& parm
== CK_DialogCancel
)
195 return MSG_NOT_HANDLED
;
197 /* handle checkboxes */
200 c_stat
^= check_perm
[id
].mode
;
201 g_snprintf (buffer
, sizeof (buffer
), "%o", (unsigned int) c_stat
);
202 label_set_text (statl
, buffer
);
203 chmod_toggle_select (h
, id
);
208 return MSG_NOT_HANDLED
;
211 if ((parm
== 'T' || parm
== 't' || parm
== KEY_IC
) && id
> 0)
213 chmod_toggle_select (h
, id
);
218 return MSG_NOT_HANDLED
;
225 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
229 /* --------------------------------------------------------------------------------------------- */
242 single_set
= (current_panel
->marked
< 2) ? 2 : 0;
245 create_dlg (TRUE
, 0, 0, 22 - single_set
, 70, dialog_colors
,
246 chmod_callback
, "[Chmod]", _("Chmod command"), DLG_CENTER
| DLG_REVERSE
);
248 for (i
= 0; i
< BUTTONS
; i
++)
250 if (i
== 2 && single_set
!= 0)
254 button_new (BY
+ chmod_but
[i
].y
- single_set
,
256 chmod_but
[i
].ret_cmd
,
257 chmod_but
[i
].flags
, chmod_but
[i
].text
, 0));
260 add_widget (ch_dlg
, groupbox_new (FY
, FX
, 10, 25, _("File")));
262 for (i
= 0; i
< PERMISSIONS
; i
++)
264 check_perm
[i
].check
= check_new (PY
+ (PERMISSIONS
- i
), PX
+ 2, 0, check_perm
[i
].text
);
265 add_widget (ch_dlg
, check_perm
[i
].check
);
268 add_widget (ch_dlg
, groupbox_new (PY
, PX
, PERMISSIONS
+ 2, 33, _("Permission")));
273 /* --------------------------------------------------------------------------------------------- */
279 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
283 /* --------------------------------------------------------------------------------------------- */
288 while (!current_panel
->dir
.list
[c_file
].f
.marked
)
291 return current_panel
->dir
.list
[c_file
].fname
;
294 /* --------------------------------------------------------------------------------------------- */
297 do_chmod (struct stat
*sf
)
299 sf
->st_mode
&= and_mask
;
300 sf
->st_mode
|= or_mask
;
302 if (mc_chmod (current_panel
->dir
.list
[c_file
].fname
, sf
->st_mode
) == -1)
303 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
304 current_panel
->dir
.list
[c_file
].fname
, unix_error_string (errno
));
306 do_file_mark (current_panel
, c_file
, 0);
309 /* --------------------------------------------------------------------------------------------- */
312 apply_mask (struct stat
*sf
)
323 fname
= next_file ();
324 if (mc_stat (fname
, sf
) != 0)
327 c_stat
= sf
->st_mode
;
331 while (current_panel
->marked
!= 0);
334 /* --------------------------------------------------------------------------------------------- */
335 /*** public functions ****************************************************************************/
336 /* --------------------------------------------------------------------------------------------- */
343 char buffer
[BUF_TINY
];
349 { /* do while any files remaining */
352 const char *c_fname
, *c_fown
, *c_fgrp
;
354 ch_dlg
= init_chmod ();
356 if (current_panel
->marked
!= 0)
357 fname
= next_file (); /* next marked file */
359 fname
= selection (current_panel
)->fname
; /* single file */
361 if (mc_stat (fname
, &sf_stat
) != 0)
362 { /* get status of file */
363 destroy_dlg (ch_dlg
);
367 c_stat
= sf_stat
.st_mode
;
368 mode_change
= FALSE
; /* clear changes flag */
370 /* set check buttons */
371 for (i
= 0; i
< PERMISSIONS
; i
++)
372 check_perm
[i
].check
->state
= (c_stat
& check_perm
[i
].mode
) != 0 ? 1 : 0;
375 c_fname
= str_trunc (fname
, 21);
376 add_widget (ch_dlg
, label_new (FY
+ 2, FX
+ 2, c_fname
));
377 c_fown
= str_trunc (get_owner (sf_stat
.st_uid
), 21);
378 add_widget (ch_dlg
, label_new (FY
+ 6, FX
+ 2, c_fown
));
379 c_fgrp
= str_trunc (get_group (sf_stat
.st_gid
), 21);
380 add_widget (ch_dlg
, label_new (FY
+ 8, FX
+ 2, c_fgrp
));
381 g_snprintf (buffer
, sizeof (buffer
), "%o", (unsigned int) c_stat
);
382 statl
= label_new (FY
+ 4, FX
+ 2, buffer
);
383 add_widget (ch_dlg
, statl
);
386 result
= run_dlg (ch_dlg
);
391 if (mode_change
&& mc_chmod (fname
, c_stat
) == -1)
392 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
393 fname
, unix_error_string (errno
));
403 and_mask
= or_mask
= 0;
404 and_mask
= ~and_mask
;
406 for (i
= 0; i
< PERMISSIONS
; i
++)
407 if (check_perm
[i
].selected
|| result
== B_ALL
)
409 if (check_perm
[i
].check
->state
& C_BOOL
)
410 or_mask
|= check_perm
[i
].mode
;
412 and_mask
&= ~check_perm
[i
].mode
;
415 apply_mask (&sf_stat
);
419 and_mask
= or_mask
= 0;
420 and_mask
= ~and_mask
;
422 for (i
= 0; i
< PERMISSIONS
; i
++)
423 if (check_perm
[i
].selected
)
424 or_mask
|= check_perm
[i
].mode
;
426 apply_mask (&sf_stat
);
430 and_mask
= or_mask
= 0;
431 and_mask
= ~and_mask
;
433 for (i
= 0; i
< PERMISSIONS
; i
++)
434 if (check_perm
[i
].selected
)
435 and_mask
&= ~check_perm
[i
].mode
;
437 apply_mask (&sf_stat
);
441 if (current_panel
->marked
!= 0 && result
!= B_CANCEL
)
443 do_file_mark (current_panel
, c_file
, 0);
447 destroy_dlg (ch_dlg
);
449 while (current_panel
->marked
!= 0 && !end_chmod
);
454 /* --------------------------------------------------------------------------------------------- */