*** empty log message ***
[midnight-commander.git] / pc / chmod.c
blobc3b76b426ceccdc5127f4058e547651d57b4e747
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.
19 #include <config.h>
21 #ifdef __os2__
22 #define INCL_DOSFILEMGR
23 #include <os2.h>
24 #endif
26 #ifdef _OS_NT
27 #include <windows.h>
28 #endif
30 #include <string.h>
31 #include <stdio.h>
32 /* for chmod and stat */
33 #include <io.h>
34 #include <sys\types.h>
35 #include <sys\stat.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"
53 #ifdef _OS_NT
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)
60 #endif
62 static int single_set;
63 struct Dlg_head *ch_dlg;
65 #define PX 5
66 #define PY 2
68 #define FX 40
69 #define FY 2
71 #define BX 6
72 #define BY 17
74 #define TX 40
75 #define TY 12
77 #define PERMISSIONS 4
78 #define BUTTONS 6
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;
90 int c_fsize;
92 static WLabel *statl;
93 static int normal_color;
94 static int title_color;
95 static int selection_color;
97 /* bsedos.h */
98 struct {
99 mode_t mode;
100 char *text;
101 int selected;
102 WCheck *check;
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,
119 struct {
120 int ret_cmd, flags, y, x;
121 char *text;
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);
159 dlg_erase (ch_dlg);
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);
166 addstr ("Name");
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);
180 addstr (" File ");
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)
196 char buffer [10];
198 switch (Msg) {
199 case DLG_ACTION:
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 ();
205 mode_change = 1;
207 break;
209 case DLG_KEY:
210 if ((Par == 'T' || Par == 't' || Par == KEY_IC) &&
211 ch_dlg->current->dlg_id >= BUTTONS - single_set * 2) {
212 chmod_toggle_select ();
213 if (Par == KEY_IC)
214 dlg_one_down (ch_dlg);
215 return 1;
217 break;
218 #ifndef HAVE_X
219 case DLG_DRAW:
220 chmod_refresh ();
221 break;
222 #endif
224 return 0;
227 static void init_chmod (void)
229 int i;
231 do_refresh ();
232 end_chmod = c_file = need_update = 0;
233 single_set = (cpanel->marked < 2) ? 2 : 0;
235 if (use_colors){
236 normal_color = COLOR_NORMAL;
237 title_color = COLOR_HOT_NORMAL;
238 selection_color = COLOR_NORMAL;
239 } else {
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)
255 break;
256 else
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,
264 XTRACT2 (i));
265 add_widget (ch_dlg, check_perm[i].check);
269 int pc_stat_file (char *filename)
271 mode_t st;
273 #ifdef _OS_NT
274 st = GetFileAttributes (filename);
275 #endif /* _OS_NT */
277 #ifdef __os2__
278 HFILE fHandle = 0L;
279 ULONG fInfoLevel = 1; /* 1st Level Info: Standard attributs */
280 FILESTATUS3 fInfoBuf;
281 ULONG fInfoBufSize;
282 ULONG fAction = 0;
283 APIRET rc;
285 fInfoBufSize = sizeof(FILESTATUS3);
286 rc = DosOpen((PSZ) filename,
287 &fHandle,
288 &fAction,
289 (ULONG) 0,
290 FILE_NORMAL,
291 OPEN_ACTION_OPEN_IF_EXISTS,
292 (OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE),
293 (PEAOP2) NULL);
294 if (rc != 0) {
295 return -1;
298 rc = DosQueryFileInfo(fHandle, fInfoLevel, &fInfoBuf, fInfoBufSize);
299 DosClose(fHandle);
300 if (rc != 0) {
301 return -1; /* error ! */
302 } else {
303 st = fInfoBuf.attrFile;
305 #endif /* __os2__ */
307 if (st & FILE_DIRECTORY)
308 st = -1;
309 return st;
312 static void chmod_done (void)
314 if (need_update)
315 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
316 repaint_screen ();
319 char *next_file (void)
321 while (!cpanel->dir.list[c_file].f.marked)
322 c_file++;
324 return cpanel->dir.list[c_file].fname;
327 #ifdef __os2__
328 static int mk_chmod (char *filename, ULONG st)
330 HFILE fHandle = 0L;
331 ULONG fInfoLevel = 1; /* 1st Level Info: Standard attributs */
332 FILESTATUS3 fInfoBuf;
333 ULONG fInfoBufSize;
334 ULONG fAction = 0L;
335 APIRET rc;
337 if (!(st & FILE_READONLY))
338 chmod(filename, (S_IWRITE | S_IREAD));
339 fInfoBufSize = sizeof(FILESTATUS3);
340 rc = DosOpen((PSZ) filename,
341 &fHandle,
342 &fAction,
343 (ULONG) 0,
344 FILE_NORMAL,
345 OPEN_ACTION_OPEN_IF_EXISTS,
346 (OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE),
347 0L);
348 if (rc != 0) {
349 return rc;
352 rc = DosQueryFileInfo(fHandle, fInfoLevel, &fInfoBuf, fInfoBufSize);
353 if (rc!=0) {
354 DosClose(fHandle);
355 return rc;
357 fInfoBuf.attrFile = st;
358 rc = DosSetFileInfo(fHandle, fInfoLevel, &fInfoBuf, fInfoBufSize);
359 rc = DosClose(fHandle);
360 return rc;
362 #endif /* __os2__ */
364 static void do_chmod (mode_t sf)
366 sf &= and_mask;
367 sf |= or_mask;
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)
376 char *fname;
377 mode_t sf_stat;
379 need_update = end_chmod = 1;
380 do_chmod (sf);
382 do {
383 fname = next_file ();
384 if ((sf_stat = pc_stat_file (fname)) < 0)
385 break;
387 c_stat = sf_stat;
388 do_chmod (c_stat);
389 } while (cpanel->marked);
392 void chmod_cmd (void)
394 char buffer [10];
395 char *fname;
396 int i;
397 mode_t sf_stat;
399 do { /* do while any files remaining */
400 init_chmod ();
401 if (cpanel->marked)
402 fname = next_file (); /* next marked file */
403 else
404 fname = selection (cpanel)->fname; /* single file */
406 if ((sf_stat = pc_stat_file (fname)) < 0) /* get status of file */
407 break;
409 c_stat = sf_stat;
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;
418 /* Set the labels */
419 c_fname = name_trunc (fname, 21);
420 add_widget (ch_dlg, label_new (FY+2, FX+2, c_fname, NULL));
421 c_fown = "unknown";
422 add_widget (ch_dlg, label_new (FY+6, FX+2, c_fown, NULL));
423 c_fgrp = "unknown";
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 */
431 /* do action */
432 switch (ch_dlg->ret_value){
433 case B_ENTER:
434 if (mode_change)
435 mk_chmod (fname, c_stat); /*.ado */
436 need_update = 1;
437 break;
439 case B_CANCEL:
440 end_chmod = 1;
441 break;
443 case B_ALL:
444 case B_MARKED:
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;
452 else
453 and_mask &= ~check_perm[i].mode;
456 apply_mask (sf_stat);
457 break;
459 case B_SETMRK:
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);
469 break;
470 case B_CLRMRK:
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);
480 break;
483 if (cpanel->marked && ch_dlg->ret_value!=B_CANCEL) {
484 do_file_mark (cpanel, c_file, 0);
485 need_update = 1;
487 destroy_dlg (ch_dlg);
488 } while (cpanel->marked && !end_chmod);
489 chmod_done ();