-
Notifications
You must be signed in to change notification settings - Fork 1
/
UMASK.PAS
396 lines (376 loc) · 8.67 KB
/
UMASK.PAS
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
{ @author: Sylvain Maltais (support@gladir.com)
@created: 2024
@website(https://www.gladir.com/corail)
@abstract(Target: Turbo Pascal 7, Free Pascal 3.2)
}
Program UMASK;
Uses DOS;
Const
FileSearchList:Array[0..2]of String[20]=(
'$HOME/.bashrc',
'$HOME/.profile',
'/etc/login.defs'
);
OctalMask:Array[0..7]of String[3]=(
'rwx','rw','rx','r','wx','w','x','-'
);
Var
I:Integer;
ShowFile,OutputSymbolic:Boolean;
BaseDir,HomeDir,UserName,NewUMask,Mask:String;
Function StrToUpper(S:String):String;
Var
I:Byte;
Begin
For I:=1 to Length(S)do Begin
If S[I] in['a'..'z']Then S[I]:=Chr(Ord(S[I])-32);
End;
StrToUpper:=S;
End;
Function TrimL(S:String):String;
Var
I:Byte;
Begin
For I:=1to Length(S)do Begin
If Not(S[I]in[#9,' '])Then Begin
TrimL:=Copy(S,I,255);
Exit;
End;
End;
TrimL:=S;
End;
Function TrimR(s:String):String;
Var
i:Integer;
Begin
i:=Length(s);
While (i>0)and(s[i]in[#9,' '])do Dec(i);
s[0]:=Chr(i);
TrimR:=S;
End;
Function Trim(s:String):String;Begin
Trim:=TrimL(TrimR(s));
End;
Function IsOctalMask(S:String):Boolean;
Var
I:Integer;
Begin
IsOctalMask:=True;
For I:=1 to Length(S)do Begin
If Not(S[I]in['0'..'7'])Then Begin
IsOctalMask:=False;
Exit;
End;
End;
End;
Procedure OctalToUGO(Octal:String;Var U,G,O:Byte);Begin
Octal:=Trim(Octal);
If Length(Octal)=4 Then Octal:=Copy(Octal,2,255);
U:=Ord(Octal[1])-Ord('0');
G:=Ord(Octal[2])-Ord('0');
O:=Ord(Octal[3])-Ord('0');
End;
Function SymbolicToOctal(U,G,O:Byte;S:String):String;
Var
Mode:Char;
I:Integer;
Remove,EqualValue:Boolean;
Begin
Mode:=#0;
Remove:=False;
For I:=1 to Length(S)do Begin
Case S[I]of
'u':Begin
Mode:='u';
Remove:=False;
EqualValue:=False;
End;
'g':Begin
Mode:='g';
Remove:=False;
EqualValue:=False;
End;
'o':Begin
Mode:='o';
Remove:=False;
EqualValue:=False;
End;
'r':Begin
If(EqualValue)Then Begin
Case Mode of
'u':U:=4;
'g':G:=4;
'o':O:=4;
End;
End
Else
If(Remove)Then Begin
Case Mode of
'u':U:=U and Not 4;
'g':G:=G and Not 4;
'o':O:=O and Not 4;
End;
End
Else
Begin
Case Mode of
'u':U:=U or 4;
'g':G:=G or 4;
'o':O:=O or 4;
End;
End;
End;
'w':Begin
If(EqualValue)Then Begin
Case Mode of
'u':U:=2;
'g':G:=2;
'o':O:=2;
End;
End
Else
If(Remove)Then Begin
Case Mode of
'u':U:=U and Not 2;
'g':G:=G and Not 2;
'o':O:=O and Not 2;
End;
End
Else
Begin
Case Mode of
'u':U:=U or 2;
'g':G:=G or 2;
'o':O:=O or 2;
End;
End;
End;
'x':Begin
If(EqualValue)Then Begin
Case Mode of
'u':U:=1;
'g':G:=1;
'o':O:=1;
End;
End
Else
If(Remove)Then Begin
Case Mode of
'u':U:=U and Not 1;
'g':G:=G and Not 1;
'o':O:=O and Not 1;
End;
End
Else
Begin
Case Mode of
'u':U:=U or 1;
'g':G:=G or 1;
'o':O:=O or 1;
End;
End;
End;
'=':EqualValue:=True;
'-':Remove:=True;
'+':Remove:=False;
End;
End;
SymbolicToOctal:=Chr(U+Ord('0'))+Chr(G+Ord('0'))+Chr(O+Ord('0'));
End;
Procedure TruncAfterSemicolon(Var S:String);
Var
I:Byte;
Begin
For I:=1to Length(S)do If S[I]=';'Then Begin
S[0]:=Chr(I-1);
Exit;
End;
End;
Function Path2Dir(Const Path:String):String;
Var
D:DirStr;
N:NameStr;
E:ExtStr;
Begin
Path2Dir:='';
If Path=''Then Exit;
FSplit(Path,D,N,E);
If E=''Then Begin
If D[Length(D)]<>'\'Then D:=D+'\';
D:=D+E;
End;
If D=''Then Path2Dir:='' Else
If D[Length(D)]<>'\'Then D:=D+'\';
Path2Dir:=D;
End;
Function IsWildCard(Const Path:String):Boolean;Begin
IsWildCard:=(Pos('*',Path)>0)or(Pos('?',Path)>0);
End;
Function DirExist(Dir:String):Boolean;
Var
Rec:SearchRec;
Begin
If Length(Dir)=0Then DirExist:=True
Else
Begin
TruncAfterSemicolon(Dir);
If Dir[Length(Dir)]='\'Then Dir:=Dir+'*.*' Else
If IsWildCard(Dir)Then Dir:=Path2Dir(Dir)+'*.*';
FindFirst(Dir,Directory,Rec);
DirExist:=DOSError=0;
End;
End;
Function GetHomeDir(UserName:String):String;Begin
If UserName=''Then GetHomeDir:='/root'
Else GetHomeDir:=BaseDir+'/home/'+UserName;
End;
Function GetCurrUMASK(UserName:String):String;
Var
J:Integer;
ConfigFile:Text;
CurrFileName,CurrLine:String;
Begin
GetCurrUMASK:='';
For J:=Low(FileSearchList) to High(FileSearchList)do Begin
CurrFileName:=FileSearchList[J];
If Copy(CurrFileName,1,5)='$HOME'Then Begin
CurrFileName:=HomeDir+Copy(CurrFileName,6,255);
End;
{$I-}Assign(ConfigFile,CurrFileName);
Reset(ConfigFile);{$I+}
If IOResult=0 Then Begin
While Not EOF(ConfigFile)do Begin
ReadLn(ConfigFile,CurrLine);
If StrToUpper(Copy(CurrLine,1,5))='UMASK'Then Begin
If(ShowFile)Then WriteLn(CurrFileName);
GetCurrUMASK:=Trim(Copy(CurrLine,6,5));
Close(ConfigFile);
Exit;
End;
End;
Close(ConfigFile);
End;
End;
End;
Procedure SetUMASK(UserName,UMask:String);
Var
Found:Boolean;
J:Integer;
U,G,O:Byte;
ConfigFile,NewConfigFile:Text;
CurrFileName,CurrLine:String;
Begin
Found:=False;
For J:=Low(FileSearchList) to High(FileSearchList)do Begin
CurrFileName:=FileSearchList[J];
If Copy(CurrFileName,1,5)='$HOME'Then Begin
CurrFileName:=HomeDir+Copy(CurrFileName,6,255);
End;
{$I-}Assign(ConfigFile,CurrFileName);
Reset(ConfigFile);{$I+}
If IOResult=0 Then Begin
{$I-}Assign(NewConfigFile,HomeDir+'/'+'UMASK.TMP');
Rewrite(NewConfigFile);{$I+}
If IOResult=0 Then Begin
While Not EOF(ConfigFile)do Begin
ReadLn(ConfigFile,CurrLine);
If StrToUpper(Copy(CurrLine,1,5))='UMASK'Then Begin
If Not IsOctalMask(UMask)Then Begin
OctalToUGO(Copy(CurrLine,6,255),U,G,O);
UMask:=SymbolicToOctal(U,G,O,UMask);
End;
WriteLn(NewConfigFile,Copy(CurrLine,1,5),' ',UMask);
Found:=True;
End
Else
WriteLn(NewConfigFile,CurrLine);
End;
If Not(Found)Then Begin
If IsOctalMask(UMask)Then Write(NewConfigFile,UMask)
Else
Begin
UMask:=SymbolicToOctal(0,0,0,UMask);
Write(NewConfigFile,UMask);
End;
WriteLn(NewConfigFile);
Found:=True;
End;
Close(NewConfigFile);
End;
Close(ConfigFile);
{$I-}Assign(ConfigFile,CurrFileName);
Erase(ConfigFile);{$I+}
If IOResult<>0 Then Begin
WriteLn('Impossible de remplacer le fichier ',CurrFileName);
Halt(1);
End;
{$I-}Assign(NewConfigFile,HomeDir+'/'+'UMASK.TMP');
Rename(NewConfigFile,CurrFileName);{$I+}
If IOResult<>0 Then Begin
WriteLn('Impossible de renommer le fichier ',HomeDir+'/'+'UMASK.TMP',
' -> ',CurrFileName);
Halt(2);
End;
End;
End;
End;
BEGIN
If(ParamStr(1)='/?')or(ParamStr(1)='--help')or(ParamStr(1)='-h')or
(ParamStr(1)='/h')or(ParamStr(1)='/H')Then Begin
WriteLn('UMASK : Cette commande permet de fixer les droits d''accŠs par ',
'd‚faut de la cr‚ation d''un fichier.');
WriteLn;
WriteLn('Syntaxe : UMASK [options] [umask]');
WriteLn;
WriteLn(' umask Indique le UMask … appliquer');
WriteLn(' -S Affiche une sortie symbolique');
WriteLn(' --cygwin Recherche dans les dossiers de Cygwin');
WriteLn(' --cygwin32 Recherche dans les dossiers de Cygwin en 32 bits');
WriteLn(' --cygwin64 Recherche dans les dossiers de Cygwin en 64 bits');
WriteLn(' --showfile Affiche le fichier o— a ‚t‚ trouv‚ le UMASK');
WriteLn(' --help Affiche l''aide de cette commande.');
End
Else
Begin
NewUMask:='';
BaseDir:='';
ShowFile:=False;
OutputSymbolic:=False;
UserName:=GetEnv('LOGNAME');
If UserName=''Then UserName:=GetEnv('USERNAME');
If UserName=''Then UserName:='root';
HomeDir:=GetHomeDir(UserName);
For I:=1 to ParamCount do Begin
If ParamStr(I)='-S'Then OutputSymbolic:=True Else
If ParamStr(I)='--cygwin'Then Begin
If DirExist('/cygwin')Then BaseDir:='/cygwin' Else
If DirExist('/cygwin32')Then BaseDir:='/cygwin32' Else
If DirExist('/cygwin64')Then BaseDir:='/cygwin64';
End
Else
If ParamStr(I)='--cygwin64'Then BaseDir:='/cygwin64' Else
If ParamStr(I)='--cygwin32'Then BaseDir:='/cygwin32' Else
If ParamStr(I)='--showfile'Then ShowFile:=True
Else NewUMask:=ParamStr(I);
End;
If NewUMask<>''Then Begin
If Length(NewUMask)=4 Then NewUMask:=Copy(NewUMask,2,255);
SetUMASK(UserName,NewUMask);
End
Else
Begin
Mask:=GetCurrUMASK(UserName);
If Mask=''Then Begin
WriteLn('Erreur de masque');
Halt(3);
End;
If(OutputSymbolic)Then Begin
WriteLn('u=',OctalMask[Ord(Mask[1])-Ord('0')],',',
'g=',OctalMask[Ord(Mask[2])-Ord('0')],',',
'o=',OctalMask[Ord(Mask[3])-Ord('0')]);
End
Else
WriteLn('0',Mask);
End;
End;
END.