-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsx_chmod.c
175 lines (120 loc) · 3 KB
/
sx_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
/* sx_chmod.c
CHMOD for Samarux.
Change file permissions / attributes.
Copyright (c) 2007 - 2015 Miguel I. Garcia Lopez.
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, 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, 675 Mass Ave, Cambridge, MA 02139, USA.
Usage:
chmod mode file ...
Modes:
w : Write permission (R/W & R/O in CP/M).
s : System attribute (DIR & SYS in CP/M).
a : Archive attribute (NORMAL & ARCHIVED in CP/M 3).
Examples:
chmod +s login.com
chmod +wa-s notes.txt calendar.doc
Changes:
21 Dec 2014 : 1.00 : 1st version.
24 Dec 2014 : 1.01 : Re-written.
11 Feb 2015 : 1.02 : Added effective change of file attributes.
05 Jun 2016 : 1.03 : Support for builtin / external.
*/
/* Built-in or external
--------------------
*/
#ifdef SX_SAMARUX
#define SX_CHMOD
#else
#include "samarux.h"
#define ChmodMain main
#endif
ChmodMain(argc, argv)
int argc, argv[];
{
char *p; int i, ch, op, wrt, sys, arc;
/* Check arguments */
if(argc < 3)
return ErrorPars();
/* Default values */
wrt = sys = arc = op = 0;
/* Get permissions */
p = argv[1];
while((ch = *p++))
{
switch(ch)
{
case '+' : op = 1; break; /* Set (on) */
case '-' : op = -1; break; /* Reset (off) */
case 'w' : wrt = op; break; /* Write perm. */
case 's' : sys = op; break; /* System file (hidden) */
case 'a' : arc = op; break; /* Archived att. */
default : return ErrorOpt();
}
}
/* Check permissions */
if(!wrt && !sys && !arc)
return ErrorIll();
/* Process files */
for(i = 2; i < argc; ++i)
{
if(ChmodFile(argv[i], wrt, sys, arc))
return 1;
}
/* Return success */
return 0;
}
ChmodFile(fn, wrt, sys, arc)
char *fn; int wrt, sys, arc;
{
int dir;
/* Check file name is not a directory */
if(FnIsDir(fn))
return ErrorFName();
/* Check file name is not ambiguous */
if(FnIsAmb(fn))
return ErrorAmb();
/* Make source FCX */
if(CpMakeFcx(fn, sv_fcx))
ErrorFName();
/* Get current file attributes */
bdos_hl(BF_DMA, sv_dma);
if((dir = bdos_fcx_a(BF_FIND1ST, sv_fcx)) == 0xFF)
return ErrorNoFile();
memcpy(sv_fcx + 2, sv_dma + dir * 32 + 1, 11);
/* Set file attributes in FCX */
if(wrt)
{
if(wrt == 1)
sv_fcx[10] &= 0x7F;
else
sv_fcx[10] |= 0x80;
}
if(sys)
{
if(sys == 1)
sv_fcx[11] |= 0x80;
else
sv_fcx[11] &= 0x7F;
}
if(arc)
{
if(arc == 1)
sv_fcx[12] |= 0x80;
else
sv_fcx[12] &= 0x7F;
}
/* Write to disk */
if(bdos_fcx_a(BF_ATTRIB, sv_fcx) == 0xFF)
return ErrorWrite();
/* Return success */
return 0;
}