Include GPL unofficial Spanish translation.
[midnight-commander.git] / pc / chmod.c
blob060aca697b5cc8f5f6d1eb6787c7998df3aec233
1 /* Chmod command for Win32
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 command", DLG_CENTER);
236 #define XTRACT(i) BY+chmod_but[i].y-single_set, BX+chmod_but[i].x, \
237 chmod_but[i].ret_cmd, chmod_but[i].flags, chmod_but[i].text, 0, 0, NULL
239 for (i = 0; i < BUTTONS; i++) {
240 if (i == 2 && single_set)
241 break;
242 else
243 add_widget (ch_dlg, button_new (XTRACT (i)));
247 #define XTRACT2(i) 0, check_perm [i].text, NULL
248 for (i = 0; i < PERMISSIONS; i++) {
249 check_perm[i].check = check_new (PY + (PERMISSIONS - i), PX + 2,
250 XTRACT2 (i));
251 add_widget (ch_dlg, check_perm[i].check);
255 int pc_stat_file (char *filename)
257 mode_t st;
259 st = GetFileAttributes (filename);
260 if (st & FILE_DIRECTORY)
261 st = -1;
262 return st;
265 static void chmod_done (void)
267 if (need_update)
268 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
269 repaint_screen ();
272 char *next_file (void)
274 while (!cpanel->dir.list[c_file].f.marked)
275 c_file++;
277 return cpanel->dir.list[c_file].fname;
280 static void do_chmod (mode_t sf)
282 sf &= and_mask;
283 sf |= or_mask;
285 mk_chmod(cpanel->dir.list[c_file].fname, sf);
287 do_file_mark (cpanel, c_file, 0);
290 static void apply_mask (mode_t sf)
292 char *fname;
293 mode_t sf_stat;
295 need_update = end_chmod = 1;
296 do_chmod (sf);
298 do {
299 fname = next_file ();
300 if ((sf_stat = pc_stat_file (fname)) < 0)
301 break;
303 c_stat = sf_stat;
304 do_chmod (c_stat);
305 } while (cpanel->marked);
308 void chmod_cmd (void)
310 char buffer [10];
311 char *fname;
312 int i;
313 mode_t sf_stat;
315 do { /* do while any files remaining */
316 init_chmod ();
317 if (cpanel->marked)
318 fname = next_file (); /* next marked file */
319 else
320 fname = selection (cpanel)->fname; /* single file */
322 if ((sf_stat = pc_stat_file (fname)) < 0) /* get status of file */
323 break;
325 c_stat = sf_stat;
326 mode_change = 0; /* clear changes flag */
328 /* set check buttons */
329 for (i = 0; i < PERMISSIONS; i++){
330 check_perm[i].check->state = (c_stat & check_perm[i].mode) ? 1 : 0;
331 check_perm[i].selected = 0;
334 /* Set the labels */
335 c_fname = name_trunc (fname, 21);
336 add_widget (ch_dlg, label_new (FY+2, FX+2, c_fname, NULL));
337 c_fown = "unknown";
338 add_widget (ch_dlg, label_new (FY+6, FX+2, c_fown, NULL));
339 c_fgrp = "unknown";
340 add_widget (ch_dlg, label_new (FY+8, FX+2, c_fgrp, NULL));
341 sprintf (buffer, "%o", c_stat);
342 statl = label_new (FY+4, FX+2, buffer, NULL);
343 add_widget (ch_dlg, statl);
345 run_dlg (ch_dlg); /* retrieve an action */
347 /* do action */
348 switch (ch_dlg->ret_value){
349 case B_ENTER:
350 if (mode_change)
351 mk_chmod (fname, c_stat); /*.ado */
352 need_update = 1;
353 break;
355 case B_CANCEL:
356 end_chmod = 1;
357 break;
359 case B_ALL:
360 case B_MARKED:
361 and_mask = or_mask = 0;
362 and_mask = ~and_mask;
364 for (i = 0; i < PERMISSIONS; i++) {
365 if (check_perm[i].selected || ch_dlg->ret_value == B_ALL)
366 if (check_perm[i].check->state & C_BOOL)
367 or_mask |= check_perm[i].mode;
368 else
369 and_mask &= ~check_perm[i].mode;
372 apply_mask (sf_stat);
373 break;
375 case B_SETMRK:
376 and_mask = or_mask = 0;
377 and_mask = ~and_mask;
379 for (i = 0; i < PERMISSIONS; i++) {
380 if (check_perm[i].selected)
381 or_mask |= check_perm[i].mode;
384 apply_mask (sf_stat);
385 break;
386 case B_CLRMRK:
387 and_mask = or_mask = 0;
388 and_mask = ~and_mask;
390 for (i = 0; i < PERMISSIONS; i++) {
391 if (check_perm[i].selected)
392 and_mask &= ~check_perm[i].mode;
395 apply_mask (sf_stat);
396 break;
399 if (cpanel->marked && ch_dlg->ret_value!=B_CANCEL) {
400 do_file_mark (cpanel, c_file, 0);
401 need_update = 1;
403 destroy_dlg (ch_dlg);
404 } while (cpanel->marked && !end_chmod);
405 chmod_done ();