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.
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
);
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 for (i
= 0; i
< BUTTONS
; i
++) {
253 if (i
== 2 && single_set
)
256 add_widget (ch_dlg
, button_new (XTRACT (i
)));
260 #define XTRACT2(i) 0, check_perm [i].text, NULL
261 for (i
= 0; i
< PERMISSIONS
; i
++) {
262 check_perm
[i
].check
= check_new (PY
+ (PERMISSIONS
- i
), PX
+ 2,
264 add_widget (ch_dlg
, check_perm
[i
].check
);
268 int pc_stat_file (char *filename
)
273 st
= GetFileAttributes (filename
);
278 ULONG fInfoLevel
= 1; /* 1st Level Info: Standard attributs */
279 FILESTATUS3 fInfoBuf
;
284 fInfoBufSize
= sizeof(FILESTATUS3
);
285 rc
= DosOpen((PSZ
) filename
,
290 OPEN_ACTION_OPEN_IF_EXISTS
,
291 (OPEN_ACCESS_READONLY
| OPEN_SHARE_DENYNONE
),
297 rc
= DosQueryFileInfo(fHandle
, fInfoLevel
, &fInfoBuf
, fInfoBufSize
);
300 return -1; /* error ! */
302 st
= fInfoBuf
.attrFile
;
306 if (st
& FILE_DIRECTORY
)
311 static void chmod_done (void)
314 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
318 char *next_file (void)
320 while (!cpanel
->dir
.list
[c_file
].f
.marked
)
323 return cpanel
->dir
.list
[c_file
].fname
;
327 static int mk_chmod (char *filename
, ULONG st
)
330 ULONG fInfoLevel
= 1; /* 1st Level Info: Standard attributs */
331 FILESTATUS3 fInfoBuf
;
336 if (!(st
& FILE_READONLY
))
337 chmod(filename
, (S_IWRITE
| S_IREAD
));
338 fInfoBufSize
= sizeof(FILESTATUS3
);
339 rc
= DosOpen((PSZ
) filename
,
344 OPEN_ACTION_OPEN_IF_EXISTS
,
345 (OPEN_ACCESS_READWRITE
| OPEN_SHARE_DENYNONE
),
351 rc
= DosQueryFileInfo(fHandle
, fInfoLevel
, &fInfoBuf
, fInfoBufSize
);
356 fInfoBuf
.attrFile
= st
;
357 rc
= DosSetFileInfo(fHandle
, fInfoLevel
, &fInfoBuf
, fInfoBufSize
);
358 rc
= DosClose(fHandle
);
363 static void do_chmod (mode_t sf
)
368 mk_chmod(cpanel
->dir
.list
[c_file
].fname
, sf
);
370 do_file_mark (cpanel
, c_file
, 0);
373 static void apply_mask (mode_t sf
)
378 need_update
= end_chmod
= 1;
382 fname
= next_file ();
383 if ((sf_stat
= pc_stat_file (fname
)) < 0)
388 } while (cpanel
->marked
);
391 void chmod_cmd (void)
398 do { /* do while any files remaining */
401 fname
= next_file (); /* next marked file */
403 fname
= selection (cpanel
)->fname
; /* single file */
405 if ((sf_stat
= pc_stat_file (fname
)) < 0) /* get status of file */
409 mode_change
= 0; /* clear changes flag */
411 /* set check buttons */
412 for (i
= 0; i
< PERMISSIONS
; i
++){
413 check_perm
[i
].check
->state
= (c_stat
& check_perm
[i
].mode
) ? 1 : 0;
414 check_perm
[i
].selected
= 0;
418 c_fname
= name_trunc (fname
, 21);
419 add_widget (ch_dlg
, label_new (FY
+2, FX
+2, c_fname
, NULL
));
420 c_fown
= _("unknown");
421 add_widget (ch_dlg
, label_new (FY
+6, FX
+2, c_fown
, NULL
));
422 c_fgrp
= _("unknown");
423 add_widget (ch_dlg
, label_new (FY
+8, FX
+2, c_fgrp
, NULL
));
424 sprintf (buffer
, "%o", c_stat
);
425 statl
= label_new (FY
+4, FX
+2, buffer
, NULL
);
426 add_widget (ch_dlg
, statl
);
428 run_dlg (ch_dlg
); /* retrieve an action */
431 switch (ch_dlg
->ret_value
){
434 mk_chmod (fname
, c_stat
); /*.ado */
444 and_mask
= or_mask
= 0;
445 and_mask
= ~and_mask
;
447 for (i
= 0; i
< PERMISSIONS
; i
++) {
448 if (check_perm
[i
].selected
|| ch_dlg
->ret_value
== B_ALL
)
449 if (check_perm
[i
].check
->state
& C_BOOL
)
450 or_mask
|= check_perm
[i
].mode
;
452 and_mask
&= ~check_perm
[i
].mode
;
455 apply_mask (sf_stat
);
459 and_mask
= or_mask
= 0;
460 and_mask
= ~and_mask
;
462 for (i
= 0; i
< PERMISSIONS
; i
++) {
463 if (check_perm
[i
].selected
)
464 or_mask
|= check_perm
[i
].mode
;
467 apply_mask (sf_stat
);
470 and_mask
= or_mask
= 0;
471 and_mask
= ~and_mask
;
473 for (i
= 0; i
< PERMISSIONS
; i
++) {
474 if (check_perm
[i
].selected
)
475 and_mask
&= ~check_perm
[i
].mode
;
478 apply_mask (sf_stat
);
482 if (cpanel
->marked
&& ch_dlg
->ret_value
!=B_CANCEL
) {
483 do_file_mark (cpanel
, c_file
, 0);
486 destroy_dlg (ch_dlg
);
487 } while (cpanel
->marked
&& !end_chmod
);