Merge 13159:13510 from trunk
[reactos.git] / rosapps / mc / pc / chmod.c
1 /* Chmod command for Windows NT and OS/2
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.
7
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.
12
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.
16
17 */
18
19 #include <config.h>
20
21 #ifdef __os2__
22 #define INCL_DOSFILEMGR
23 #include <os2.h>
24 #endif
25
26 #ifdef _OS_NT
27 #include <windows.h>
28 #endif
29
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() */
44
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"
52
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
61
62 static int single_set;
63 struct Dlg_head *ch_dlg;
64
65 #define PX 5
66 #define PY 2
67
68 #define FX 40
69 #define FY 2
70
71 #define BX 6
72 #define BY 17
73
74 #define TX 40
75 #define TY 12
76
77 #define PERMISSIONS 4
78 #define BUTTONS 6
79
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
84
85
86 int mode_change, need_update;
87 int c_file, end_chmod;
88
89 umode_t and_mask, or_mask, c_stat;
90 char *c_fname, *c_fown, *c_fgrp, *c_fperm;
91 int c_fsize;
92
93 static WLabel *statl;
94 static int normal_color;
95 static int title_color;
96 static int selection_color;
97
98 /* bsedos.h */
99 struct {
100 mode_t mode;
101 char *text;
102 int selected;
103 WCheck *check;
104 } check_perm[PERMISSIONS] = {
105
106 {
107 FILE_ARCHIVED, N_("Archive"), 0, 0,
108 },
109 {
110 FILE_READONLY, N_("Read Only"), 0, 0,
111 },
112 {
113 FILE_HIDDEN, N_("Hidden"), 0, 0,
114 },
115 {
116 FILE_SYSTEM, N_("System"), 0, 0,
117 },
118 };
119
120 struct {
121 int ret_cmd, flags, y, x;
122 char *text;
123 } chmod_but[BUTTONS] = {
124
125 {
126 B_CANCEL, NORMAL_BUTTON, 2, 33, N_("&Cancel"),
127 },
128 {
129 B_ENTER, DEFPUSH_BUTTON, 2, 17, N_("&Set"),
130 },
131 {
132 B_CLRMRK, NORMAL_BUTTON, 0, 42, N_("C&lear marked"),
133 },
134 {
135 B_SETMRK, NORMAL_BUTTON, 0, 27, N_("S&et marked"),
136 },
137 {
138 B_MARKED, NORMAL_BUTTON, 0, 12, N_("&Marked all"),
139 },
140 {
141 B_ALL, NORMAL_BUTTON, 0, 0, N_("Set &all"),
142 },
143 };
144
145 static void chmod_toggle_select (void)
146 {
147 int Id = ch_dlg->current->dlg_id - BUTTONS + single_set * 2;
148
149 attrset (normal_color);
150 check_perm[Id].selected ^= 1;
151
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);
155 }
156
157 static void chmod_refresh (void)
158 {
159 attrset (normal_color);
160 dlg_erase (ch_dlg);
161
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);
165
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"));
174
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 "));
182
183 attrset (selection_color);
184
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"));
193 }
194
195 static int chmod_callback (Dlg_head *h, int Par, int Msg)
196 {
197 char buffer [10];
198
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;
207 }
208 break;
209
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;
217 }
218 break;
219 #ifndef HAVE_X
220 case DLG_DRAW:
221 chmod_refresh ();
222 break;
223 #endif
224 }
225 return 0;
226 }
227
228 static void init_chmod (void)
229 {
230 int i;
231
232 do_refresh ();
233 end_chmod = c_file = need_update = 0;
234 single_set = (cpanel->marked < 2) ? 2 : 0;
235
236 if (use_colors){
237 normal_color = COLOR_NORMAL;
238 title_color = COLOR_HOT_NORMAL;
239 selection_color = COLOR_NORMAL;
240 } else {
241 normal_color = NORMAL_COLOR;
242 title_color = SELECTED_COLOR;
243 selection_color = SELECTED_COLOR;
244 }
245
246 ch_dlg = create_dlg (0, 0, 22 - single_set, 70, dialog_colors,
247 chmod_callback, _("[Chmod]"), _("chmod"), DLG_CENTER);
248
249 x_set_dialog_title (ch_dlg, _("Chmod command"));
250
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
253
254 tk_new_frame (ch_dlg, "b.");
255 for (i = 0; i < BUTTONS; i++) {
256 if (i == 2 && single_set)
257 break;
258 else
259 add_widgetl (ch_dlg, button_new (XTRACT (i)), XV_WLAY_RIGHTOF);
260 }
261
262
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,
267 XTRACT2 (i));
268 add_widget (ch_dlg, check_perm[i].check);
269 }
270 }
271
272 int pc_stat_file (char *filename)
273 {
274 mode_t st;
275
276 #ifdef _OS_NT
277 st = GetFileAttributes (filename);
278 #endif /* _OS_NT */
279
280 #ifdef __os2__
281 HFILE fHandle = 0L;
282 ULONG fInfoLevel = 1; /* 1st Level Info: Standard attributs */
283 FILESTATUS3 fInfoBuf;
284 ULONG fInfoBufSize;
285 ULONG fAction = 0;
286 APIRET rc;
287
288 fInfoBufSize = sizeof(FILESTATUS3);
289 rc = DosOpen((PSZ) filename,
290 &fHandle,
291 &fAction,
292 (ULONG) 0,
293 FILE_NORMAL,
294 OPEN_ACTION_OPEN_IF_EXISTS,
295 (OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE),
296 (PEAOP2) NULL);
297 if (rc != 0) {
298 return -1;
299 }
300
301 rc = DosQueryFileInfo(fHandle, fInfoLevel, &fInfoBuf, fInfoBufSize);
302 DosClose(fHandle);
303 if (rc != 0) {
304 return -1; /* error ! */
305 } else {
306 st = fInfoBuf.attrFile;
307 }
308 #endif /* __os2__ */
309
310 if (st & FILE_DIRECTORY)
311 st = -1;
312 return st;
313 }
314
315 static void chmod_done (void)
316 {
317 if (need_update)
318 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
319 repaint_screen ();
320 }
321
322 char *next_file (void)
323 {
324 while (!cpanel->dir.list[c_file].f.marked)
325 c_file++;
326
327 return cpanel->dir.list[c_file].fname;
328 }
329
330 #ifdef __os2__
331 static int mk_chmod (char *filename, ULONG st)
332 {
333 HFILE fHandle = 0L;
334 ULONG fInfoLevel = 1; /* 1st Level Info: Standard attributs */
335 FILESTATUS3 fInfoBuf;
336 ULONG fInfoBufSize;
337 ULONG fAction = 0L;
338 APIRET rc;
339
340 if (!(st & FILE_READONLY))
341 chmod(filename, (S_IWRITE | S_IREAD));
342 fInfoBufSize = sizeof(FILESTATUS3);
343 rc = DosOpen((PSZ) filename,
344 &fHandle,
345 &fAction,
346 (ULONG) 0,
347 FILE_NORMAL,
348 OPEN_ACTION_OPEN_IF_EXISTS,
349 (OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE),
350 0L);
351 if (rc != 0) {
352 return rc;
353 }
354
355 rc = DosQueryFileInfo(fHandle, fInfoLevel, &fInfoBuf, fInfoBufSize);
356 if (rc!=0) {
357 DosClose(fHandle);
358 return rc;
359 }
360 fInfoBuf.attrFile = st;
361 rc = DosSetFileInfo(fHandle, fInfoLevel, &fInfoBuf, fInfoBufSize);
362 rc = DosClose(fHandle);
363 return rc;
364 }
365 #endif /* __os2__ */
366
367 static void do_chmod (mode_t sf)
368 {
369 sf &= and_mask;
370 sf |= or_mask;
371
372 mk_chmod(cpanel->dir.list[c_file].fname, sf);
373
374 do_file_mark (cpanel, c_file, 0);
375 }
376
377 static void apply_mask (mode_t sf)
378 {
379 char *fname;
380 mode_t sf_stat;
381
382 need_update = end_chmod = 1;
383 do_chmod (sf);
384
385 do {
386 fname = next_file ();
387 if ((sf_stat = pc_stat_file (fname)) < 0)
388 break;
389
390 c_stat = sf_stat;
391 do_chmod (c_stat);
392 } while (cpanel->marked);
393 }
394
395 void chmod_cmd (void)
396 {
397 char buffer [10];
398 char *fname;
399 int i;
400 mode_t sf_stat;
401
402 do { /* do while any files remaining */
403 init_chmod ();
404 if (cpanel->marked)
405 fname = next_file (); /* next marked file */
406 else
407 fname = selection (cpanel)->fname; /* single file */
408
409 if ((sf_stat = pc_stat_file (fname)) < 0) /* get status of file */
410 break;
411
412 c_stat = sf_stat;
413 mode_change = 0; /* clear changes flag */
414
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;
419 }
420
421 tk_new_frame (ch_dlg, "l.");
422 /* Set the labels */
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);
432 tk_end_frame ();
433
434 run_dlg (ch_dlg); /* retrieve an action */
435
436 /* do action */
437 switch (ch_dlg->ret_value){
438 case B_ENTER:
439 if (mode_change)
440 mk_chmod (fname, c_stat); /*.ado */
441 need_update = 1;
442 break;
443
444 case B_CANCEL:
445 end_chmod = 1;
446 break;
447
448 case B_ALL:
449 case B_MARKED:
450 and_mask = or_mask = 0;
451 and_mask = ~and_mask;
452
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;
457 else
458 and_mask &= ~check_perm[i].mode;
459 }
460
461 apply_mask (sf_stat);
462 break;
463
464 case B_SETMRK:
465 and_mask = or_mask = 0;
466 and_mask = ~and_mask;
467
468 for (i = 0; i < PERMISSIONS; i++) {
469 if (check_perm[i].selected)
470 or_mask |= check_perm[i].mode;
471 }
472
473 apply_mask (sf_stat);
474 break;
475 case B_CLRMRK:
476 and_mask = or_mask = 0;
477 and_mask = ~and_mask;
478
479 for (i = 0; i < PERMISSIONS; i++) {
480 if (check_perm[i].selected)
481 and_mask &= ~check_perm[i].mode;
482 }
483
484 apply_mask (sf_stat);
485 break;
486 }
487
488 if (cpanel->marked && ch_dlg->ret_value!=B_CANCEL) {
489 do_file_mark (cpanel, c_file, 0);
490 need_update = 1;
491 }
492 destroy_dlg (ch_dlg);
493 } while (cpanel->marked && !end_chmod);
494 chmod_done ();
495 }