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
86 int mode_change
, need_update
;
87 int c_file
, end_chmod
;
89 umode_t and_mask
, or_mask
, c_stat
;
90 char *c_fname
, *c_fown
, *c_fgrp
, *c_fperm
;
94 static int normal_color
;
95 static int title_color
;
96 static int selection_color
;
104 } check_perm
[PERMISSIONS
] = {
107 FILE_ARCHIVED
, N_("Archive"), 0, 0,
110 FILE_READONLY
, N_("Read Only"), 0, 0,
113 FILE_HIDDEN
, N_("Hidden"), 0, 0,
116 FILE_SYSTEM
, N_("System"), 0, 0,
121 int ret_cmd
, flags
, y
, x
;
123 } chmod_but
[BUTTONS
] = {
126 B_CANCEL
, NORMAL_BUTTON
, 2, 33, N_("&Cancel"),
129 B_ENTER
, DEFPUSH_BUTTON
, 2, 17, N_("&Set"),
132 B_CLRMRK
, NORMAL_BUTTON
, 0, 42, N_("C&lear marked"),
135 B_SETMRK
, NORMAL_BUTTON
, 0, 27, N_("S&et marked"),
138 B_MARKED
, NORMAL_BUTTON
, 0, 12, N_("&Marked all"),
141 B_ALL
, NORMAL_BUTTON
, 0, 0, N_("Set &all"),
145 static void chmod_toggle_select (void)
147 int Id
= ch_dlg
->current
->dlg_id
- BUTTONS
+ single_set
* 2;
149 attrset (normal_color
);
150 check_perm
[Id
].selected
^= 1;
152 dlg_move (ch_dlg
, PY
+ PERMISSIONS
- Id
, PX
+ 1);
153 addch ((check_perm
[Id
].selected
) ? '*' : ' ');
154 dlg_move (ch_dlg
, PY
+ PERMISSIONS
- Id
, PX
+ 3);
157 static void chmod_refresh (void)
159 attrset (normal_color
);
162 draw_box (ch_dlg
, 1, 2, 20 - single_set
, 66);
163 draw_box (ch_dlg
, PY
, PX
, PERMISSIONS
+ 2, 33);
164 draw_box (ch_dlg
, FY
, FX
, 10, 25);
166 dlg_move (ch_dlg
, FY
+ 1, FX
+ 2);
168 dlg_move (ch_dlg
, FY
+ 3, FX
+ 2);
169 addstr (_("Permissions (Octal)"));
170 dlg_move (ch_dlg
, FY
+ 5, FX
+ 2);
171 addstr (_("Owner name"));
172 dlg_move (ch_dlg
, FY
+ 7, FX
+ 2);
173 addstr (_("Group name"));
175 attrset (title_color
);
176 dlg_move (ch_dlg
, 1, 28);
177 addstr (_(" Chmod command "));
178 dlg_move (ch_dlg
, PY
, PX
+ 1);
179 addstr (_(" Permission "));
180 dlg_move (ch_dlg
, FY
, FX
+ 1);
181 addstr (_(" File "));
183 attrset (selection_color
);
185 dlg_move (ch_dlg
, TY
, TX
);
186 addstr (_("Use SPACE to change"));
187 dlg_move (ch_dlg
, TY
+ 1, TX
);
188 addstr (_("an option, ARROW KEYS"));
189 dlg_move (ch_dlg
, TY
+ 2, TX
);
190 addstr (_("to move between options"));
191 dlg_move (ch_dlg
, TY
+ 3, TX
);
192 addstr (_("and T or INS to mark"));
195 static int chmod_callback (Dlg_head
*h
, int Par
, int Msg
)
201 if (Par
>= BUTTONS
- single_set
* 2){
202 c_stat
^= check_perm
[Par
- BUTTONS
+ single_set
* 2].mode
;
203 sprintf (buffer
, "%o", c_stat
);
204 label_set_text (statl
, buffer
);
205 chmod_toggle_select ();
211 if ((Par
== 'T' || Par
== 't' || Par
== KEY_IC
) &&
212 ch_dlg
->current
->dlg_id
>= BUTTONS
- single_set
* 2) {
213 chmod_toggle_select ();
215 dlg_one_down (ch_dlg
);
228 static void init_chmod (void)
233 end_chmod
= c_file
= need_update
= 0;
234 single_set
= (cpanel
->marked
< 2) ? 2 : 0;
237 normal_color
= COLOR_NORMAL
;
238 title_color
= COLOR_HOT_NORMAL
;
239 selection_color
= COLOR_NORMAL
;
241 normal_color
= NORMAL_COLOR
;
242 title_color
= SELECTED_COLOR
;
243 selection_color
= SELECTED_COLOR
;
246 ch_dlg
= create_dlg (0, 0, 22 - single_set
, 70, dialog_colors
,
247 chmod_callback
, _("[Chmod]"), _("chmod"), DLG_CENTER
);
249 x_set_dialog_title (ch_dlg
, _("Chmod command"));
251 #define XTRACT(i) BY+chmod_but[i].y-single_set, BX+chmod_but[i].x, \
252 chmod_but[i].ret_cmd, chmod_but[i].flags, chmod_but[i].text, 0, 0, NULL
254 tk_new_frame (ch_dlg
, "b.");
255 for (i
= 0; i
< BUTTONS
; i
++) {
256 if (i
== 2 && single_set
)
259 add_widgetl (ch_dlg
, button_new (XTRACT (i
)), XV_WLAY_RIGHTOF
);
263 #define XTRACT2(i) 0, check_perm [i].text, NULL
264 tk_new_frame (ch_dlg
, "c.");
265 for (i
= 0; i
< PERMISSIONS
; i
++) {
266 check_perm
[i
].check
= check_new (PY
+ (PERMISSIONS
- i
), PX
+ 2,
268 add_widget (ch_dlg
, check_perm
[i
].check
);
272 int pc_stat_file (char *filename
)
277 st
= GetFileAttributes (filename
);
282 ULONG fInfoLevel
= 1; /* 1st Level Info: Standard attributs */
283 FILESTATUS3 fInfoBuf
;
288 fInfoBufSize
= sizeof(FILESTATUS3
);
289 rc
= DosOpen((PSZ
) filename
,
294 OPEN_ACTION_OPEN_IF_EXISTS
,
295 (OPEN_ACCESS_READONLY
| OPEN_SHARE_DENYNONE
),
301 rc
= DosQueryFileInfo(fHandle
, fInfoLevel
, &fInfoBuf
, fInfoBufSize
);
304 return -1; /* error ! */
306 st
= fInfoBuf
.attrFile
;
310 if (st
& FILE_DIRECTORY
)
315 static void chmod_done (void)
318 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
322 char *next_file (void)
324 while (!cpanel
->dir
.list
[c_file
].f
.marked
)
327 return cpanel
->dir
.list
[c_file
].fname
;
331 static int mk_chmod (char *filename
, ULONG st
)
334 ULONG fInfoLevel
= 1; /* 1st Level Info: Standard attributs */
335 FILESTATUS3 fInfoBuf
;
340 if (!(st
& FILE_READONLY
))
341 chmod(filename
, (S_IWRITE
| S_IREAD
));
342 fInfoBufSize
= sizeof(FILESTATUS3
);
343 rc
= DosOpen((PSZ
) filename
,
348 OPEN_ACTION_OPEN_IF_EXISTS
,
349 (OPEN_ACCESS_READWRITE
| OPEN_SHARE_DENYNONE
),
355 rc
= DosQueryFileInfo(fHandle
, fInfoLevel
, &fInfoBuf
, fInfoBufSize
);
360 fInfoBuf
.attrFile
= st
;
361 rc
= DosSetFileInfo(fHandle
, fInfoLevel
, &fInfoBuf
, fInfoBufSize
);
362 rc
= DosClose(fHandle
);
367 static void do_chmod (mode_t sf
)
372 mk_chmod(cpanel
->dir
.list
[c_file
].fname
, sf
);
374 do_file_mark (cpanel
, c_file
, 0);
377 static void apply_mask (mode_t sf
)
382 need_update
= end_chmod
= 1;
386 fname
= next_file ();
387 if ((sf_stat
= pc_stat_file (fname
)) < 0)
390 c_stat
= (mode_t
) sf_stat
;
392 } while (cpanel
->marked
);
395 void chmod_cmd (void)
402 do { /* do while any files remaining */
405 fname
= next_file (); /* next marked file */
407 fname
= selection (cpanel
)->fname
; /* single file */
409 if ((sf_stat
= pc_stat_file (fname
)) < 0) /* get status of file */
412 c_stat
= (mode_t
) sf_stat
;
413 mode_change
= 0; /* clear changes flag */
415 /* set check buttons */
416 for (i
= 0; i
< PERMISSIONS
; i
++){
417 check_perm
[i
].check
->state
= (c_stat
& check_perm
[i
].mode
) ? 1 : 0;
418 check_perm
[i
].selected
= 0;
421 tk_new_frame (ch_dlg
, "l.");
423 c_fname
= name_trunc (fname
, 21);
424 add_widget (ch_dlg
, label_new (FY
+2, FX
+2, c_fname
, NULL
));
425 c_fown
= _("unknown");
426 add_widget (ch_dlg
, label_new (FY
+6, FX
+2, c_fown
, NULL
));
427 c_fgrp
= _("unknown");
428 add_widget (ch_dlg
, label_new (FY
+8, FX
+2, c_fgrp
, NULL
));
429 sprintf (buffer
, "%o", c_stat
);
430 statl
= label_new (FY
+4, FX
+2, buffer
, NULL
);
431 add_widget (ch_dlg
, statl
);
434 run_dlg (ch_dlg
); /* retrieve an action */
437 switch (ch_dlg
->ret_value
){
440 mk_chmod (fname
, c_stat
); /*.ado */
450 and_mask
= or_mask
= 0;
451 and_mask
= ~and_mask
;
453 for (i
= 0; i
< PERMISSIONS
; i
++) {
454 if (check_perm
[i
].selected
|| ch_dlg
->ret_value
== B_ALL
)
455 if (check_perm
[i
].check
->state
& C_BOOL
)
456 or_mask
|= check_perm
[i
].mode
;
458 and_mask
&= ~check_perm
[i
].mode
;
461 apply_mask (sf_stat
);
465 and_mask
= or_mask
= 0;
466 and_mask
= ~and_mask
;
468 for (i
= 0; i
< PERMISSIONS
; i
++) {
469 if (check_perm
[i
].selected
)
470 or_mask
|= check_perm
[i
].mode
;
473 apply_mask (sf_stat
);
476 and_mask
= or_mask
= 0;
477 and_mask
= ~and_mask
;
479 for (i
= 0; i
< PERMISSIONS
; i
++) {
480 if (check_perm
[i
].selected
)
481 and_mask
&= ~check_perm
[i
].mode
;
484 apply_mask (sf_stat
);
488 if (cpanel
->marked
&& ch_dlg
->ret_value
!=B_CANCEL
) {
489 do_file_mark (cpanel
, c_file
, 0);
492 destroy_dlg (ch_dlg
);
493 } while (cpanel
->marked
&& !end_chmod
);