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.
18 - Preliminar. Work to be done here. (Maybe should write a command to
19 change attributes and another to change rwx permissions.
23 #error This file is for the NT operating system.
36 #include "dialog.h" /* For do_refresh() */
39 #include "panel.h" /* Needed for the externs */
46 static int single_set
;
47 struct Dlg_head
*ch_dlg
;
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
;
77 static int normal_color
;
78 static int title_color
;
79 static int selection_color
;
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,
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,
119 int ret_cmd
, flags
, y
, x
;
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
);
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);
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);
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
)
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 ();
209 if ((Par
== 'T' || Par
== 't' || Par
== KEY_IC
) &&
210 ch_dlg
->current
->dlg_id
>= BUTTONS
- single_set
* 2) {
211 chmod_toggle_select ();
213 dlg_one_down (ch_dlg
);
226 static void init_chmod (void)
231 end_chmod
= c_file
= need_update
= 0;
232 single_set
= (cpanel
->marked
< 2) ? 2 : 0;
235 normal_color
= COLOR_NORMAL
;
236 title_color
= COLOR_HOT_NORMAL
;
237 selection_color
= COLOR_NORMAL
;
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
)
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,
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
))
277 st
->st_mode
= GetFileAttributes (filename
);
279 if (st
->st_mode
& FILE_ATTRIBUTE_DIRECTORY
)
284 static void chmod_done (void)
287 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
, UP_KEEPSEL
);
291 char *next_file (void)
293 while (!cpanel
->dir
.list
[c_file
].f
.marked
)
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
)
311 need_update
= end_chmod
= 1;
315 fname
= next_file ();
316 if (!stat_file (fname
, sf
))
318 c_stat
= sf
->st_mode
;
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)
337 do { /* do while any files remaining */
340 fname
= next_file (); /* next marked file */
342 fname
= selection (cpanel
)->fname
; /* single file */
344 if (!stat_file (fname
, &sf_stat
)) /* get status of file */
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.");
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
);
369 run_dlg (ch_dlg
); /* retrieve an action */
372 switch (ch_dlg
->ret_value
){
375 if (my_chmod (fname
, c_stat
))
376 message(1, "Chmod", "Change mode failed!");
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
;
394 and_mask
&= ~check_perm
[i
].mode
;
397 apply_mask (&sf_stat
);
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
);
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
);
424 if (cpanel
->marked
&& ch_dlg
->ret_value
!=B_CANCEL
) {
425 do_file_mark (cpanel
, c_file
, 0);
428 destroy_dlg (ch_dlg
);
429 } while (cpanel
->marked
&& !end_chmod
);