Last fuzzy trimmed...
[midnight-commander.git] / pc / chmod.c
blob6f67dfb761fa5a3ce1098983ad08faf76ad1ebc5
1 /* Chmod command for Windows NT and OS/2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #include <config.h>
21 #include <string.h>
22 #include <stdio.h>
23 /* for chmod and stat */
24 #include <io.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include "../src/global.h"
28 #include "../src/tty.h"
29 #include "../src/util.h"
30 #include "../src/win.h"
31 #include "../src/color.h"
32 #include "../src/dlg.h"
33 #include "../src/widget.h"
34 #include "../src/dialog.h" /* For do_refresh() */
36 #include "../src/dir.h"
37 #include "../src/panel.h" /* Needed for the externs */
38 #include "../src/file.h"
39 #include "../src/main.h"
40 #include "../src/chmod.h"
41 #include "../src/achown.h"
42 #include "../src/chown.h"
44 #define FILE_ARCHIVED FILE_ATTRIBUTE_ARCHIVE
45 #define FILE_DIRECTORY FILE_ATTRIBUTE_DIRECTORY
46 #define FILE_HIDDEN FILE_ATTRIBUTE_HIDDEN
47 #define FILE_READONLY FILE_ATTRIBUTE_READONLY
48 #define FILE_SYSTEM FILE_ATTRIBUTE_SYSTEM
49 #define mk_chmod(fname,st) SetFileAttributes(fname,st)
51 static int single_set;
52 struct Dlg_head *ch_dlg;
54 #define PX 5
55 #define PY 2
57 #define FX 40
58 #define FY 2
60 #define BX 6
61 #define BY 17
63 #define TX 40
64 #define TY 12
66 #define PERMISSIONS 4
67 #define BUTTONS 6
69 #define B_MARKED B_USER
70 #define B_ALL B_USER+1
71 #define B_SETMRK B_USER+2
72 #define B_CLRMRK B_USER+3
75 int mode_change, need_update;
76 int c_file, end_chmod;
78 umode_t and_mask, or_mask, c_stat;
79 char *c_fname, *c_fown, *c_fgrp, *c_fperm;
80 int c_fsize;
82 static WLabel *statl;
83 static int normal_color;
84 static int title_color;
85 static int selection_color;
87 /* bsedos.h */
88 struct {
89 mode_t mode;
90 char *text;
91 int selected;
92 WCheck *check;
93 } check_perm[PERMISSIONS] = {
96 FILE_ARCHIVED, "Archive", 0, 0,
99 FILE_READONLY, "Read Only", 0, 0,
102 FILE_HIDDEN, "Hidden", 0, 0,
105 FILE_SYSTEM, "System", 0, 0,
109 struct {
110 int ret_cmd, flags, y, x;
111 char *text;
112 } chmod_but[BUTTONS] = {
115 B_CANCEL, NORMAL_BUTTON, 2, 33, "&Cancel",
118 B_ENTER, DEFPUSH_BUTTON, 2, 17, "&Set",
121 B_CLRMRK, NORMAL_BUTTON, 0, 42, "C&lear marked",
124 B_SETMRK, NORMAL_BUTTON, 0, 27, "S&et marked",
127 B_MARKED, NORMAL_BUTTON, 0, 12, "&Marked all",
130 B_ALL, NORMAL_BUTTON, 0, 0, "Set &all",
134 static void chmod_toggle_select (void)
136 int Id = ch_dlg->current->dlg_id - BUTTONS + single_set * 2;
138 attrset (normal_color);
139 check_perm[Id].selected ^= 1;
141 dlg_move (ch_dlg, PY + PERMISSIONS - Id, PX + 1);
142 addch ((check_perm[Id].selected) ? '*' : ' ');
143 dlg_move (ch_dlg, PY + PERMISSIONS - Id, PX + 3);
146 static void chmod_refresh (void)
148 attrset (normal_color);
149 dlg_erase (ch_dlg);
151 draw_box (ch_dlg, 1, 2, 20 - single_set, 66);
152 draw_box (ch_dlg, PY, PX, PERMISSIONS + 2, 33);
153 draw_box (ch_dlg, FY, FX, 10, 25);
155 dlg_move (ch_dlg, FY + 1, FX + 2);
156 addstr ("Name");
157 dlg_move (ch_dlg, FY + 3, FX + 2);
158 addstr ("Permissions (Octal)");
159 dlg_move (ch_dlg, FY + 5, FX + 2);
160 addstr ("Owner name");
161 dlg_move (ch_dlg, FY + 7, FX + 2);
162 addstr ("Group name");
164 attrset (title_color);
165 dlg_move (ch_dlg, 1, 28);
166 addstr (" Chmod command ");
167 dlg_move (ch_dlg, PY, PX + 1);
168 addstr (" Permission ");
169 dlg_move (ch_dlg, FY, FX + 1);
170 addstr (" File ");
172 attrset (selection_color);
174 dlg_move (ch_dlg, TY, TX);
175 addstr ("Use SPACE to change");
176 dlg_move (ch_dlg, TY + 1, TX);
177 addstr ("an option, ARROW KEYS");
178 dlg_move (ch_dlg, TY + 2, TX);
179 addstr ("to move between options");
180 dlg_move (ch_dlg, TY + 3, TX);
181 addstr ("and T or INS to mark");
184 static int chmod_callback (Dlg_head *h, int Par, int Msg)
186 char buffer [10];
188 switch (Msg) {
189 case DLG_ACTION:
190 if (Par >= BUTTONS - single_set * 2){
191 c_stat ^= check_perm[Par - BUTTONS + single_set * 2].mode;
192 sprintf (buffer, "%o", c_stat);
193 label_set_text (statl, buffer);
194 chmod_toggle_select ();
195 mode_change = 1;
197 break;
199 case DLG_KEY:
200 if ((Par == 'T' || Par == 't' || Par == KEY_IC) &&
201 ch_dlg->current->dlg_id >= BUTTONS - single_set * 2) {
202 chmod_toggle_select ();
203 if (Par == KEY_IC)
204 dlg_one_down (ch_dlg);
205 return 1;
207 break;
208 case DLG_DRAW:
209 chmod_refresh ();
210 break;
212 return 0;
215 static void init_chmod (void)
217 int i;
219 do_refresh ();
220 end_chmod = c_file = need_update = 0;
221 single_set = (cpanel->marked < 2) ? 2 : 0;
223 if (use_colors){
224 normal_color = COLOR_NORMAL;
225 title_color = COLOR_HOT_NORMAL;
226 selection_color = COLOR_NORMAL;
227 } else {
228 normal_color = NORMAL_COLOR;
229 title_color = SELECTED_COLOR;
230 selection_color = SELECTED_COLOR;
233 ch_dlg = create_dlg (0, 0, 22 - single_set, 70, dialog_colors,
234 chmod_callback, "[Chmod]", "chmod", DLG_CENTER);
236 x_set_dialog_title (ch_dlg, "Chmod command");
238 #define XTRACT(i) BY+chmod_but[i].y-single_set, BX+chmod_but[i].x, \
239 chmod_but[i].ret_cmd, chmod_but[i].flags, chmod_but[i].text, 0, 0, NULL
241 for (i = 0; i < BUTTONS; i++) {
242 if (i == 2 && single_set)
243 break;
244 else
245 add_widget (ch_dlg, button_new (XTRACT (i)));
249 #define XTRACT2(i) 0, check_perm [i].text, NULL
250 for (i = 0; i < PERMISSIONS; i++) {
251 check_perm[i].check = check_new (PY + (PERMISSIONS - i), PX + 2,
252 XTRACT2 (i));
253 add_widget (ch_dlg, check_perm[i].check);
257 int pc_stat_file (char *filename)
259 mode_t st;
261 st = GetFileAttributes (filename);
262 if (st & FILE_DIRECTORY)
263 st = -1;
264 return st;
267 static void chmod_done (void)
269 if (need_update)
270 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
271 repaint_screen ();
274 char *next_file (void)
276 while (!cpanel->dir.list[c_file].f.marked)
277 c_file++;
279 return cpanel->dir.list[c_file].fname;
282 static void do_chmod (mode_t sf)
284 sf &= and_mask;
285 sf |= or_mask;
287 mk_chmod(cpanel->dir.list[c_file].fname, sf);
289 do_file_mark (cpanel, c_file, 0);
292 static void apply_mask (mode_t sf)
294 char *fname;
295 mode_t sf_stat;
297 need_update = end_chmod = 1;
298 do_chmod (sf);
300 do {
301 fname = next_file ();
302 if ((sf_stat = pc_stat_file (fname)) < 0)
303 break;
305 c_stat = sf_stat;
306 do_chmod (c_stat);
307 } while (cpanel->marked);
310 void chmod_cmd (void)
312 char buffer [10];
313 char *fname;
314 int i;
315 mode_t sf_stat;
317 do { /* do while any files remaining */
318 init_chmod ();
319 if (cpanel->marked)
320 fname = next_file (); /* next marked file */
321 else
322 fname = selection (cpanel)->fname; /* single file */
324 if ((sf_stat = pc_stat_file (fname)) < 0) /* get status of file */
325 break;
327 c_stat = sf_stat;
328 mode_change = 0; /* clear changes flag */
330 /* set check buttons */
331 for (i = 0; i < PERMISSIONS; i++){
332 check_perm[i].check->state = (c_stat & check_perm[i].mode) ? 1 : 0;
333 check_perm[i].selected = 0;
336 /* Set the labels */
337 c_fname = name_trunc (fname, 21);
338 add_widget (ch_dlg, label_new (FY+2, FX+2, c_fname, NULL));
339 c_fown = "unknown";
340 add_widget (ch_dlg, label_new (FY+6, FX+2, c_fown, NULL));
341 c_fgrp = "unknown";
342 add_widget (ch_dlg, label_new (FY+8, FX+2, c_fgrp, NULL));
343 sprintf (buffer, "%o", c_stat);
344 statl = label_new (FY+4, FX+2, buffer, NULL);
345 add_widget (ch_dlg, statl);
347 run_dlg (ch_dlg); /* retrieve an action */
349 /* do action */
350 switch (ch_dlg->ret_value){
351 case B_ENTER:
352 if (mode_change)
353 mk_chmod (fname, c_stat); /*.ado */
354 need_update = 1;
355 break;
357 case B_CANCEL:
358 end_chmod = 1;
359 break;
361 case B_ALL:
362 case B_MARKED:
363 and_mask = or_mask = 0;
364 and_mask = ~and_mask;
366 for (i = 0; i < PERMISSIONS; i++) {
367 if (check_perm[i].selected || ch_dlg->ret_value == B_ALL)
368 if (check_perm[i].check->state & C_BOOL)
369 or_mask |= check_perm[i].mode;
370 else
371 and_mask &= ~check_perm[i].mode;
374 apply_mask (sf_stat);
375 break;
377 case B_SETMRK:
378 and_mask = or_mask = 0;
379 and_mask = ~and_mask;
381 for (i = 0; i < PERMISSIONS; i++) {
382 if (check_perm[i].selected)
383 or_mask |= check_perm[i].mode;
386 apply_mask (sf_stat);
387 break;
388 case B_CLRMRK:
389 and_mask = or_mask = 0;
390 and_mask = ~and_mask;
392 for (i = 0; i < PERMISSIONS; i++) {
393 if (check_perm[i].selected)
394 and_mask &= ~check_perm[i].mode;
397 apply_mask (sf_stat);
398 break;
401 if (cpanel->marked && ch_dlg->ret_value!=B_CANCEL) {
402 do_file_mark (cpanel, c_file, 0);
403 need_update = 1;
405 destroy_dlg (ch_dlg);
406 } while (cpanel->marked && !end_chmod);
407 chmod_done ();