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.
25 /* for chmod and stat */
27 #include <sys/types.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
;
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
;
85 static int normal_color
;
86 static int title_color
;
87 static int selection_color
;
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,
112 int ret_cmd
, flags
, y
, x
;
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
);
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);
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
)
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 ();
202 if ((Par
== 'T' || Par
== 't' || Par
== KEY_IC
) &&
203 ch_dlg
->current
->dlg_id
>= BUTTONS
- single_set
* 2) {
204 chmod_toggle_select ();
206 dlg_one_down (ch_dlg
);
217 static void init_chmod (void)
222 end_chmod
= c_file
= need_update
= 0;
223 single_set
= (cpanel
->marked
< 2) ? 2 : 0;
226 normal_color
= COLOR_NORMAL
;
227 title_color
= COLOR_HOT_NORMAL
;
228 selection_color
= COLOR_NORMAL
;
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
)
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,
255 add_widget (ch_dlg
, check_perm
[i
].check
);
259 int pc_stat_file (char *filename
)
263 st
= GetFileAttributes (filename
);
264 if (st
& FILE_DIRECTORY
)
269 static void chmod_done (void)
272 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
276 char *next_file (void)
278 while (!cpanel
->dir
.list
[c_file
].f
.marked
)
281 return cpanel
->dir
.list
[c_file
].fname
;
284 static void do_chmod (mode_t sf
)
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
)
299 need_update
= end_chmod
= 1;
303 fname
= next_file ();
304 if ((sf_stat
= pc_stat_file (fname
)) < 0)
309 } while (cpanel
->marked
);
312 void chmod_cmd (void)
319 do { /* do while any files remaining */
322 fname
= next_file (); /* next marked file */
324 fname
= selection (cpanel
)->fname
; /* single file */
326 if ((sf_stat
= pc_stat_file (fname
)) < 0) /* get status of file */
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;
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 */
352 switch (ch_dlg
->ret_value
){
355 mk_chmod (fname
, c_stat
); /*.ado */
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
;
373 and_mask
&= ~check_perm
[i
].mode
;
376 apply_mask (sf_stat
);
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
);
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
);
403 if (cpanel
->marked
&& ch_dlg
->ret_value
!=B_CANCEL
) {
404 do_file_mark (cpanel
, c_file
, 0);
407 destroy_dlg (ch_dlg
);
408 } while (cpanel
->marked
&& !end_chmod
);