*** empty log message ***
[midnight-commander.git] / pc / chmod.c
blob111f7be4910fed6f61ea7a467060bc57d18873c3
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.
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
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;
91 int c_fsize;
93 static WLabel *statl;
94 static int normal_color;
95 static int title_color;
96 static int selection_color;
98 /* bsedos.h */
99 struct {
100 mode_t mode;
101 char *text;
102 int selected;
103 WCheck *check;
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,
120 struct {
121 int ret_cmd, flags, y, x;
122 char *text;
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);
160 dlg_erase (ch_dlg);
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);
167 addstr (_("Name"));
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)
197 char buffer [10];
199 switch (Msg) {
200 case DLG_ACTION:
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 ();
206 mode_change = 1;
208 break;
210 case DLG_KEY:
211 if ((Par == 'T' || Par == 't' || Par == KEY_IC) &&
212 ch_dlg->current->dlg_id >= BUTTONS - single_set * 2) {
213 chmod_toggle_select ();
214 if (Par == KEY_IC)
215 dlg_one_down (ch_dlg);
216 return 1;
218 break;
219 case DLG_DRAW:
220 chmod_refresh ();
221 break;
223 return 0;
226 static void init_chmod (void)
228 int i;
230 do_refresh ();
231 end_chmod = c_file = need_update = 0;
232 single_set = (cpanel->marked < 2) ? 2 : 0;
234 if (use_colors){
235 normal_color = COLOR_NORMAL;
236 title_color = COLOR_HOT_NORMAL;
237 selection_color = COLOR_NORMAL;
238 } else {
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)
254 break;
255 else
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,
263 XTRACT2 (i));
264 add_widget (ch_dlg, check_perm[i].check);
268 int pc_stat_file (char *filename)
270 mode_t st;
272 #ifdef _OS_NT
273 st = GetFileAttributes (filename);
274 #endif /* _OS_NT */
276 #ifdef __os2__
277 HFILE fHandle = 0L;
278 ULONG fInfoLevel = 1; /* 1st Level Info: Standard attributs */
279 FILESTATUS3 fInfoBuf;
280 ULONG fInfoBufSize;
281 ULONG fAction = 0;
282 APIRET rc;
284 fInfoBufSize = sizeof(FILESTATUS3);
285 rc = DosOpen((PSZ) filename,
286 &fHandle,
287 &fAction,
288 (ULONG) 0,
289 FILE_NORMAL,
290 OPEN_ACTION_OPEN_IF_EXISTS,
291 (OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE),
292 (PEAOP2) NULL);
293 if (rc != 0) {
294 return -1;
297 rc = DosQueryFileInfo(fHandle, fInfoLevel, &fInfoBuf, fInfoBufSize);
298 DosClose(fHandle);
299 if (rc != 0) {
300 return -1; /* error ! */
301 } else {
302 st = fInfoBuf.attrFile;
304 #endif /* __os2__ */
306 if (st & FILE_DIRECTORY)
307 st = -1;
308 return st;
311 static void chmod_done (void)
313 if (need_update)
314 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
315 repaint_screen ();
318 char *next_file (void)
320 while (!cpanel->dir.list[c_file].f.marked)
321 c_file++;
323 return cpanel->dir.list[c_file].fname;
326 #ifdef __os2__
327 static int mk_chmod (char *filename, ULONG st)
329 HFILE fHandle = 0L;
330 ULONG fInfoLevel = 1; /* 1st Level Info: Standard attributs */
331 FILESTATUS3 fInfoBuf;
332 ULONG fInfoBufSize;
333 ULONG fAction = 0L;
334 APIRET rc;
336 if (!(st & FILE_READONLY))
337 chmod(filename, (S_IWRITE | S_IREAD));
338 fInfoBufSize = sizeof(FILESTATUS3);
339 rc = DosOpen((PSZ) filename,
340 &fHandle,
341 &fAction,
342 (ULONG) 0,
343 FILE_NORMAL,
344 OPEN_ACTION_OPEN_IF_EXISTS,
345 (OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE),
346 0L);
347 if (rc != 0) {
348 return rc;
351 rc = DosQueryFileInfo(fHandle, fInfoLevel, &fInfoBuf, fInfoBufSize);
352 if (rc!=0) {
353 DosClose(fHandle);
354 return rc;
356 fInfoBuf.attrFile = st;
357 rc = DosSetFileInfo(fHandle, fInfoLevel, &fInfoBuf, fInfoBufSize);
358 rc = DosClose(fHandle);
359 return rc;
361 #endif /* __os2__ */
363 static void do_chmod (mode_t sf)
365 sf &= and_mask;
366 sf |= or_mask;
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)
375 char *fname;
376 mode_t sf_stat;
378 need_update = end_chmod = 1;
379 do_chmod (sf);
381 do {
382 fname = next_file ();
383 if ((sf_stat = pc_stat_file (fname)) < 0)
384 break;
386 c_stat = sf_stat;
387 do_chmod (c_stat);
388 } while (cpanel->marked);
391 void chmod_cmd (void)
393 char buffer [10];
394 char *fname;
395 int i;
396 mode_t sf_stat;
398 do { /* do while any files remaining */
399 init_chmod ();
400 if (cpanel->marked)
401 fname = next_file (); /* next marked file */
402 else
403 fname = selection (cpanel)->fname; /* single file */
405 if ((sf_stat = pc_stat_file (fname)) < 0) /* get status of file */
406 break;
408 c_stat = sf_stat;
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;
417 /* Set the labels */
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 */
430 /* do action */
431 switch (ch_dlg->ret_value){
432 case B_ENTER:
433 if (mode_change)
434 mk_chmod (fname, c_stat); /*.ado */
435 need_update = 1;
436 break;
438 case B_CANCEL:
439 end_chmod = 1;
440 break;
442 case B_ALL:
443 case B_MARKED:
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;
451 else
452 and_mask &= ~check_perm[i].mode;
455 apply_mask (sf_stat);
456 break;
458 case B_SETMRK:
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);
468 break;
469 case B_CLRMRK:
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);
479 break;
482 if (cpanel->marked && ch_dlg->ret_value!=B_CANCEL) {
483 do_file_mark (cpanel, c_file, 0);
484 need_update = 1;
486 destroy_dlg (ch_dlg);
487 } while (cpanel->marked && !end_chmod);
488 chmod_done ();