-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathID.PAS
469 lines (453 loc) · 11.4 KB
/
ID.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
{ @author: Sylvain Maltais ([email protected])
@created: 2024
@website(https://www.gladir.com/linux-0)
@abstract(Target: Turbo Pascal 7, Free Pascal 3.2)
}
Program ID;
Uses DOS;
Var
Language:(_Albanian,_French,_English,_Germany,_Italian,_Spain);
TmpLanguage:String;
Mode:(_None,_User,_GroupId,_Group);
I:Integer;
BaseDir,Entity,UserName,UserID,GroupName,GroupID: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;
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 GroupIDToGroupName(GroupID:String):String;
Var
FileGroup:Text;
I,CellPos:Integer;
CurrLine,CurrWord:String;
Cells:Array[0..6]of String;
Begin
GroupIDToGroupName:='';
{$I-}Assign(FileGroup,BaseDir+'/etc/group');
Reset(FileGroup);{$I+}
If IOResult<>0 Then Begin
WriteLn('Impossible de lire le fichier ',BaseDir,'/etc/group');
Halt(1);
End;
While Not EOF(FileGroup)do Begin
ReadLn(FileGroup,CurrLine);
FillChar(Cells,SizeOf(Cells),0);
CurrWord:='';
CellPos:=0;
For I:=1 to Length(CurrLine) do Begin
If CurrLine[I]=':'Then Begin
Cells[CellPos]:=CurrWord;
CurrWord:='';
Inc(CellPos);
If CellPos>5 Then Break;
End
Else
CurrWord:=CurrWord+CurrLine[I];
End;
If CurrWord<>''Then Begin
Cells[CellPos]:=CurrWord;
Inc(CellPos);
End;
If Cells[0]<>''Then Begin
If(Cells[2]=GroupID)Then Begin
GroupIDToGroupName:=Cells[0];
Close(FileGroup);
Exit;
End;
End;
End;
Close(FileGroup);
End;
Function UsersInGroupExist(GroupID:String):Boolean;
Var
FilePasswd:Text;
I,CellPos:Integer;
CurrLine,CurrWord:String;
Cells:Array[0..6]of String;
Begin
UsersInGroupExist:=False;
{$I-}Assign(FilePasswd,BaseDir+'/etc/passwd');
Reset(FilePasswd);{$I+}
If IOResult<>0 Then Begin
WriteLn('Impossible de lire le fichier ',BaseDir,'/etc/passwd');
Halt(1);
End;
While Not EOF(Filepasswd)do Begin
ReadLn(Filepasswd,CurrLine);
FillChar(Cells,SizeOf(Cells),0);
CurrWord:='';
CellPos:=0;
For I:=1 to Length(CurrLine) do Begin
If CurrLine[I]=':'Then Begin
Cells[CellPos]:=CurrWord;
CurrWord:='';
Inc(CellPos);
If CellPos>5 Then Break;
End
Else
CurrWord:=CurrWord+CurrLine[I];
End;
If CurrWord<>''Then Begin
Cells[CellPos]:=CurrWord;
Inc(CellPos);
End;
If Cells[0]<>''Then Begin
If(Cells[3]=GroupID)Then Begin
Close(FilePasswd);
UsersInGroupExist:=True;
Exit;
End;
End;
End;
Close(FilePasswd);
End;
Function GroupNameToGroupID(GroupName:String):String;
Var
FileGroup:Text;
I,CellPos:Integer;
CurrLine,CurrWord:String;
Cells:Array[0..6]of String;
Begin
GroupNameToGroupID:='';
{$I-}Assign(FileGroup,BaseDir+'/etc/group');
Reset(FileGroup);{$I+}
If IOResult<>0 Then Begin
WriteLn('Impossible de lire le fichier ',BaseDir,'/etc/group');
Halt(1);
End;
While Not EOF(FileGroup)do Begin
ReadLn(FileGroup,CurrLine);
FillChar(Cells,SizeOf(Cells),0);
CurrWord:='';
CellPos:=0;
For I:=1 to Length(CurrLine) do Begin
If CurrLine[I]=':'Then Begin
Cells[CellPos]:=CurrWord;
CurrWord:='';
Inc(CellPos);
If CellPos>5 Then Break;
End
Else
CurrWord:=CurrWord+CurrLine[I];
End;
If CurrWord<>''Then Begin
Cells[CellPos]:=CurrWord;
Inc(CellPos);
End;
If Cells[0]<>''Then Begin
If(Cells[0]=GroupName)Then Begin
Close(FileGroup);
GroupNameToGroupID:=Cells[2];
Exit;
End;
End;
End;
Close(FileGroup);
End;
Function GroupNameToGroupList(GroupName:String;Verbose:Boolean):String;
Var
FileGroup:Text;
I,CellPos:Integer;
CurrLine,CurrWord,GroupList:String;
Cells:Array[0..6]of String;
Begin
GroupNameToGroupList:='';
If GroupName=''Then Exit;
GroupList:=GroupNameToGroupID(GroupName);
If(Verbose)Then GroupList:=GroupList+'('+GroupName+')';
{$I-}Assign(FileGroup,BaseDir+'/etc/group');
Reset(FileGroup);{$I+}
If IOResult<>0 Then Begin
WriteLn('Impossible de lire le fichier ',BaseDir,'/etc/group');
Halt(1);
End;
While Not EOF(FileGroup)do Begin
ReadLn(FileGroup,CurrLine);
FillChar(Cells,SizeOf(Cells),0);
CurrWord:='';
CellPos:=0;
For I:=1 to Length(CurrLine) do Begin
If CurrLine[I]=':'Then Begin
Cells[CellPos]:=CurrWord;
CurrWord:='';
Inc(CellPos);
If CellPos>5 Then Break;
End
Else
CurrWord:=CurrWord+CurrLine[I];
End;
If CurrWord<>''Then Begin
Cells[CellPos]:=CurrWord;
Inc(CellPos);
End;
If Cells[0]<>''Then Begin
If(Cells[3]=GroupName)Then Begin
If GroupList<>''Then Begin
If(Verbose)Then GroupList:=GroupList+','
ELse GroupList:=GroupList+' ';
End;
GroupList:=GroupList+Cells[2];
If(Verbose)Then GroupList:=GroupList+'('+Cells[0]+')';
End;
End;
End;
Close(FileGroup);
GroupNameToGroupList:=GroupList;
End;
Function UserIdToUserName(UserOrID:String):String;
Var
FilePasswd:Text;
I,CellPos:Integer;
CurrLine,CurrWord:String;
Cells:Array[0..6]of String;
Begin
UserIdToUserName:='';
{$I-}Assign(FilePasswd,BaseDir+'/etc/passwd');
Reset(FilePasswd);{$I+}
If IOResult<>0 Then Begin
WriteLn('Impossible de lire le fichier ',BaseDir,'/etc/passwd');
Halt(1);
End;
While Not EOF(Filepasswd)do Begin
ReadLn(Filepasswd,CurrLine);
FillChar(Cells,SizeOf(Cells),0);
CurrWord:='';
CellPos:=0;
For I:=1 to Length(CurrLine) do Begin
If CurrLine[I]=':'Then Begin
Cells[CellPos]:=CurrWord;
CurrWord:='';
Inc(CellPos);
If CellPos>5 Then Break;
End
Else
CurrWord:=CurrWord+CurrLine[I];
End;
If CurrWord<>''Then Begin
Cells[CellPos]:=CurrWord;
Inc(CellPos);
End;
If Cells[0]<>''Then Begin
If(Cells[0]=UserOrID)or(Cells[2]=UserOrID)Then Begin
UserIdToUserName:=Cells[0];
Close(FilePasswd);
Exit;
End;
End;
End;
Close(FilePasswd);
End;
Function UserNameToUserID(UserOrID:String):String;
Var
FilePasswd:Text;
I,CellPos:Integer;
CurrLine,CurrWord:String;
Cells:Array[0..6]of String;
Begin
UserNameToUserID:='';
{$I-}Assign(FilePasswd,BaseDir+'/etc/passwd');
Reset(FilePasswd);{$I+}
If IOResult<>0 Then Begin
WriteLn('Impossible de lire le fichier ',BaseDir,'/etc/passwd');
Halt(1);
End;
While Not EOF(Filepasswd)do Begin
ReadLn(Filepasswd,CurrLine);
FillChar(Cells,SizeOf(Cells),0);
CurrWord:='';
CellPos:=0;
For I:=1 to Length(CurrLine) do Begin
If CurrLine[I]=':'Then Begin
Cells[CellPos]:=CurrWord;
CurrWord:='';
Inc(CellPos);
If CellPos>5 Then Break;
End
Else
CurrWord:=CurrWord+CurrLine[I];
End;
If CurrWord<>''Then Begin
Cells[CellPos]:=CurrWord;
Inc(CellPos);
End;
If Cells[0]<>''Then Begin
If(Cells[0]=UserOrID)Then Begin
UserNameToUserID:=Cells[2];
Close(FilePasswd);
Exit;
End;
End;
End;
Close(FilePasswd);
End;
Function GroupIdFromUserName(UserOrID:String):String;
Var
FilePasswd:Text;
I,CellPos:Integer;
CurrLine,CurrWord:String;
Cells:Array[0..6]of String;
Begin
GroupIdFromUserName:='';
{$I-}Assign(FilePasswd,BaseDir+'/etc/passwd');
Reset(FilePasswd);{$I+}
If IOResult<>0 Then Begin
WriteLn('Impossible de lire le fichier ',BaseDir,'/etc/passwd');
Halt(1);
End;
While Not EOF(Filepasswd)do Begin
ReadLn(Filepasswd,CurrLine);
FillChar(Cells,SizeOf(Cells),0);
CurrWord:='';
CellPos:=0;
For I:=1 to Length(CurrLine) do Begin
If CurrLine[I]=':'Then Begin
Cells[CellPos]:=CurrWord;
CurrWord:='';
Inc(CellPos);
If CellPos>5 Then Break;
End
Else
CurrWord:=CurrWord+CurrLine[I];
End;
If CurrWord<>''Then Begin
Cells[CellPos]:=CurrWord;
Inc(CellPos);
End;
If Cells[0]<>''Then Begin
If(Cells[0]=UserOrID)Then Begin
GroupIdFromUserName:=Cells[3];
Close(FilePasswd);
Exit;
End;
End;
End;
Close(FilePasswd);
End;
BEGIN
Language:=_French;
TmpLanguage:=GetEnv('LANGUAGE');
If TmpLanguage<>''Then Begin
If TmpLanguage[1]='"'Then TmpLanguage:=Copy(TmpLanguage,2,255);
If StrToUpper(Copy(TmpLanguage,1,2))='EN'Then Language:=_English Else
If StrToUpper(Copy(TmpLanguage,1,2))='GR'Then Language:=_Germany Else
If StrToUpper(Copy(TmpLanguage,1,2))='IT'Then Language:=_Italian Else
If StrToUpper(Copy(TmpLanguage,1,2))='SP'Then Language:=_Spain Else
If(StrToUpper(Copy(TmpLanguage,1,2))='SQ')or
(StrToUpper(Copy(TmpLanguage,1,3))='ALB')Then Language:=_Albanian;
End;
If(ParamStr(1)='/?')or(ParamStr(1)='--help')or(ParamStr(1)='-h')or
(ParamStr(1)='/h')or(ParamStr(1)='/H')Then Begin
Case Language of
_English:Begin
WriteLn('id - Print user and group information for the specified USER,');
WriteLn(' or (when USER omitted) for the current user.');
WriteLn;
WriteLn('Syntax: id [OPTION]... [USER]');
WriteLn;
End;
Else Begin
WriteLn('id : Cette commande permet d''afficher les informations ',
'd''utilisateur ou de groupe pour l''utilisateur spcifi ',
'ou (quand il est omis) pour l''utilisateur courant');
WriteLn;
WriteLn('Syntaxe : id [OPTION]... [user]');
WriteLn;
WriteLn(' user Indique le nom de l''utilisateur');
WriteLn(' -g Affiche uniquement l''ID de groupe effectif');
WriteLn(' -G Affiche tous les ID de groupe');
WriteLn(' -u Affiche uniquement l''ID de l''utilisateur effectif');
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');
End;
End;
End
Else
Begin
Mode:=_None;
BaseDir:='';
Entity:=GetEnv('LOGNAME');
If Entity=''Then Entity:=GetEnv('USERNAME');
If Entity=''Then Entity:='root';
UserName:=Entity;
For I:=1 to ParamCount do Begin
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)='-g'Then Mode:=_GroupId Else
If ParamStr(I)='-G'Then Mode:=_Group Else
If ParamStr(I)='-u'Then Mode:=_User
Else Entity:=ParamStr(I);
End;
Case Mode of
_Group:WriteLn(GroupNameToGroupList(Entity,False));
_GroupId:Begin
UserId:=UserNameToUserID(Entity);
GroupId:=GroupIdFromUserName(Entity);
WriteLn(GroupId);
End;
_User:WriteLn(UserNameToUserID(Entity));
Else Begin
UserId:=UserNameToUserID(Entity);
GroupId:=GroupIdFromUserName(Entity);
GroupName:=GroupIdToGroupName(GroupId);
WriteLn('uid=',UserId,'(',UserName,')',' ',
'gid=',GroupId,'(',GroupName,')',' ',
'groups=',GroupNameToGroupList(GroupName,True));
End;
End;
End;
END.