Header modified...
[midnight-commander.git] / pc / chmod.c
blob8971919b22c3fce21f787fc107d0c400107ba905
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 <windows.h>
23 #include <string.h>
24 #include <stdio.h>
25 /* for chmod and stat */
26 #include <io.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include "../src/tty.h"
30 #include "../src/mad.h"
31 #include "../src/util.h"
32 #include "../src/win.h"
33 #include "../src/color.h"
34 #include "../src/dlg.h"
35 #include "../src/widget.h"
36 #include "../src/dialog.h" /* For do_refresh() */
38 #include "../src/dir.h"
39 #include "../src/panel.h" /* Needed for the externs */
40 #include "../src/file.h"
41 #include "../src/main.h"
42 #include "../src/chmod.h"
43 #include "../src/achown.h"
44 #include "../src/chown.h"
46 #define FILE_ARCHIVED FILE_ATTRIBUTE_ARCHIVE
47 #define FILE_DIRECTORY FILE_ATTRIBUTE_DIRECTORY
48 #define FILE_HIDDEN FILE_ATTRIBUTE_HIDDEN
49 #define FILE_READONLY FILE_ATTRIBUTE_READONLY
50 #define FILE_SYSTEM FILE_ATTRIBUTE_SYSTEM
51 #define mk_chmod(fname,st) SetFileAttributes(fname,st)
53 static int single_set;
54 struct Dlg_head *ch_dlg;
56 #define PX 5
57 #define PY 2
59 #define FX 40
60 #define FY 2
62 #define BX 6
63 #define BY 17
65 #define TX 40
66 #define TY 12
68 #define PERMISSIONS 4
69 #define BUTTONS 6
71 #define B_MARKED B_USER
72 #define B_ALL B_USER+1
73 #define B_SETMRK B_USER+2
74 #define B_CLRMRK B_USER+3
77 int mode_change, need_update;
78 int c_file, end_chmod;
80 umode_t and_mask, or_mask, c_stat;
81 char *c_fname, *c_fown, *c_fgrp, *c_fperm;
82 int c_fsize;
84 static WLabel *statl;
85 static int normal_color;
86 static int title_color;
87 static int selection_color;
89 /* bsedos.h */
90 struct {
91 mode_t mode;
92 char *text;
93 int selected;
94 WCheck *check;
95 } check_perm[PERMISSIONS] = {
98 FILE_ARCHIVED, N_("Archive"), 0, 0,
101 FILE_READONLY, N_("Read Only"), 0, 0,
104 FILE_HIDDEN, N_("Hidden"), 0, 0,
107 FILE_SYSTEM, N_("System"), 0, 0,
111 struct {
112 int ret_cmd, flags, y, x;
113 char *text;
114 } chmod_but[BUTTONS] = {
117 B_CANCEL, NORMAL_BUTTON, 2, 33, N_("&Cancel"),
120 B_ENTER, DEFPUSH_BUTTON, 2, 17, N_("&Set"),
123 B_CLRMRK, NORMAL_BUTTON, 0, 42, N_("C&lear marked"),
126 B_SETMRK, NORMAL_BUTTON, 0, 27, N_("S&et marked"),
129 B_MARKED, NORMAL_BUTTON, 0, 12, N_("&Marked all"),
132 B_ALL, NORMAL_BUTTON, 0, 0, N_("Set &all"),
136 static void chmod_toggle_select (void)
138 int Id = ch_dlg->current->dlg_id - BUTTONS + single_set * 2;
140 attrset (normal_color);
141 check_perm[Id].selected ^= 1;
143 dlg_move (ch_dlg, PY + PERMISSIONS - Id, PX + 1);
144 addch ((check_perm[Id].selected) ? '*' : ' ');
145 dlg_move (ch_dlg, PY + PERMISSIONS - Id, PX + 3);
148 static void chmod_refresh (void)
150 attrset (normal_color);
151 dlg_erase (ch_dlg);
153 draw_box (ch_dlg, 1, 2, 20 - single_set, 66);
154 draw_box (ch_dlg, PY, PX, PERMISSIONS + 2, 33);
155 draw_box (ch_dlg, FY, FX, 10, 25);
157 dlg_move (ch_dlg, FY + 1, FX + 2);
158 addstr (_("Name"));
159 dlg_move (ch_dlg, FY + 3, FX + 2);
160 addstr (_("Permissions (Octal)"));
161 dlg_move (ch_dlg, FY + 5, FX + 2);
162 addstr (_("Owner name"));
163 dlg_move (ch_dlg, FY + 7, FX + 2);
164 addstr (_("Group name"));
166 attrset (title_color);
167 dlg_move (ch_dlg, 1, 28);
168 addstr (_(" Chmod command "));
169 dlg_move (ch_dlg, PY, PX + 1);
170 addstr (_(" Permission "));
171 dlg_move (ch_dlg, FY, FX + 1);
172 addstr (_(" File "));
174 attrset (selection_color);
176 dlg_move (ch_dlg, TY, TX);
177 addstr (_("Use SPACE to change"));
178 dlg_move (ch_dlg, TY + 1, TX);
179 addstr (_("an option, ARROW KEYS"));
180 dlg_move (ch_dlg, TY + 2, TX);
181 addstr (_("to move between options"));
182 dlg_move (ch_dlg, TY + 3, TX);
183 addstr (_("and T or INS to mark"));
186 static int chmod_callback (Dlg_head *h, int Par, int Msg)
188 char buffer [10];
190 switch (Msg) {
191 case DLG_ACTION:
192 if (Par >= BUTTONS - single_set * 2){
193 c_stat ^= check_perm[Par - BUTTONS + single_set * 2].mode;
194 sprintf (buffer, "%o", c_stat);
195 label_set_text (statl, buffer);
196 chmod_toggle_select ();
197 mode_change = 1;
199 break;
201 case DLG_KEY:
202 if ((Par == 'T' || Par == 't' || Par == KEY_IC) &&
203 ch_dlg->current->dlg_id >= BUTTONS - single_set * 2) {
204 chmod_toggle_select ();
205 if (Par == KEY_IC)
206 dlg_one_down (ch_dlg);
207 return 1;
209 break;
210 case DLG_DRAW:
211 chmod_refresh ();
212 break;
214 return 0;
217 static void init_chmod (void)
219 int i;
221 do_refresh ();
222 end_chmod = c_file = need_update = 0;
223 single_set = (cpanel->marked < 2) ? 2 : 0;
225 if (use_colors){
226 normal_color = COLOR_NORMAL;
227 title_color = COLOR_HOT_NORMAL;
228 selection_color = COLOR_NORMAL;
229 } else {
230 normal_color = NORMAL_COLOR;
231 title_color = SELECTED_COLOR;
232 selection_color = SELECTED_COLOR;
235 ch_dlg = create_dlg (0, 0, 22 - single_set, 70, dialog_colors,
236 chmod_callback, _("[Chmod]"), _("chmod"), DLG_CENTER);
238 x_set_dialog_title (ch_dlg, _("Chmod command"));
240 #define XTRACT(i) BY+chmod_but[i].y-single_set, BX+chmod_but[i].x, \
241 chmod_but[i].ret_cmd, chmod_but[i].flags, chmod_but[i].text, 0, 0, NULL
243 for (i = 0; i < BUTTONS; i++) {
244 if (i == 2 && single_set)
245 break;
246 else
247 add_widget (ch_dlg, button_new (XTRACT (i)));
251 #define XTRACT2(i) 0, check_perm [i].text, NULL
252 for (i = 0; i < PERMISSIONS; i++) {
253 check_perm[i].check = check_new (PY + (PERMISSIONS - i), PX + 2,
254 XTRACT2 (i));
255 add_widget (ch_dlg, check_perm[i].check);
259 int pc_stat_file (char *filename)
261 mode_t st;
263 st = GetFileAttributes (filename);
264 if (st & FILE_DIRECTORY)
265 st = -1;
266 return st;
269 static void chmod_done (void)
271 if (need_update)
272 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
273 repaint_screen ();
276 char *next_file (void)
278 while (!cpanel->dir.list[c_file].f.marked)
279 c_file++;
281 return cpanel->dir.list[c_file].fname;
284 static void do_chmod (mode_t sf)
286 sf &= and_mask;
287 sf |= or_mask;
289 mk_chmod(cpanel->dir.list[c_file].fname, sf);
291 do_file_mark (cpanel, c_file, 0);
294 static void apply_mask (mode_t sf)
296 char *fname;
297 mode_t sf_stat;
299 need_update = end_chmod = 1;
300 do_chmod (sf);
302 do {
303 fname = next_file ();
304 if ((sf_stat = pc_stat_file (fname)) < 0)
305 break;
307 c_stat = sf_stat;
308 do_chmod (c_stat);
309 } while (cpanel->marked);
312 void chmod_cmd (void)
314 char buffer [10];
315 char *fname;
316 int i;
317 mode_t sf_stat;
319 do { /* do while any files remaining */
320 init_chmod ();
321 if (cpanel->marked)
322 fname = next_file (); /* next marked file */
323 else
324 fname = selection (cpanel)->fname; /* single file */
326 if ((sf_stat = pc_stat_file (fname)) < 0) /* get status of file */
327 break;
329 c_stat = sf_stat;
330 mode_change = 0; /* clear changes flag */
332 /* set check buttons */
333 for (i = 0; i < PERMISSIONS; i++){
334 check_perm[i].check->state = (c_stat & check_perm[i].mode) ? 1 : 0;
335 check_perm[i].selected = 0;
338 /* Set the labels */
339 c_fname = name_trunc (fname, 21);
340 add_widget (ch_dlg, label_new (FY+2, FX+2, c_fname, NULL));
341 c_fown = _("unknown");
342 add_widget (ch_dlg, label_new (FY+6, FX+2, c_fown, NULL));
343 c_fgrp = _("unknown");
344 add_widget (ch_dlg, label_new (FY+8, FX+2, c_fgrp, NULL));
345 sprintf (buffer, "%o", c_stat);
346 statl = label_new (FY+4, FX+2, buffer, NULL);
347 add_widget (ch_dlg, statl);
349 run_dlg (ch_dlg); /* retrieve an action */
351 /* do action */
352 switch (ch_dlg->ret_value){
353 case B_ENTER:
354 if (mode_change)
355 mk_chmod (fname, c_stat); /*.ado */
356 need_update = 1;
357 break;
359 case B_CANCEL:
360 end_chmod = 1;
361 break;
363 case B_ALL:
364 case B_MARKED:
365 and_mask = or_mask = 0;
366 and_mask = ~and_mask;
368 for (i = 0; i < PERMISSIONS; i++) {
369 if (check_perm[i].selected || ch_dlg->ret_value == B_ALL)
370 if (check_perm[i].check->state & C_BOOL)
371 or_mask |= check_perm[i].mode;
372 else
373 and_mask &= ~check_perm[i].mode;
376 apply_mask (sf_stat);
377 break;
379 case B_SETMRK:
380 and_mask = or_mask = 0;
381 and_mask = ~and_mask;
383 for (i = 0; i < PERMISSIONS; i++) {
384 if (check_perm[i].selected)
385 or_mask |= check_perm[i].mode;
388 apply_mask (sf_stat);
389 break;
390 case B_CLRMRK:
391 and_mask = or_mask = 0;
392 and_mask = ~and_mask;
394 for (i = 0; i < PERMISSIONS; i++) {
395 if (check_perm[i].selected)
396 and_mask &= ~check_perm[i].mode;
399 apply_mask (sf_stat);
400 break;
403 if (cpanel->marked && ch_dlg->ret_value!=B_CANCEL) {
404 do_file_mark (cpanel, c_file, 0);
405 need_update = 1;
407 destroy_dlg (ch_dlg);
408 } while (cpanel->marked && !end_chmod);
409 chmod_done ();