added last 20 new msgs and resized/adjusted many others...
[midnight-commander.git] / nt / chmod.nt.c
blob114aab6302df074eddae3914995c063c80d92fa6
1 /* Chmod command for Windows NT operating system
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., 675 Mass Ave, Cambridge, MA 02139, USA.
17 Notes:
18 - Preliminar. Work to be done here. (Maybe should write a command to
19 change attributes and another to change rwx permissions.
21 #include <config.h>
22 #ifndef _OS_NT
23 #error This file is for the NT operating system.
24 #else
26 #include <windows.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include "tty.h"
30 #include "mad.h"
31 #include "util.h"
32 #include "win.h"
33 #include "color.h"
34 #include "dlg.h"
35 #include "widget.h"
36 #include "dialog.h" /* For do_refresh() */
38 #include "dir.h"
39 #include "panel.h" /* Needed for the externs */
40 #include "file.h"
41 #include "main.h"
42 #include "chmod.h"
43 #include "achown.h"
44 #include "chown.h"
46 static int single_set;
47 struct Dlg_head *ch_dlg;
49 #define PX 5
50 #define PY 2
52 #define FX 40
53 #define FY 2
55 #define BX 6
56 #define BY 17
58 #define TX 40
59 #define TY 12
61 #define PERMISSIONS 4
62 #define BUTTONS 6
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 int mode_change, need_update;
70 int c_file, end_chmod;
72 umode_t and_mask, or_mask, c_stat;
73 char *c_fname, *c_fown, *c_fgrp, *c_fperm;
74 int c_fsize;
76 static WLabel *statl;
77 static int normal_color;
78 static int title_color;
79 static int selection_color;
81 struct {
82 mode_t mode;
83 char *text;
84 int selected;
85 WCheck *check;
86 } check_perm[PERMISSIONS] = {
89 FILE_ATTRIBUTE_ARCHIVE, "Archive", 0, 0,
92 FILE_ATTRIBUTE_READONLY, "Read Only", 0, 0,
95 FILE_ATTRIBUTE_HIDDEN, "Hidden", 0, 0,
98 FILE_ATTRIBUTE_SYSTEM, "System", 0, 0,
100 /* {
101 S_IWGRP, "write by group", 0, 0,
104 S_IRGRP, "read by group", 0, 0,
107 S_IXUSR, "execute/search by owner", 0, 0,
110 S_IWUSR, "write by owner", 0, 0,
113 S_IRUSR, "read by owner", 0, 0,
118 struct {
119 int ret_cmd, flags, y, x;
120 char *text;
121 } chmod_but[BUTTONS] = {
124 B_CANCEL, NORMAL_BUTTON, 2, 33, "&Cancel",
127 B_ENTER, DEFPUSH_BUTTON, 2, 17, "&Set",
130 B_CLRMRK, NORMAL_BUTTON, 0, 42, "C&lear marked",
133 B_SETMRK, NORMAL_BUTTON, 0, 27, "S&et marked",
136 B_MARKED, NORMAL_BUTTON, 0, 12, "&Marked all",
139 B_ALL, NORMAL_BUTTON, 0, 0, "Set &all",
143 static void chmod_toggle_select (void)
145 int Id = ch_dlg->current->dlg_id - BUTTONS + single_set * 2;
147 attrset (normal_color);
148 check_perm[Id].selected ^= 1;
150 dlg_move (ch_dlg, PY + PERMISSIONS - Id, PX + 1);
151 addch ((check_perm[Id].selected) ? '*' : ' ');
152 dlg_move (ch_dlg, PY + PERMISSIONS - Id, PX + 3);
155 static void chmod_refresh (void)
157 attrset (normal_color);
158 dlg_erase (ch_dlg);
160 draw_box (ch_dlg, 1, 2, 20 - single_set, 66);
161 draw_box (ch_dlg, PY, PX, PERMISSIONS + 2, 33);
162 draw_box (ch_dlg, FY, FX, 10, 25);
164 dlg_move (ch_dlg, FY + 1, FX + 2);
165 addstr ("Name");
166 dlg_move (ch_dlg, FY + 3, FX + 2);
167 addstr ("Permissions (Octal)");
168 dlg_move (ch_dlg, FY + 5, FX + 2);
169 addstr ("Owner name");
170 dlg_move (ch_dlg, FY + 7, FX + 2);
171 addstr ("Group name");
173 attrset (title_color);
174 dlg_move (ch_dlg, 1, 28);
175 addstr (" Chmod command ");
176 dlg_move (ch_dlg, PY, PX + 1);
177 addstr (" Permission ");
178 dlg_move (ch_dlg, FY, FX + 1);
179 addstr (" File ");
181 attrset (selection_color);
183 dlg_move (ch_dlg, TY, TX);
184 addstr ("Use SPACE to change");
185 dlg_move (ch_dlg, TY + 1, TX);
186 addstr ("an option, ARROW KEYS");
187 dlg_move (ch_dlg, TY + 2, TX);
188 addstr ("to move between options");
189 dlg_move (ch_dlg, TY + 3, TX);
190 addstr ("and T or INS to mark");
193 static int chmod_callback (Dlg_head *h, int Par, int Msg)
195 char buffer [10];
197 switch (Msg) {
198 case DLG_ACTION:
199 if (Par >= BUTTONS - single_set * 2){
200 c_stat ^= check_perm[Par - BUTTONS + single_set * 2].mode;
201 sprintf (buffer, "%o", c_stat);
202 label_set_text (statl, buffer);
203 chmod_toggle_select ();
204 mode_change = 1;
206 break;
208 case DLG_KEY:
209 if ((Par == 'T' || Par == 't' || Par == KEY_IC) &&
210 ch_dlg->current->dlg_id >= BUTTONS - single_set * 2) {
211 chmod_toggle_select ();
212 if (Par == KEY_IC)
213 dlg_one_down (ch_dlg);
214 return 1;
216 break;
217 #ifndef HAVE_X
218 case DLG_DRAW:
219 chmod_refresh ();
220 break;
221 #endif
223 return 0;
226 static void init_chmod (void)
228 int i;
230 do_refresh ();
231 end_chmod = c_file = need_update = 0;
232 single_set = (cpanel->marked < 2) ? 2 : 0;
234 if (use_colors){
235 normal_color = COLOR_NORMAL;
236 title_color = COLOR_HOT_NORMAL;
237 selection_color = COLOR_NORMAL;
238 } else {
239 normal_color = NORMAL_COLOR;
240 title_color = SELECTED_COLOR;
241 selection_color = SELECTED_COLOR;
244 ch_dlg = create_dlg (0, 0, 22 - single_set, 70, dialog_colors,
245 chmod_callback, "[Chmod]", "chmod", DLG_CENTER);
247 x_set_dialog_title (ch_dlg, "Chmod command");
249 #define XTRACT(i) BY+chmod_but[i].y-single_set, BX+chmod_but[i].x, \
250 chmod_but[i].ret_cmd, chmod_but[i].flags, chmod_but[i].text, 0, 0, NULL
252 tk_new_frame (ch_dlg, "b.");
253 for (i = 0; i < BUTTONS; i++) {
254 if (i == 2 && single_set)
255 break;
256 else
257 add_widgetl (ch_dlg, button_new (XTRACT (i)), XV_WLAY_RIGHTOF);
261 #define XTRACT2(i) 0, check_perm [i].text, NULL
262 tk_new_frame (ch_dlg, "c.");
263 for (i = 0; i < PERMISSIONS; i++) {
264 check_perm[i].check = check_new (PY + (PERMISSIONS - i), PX + 2,
265 XTRACT2 (i));
266 add_widget (ch_dlg, check_perm[i].check);
270 static int stat_file (char *filename, struct stat *st)
272 // Note: - we assume attribute values fit in st.st_mode (a word, not a dword)
273 // - we change st_mode value with attributes, not RWX permissions
275 if (stat (filename, st))
276 return 0;
277 st->st_mode = GetFileAttributes (filename);
279 if (st->st_mode & FILE_ATTRIBUTE_DIRECTORY)
280 return 0;
281 return 1;
284 static void chmod_done (void)
286 if (need_update)
287 update_panels (UP_OPTIMIZE, UP_KEEPSEL, UP_KEEPSEL);
288 repaint_screen ();
291 char *next_file (void)
293 while (!cpanel->dir.list[c_file].f.marked)
294 c_file++;
296 return cpanel->dir.list[c_file].fname;
299 static void do_chmod (struct stat *sf)
301 sf->st_mode &= and_mask;
302 sf->st_mode |= or_mask;
303 SetFileAttributes (cpanel->dir.list [c_file].fname, sf->st_mode);
304 do_file_mark (cpanel, c_file, 0);
307 static void apply_mask (struct stat *sf)
309 char *fname;
311 need_update = end_chmod = 1;
312 do_chmod (sf);
314 do {
315 fname = next_file ();
316 if (!stat_file (fname, sf))
317 return;
318 c_stat = sf->st_mode;
320 do_chmod (sf);
321 } while (cpanel->marked);
324 static int my_chmod (char *path, int mode)
326 SetFileAttributes(path, mode);
327 return (GetFileAttributes(path) != mode);
330 void chmod_cmd (void)
332 char buffer [10];
333 char *fname;
334 int i;
335 struct stat sf_stat;
337 do { /* do while any files remaining */
338 init_chmod ();
339 if (cpanel->marked)
340 fname = next_file (); /* next marked file */
341 else
342 fname = selection (cpanel)->fname; /* single file */
344 if (!stat_file (fname, &sf_stat)) /* get status of file */
345 break;
347 c_stat = sf_stat.st_mode;
348 mode_change = 0; /* clear changes flag */
350 /* set check buttons */
351 for (i = 0; i < PERMISSIONS; i++){
352 check_perm[i].check->state = (c_stat & check_perm[i].mode) ? 1 : 0;
353 check_perm[i].selected = 0;
356 tk_new_frame (ch_dlg, "l.");
357 /* Set the labels */
358 c_fname = name_trunc (fname, 21);
359 add_widget (ch_dlg, label_new (FY+2, FX+2, c_fname, NULL));
360 c_fown = name_trunc (get_owner (sf_stat.st_uid), 21);
361 add_widget (ch_dlg, label_new (FY+6, FX+2, c_fown, NULL));
362 c_fgrp = name_trunc (get_group (sf_stat.st_gid), 21);
363 add_widget (ch_dlg, label_new (FY+8, FX+2, c_fgrp, NULL));
364 sprintf (buffer, "%o", c_stat);
365 statl = label_new (FY+4, FX+2, buffer, NULL);
366 add_widget (ch_dlg, statl);
367 tk_end_frame ();
369 run_dlg (ch_dlg); /* retrieve an action */
371 /* do action */
372 switch (ch_dlg->ret_value){
373 case B_ENTER:
374 if (mode_change)
375 if (my_chmod (fname, c_stat))
376 message(1, "Chmod", "Change mode failed!");
377 need_update = 1;
378 break;
380 case B_CANCEL:
381 end_chmod = 1;
382 break;
384 case B_ALL:
385 case B_MARKED:
386 and_mask = or_mask = 0;
387 and_mask = ~and_mask;
389 for (i = 0; i < PERMISSIONS; i++) {
390 if (check_perm[i].selected || ch_dlg->ret_value == B_ALL)
391 if (check_perm[i].check->state & C_BOOL)
392 or_mask |= check_perm[i].mode;
393 else
394 and_mask &= ~check_perm[i].mode;
397 apply_mask (&sf_stat);
398 break;
400 case B_SETMRK:
401 and_mask = or_mask = 0;
402 and_mask = ~and_mask;
404 for (i = 0; i < PERMISSIONS; i++) {
405 if (check_perm[i].selected)
406 or_mask |= check_perm[i].mode;
409 apply_mask (&sf_stat);
410 break;
411 case B_CLRMRK:
412 and_mask = or_mask = 0;
413 and_mask = ~and_mask;
415 for (i = 0; i < PERMISSIONS; i++) {
416 if (check_perm[i].selected)
417 and_mask &= ~check_perm[i].mode;
420 apply_mask (&sf_stat);
421 break;
424 if (cpanel->marked && ch_dlg->ret_value!=B_CANCEL) {
425 do_file_mark (cpanel, c_file, 0);
426 need_update = 1;
428 destroy_dlg (ch_dlg);
429 } while (cpanel->marked && !end_chmod);
430 chmod_done ();
433 #endif /*_OS_NT*/