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/vfs.h"
36 #include "lib/strutil.h"
38 #include "lib/widget.h"
39 #include "lib/keybind.h" /* CK_Cancel */
41 #include "midnight.h" /* current_panel */
44 /*** global variables ****************************************************************************/
46 /*** file scope macro definitions ****************************************************************/
60 #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
}
106 static const char *file_info_labels
[LABELS
] =
109 N_("Permissions (octal):"),
121 } chmod_but
[BUTTONS
] =
124 { B_CANCEL
, NORMAL_BUTTON
, 2, 33, N_("&Cancel") },
125 { B_ENTER
, DEFPUSH_BUTTON
, 2, 17, N_("&Set") },
126 { B_CLRMRK
, NORMAL_BUTTON
, 0, 42, N_("C&lear marked") },
127 { B_SETMRK
, NORMAL_BUTTON
, 0, 27, N_("S&et marked") },
128 { B_MARKED
, NORMAL_BUTTON
, 0, 12, N_("&Marked all") },
129 { B_ALL
, NORMAL_BUTTON
, 0, 0, N_("Set &all") }
133 /*** file scope functions ************************************************************************/
134 /* --------------------------------------------------------------------------------------------- */
140 static gboolean i18n
= FALSE
;
148 for (i
= 0; i
< PERMISSIONS
; i
++)
149 check_perm
[i
].text
= _(check_perm
[i
].text
);
151 for (i
= 0; i
< LABELS
; i
++)
152 file_info_labels
[i
] = _(file_info_labels
[i
]);
154 for (i
= 0; i
< BUTTONS
; i
++)
155 chmod_but
[i
].text
= _(chmod_but
[i
].text
);
156 #endif /* ENABLE_NLS */
159 /* --------------------------------------------------------------------------------------------- */
162 chmod_toggle_select (Dlg_head
* h
, int Id
)
164 tty_setcolor (COLOR_NORMAL
);
165 check_perm
[Id
].selected
= !check_perm
[Id
].selected
;
167 dlg_move (h
, PY
+ PERMISSIONS
- Id
, PX
+ 1);
168 tty_print_char (check_perm
[Id
].selected
? '*' : ' ');
169 dlg_move (h
, PY
+ PERMISSIONS
- Id
, PX
+ 3);
172 /* --------------------------------------------------------------------------------------------- */
175 chmod_refresh (Dlg_head
* h
)
177 common_dialog_repaint (h
);
179 tty_setcolor (COLOR_NORMAL
);
181 dlg_move (h
, FY
+ 1, FX
+ 2);
182 tty_print_string (file_info_labels
[0]);
183 dlg_move (h
, FY
+ 3, FX
+ 2);
184 tty_print_string (file_info_labels
[1]);
185 dlg_move (h
, FY
+ 5, FX
+ 2);
186 tty_print_string (file_info_labels
[2]);
187 dlg_move (h
, FY
+ 7, FX
+ 2);
188 tty_print_string (file_info_labels
[3]);
191 /* --------------------------------------------------------------------------------------------- */
194 chmod_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
196 char buffer
[BUF_TINY
];
199 id
= dlg_get_current_widget_id (h
) - BUTTONS
+ single_set
* 2 - 1;
204 /* close dialog due to SIGINT (ctrl-g) */
205 if (sender
== NULL
&& parm
== CK_Cancel
)
206 return MSG_NOT_HANDLED
;
208 /* handle checkboxes */
211 c_stat
^= check_perm
[id
].mode
;
212 g_snprintf (buffer
, sizeof (buffer
), "%o", (unsigned int) c_stat
);
213 label_set_text (statl
, buffer
);
214 chmod_toggle_select (h
, id
);
219 return MSG_NOT_HANDLED
;
222 if ((parm
== 'T' || parm
== 't' || parm
== KEY_IC
) && id
> 0)
224 chmod_toggle_select (h
, id
);
229 return MSG_NOT_HANDLED
;
236 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
240 /* --------------------------------------------------------------------------------------------- */
243 init_chmod (const char *fname
, const struct stat
*sf_stat
)
247 const char *c_fname
, *c_fown
, *c_fgrp
;
248 char buffer
[BUF_TINY
];
250 single_set
= (current_panel
->marked
< 2) ? 2 : 0;
253 create_dlg (TRUE
, 0, 0, 22 - single_set
, 70, dialog_colors
,
254 chmod_callback
, "[Chmod]", _("Chmod command"), DLG_CENTER
| DLG_REVERSE
);
256 for (i
= 0; i
< BUTTONS
; i
++)
258 if (i
== 2 && single_set
!= 0)
262 button_new (BY
+ chmod_but
[i
].y
- single_set
,
264 chmod_but
[i
].ret_cmd
,
265 chmod_but
[i
].flags
, chmod_but
[i
].text
, 0));
268 add_widget (ch_dlg
, groupbox_new (FY
, FX
, 10, 25, _("File")));
270 for (i
= 0; i
< PERMISSIONS
; i
++)
272 check_perm
[i
].check
= check_new (PY
+ (PERMISSIONS
- i
), PX
+ 2,
273 (c_stat
& check_perm
[i
].mode
) != 0 ? 1 : 0,
275 add_widget (ch_dlg
, check_perm
[i
].check
);
278 add_widget (ch_dlg
, groupbox_new (PY
, PX
, PERMISSIONS
+ 2, 33, _("Permission")));
281 /* Do this at end to have a widget id in a simple way */
282 c_fname
= str_trunc (fname
, 21);
283 add_widget (ch_dlg
, label_new (FY
+ 2, FX
+ 2, c_fname
));
284 c_fown
= str_trunc (get_owner (sf_stat
->st_uid
), 21);
285 add_widget (ch_dlg
, label_new (FY
+ 6, FX
+ 2, c_fown
));
286 c_fgrp
= str_trunc (get_group (sf_stat
->st_gid
), 21);
287 add_widget (ch_dlg
, label_new (FY
+ 8, FX
+ 2, c_fgrp
));
288 g_snprintf (buffer
, sizeof (buffer
), "%o", (unsigned int) c_stat
);
289 statl
= label_new (FY
+ 4, FX
+ 2, buffer
);
290 add_widget (ch_dlg
, statl
);
295 /* --------------------------------------------------------------------------------------------- */
301 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
305 /* --------------------------------------------------------------------------------------------- */
310 while (!current_panel
->dir
.list
[c_file
].f
.marked
)
313 return current_panel
->dir
.list
[c_file
].fname
;
316 /* --------------------------------------------------------------------------------------------- */
319 do_chmod (struct stat
*sf
)
321 sf
->st_mode
&= and_mask
;
322 sf
->st_mode
|= or_mask
;
324 if (mc_chmod (current_panel
->dir
.list
[c_file
].fname
, sf
->st_mode
) == -1)
325 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
326 current_panel
->dir
.list
[c_file
].fname
, unix_error_string (errno
));
328 do_file_mark (current_panel
, c_file
, 0);
331 /* --------------------------------------------------------------------------------------------- */
334 apply_mask (struct stat
*sf
)
345 fname
= next_file ();
346 if (mc_stat (fname
, sf
) != 0)
349 c_stat
= sf
->st_mode
;
353 while (current_panel
->marked
!= 0);
356 /* --------------------------------------------------------------------------------------------- */
357 /*** public functions ****************************************************************************/
358 /* --------------------------------------------------------------------------------------------- */
366 { /* do while any files remaining */
380 if (current_panel
->marked
!= 0)
381 fname
= next_file (); /* next marked file */
383 fname
= selection (current_panel
)->fname
; /* single file */
385 if (mc_stat (fname
, &sf_stat
) != 0)
388 c_stat
= sf_stat
.st_mode
;
390 ch_dlg
= init_chmod (fname
, &sf_stat
);
393 result
= run_dlg (ch_dlg
);
398 if (mode_change
&& mc_chmod (fname
, c_stat
) == -1)
399 message (D_ERROR
, MSG_ERROR
, _("Cannot chmod \"%s\"\n%s"),
400 fname
, unix_error_string (errno
));
410 and_mask
= or_mask
= 0;
411 and_mask
= ~and_mask
;
413 for (i
= 0; i
< PERMISSIONS
; i
++)
414 if (check_perm
[i
].selected
|| result
== B_ALL
)
416 if (check_perm
[i
].check
->state
& C_BOOL
)
417 or_mask
|= check_perm
[i
].mode
;
419 and_mask
&= ~check_perm
[i
].mode
;
422 apply_mask (&sf_stat
);
426 and_mask
= or_mask
= 0;
427 and_mask
= ~and_mask
;
429 for (i
= 0; i
< PERMISSIONS
; i
++)
430 if (check_perm
[i
].selected
)
431 or_mask
|= check_perm
[i
].mode
;
433 apply_mask (&sf_stat
);
437 and_mask
= or_mask
= 0;
438 and_mask
= ~and_mask
;
440 for (i
= 0; i
< PERMISSIONS
; i
++)
441 if (check_perm
[i
].selected
)
442 and_mask
&= ~check_perm
[i
].mode
;
444 apply_mask (&sf_stat
);
448 if (current_panel
->marked
!= 0 && result
!= B_CANCEL
)
450 do_file_mark (current_panel
, c_file
, 0);
454 destroy_dlg (ch_dlg
);
456 while (current_panel
->marked
!= 0 && !end_chmod
);
461 /* --------------------------------------------------------------------------------------------- */