forked from MidnightCommander/mc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchmod.c
406 lines (333 loc) · 9.01 KB
/
chmod.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/* Chmod command for Win32
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <string.h>
#include <stdio.h>
/* for chmod and stat */
#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "../src/global.h"
#include "../src/tty.h"
#include "../src/util.h"
#include "../src/win.h"
#include "../src/color.h"
#include "../src/dlg.h"
#include "../src/widget.h"
#include "../src/dialog.h" /* For do_refresh() */
#include "../src/dir.h"
#include "../src/panel.h" /* Needed for the externs */
#include "../src/file.h"
#include "../src/main.h"
#include "../src/chmod.h"
#include "../src/achown.h"
#include "../src/chown.h"
#define FILE_ARCHIVED FILE_ATTRIBUTE_ARCHIVE
#define FILE_DIRECTORY FILE_ATTRIBUTE_DIRECTORY
#define FILE_HIDDEN FILE_ATTRIBUTE_HIDDEN
#define FILE_READONLY FILE_ATTRIBUTE_READONLY
#define FILE_SYSTEM FILE_ATTRIBUTE_SYSTEM
#define mk_chmod(fname,st) SetFileAttributes(fname,st)
static int single_set;
struct Dlg_head *ch_dlg;
#define PX 5
#define PY 2
#define FX 40
#define FY 2
#define BX 6
#define BY 17
#define TX 40
#define TY 12
#define PERMISSIONS 4
#define BUTTONS 6
#define B_MARKED B_USER
#define B_ALL B_USER+1
#define B_SETMRK B_USER+2
#define B_CLRMRK B_USER+3
int mode_change, need_update;
int c_file, end_chmod;
umode_t and_mask, or_mask, c_stat;
char *c_fname, *c_fown, *c_fgrp, *c_fperm;
int c_fsize;
static WLabel *statl;
static int normal_color;
static int title_color;
static int selection_color;
/* bsedos.h */
struct {
mode_t mode;
char *text;
int selected;
WCheck *check;
} check_perm[PERMISSIONS] = {
{
FILE_ARCHIVED, "Archive", 0, 0,
},
{
FILE_READONLY, "Read Only", 0, 0,
},
{
FILE_HIDDEN, "Hidden", 0, 0,
},
{
FILE_SYSTEM, "System", 0, 0,
},
};
struct {
int ret_cmd, flags, y, x;
char *text;
} chmod_but[BUTTONS] = {
{
B_CANCEL, NORMAL_BUTTON, 2, 33, "&Cancel",
},
{
B_ENTER, DEFPUSH_BUTTON, 2, 17, "&Set",
},
{
B_CLRMRK, NORMAL_BUTTON, 0, 42, "C&lear marked",
},
{
B_SETMRK, NORMAL_BUTTON, 0, 27, "S&et marked",
},
{
B_MARKED, NORMAL_BUTTON, 0, 12, "&Marked all",
},
{
B_ALL, NORMAL_BUTTON, 0, 0, "Set &all",
},
};
static void chmod_toggle_select (void)
{
int Id = ch_dlg->current->dlg_id - BUTTONS + single_set * 2;
attrset (normal_color);
check_perm[Id].selected ^= 1;
dlg_move (ch_dlg, PY + PERMISSIONS - Id, PX + 1);
addch ((check_perm[Id].selected) ? '*' : ' ');
dlg_move (ch_dlg, PY + PERMISSIONS - Id, PX + 3);
}
static void chmod_refresh (void)
{
attrset (normal_color);
dlg_erase (ch_dlg);
draw_box (ch_dlg, 1, 2, 20 - single_set, 66);
draw_box (ch_dlg, PY, PX, PERMISSIONS + 2, 33);
draw_box (ch_dlg, FY, FX, 10, 25);
dlg_move (ch_dlg, FY + 1, FX + 2);
addstr ("Name");
dlg_move (ch_dlg, FY + 3, FX + 2);
addstr ("Permissions (Octal)");
dlg_move (ch_dlg, FY + 5, FX + 2);
addstr ("Owner name");
dlg_move (ch_dlg, FY + 7, FX + 2);
addstr ("Group name");
attrset (title_color);
dlg_move (ch_dlg, 1, 28);
addstr (" Chmod command ");
dlg_move (ch_dlg, PY, PX + 1);
addstr (" Permission ");
dlg_move (ch_dlg, FY, FX + 1);
addstr (" File ");
attrset (selection_color);
dlg_move (ch_dlg, TY, TX);
addstr ("Use SPACE to change");
dlg_move (ch_dlg, TY + 1, TX);
addstr ("an option, ARROW KEYS");
dlg_move (ch_dlg, TY + 2, TX);
addstr ("to move between options");
dlg_move (ch_dlg, TY + 3, TX);
addstr ("and T or INS to mark");
}
static int chmod_callback (Dlg_head *h, int Par, int Msg)
{
char buffer [10];
switch (Msg) {
case DLG_ACTION:
if (Par >= BUTTONS - single_set * 2){
c_stat ^= check_perm[Par - BUTTONS + single_set * 2].mode;
sprintf (buffer, "%o", c_stat);
label_set_text (statl, buffer);
chmod_toggle_select ();
mode_change = 1;
}
break;
case DLG_KEY:
if ((Par == 'T' || Par == 't' || Par == KEY_IC) &&
ch_dlg->current->dlg_id >= BUTTONS - single_set * 2) {
chmod_toggle_select ();
if (Par == KEY_IC)
dlg_one_down (ch_dlg);
return 1;
}
break;
case DLG_DRAW:
chmod_refresh ();
break;
}
return 0;
}
static void init_chmod (void)
{
int i;
do_refresh ();
end_chmod = c_file = need_update = 0;
single_set = (cpanel->marked < 2) ? 2 : 0;
if (use_colors){
normal_color = COLOR_NORMAL;
title_color = COLOR_HOT_NORMAL;
selection_color = COLOR_NORMAL;
} else {
normal_color = NORMAL_COLOR;
title_color = SELECTED_COLOR;
selection_color = SELECTED_COLOR;
}
ch_dlg = create_dlg (0, 0, 22 - single_set, 70, dialog_colors,
chmod_callback, "[Chmod]", "Chmod command", DLG_CENTER);
#define XTRACT(i) BY+chmod_but[i].y-single_set, BX+chmod_but[i].x, \
chmod_but[i].ret_cmd, chmod_but[i].flags, chmod_but[i].text, 0, 0, NULL
for (i = 0; i < BUTTONS; i++) {
if (i == 2 && single_set)
break;
else
add_widget (ch_dlg, button_new (XTRACT (i)));
}
#define XTRACT2(i) 0, check_perm [i].text, NULL
for (i = 0; i < PERMISSIONS; i++) {
check_perm[i].check = check_new (PY + (PERMISSIONS - i), PX + 2,
XTRACT2 (i));
add_widget (ch_dlg, check_perm[i].check);
}
}
int pc_stat_file (char *filename)
{
mode_t st;
st = GetFileAttributes (filename);
if (st & FILE_DIRECTORY)
st = -1;
return st;
}
static void chmod_done (void)
{
if (need_update)
update_panels (UP_OPTIMIZE, UP_KEEPSEL);
repaint_screen ();
}
char *next_file (void)
{
while (!cpanel->dir.list[c_file].f.marked)
c_file++;
return cpanel->dir.list[c_file].fname;
}
static void do_chmod (mode_t sf)
{
sf &= and_mask;
sf |= or_mask;
mk_chmod(cpanel->dir.list[c_file].fname, sf);
do_file_mark (cpanel, c_file, 0);
}
static void apply_mask (mode_t sf)
{
char *fname;
mode_t sf_stat;
need_update = end_chmod = 1;
do_chmod (sf);
do {
fname = next_file ();
if ((sf_stat = pc_stat_file (fname)) < 0)
break;
c_stat = sf_stat;
do_chmod (c_stat);
} while (cpanel->marked);
}
void chmod_cmd (void)
{
char buffer [10];
char *fname;
int i;
mode_t sf_stat;
do { /* do while any files remaining */
init_chmod ();
if (cpanel->marked)
fname = next_file (); /* next marked file */
else
fname = selection (cpanel)->fname; /* single file */
if ((sf_stat = pc_stat_file (fname)) < 0) /* get status of file */
break;
c_stat = sf_stat;
mode_change = 0; /* clear changes flag */
/* set check buttons */
for (i = 0; i < PERMISSIONS; i++){
check_perm[i].check->state = (c_stat & check_perm[i].mode) ? 1 : 0;
check_perm[i].selected = 0;
}
/* Set the labels */
c_fname = name_trunc (fname, 21);
add_widget (ch_dlg, label_new (FY+2, FX+2, c_fname, NULL));
c_fown = "unknown";
add_widget (ch_dlg, label_new (FY+6, FX+2, c_fown, NULL));
c_fgrp = "unknown";
add_widget (ch_dlg, label_new (FY+8, FX+2, c_fgrp, NULL));
sprintf (buffer, "%o", c_stat);
statl = label_new (FY+4, FX+2, buffer, NULL);
add_widget (ch_dlg, statl);
run_dlg (ch_dlg); /* retrieve an action */
/* do action */
switch (ch_dlg->ret_value){
case B_ENTER:
if (mode_change)
mk_chmod (fname, c_stat); /*.ado */
need_update = 1;
break;
case B_CANCEL:
end_chmod = 1;
break;
case B_ALL:
case B_MARKED:
and_mask = or_mask = 0;
and_mask = ~and_mask;
for (i = 0; i < PERMISSIONS; i++) {
if (check_perm[i].selected || ch_dlg->ret_value == B_ALL)
if (check_perm[i].check->state & C_BOOL)
or_mask |= check_perm[i].mode;
else
and_mask &= ~check_perm[i].mode;
}
apply_mask (sf_stat);
break;
case B_SETMRK:
and_mask = or_mask = 0;
and_mask = ~and_mask;
for (i = 0; i < PERMISSIONS; i++) {
if (check_perm[i].selected)
or_mask |= check_perm[i].mode;
}
apply_mask (sf_stat);
break;
case B_CLRMRK:
and_mask = or_mask = 0;
and_mask = ~and_mask;
for (i = 0; i < PERMISSIONS; i++) {
if (check_perm[i].selected)
and_mask &= ~check_perm[i].mode;
}
apply_mask (sf_stat);
break;
}
if (cpanel->marked && ch_dlg->ret_value!=B_CANCEL) {
do_file_mark (cpanel, c_file, 0);
need_update = 1;
}
destroy_dlg (ch_dlg);
} while (cpanel->marked && !end_chmod);
chmod_done ();
}