1 /* Chmod command for OS/2 operating system
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.
21 #error This file is for the OS/2 operating system.
24 #define INCL_DOSFILEMGR
28 /* for chmod and stat */
30 #include <sys\types.h>
39 #include "dialog.h" /* For do_refresh() */
42 #include "panel.h" /* Needed for the externs */
49 static int single_set
;
50 struct Dlg_head
*ch_dlg
;
67 #define B_MARKED B_USER
68 #define B_ALL B_USER+1
69 #define B_SETMRK B_USER+2
70 #define B_CLRMRK B_USER+3
72 int mode_change
, need_update
;
73 int c_file
, end_chmod
;
75 umode_t and_mask
, or_mask
, c_stat
;
76 char *c_fname
, *c_fown
, *c_fgrp
, *c_fperm
;
80 static int normal_color
;
81 static int title_color
;
82 static int selection_color
;
90 } check_perm
[PERMISSIONS
] = {
93 FILE_ARCHIVED
, "Archive", 0, 0,
96 FILE_READONLY
, "Read Only", 0, 0,
99 FILE_HIDDEN
, "Hidden", 0, 0,
102 FILE_SYSTEM
, "System", 0, 0,
107 int ret_cmd
, flags
, y
, x
;
109 } chmod_but
[BUTTONS
] = {
112 B_CANCEL
, NORMAL_BUTTON
, 2, 33, "&Cancel",
115 B_ENTER
, DEFPUSH_BUTTON
, 2, 17, "&Set",
118 B_CLRMRK
, NORMAL_BUTTON
, 0, 42, "C&lear marked",
121 B_SETMRK
, NORMAL_BUTTON
, 0, 27, "S&et marked",
124 B_MARKED
, NORMAL_BUTTON
, 0, 12, "&Marked all",
127 B_ALL
, NORMAL_BUTTON
, 0, 0, "Set &all",
132 static void mk_chmod (char *filename
, ULONG st
);
135 static void chmod_toggle_select (void)
137 int Id
= ch_dlg
->current
->dlg_id
- BUTTONS
+ single_set
* 2;
139 attrset (normal_color
);
140 check_perm
[Id
].selected
^= 1;
142 dlg_move (ch_dlg
, PY
+ PERMISSIONS
- Id
, PX
+ 1);
143 addch ((check_perm
[Id
].selected
) ? '*' : ' ');
144 dlg_move (ch_dlg
, PY
+ PERMISSIONS
- Id
, PX
+ 3);
147 static void chmod_refresh (void)
149 attrset (normal_color
);
152 draw_box (ch_dlg
, 1, 2, 20 - single_set
, 66);
153 draw_box (ch_dlg
, PY
, PX
, PERMISSIONS
+ 2, 33);
154 draw_box (ch_dlg
, FY
, FX
, 10, 25);
156 dlg_move (ch_dlg
, FY
+ 1, FX
+ 2);
158 dlg_move (ch_dlg
, FY
+ 3, FX
+ 2);
159 addstr ("Permissions (Octal)");
160 dlg_move (ch_dlg
, FY
+ 5, FX
+ 2);
161 addstr ("Owner name");
162 dlg_move (ch_dlg
, FY
+ 7, FX
+ 2);
163 addstr ("Group name");
165 attrset (title_color
);
166 dlg_move (ch_dlg
, 1, 28);
167 addstr (" Chmod command ");
168 dlg_move (ch_dlg
, PY
, PX
+ 1);
169 addstr (" Permission ");
170 dlg_move (ch_dlg
, FY
, FX
+ 1);
173 attrset (selection_color
);
175 dlg_move (ch_dlg
, TY
, TX
);
176 addstr ("Use SPACE to change");
177 dlg_move (ch_dlg
, TY
+ 1, TX
);
178 addstr ("an option, ARROW KEYS");
179 dlg_move (ch_dlg
, TY
+ 2, TX
);
180 addstr ("to move between options");
181 dlg_move (ch_dlg
, TY
+ 3, TX
);
182 addstr ("and T or INS to mark");
185 static int chmod_callback (Dlg_head
*h
, int Par
, int Msg
)
191 if (Par
>= BUTTONS
- single_set
* 2){
192 c_stat
^= check_perm
[Par
- BUTTONS
+ single_set
* 2].mode
;
193 sprintf (buffer
, "%o", c_stat
);
194 label_set_text (statl
, buffer
);
195 chmod_toggle_select ();
201 if ((Par
== 'T' || Par
== 't' || Par
== KEY_IC
) &&
202 ch_dlg
->current
->dlg_id
>= BUTTONS
- single_set
* 2) {
203 chmod_toggle_select ();
205 dlg_one_down (ch_dlg
);
218 static void init_chmod (void)
223 end_chmod
= c_file
= need_update
= 0;
224 single_set
= (cpanel
->marked
< 2) ? 2 : 0;
227 normal_color
= COLOR_NORMAL
;
228 title_color
= COLOR_HOT_NORMAL
;
229 selection_color
= COLOR_NORMAL
;
231 normal_color
= NORMAL_COLOR
;
232 title_color
= SELECTED_COLOR
;
233 selection_color
= SELECTED_COLOR
;
236 ch_dlg
= create_dlg (0, 0, 22 - single_set
, 70, dialog_colors
,
237 chmod_callback
, "[Chmod]", "chmod", DLG_CENTER
);
239 x_set_dialog_title (ch_dlg
, "Chmod command");
241 #define XTRACT(i) BY+chmod_but[i].y-single_set, BX+chmod_but[i].x, \
242 chmod_but[i].ret_cmd, chmod_but[i].flags, chmod_but[i].text, 0, 0, NULL
244 tk_new_frame (ch_dlg
, "b.");
245 for (i
= 0; i
< BUTTONS
; i
++) {
246 if (i
== 2 && single_set
)
249 add_widgetl (ch_dlg
, button_new (XTRACT (i
)), XV_WLAY_RIGHTOF
);
253 #define XTRACT2(i) 0, check_perm [i].text, NULL
254 tk_new_frame (ch_dlg
, "c.");
255 for (i
= 0; i
< PERMISSIONS
; i
++) {
256 check_perm
[i
].check
= check_new (PY
+ (PERMISSIONS
- i
), PX
+ 2,
258 add_widget (ch_dlg
, check_perm
[i
].check
);
262 int stat_file (char *filename
, struct stat
*st
)
265 ULONG fInfoLevel
= 1; /* 1st Level Info: Standard attributs */
266 FILESTATUS3 fInfoBuf
;
271 fInfoBufSize
= sizeof(FILESTATUS3
);
272 rc
= DosOpen((PSZ
) filename
,
277 OPEN_ACTION_OPEN_IF_EXISTS
,
278 (OPEN_ACCESS_READONLY
| OPEN_SHARE_DENYNONE
),
284 rc
= DosQueryFileInfo(fHandle
, fInfoLevel
, &fInfoBuf
, fInfoBufSize
);
287 return 0; /* error ! */
289 st
->st_mode
= fInfoBuf
.attrFile
;
292 if (st
->st_mode
& FILE_DIRECTORY
)
297 static void chmod_done (void)
300 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
, UP_KEEPSEL
);
304 char *next_file (void)
306 while (!cpanel
->dir
.list
[c_file
].f
.marked
)
309 return cpanel
->dir
.list
[c_file
].fname
;
312 static int os2_chmod (char *filename
, ULONG st
)
315 ULONG fInfoLevel
= 1; /* 1st Level Info: Standard attributs */
316 FILESTATUS3 fInfoBuf
;
321 fInfoBufSize
= sizeof(FILESTATUS3
);
322 rc
= DosOpen((PSZ
) filename
,
327 OPEN_ACTION_OPEN_IF_EXISTS
,
328 (OPEN_ACCESS_READWRITE
| OPEN_SHARE_DENYNONE
),
334 rc
= DosQueryFileInfo(fHandle
, fInfoLevel
, &fInfoBuf
, fInfoBufSize
);
339 fInfoBuf
.attrFile
= st
;
340 rc
= DosSetFileInfo(fHandle
, fInfoLevel
, &fInfoBuf
, fInfoBufSize
);
341 rc
= DosClose(fHandle
);
345 static void do_chmod (struct stat
*sf
)
348 ULONG fInfoLevel
= 1; /* 1st Level Info: Standard attributs */
349 FILESTATUS3 fInfoBuf
;
355 sf
->st_mode
&= and_mask
;
356 sf
->st_mode
|= or_mask
;
358 mk_chmod(cpanel
->dir
.list
[c_file
].fname
, sf
->st_mode
);
360 do_file_mark (cpanel
, c_file
, 0);
364 static void mk_chmod (char *filename
, ULONG st
)
369 if (st
& FILE_READONLY
) {
372 os2_chmod(filename
, st
);
374 fileMode
|= (S_IWRITE
| S_IREAD
);
375 rc
= chmod(filename
, fileMode
);
376 // Then set the other flags
377 os2_chmod(filename
, st
);
383 static void apply_mask (struct stat
*sf
)
387 need_update
= end_chmod
= 1;
391 fname
= next_file ();
392 if (!stat_file (fname
, sf
))
394 c_stat
= sf
->st_mode
;
397 } while (cpanel
->marked
);
400 void chmod_cmd (void)
407 do { /* do while any files remaining */
410 fname
= next_file (); /* next marked file */
412 fname
= selection (cpanel
)->fname
; /* single file */
414 if (!stat_file (fname
, &sf_stat
)) /* get status of file */
417 c_stat
= sf_stat
.st_mode
;
418 mode_change
= 0; /* clear changes flag */
420 /* set check buttons */
421 for (i
= 0; i
< PERMISSIONS
; i
++){
422 check_perm
[i
].check
->state
= (c_stat
& check_perm
[i
].mode
) ? 1 : 0;
423 check_perm
[i
].selected
= 0;
426 tk_new_frame (ch_dlg
, "l.");
428 c_fname
= name_trunc (fname
, 21);
429 add_widget (ch_dlg
, label_new (FY
+2, FX
+2, c_fname
, NULL
));
430 c_fown
= name_trunc (get_owner (sf_stat
.st_uid
), 21);
431 add_widget (ch_dlg
, label_new (FY
+6, FX
+2, c_fown
, NULL
));
432 c_fgrp
= name_trunc (get_group (sf_stat
.st_gid
), 21);
433 add_widget (ch_dlg
, label_new (FY
+8, FX
+2, c_fgrp
, NULL
));
434 sprintf (buffer
, "%o", c_stat
);
435 statl
= label_new (FY
+4, FX
+2, buffer
, NULL
);
436 add_widget (ch_dlg
, statl
);
439 run_dlg (ch_dlg
); /* retrieve an action */
442 switch (ch_dlg
->ret_value
){
445 mk_chmod (fname
, c_stat
); /*.ado */
455 and_mask
= or_mask
= 0;
456 and_mask
= ~and_mask
;
458 for (i
= 0; i
< PERMISSIONS
; i
++) {
459 if (check_perm
[i
].selected
|| ch_dlg
->ret_value
== B_ALL
)
460 if (check_perm
[i
].check
->state
& C_BOOL
)
461 or_mask
|= check_perm
[i
].mode
;
463 and_mask
&= ~check_perm
[i
].mode
;
466 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 or_mask
|= check_perm
[i
].mode
;
478 apply_mask (&sf_stat
);
481 and_mask
= or_mask
= 0;
482 and_mask
= ~and_mask
;
484 for (i
= 0; i
< PERMISSIONS
; i
++) {
485 if (check_perm
[i
].selected
)
486 and_mask
&= ~check_perm
[i
].mode
;
489 apply_mask (&sf_stat
);
493 if (cpanel
->marked
&& ch_dlg
->ret_value
!=B_CANCEL
) {
494 do_file_mark (cpanel
, c_file
, 0);
497 destroy_dlg (ch_dlg
);
498 } while (cpanel
->marked
&& !end_chmod
);