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., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define INCL_DOSFILEMGR
32 /* for chmod and stat */
34 #include <sys\types.h>
36 #include "../src/tty.h"
37 #include "../src/mad.h"
38 #include "../src/util.h"
39 #include "../src/win.h"
40 #include "../src/color.h"
41 #include "../src/dlg.h"
42 #include "../src/widget.h"
43 #include "../src/dialog.h" /* For do_refresh() */
45 #include "../src/dir.h"
46 #include "../src/panel.h" /* Needed for the externs */
47 #include "../src/file.h"
48 #include "../src/main.h"
49 #include "../src/chmod.h"
50 #include "../src/achown.h"
51 #include "../src/chown.h"
54 #define FILE_ARCHIVED FILE_ATTRIBUTE_ARCHIVE
55 #define FILE_DIRECTORY FILE_ATTRIBUTE_DIRECTORY
56 #define FILE_HIDDEN FILE_ATTRIBUTE_HIDDEN
57 #define FILE_READONLY FILE_ATTRIBUTE_READONLY
58 #define FILE_SYSTEM FILE_ATTRIBUTE_SYSTEM
59 #define mk_chmod(fname,st) SetFileAttributes(fname,st)
62 static int single_set
;
63 struct Dlg_head
*ch_dlg
;
80 #define B_MARKED B_USER
81 #define B_ALL B_USER+1
82 #define B_SETMRK B_USER+2
83 #define B_CLRMRK B_USER+3
85 int mode_change
, need_update
;
86 int c_file
, end_chmod
;
88 umode_t and_mask
, or_mask
, c_stat
;
89 char *c_fname
, *c_fown
, *c_fgrp
, *c_fperm
;
93 static int normal_color
;
94 static int title_color
;
95 static int selection_color
;
103 } check_perm
[PERMISSIONS
] = {
106 FILE_ARCHIVED
, "Archive", 0, 0,
109 FILE_READONLY
, "Read Only", 0, 0,
112 FILE_HIDDEN
, "Hidden", 0, 0,
115 FILE_SYSTEM
, "System", 0, 0,
120 int ret_cmd
, flags
, y
, x
;
122 } chmod_but
[BUTTONS
] = {
125 B_CANCEL
, NORMAL_BUTTON
, 2, 33, "&Cancel",
128 B_ENTER
, DEFPUSH_BUTTON
, 2, 17, "&Set",
131 B_CLRMRK
, NORMAL_BUTTON
, 0, 42, "C&lear marked",
134 B_SETMRK
, NORMAL_BUTTON
, 0, 27, "S&et marked",
137 B_MARKED
, NORMAL_BUTTON
, 0, 12, "&Marked all",
140 B_ALL
, NORMAL_BUTTON
, 0, 0, "Set &all",
144 static void chmod_toggle_select (void)
146 int Id
= ch_dlg
->current
->dlg_id
- BUTTONS
+ single_set
* 2;
148 attrset (normal_color
);
149 check_perm
[Id
].selected
^= 1;
151 dlg_move (ch_dlg
, PY
+ PERMISSIONS
- Id
, PX
+ 1);
152 addch ((check_perm
[Id
].selected
) ? '*' : ' ');
153 dlg_move (ch_dlg
, PY
+ PERMISSIONS
- Id
, PX
+ 3);
156 static void chmod_refresh (void)
158 attrset (normal_color
);
161 draw_box (ch_dlg
, 1, 2, 20 - single_set
, 66);
162 draw_box (ch_dlg
, PY
, PX
, PERMISSIONS
+ 2, 33);
163 draw_box (ch_dlg
, FY
, FX
, 10, 25);
165 dlg_move (ch_dlg
, FY
+ 1, FX
+ 2);
167 dlg_move (ch_dlg
, FY
+ 3, FX
+ 2);
168 addstr ("Permissions (Octal)");
169 dlg_move (ch_dlg
, FY
+ 5, FX
+ 2);
170 addstr ("Owner name");
171 dlg_move (ch_dlg
, FY
+ 7, FX
+ 2);
172 addstr ("Group name");
174 attrset (title_color
);
175 dlg_move (ch_dlg
, 1, 28);
176 addstr (" Chmod command ");
177 dlg_move (ch_dlg
, PY
, PX
+ 1);
178 addstr (" Permission ");
179 dlg_move (ch_dlg
, FY
, FX
+ 1);
182 attrset (selection_color
);
184 dlg_move (ch_dlg
, TY
, TX
);
185 addstr ("Use SPACE to change");
186 dlg_move (ch_dlg
, TY
+ 1, TX
);
187 addstr ("an option, ARROW KEYS");
188 dlg_move (ch_dlg
, TY
+ 2, TX
);
189 addstr ("to move between options");
190 dlg_move (ch_dlg
, TY
+ 3, TX
);
191 addstr ("and T or INS to mark");
194 static int chmod_callback (Dlg_head
*h
, int Par
, int Msg
)
200 if (Par
>= BUTTONS
- single_set
* 2){
201 c_stat
^= check_perm
[Par
- BUTTONS
+ single_set
* 2].mode
;
202 sprintf (buffer
, "%o", c_stat
);
203 label_set_text (statl
, buffer
);
204 chmod_toggle_select ();
210 if ((Par
== 'T' || Par
== 't' || Par
== KEY_IC
) &&
211 ch_dlg
->current
->dlg_id
>= BUTTONS
- single_set
* 2) {
212 chmod_toggle_select ();
214 dlg_one_down (ch_dlg
);
227 static void init_chmod (void)
232 end_chmod
= c_file
= need_update
= 0;
233 single_set
= (cpanel
->marked
< 2) ? 2 : 0;
236 normal_color
= COLOR_NORMAL
;
237 title_color
= COLOR_HOT_NORMAL
;
238 selection_color
= COLOR_NORMAL
;
240 normal_color
= NORMAL_COLOR
;
241 title_color
= SELECTED_COLOR
;
242 selection_color
= SELECTED_COLOR
;
245 ch_dlg
= create_dlg (0, 0, 22 - single_set
, 70, dialog_colors
,
246 chmod_callback
, "[Chmod]", "chmod", DLG_CENTER
);
248 x_set_dialog_title (ch_dlg
, "Chmod command");
250 #define XTRACT(i) BY+chmod_but[i].y-single_set, BX+chmod_but[i].x, \
251 chmod_but[i].ret_cmd, chmod_but[i].flags, chmod_but[i].text, 0, 0, NULL
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 for (i
= 0; i
< PERMISSIONS
; i
++) {
263 check_perm
[i
].check
= check_new (PY
+ (PERMISSIONS
- i
), PX
+ 2,
265 add_widget (ch_dlg
, check_perm
[i
].check
);
269 int pc_stat_file (char *filename
)
274 st
= GetFileAttributes (filename
);
279 ULONG fInfoLevel
= 1; /* 1st Level Info: Standard attributs */
280 FILESTATUS3 fInfoBuf
;
285 fInfoBufSize
= sizeof(FILESTATUS3
);
286 rc
= DosOpen((PSZ
) filename
,
291 OPEN_ACTION_OPEN_IF_EXISTS
,
292 (OPEN_ACCESS_READONLY
| OPEN_SHARE_DENYNONE
),
298 rc
= DosQueryFileInfo(fHandle
, fInfoLevel
, &fInfoBuf
, fInfoBufSize
);
301 return -1; /* error ! */
303 st
= fInfoBuf
.attrFile
;
307 if (st
& FILE_DIRECTORY
)
312 static void chmod_done (void)
315 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
319 char *next_file (void)
321 while (!cpanel
->dir
.list
[c_file
].f
.marked
)
324 return cpanel
->dir
.list
[c_file
].fname
;
328 static int mk_chmod (char *filename
, ULONG st
)
331 ULONG fInfoLevel
= 1; /* 1st Level Info: Standard attributs */
332 FILESTATUS3 fInfoBuf
;
337 if (!(st
& FILE_READONLY
))
338 chmod(filename
, (S_IWRITE
| S_IREAD
));
339 fInfoBufSize
= sizeof(FILESTATUS3
);
340 rc
= DosOpen((PSZ
) filename
,
345 OPEN_ACTION_OPEN_IF_EXISTS
,
346 (OPEN_ACCESS_READWRITE
| OPEN_SHARE_DENYNONE
),
352 rc
= DosQueryFileInfo(fHandle
, fInfoLevel
, &fInfoBuf
, fInfoBufSize
);
357 fInfoBuf
.attrFile
= st
;
358 rc
= DosSetFileInfo(fHandle
, fInfoLevel
, &fInfoBuf
, fInfoBufSize
);
359 rc
= DosClose(fHandle
);
364 static void do_chmod (mode_t sf
)
369 mk_chmod(cpanel
->dir
.list
[c_file
].fname
, sf
);
371 do_file_mark (cpanel
, c_file
, 0);
374 static void apply_mask (mode_t sf
)
379 need_update
= end_chmod
= 1;
383 fname
= next_file ();
384 if ((sf_stat
= pc_stat_file (fname
)) < 0)
389 } while (cpanel
->marked
);
392 void chmod_cmd (void)
399 do { /* do while any files remaining */
402 fname
= next_file (); /* next marked file */
404 fname
= selection (cpanel
)->fname
; /* single file */
406 if ((sf_stat
= pc_stat_file (fname
)) < 0) /* get status of file */
410 mode_change
= 0; /* clear changes flag */
412 /* set check buttons */
413 for (i
= 0; i
< PERMISSIONS
; i
++){
414 check_perm
[i
].check
->state
= (c_stat
& check_perm
[i
].mode
) ? 1 : 0;
415 check_perm
[i
].selected
= 0;
419 c_fname
= name_trunc (fname
, 21);
420 add_widget (ch_dlg
, label_new (FY
+2, FX
+2, c_fname
, NULL
));
422 add_widget (ch_dlg
, label_new (FY
+6, FX
+2, c_fown
, NULL
));
424 add_widget (ch_dlg
, label_new (FY
+8, FX
+2, c_fgrp
, NULL
));
425 sprintf (buffer
, "%o", c_stat
);
426 statl
= label_new (FY
+4, FX
+2, buffer
, NULL
);
427 add_widget (ch_dlg
, statl
);
429 run_dlg (ch_dlg
); /* retrieve an action */
432 switch (ch_dlg
->ret_value
){
435 mk_chmod (fname
, c_stat
); /*.ado */
445 and_mask
= or_mask
= 0;
446 and_mask
= ~and_mask
;
448 for (i
= 0; i
< PERMISSIONS
; i
++) {
449 if (check_perm
[i
].selected
|| ch_dlg
->ret_value
== B_ALL
)
450 if (check_perm
[i
].check
->state
& C_BOOL
)
451 or_mask
|= check_perm
[i
].mode
;
453 and_mask
&= ~check_perm
[i
].mode
;
456 apply_mask (sf_stat
);
460 and_mask
= or_mask
= 0;
461 and_mask
= ~and_mask
;
463 for (i
= 0; i
< PERMISSIONS
; i
++) {
464 if (check_perm
[i
].selected
)
465 or_mask
|= check_perm
[i
].mode
;
468 apply_mask (sf_stat
);
471 and_mask
= or_mask
= 0;
472 and_mask
= ~and_mask
;
474 for (i
= 0; i
< PERMISSIONS
; i
++) {
475 if (check_perm
[i
].selected
)
476 and_mask
&= ~check_perm
[i
].mode
;
479 apply_mask (sf_stat
);
483 if (cpanel
->marked
&& ch_dlg
->ret_value
!=B_CANCEL
) {
484 do_file_mark (cpanel
, c_file
, 0);
487 destroy_dlg (ch_dlg
);
488 } while (cpanel
->marked
&& !end_chmod
);