fix include file case
[reactos.git] / rosapps / mc / src / chmod.c
1 /* Chmod command -- for the Midnight Commander
2 Copyright (C) 1994 Radek Doulik
3
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.
8
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.
13
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., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <config.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <errno.h> /* For errno on SunOS systems */
23 /* Needed for the extern declarations of integer parameters */
24 #include <sys/types.h>
25 #include <sys/param.h>
26 #include <sys/stat.h>
27 #include <grp.h>
28 #include <pwd.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include "tty.h"
33 #include "mad.h"
34 #include "util.h"
35 #include "win.h"
36 #include "color.h"
37 #include "dlg.h"
38 #include "widget.h"
39 #include "dialog.h" /* For do_refresh() */
40
41 #include "dir.h"
42 #include "panel.h" /* Needed for the externs */
43 #include "file.h"
44 #include "main.h"
45 #include "chmod.h"
46 #include "achown.h"
47 #include "chown.h"
48 #include "../vfs/vfs.h"
49
50 #ifdef HAVE_TK
51 # include "tkmain.h"
52 #endif
53
54 static int single_set;
55 struct Dlg_head *ch_dlg;
56
57 #define PX 5
58 #define PY 2
59
60 #define FX 40
61 #define FY 2
62
63 #define BX 6
64 #define BY 17
65
66 #define TX 40
67 #define TY 12
68
69 #define PERMISSIONS 12
70 #define BUTTONS 6
71
72 #define B_MARKED B_USER
73 #define B_ALL B_USER+1
74 #define B_SETMRK B_USER+2
75 #define B_CLRMRK B_USER+3
76
77 int mode_change, need_update;
78 int c_file, end_chmod;
79
80 umode_t and_mask, or_mask, c_stat;
81 char *c_fname, *c_fown, *c_fgrp, *c_fperm;
82 int c_fsize;
83
84 static WLabel *statl;
85 static int normal_color;
86 static int title_color;
87 static int selection_color;
88
89 struct {
90 mode_t mode;
91 char *text;
92 int selected;
93 WCheck *check;
94 } check_perm[PERMISSIONS] =
95 {
96 { S_IXOTH, N_("execute/search by others"), 0, 0, },
97 { S_IWOTH, N_("write by others"), 0, 0, },
98 { S_IROTH, N_("read by others"), 0, 0, },
99 { S_IXGRP, N_("execute/search by group"), 0, 0, },
100 { S_IWGRP, N_("write by group"), 0, 0, },
101 { S_IRGRP, N_("read by group"), 0, 0, },
102 { S_IXUSR, N_("execute/search by owner"), 0, 0, },
103 { S_IWUSR, N_("write by owner"), 0, 0, },
104 { S_IRUSR, N_("read by owner"), 0, 0, },
105 { S_ISVTX, N_("sticky bit"), 0, 0, },
106 { S_ISGID, N_("set group ID on execution"), 0, 0, },
107 { S_ISUID, N_("set user ID on execution"), 0, 0, },
108 };
109
110 struct {
111 int ret_cmd, flags, y, x;
112 char *text;
113 } chmod_but[BUTTONS] =
114 {
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") },
121 };
122
123 #ifdef HAVE_X
124 static void chmod_toggle_select (void)
125 {
126 #ifdef HAVE_TK
127 char *wn = (char *) ch_dlg->current->widget->wdata;
128 int id = ch_dlg->current->dlg_id -BUTTONS + single_set * 2;
129
130 check_perm [id].selected ^= 1;
131
132 tk_evalf ("%s configure -color $setup(%s)",
133 wn+1, check_perm [id].selected ? "marked":"black");
134 #endif
135 }
136
137 #else
138 static void chmod_toggle_select (void)
139 {
140 int Id = ch_dlg->current->dlg_id - BUTTONS + single_set * 2;
141
142 attrset (normal_color);
143 check_perm[Id].selected ^= 1;
144
145 dlg_move (ch_dlg, PY + PERMISSIONS - Id, PX + 1);
146 addch ((check_perm[Id].selected) ? '*' : ' ');
147 dlg_move (ch_dlg, PY + PERMISSIONS - Id, PX + 3);
148 }
149
150 static void chmod_refresh (void)
151 {
152 attrset (COLOR_NORMAL);
153 dlg_erase (ch_dlg);
154
155 draw_box (ch_dlg, 1, 2, 20 - single_set, 66);
156 draw_box (ch_dlg, PY, PX, PERMISSIONS + 2, 33);
157 draw_box (ch_dlg, FY, FX, 10, 25);
158
159 dlg_move (ch_dlg, FY + 1, FX + 2);
160 addstr (_("Name"));
161 dlg_move (ch_dlg, FY + 3, FX + 2);
162 addstr (_("Permissions (Octal)"));
163 dlg_move (ch_dlg, FY + 5, FX + 2);
164 addstr (_("Owner name"));
165 dlg_move (ch_dlg, FY + 7, FX + 2);
166 addstr (_("Group name"));
167
168 attrset (title_color);
169 dlg_move (ch_dlg, 1, 28);
170 addstr (_(" Chmod command "));
171 dlg_move (ch_dlg, PY, PX + 1);
172 addstr (_(" Permission "));
173 dlg_move (ch_dlg, FY, FX + 1);
174 addstr (_(" File "));
175
176 attrset (selection_color);
177
178 dlg_move (ch_dlg, TY, TX);
179 addstr (_("Use SPACE to change"));
180 dlg_move (ch_dlg, TY + 1, TX);
181 addstr (_("an option, ARROW KEYS"));
182 dlg_move (ch_dlg, TY + 2, TX);
183 addstr (_("to move between options"));
184 dlg_move (ch_dlg, TY + 3, TX);
185 addstr (_("and T or INS to mark"));
186 }
187 #endif
188
189 static int chmod_callback (Dlg_head *h, int Par, int Msg)
190 {
191 char buffer [10];
192
193 switch (Msg) {
194 case DLG_ACTION:
195 if (Par >= BUTTONS - single_set * 2){
196 c_stat ^= check_perm[Par - BUTTONS + single_set * 2].mode;
197 sprintf (buffer, "%o", c_stat);
198 label_set_text (statl, buffer);
199 chmod_toggle_select ();
200 mode_change = 1;
201 }
202 break;
203
204 case DLG_KEY:
205 if ((Par == 'T' || Par == 't' || Par == KEY_IC) &&
206 ch_dlg->current->dlg_id >= BUTTONS - single_set * 2) {
207 chmod_toggle_select ();
208 if (Par == KEY_IC)
209 dlg_one_down (ch_dlg);
210 return 1;
211 }
212 break;
213 #ifndef HAVE_X
214 case DLG_DRAW:
215 chmod_refresh ();
216 break;
217 #endif
218 }
219 return 0;
220 }
221
222 static void init_chmod (void)
223 {
224 int i;
225
226 do_refresh ();
227 end_chmod = c_file = need_update = 0;
228 single_set = (cpanel->marked < 2) ? 2 : 0;
229
230 if (use_colors){
231 normal_color = COLOR_NORMAL;
232 title_color = COLOR_HOT_NORMAL;
233 selection_color = COLOR_NORMAL;
234 } else {
235 normal_color = NORMAL_COLOR;
236 title_color = SELECTED_COLOR;
237 selection_color = SELECTED_COLOR;
238 }
239
240 ch_dlg = create_dlg (0, 0, 22 - single_set, 70, dialog_colors,
241 chmod_callback, "[Chmod]", "chmod", DLG_CENTER);
242
243 x_set_dialog_title (ch_dlg, _("Chmod command"));
244
245 #define XTRACT(i) BY+chmod_but[i].y-single_set, BX+chmod_but[i].x, \
246 chmod_but[i].ret_cmd, chmod_but[i].flags, _(chmod_but[i].text), 0, 0, NULL
247
248 tk_new_frame (ch_dlg, "b.");
249 for (i = 0; i < BUTTONS; i++) {
250 if (i == 2 && single_set)
251 break;
252 else
253 add_widgetl (ch_dlg, button_new (XTRACT (i)), XV_WLAY_RIGHTOF);
254 }
255
256
257 #define XTRACT2(i) 0, _(check_perm [i].text), NULL
258 tk_new_frame (ch_dlg, "c.");
259 for (i = 0; i < PERMISSIONS; i++) {
260 check_perm[i].check = check_new (PY + (PERMISSIONS - i), PX + 2,
261 XTRACT2 (i));
262 add_widget (ch_dlg, check_perm[i].check);
263 }
264 }
265
266 int stat_file (char *filename, struct stat *st)
267 {
268 if (mc_stat (filename, st))
269 return 0;
270 if (!(S_ISREG(st->st_mode) || S_ISDIR(st->st_mode) ||
271 S_ISLNK(st->st_mode)))
272 return 0;
273
274 return 1;
275 }
276
277 static void chmod_done (void)
278 {
279 if (need_update)
280 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
281 repaint_screen ();
282 }
283
284 static char *next_file (void)
285 {
286 while (!cpanel->dir.list[c_file].f.marked)
287 c_file++;
288
289 return cpanel->dir.list[c_file].fname;
290 }
291
292 static void do_chmod (struct stat *sf)
293 {
294 sf->st_mode &= and_mask;
295 sf->st_mode |= or_mask;
296 if (mc_chmod (cpanel->dir.list [c_file].fname, sf->st_mode) == -1)
297 message (1, MSG_ERROR, _(" Couldn't chmod \"%s\" \n %s "),
298 cpanel->dir.list [c_file].fname, unix_error_string (errno));
299
300 do_file_mark (cpanel, c_file, 0);
301 }
302
303 static void apply_mask (struct stat *sf)
304 {
305 char *fname;
306
307 need_update = end_chmod = 1;
308 do_chmod (sf);
309
310 do {
311 fname = next_file ();
312 if (!stat_file (fname, sf))
313 return;
314 c_stat = sf->st_mode;
315
316 do_chmod (sf);
317 } while (cpanel->marked);
318 }
319
320 void chmod_cmd (void)
321 {
322 char buffer [10];
323 char *fname;
324 int i;
325 struct stat sf_stat;
326
327 if (!vfs_current_is_local ()) {
328 if (vfs_current_is_extfs ()) {
329 message (1, _(" Oops... "),
330 _(" I can't run the Chmod command on an extfs "));
331 return;
332 } else if (vfs_current_is_tarfs ()) {
333 message (1, _(" Oops... "),
334 (" I can't run the Chmod command on a tarfs "));
335 return;
336 }
337 }
338
339 do { /* do while any files remaining */
340 init_chmod ();
341 if (cpanel->marked)
342 fname = next_file (); /* next marked file */
343 else
344 fname = selection (cpanel)->fname; /* single file */
345
346 if (!stat_file (fname, &sf_stat)){ /* get status of file */
347 destroy_dlg (ch_dlg);
348 break;
349 }
350
351 c_stat = sf_stat.st_mode;
352 mode_change = 0; /* clear changes flag */
353
354 /* set check buttons */
355 for (i = 0; i < PERMISSIONS; i++){
356 check_perm[i].check->state = (c_stat & check_perm[i].mode) ? 1 : 0;
357 check_perm[i].selected = 0;
358 }
359
360 tk_new_frame (ch_dlg, "l.");
361 /* Set the labels */
362 c_fname = name_trunc (fname, 21);
363 add_widget (ch_dlg, label_new (FY+2, FX+2, c_fname, NULL));
364 c_fown = name_trunc (get_owner (sf_stat.st_uid), 21);
365 add_widget (ch_dlg, label_new (FY+6, FX+2, c_fown, NULL));
366 c_fgrp = name_trunc (get_group (sf_stat.st_gid), 21);
367 add_widget (ch_dlg, label_new (FY+8, FX+2, c_fgrp, NULL));
368 sprintf (buffer, "%o", c_stat);
369 statl = label_new (FY+4, FX+2, buffer, NULL);
370 add_widget (ch_dlg, statl);
371 tk_end_frame ();
372
373 run_dlg (ch_dlg); /* retrieve an action */
374
375 /* do action */
376 switch (ch_dlg->ret_value){
377 case B_ENTER:
378 if (mode_change)
379 if (mc_chmod (fname, c_stat) == -1)
380 message (1, MSG_ERROR, _(" Couldn't chmod \"%s\" \n %s "),
381 fname, unix_error_string (errno));
382 need_update = 1;
383 break;
384
385 case B_CANCEL:
386 end_chmod = 1;
387 break;
388
389 case B_ALL:
390 case B_MARKED:
391 and_mask = or_mask = 0;
392 and_mask = ~and_mask;
393
394 for (i = 0; i < PERMISSIONS; i++) {
395 if (check_perm[i].selected || ch_dlg->ret_value == B_ALL)
396 if (check_perm[i].check->state & C_BOOL)
397 or_mask |= check_perm[i].mode;
398 else
399 and_mask &= ~check_perm[i].mode;
400 }
401
402 apply_mask (&sf_stat);
403 break;
404
405 case B_SETMRK:
406 and_mask = or_mask = 0;
407 and_mask = ~and_mask;
408
409 for (i = 0; i < PERMISSIONS; i++) {
410 if (check_perm[i].selected)
411 or_mask |= check_perm[i].mode;
412 }
413
414 apply_mask (&sf_stat);
415 break;
416 case B_CLRMRK:
417 and_mask = or_mask = 0;
418 and_mask = ~and_mask;
419
420 for (i = 0; i < PERMISSIONS; i++) {
421 if (check_perm[i].selected)
422 and_mask &= ~check_perm[i].mode;
423 }
424
425 apply_mask (&sf_stat);
426 break;
427 }
428
429 if (cpanel->marked && ch_dlg->ret_value!=B_CANCEL) {
430 do_file_mark (cpanel, c_file, 0);
431 need_update = 1;
432 }
433 destroy_dlg (ch_dlg);
434 } while (cpanel->marked && !end_chmod);
435 chmod_done ();
436 }
437
438 void ch1_cmd (int id)
439 {
440 if (advanced_chfns)
441 chown_advanced_cmd ();
442 else
443 chmod_cmd ();
444 }
445
446 void ch2_cmd (int id)
447 {
448 if (advanced_chfns)
449 chown_advanced_cmd ();
450 else
451 chown_cmd ();
452 }
453