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.
23 /* for chmod and stat */
25 #include <sys/types.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
;
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
;
83 static int normal_color
;
84 static int title_color
;
85 static int selection_color
;
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,
110 int ret_cmd
, flags
, y
, x
;
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
);
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);
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);
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
)
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 ();
200 if ((Par
== 'T' || Par
== 't' || Par
== KEY_IC
) &&
201 ch_dlg
->current
->dlg_id
>= BUTTONS
- single_set
* 2) {
202 chmod_toggle_select ();
204 dlg_one_down (ch_dlg
);
215 static void init_chmod (void)
220 end_chmod
= c_file
= need_update
= 0;
221 single_set
= (cpanel
->marked
< 2) ? 2 : 0;
224 normal_color
= COLOR_NORMAL
;
225 title_color
= COLOR_HOT_NORMAL
;
226 selection_color
= COLOR_NORMAL
;
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
)
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,
251 add_widget (ch_dlg
, check_perm
[i
].check
);
255 int pc_stat_file (char *filename
)
259 st
= GetFileAttributes (filename
);
260 if (st
& FILE_DIRECTORY
)
265 static void chmod_done (void)
268 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
272 char *next_file (void)
274 while (!cpanel
->dir
.list
[c_file
].f
.marked
)
277 return cpanel
->dir
.list
[c_file
].fname
;
280 static void do_chmod (mode_t sf
)
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
)
295 need_update
= end_chmod
= 1;
299 fname
= next_file ();
300 if ((sf_stat
= pc_stat_file (fname
)) < 0)
305 } while (cpanel
->marked
);
308 void chmod_cmd (void)
315 do { /* do while any files remaining */
318 fname
= next_file (); /* next marked file */
320 fname
= selection (cpanel
)->fname
; /* single file */
322 if ((sf_stat
= pc_stat_file (fname
)) < 0) /* get status of file */
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;
335 c_fname
= name_trunc (fname
, 21);
336 add_widget (ch_dlg
, label_new (FY
+2, FX
+2, c_fname
, NULL
));
338 add_widget (ch_dlg
, label_new (FY
+6, FX
+2, c_fown
, NULL
));
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 */
348 switch (ch_dlg
->ret_value
){
351 mk_chmod (fname
, c_stat
); /*.ado */
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
;
369 and_mask
&= ~check_perm
[i
].mode
;
372 apply_mask (sf_stat
);
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
);
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
);
399 if (cpanel
->marked
&& ch_dlg
->ret_value
!=B_CANCEL
) {
400 do_file_mark (cpanel
, c_file
, 0);
403 destroy_dlg (ch_dlg
);
404 } while (cpanel
->marked
&& !end_chmod
);