This repository was archived by the owner on Oct 11, 2022. It is now read-only.
forked from nickelsworth/swag
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0078.pas
More file actions
486 lines (453 loc) · 15.8 KB
/
Copy path0078.pas
File metadata and controls
486 lines (453 loc) · 15.8 KB
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
{
Here's a unit I wrote to handle files and directories. It has procedures
similare to SetFAttr and GetFAttr, plus two others dealing with file
attributes. It also has a procedure to return a linked list of all the
files in the current directory, three procedure to work with that (I may
write one to sort it later), and one to dispose of the linked list.
At the end of the unit will be a program called attribs that uses it. It's
basically the same as DOS's attrib with some added features, such as: It
now works on directories too (i.e. you can now hide directorys), you can
list only the files and directories with certain attributes set, you can
list only directorys, etc...
As always, comments, flames, criticism (constructive or otherwise), and
even "this sucks!" or "cool!" are welcome.
-Rick
rick.haines@cde.com
}
{$A+,B-,D-,E+,F-,G+,I+,L+,N-,O-,P-,Q-,R-,S-,T-,V+,X+,Y+}
{$M 16384,0,655360}
{ ********************************************************** }
{ *********************** Files Unit *********************** }
{ ********************************************************** }
{ **************** Written by: Rick Haines ***************** }
{ **************************** rick.haines@cde.com ********* }
{ ********************************************************** }
{ ***************** Last Revised 03/29/95 ****************** }
{ ********************************************************** }
Unit Files;
Interface
Const
NormalF = $0; { Normal File }
ReadOnlyF = $1; { ReadOnly File }
HiddenF = $2; { Hidden File }
SystemF = $4; { System File }
VolLabel = $8; { Volume Label }
SubDir = $10; { Sub Directory }
ArchiveF = $20; { Archive File }
AllFiles = $3F; { All Files }
{Reserved = $40;}
{Reserved = $80;}
fOK = $0; { No Error }
fFileNF = $2; { File Not Found }
fPathNF = $3; { Path Not Found }
fAccessD = $5; { Access Denied }
fgError = $120; { Other Error }
Type
FileListP = ^FileListT;
FileListT = Record
Name : String[12];
Attr : Byte;
Size : LongInt;
Next : FileListP;
End;
Function SetNewFileAttr(FileName : String; Attr : Byte) : Integer;
{ Sets Attr, Clears what is already set }
Function SetFileAttr(FileName : String; Attr : Byte) : Integer;
{ Sets Attr, leaves the rest }
Function ClearFileAttr(FileName : String; Attr : Byte) : Integer;
{ Clears Attr, leaves the rest }
Function GetFileAttr(FileName : String) : Byte;
{ Returns Attr }
Function GetFileList : FileListP;
{ Returns a Linked List of all files in current directory }
Procedure FilterAttr(Var List : FileListP; Attr : Byte);
{ Filter out all files without Attr }
Procedure FilterName(Var List : FileListP; Name : String);
{ Filter out all files that don't match Name }
Procedure FilterNameAttr(Var List : FileListP; Name : String; Attr : Byte);
{ Last two Procedures Combined }
Procedure DisposeFileList(Var List : FileListP);
{ Disposes of the Linked List }
Implementation
Uses Dos;
Procedure NullString; Assembler;
{ DS:DX = Pascal String }
{ Return : DS:DX = Null String }
{ AX = fOK, Success }
Asm
Mov bx, dx
Mov cl, Byte Ptr ds:[bx] { Get Length }
Mov ax, fFileNF { Set Error }
Cmp cl, 254 { Is it too long? }
JA @Done { Yes, then exit }
Xor ch, ch
Add bx, cx { Offset + Length }
Inc bx { Next Byte }
Mov Byte Ptr ds:[bx], 0 { Null Term. String }
Inc dx { Get rid of length Byte }
Mov ax, fOK { Return No Error }
@Done:
End;
Function SetNewFileAttr(FileName : String; Attr : Byte) : Integer; Assembler;
Asm
Push ds
Lds dx, FileName { Pascal String of FileName }
Call NullString { Change to a Null String }
Cmp ax, fOK { Change OK? }
JA @Done { If not then Exit }
Mov ah, 43h { Dos Function 43h, File Change Mode }
Mov al, 1 { Change Attributes }
Mov cl, Attr { Set Whatever Attributes }
Int 21h { Call Dos }
JC @Done { See if there was an error }
Mov ax, fOK { If Not, Then No Error }
@Done:
Pop ds
End;
Function SetFileAttr(FileName : String; Attr : Byte) : Integer; Assembler;
Asm
Push ds
Lds dx, FileName { Pascal String of FileName }
Call NullString { Change to a Null String }
Cmp ax, fOK { Change OK? }
JA @Done { If not then Exit }
Mov ah, 43h { Dos Function 43h, File Change Mode }
Mov al, 0 { Return Attributes }
Int 21h { Call Dos }
JC @Done { See if there was an error }
Mov ah, 43h { Dos Function 43h, File Change Mode }
Mov al, 1 { Set File Attributes }
Or cl, Attr { Set Whatever Attributes }
Int 21h { Call Dos }
JC @Done { See if there was an error }
Mov ax, fOK { If Not, Then No Error }
@Done:
Pop ds
End;
Function ClearFileAttr(FileName : String; Attr : Byte) : Integer; Assembler;
Asm
Push ds
Lds dx, FileName { Pascal String of FileName }
Call NullString { Change to a Null String }
Cmp ax, fOK { Change OK? }
JA @Done { If not then Exit }
Mov ah, 43h { Dos Function 43h, File Change Mode }
Mov al, 0 { Return Attributes }
Int 21h { Call Dos }
JC @Done { See if there was an error }
Mov ah, 43h
Mov al, 1 { Set File Attributes }
Mov bl, Attr { bl := Attr }
Not bl { Not bl (Attr) }
And cl, bl { Clear Whatever Attributes }
Int 21h { Call Dos }
JC @Done { See if there was an error }
Mov ax, fOK { If Not, Then No Error }
@Done:
Pop ds
End;
Function GetFileAttr(FileName : String) : Byte; Assembler;
Asm
Push ds { Push Data Segment }
Lds dx, FileName { Pascal String of FileName }
Call NullString { Change to a Null String }
Cmp ax, fOK { Change OK? }
JA @Done { If not then Exit }
Mov ah, 43h { Dos Function 43h, File Change Mode }
Mov al, 0 { Return Attributes }
Int 21h { Call Dos }
JC @Error { See if there was an error }
Mov ax, cx { Return Attributes }
Jmp @Done
@Error:
Mov ax, fgError { Return Error }
@Done:
Pop ds { Pop Data Segment }
End;
Function GetFileList : FileListP;
Var
Dir : SearchRec;
Temp,
Last : FileListP;
I : Word;
Begin
FindFirst('????????.???', AllFiles, Dir);
New(Temp);
GetFileList := Temp;
Repeat
Temp^.Name := Dir.Name;
Temp^.Attr := Dir.Attr;
Temp^.Size := Dir.Size;
Last := Temp;
New(Temp^.Next);
Temp := Temp^.Next;
FindNext(Dir);
Until DosError <> 0;
Dispose(Temp);
Last^.Next := Nil;
End;
Procedure RemoveLink(List : FileListP);
Var
Next : FileListP;
Begin
If List^.Next = Nil Then Exit;
Next := List^.Next^.Next;
Dispose(List^.Next);
List^.Next := Next;
End;
Procedure FilterAttr(Var List : FileListP; Attr : Byte);
Var
Temp,
Last : FileListP;
Begin
If List = Nil Then Exit;
Last := List;
Temp := Last^.Next;
While Temp <> Nil Do
Begin
If Temp^.Attr And Attr <> Attr Then RemoveLink(Last)
Else Last := Last^.Next;
Temp := Last^.Next;
End;
Temp := List;
If Temp^.Attr And Attr <> Attr Then
Begin
New(Last);
Last := Temp^.Next;
Dispose(Temp);
Temp := Last;
List := Temp;
End;
End;
Function EqualNames(S1, S2 : String) : Boolean; { Borrowed from SWAG }
Var
STmp1 : String[8];
STmp2 : String[3];
SS1, SS2 : String[12];
I : Integer;
Begin
STmp1 := Copy(S1, 1, Pos('.', S1+'.'))+'????????';
If (Pos('.', S1) > 1) Then STmp2 := Copy(S1, Pos('.', S1)+1, 3)+'???'
Else STmp2 := '???';
For I := 1 To 8 Do If STmp1[I] = '*' Then For I := I To 8 Do
STmp1[I] := '?';
For I := 1 To 3 Do If STmp2[I] = '*' Then For I := I To 3 Do
STmp2[I] := '?';
SS1 := STmp1+'.'+STmp2;
STmp1 := Copy(S2, 1, Pos('.', S2+'.'))+'????????';
If (Pos('.', S2) > 1) Then STmp2 := Copy(S2, Pos('.', S2)+1, 3)+'???'
Else STmp2 := '???';
For I := 1 To 8 Do If STmp1[I] = '*' Then For I := I To 8 Do
STmp1[I] := '?';
For I := 1 To 3 Do If STmp2[I] = '*' Then For I := I To 3 Do
STmp2[I] := '?';
SS2 := STmp1+'.'+STmp2;
EqualNames := False;
For I := 1 To 12 Do If (UpCase(SS1[I]) <> UpCase(SS2[I])) And
(SS2[I] <> '?') Then Exit;
EqualNames := True;
End;
Procedure FilterName(Var List : FileListP; Name : String);
Var
Temp,
Last : FileListP;
Begin
If List = Nil Then Exit;
Last := List;
Temp := Last^.Next;
While Temp <> Nil Do
Begin
If Not EqualNames(Temp^.Name, Name) Then RemoveLink(Last)
Else Last := Last^.Next;
Temp := Last^.Next;
End;
Temp := List;
If Not EqualNames(Temp^.Name, Name) Then
Begin
New(Last);
Last := Temp^.Next;
Dispose(Temp);
Temp := Last;
List := Temp;
End;
End;
Procedure FilterNameAttr(Var List : FileListP; Name : String; Attr : Byte);
Begin
FilterName(List, Name);
FilterAttr(List, Attr);
End;
Procedure DisposeFileList(Var List : FileListP);
Var
Temp,
Next : FileListP;
Begin
Temp := List;
While Temp <> Nil Do
Begin
Next := Temp^.Next;
Dispose(Temp);
Temp := Next;
End;
List := Nil;
End;
End.
{ --------------------------- TEST PROGRAM ------------------- }
{$A+,B-,D-,E+,F-,G+,I+,L+,N-,O-,P-,Q-,R-,S-,T-,V+,X+,Y+}
{$M 16384,0,655360}
{ ********************************************************** }
{ ************************* Attribs ************************ }
{ ********************************************************** }
{ **************** Written by: Rick Haines ***************** }
{ **************************** rick.haines@cde.com ********* }
{ ********************************************************** }
{ ***************** Last Revised 03/29/95 ****************** }
{ ********************************************************** }
Program Attribs;
Uses Files;
Var
Path : String;
Lines,
SetAttr,
ClearAttr : Byte;
ListIt : Boolean;
Directory,
TempDir : FileListP;
Procedure HelpMe;
Begin
Writeln;
Writeln('Attribs v1.0a -- Written by Rick Haines.');
Writeln;
Writeln('Format is:');
Writeln(' Attribs [/L] [/D] [FileName] [R+|R-] [H+|H-] [S+|S-] [A+|A-] [D+]');
Writeln;
Writeln('WARNING:');
Writeln(' Without the /L switch, Attribs will change the attributes');
Writeln(' of files instead of listing them!');
Writeln;
Writeln('[/L] - List files & their attributes (If no params, it is assumed)');
Writeln('[/D] - Use with /L to list only directories and their attributes');
Writeln;
Writeln('[FileName] - File(s) to Change/List (WildCards Accepted)');
Writeln(' If not included it is assumed to be *.* ');
Writeln;
Writeln(' Without /L With /L ');
Writeln(' ~~~~~~~~~~ ~~~~~~~ ');
Writeln('[R+|R-] - Make File(s) ReadOnly | View ReadOnly Files');
Writeln('[H+|H-] - Make File(s) Hidden | View Hidden Files ');
Writeln('[S+|S-] - Make File(s) System | View System Files ');
Writeln('[A+|A-] - Make File(s) Archive | View Archive Files ');
Writeln('[D+] - Change Dir Attribs | Do Not Use With /L ');
Halt;
End;
Procedure ParseCommandLine;
Var
I : Byte;
Par : String;
Begin
Path := '*.*';
If ParamCount < 1 Then
Begin
ListIt := True;
Exit;
End;
For I := 1 To ParamCount Do
Begin
Par := ParamStr(I);
Case UpCase(Par[1]) Of
'D' : Case Par[2] Of
'+' : ClearAttr := ClearAttr Or SubDir;
'-' : SetAttr := SetAttr Or SubDir;
Else Path := Par;
End;
'H' : Case Par[2] Of
'+' : SetAttr := SetAttr Or HiddenF;
'-' : ClearAttr := ClearAttr Or HiddenF;
Else Path := Par;
End;
'S' : Case Par[2] Of
'+' : SetAttr := SetAttr Or SystemF;
'-' : ClearAttr := ClearAttr Or SystemF;
Else Path := Par;
End;
'R' : Case Par[2] Of
'+' : SetAttr := SetAttr Or ReadOnlyF;
'-' : ClearAttr := SetAttr Or ReadOnlyF;
Else Path := Par;
End;
'A' : Case Par[2] Of
'+' : SetAttr := SetAttr Or ArchiveF;
'-' : ClearAttr := ClearAttr Or ArchiveF;
Else Path := Par;
End;
'/' : Case UpCase(Par[2]) Of
'L' : ListIt := True;
'D' : SetAttr := SetAttr Or SubDir;
'?' : HelpMe;
Else Path := Par;
End;
Else Path := Par;
End;
End;
End;
Function GetBit(Byte, Bit : Word) : Boolean;
Begin
Byte := Byte And (1 ShL Bit);
GetBit := (Byte = (1 ShL Bit));
End;
Procedure WriteAttr(Attr : Byte);
Begin
If GetBit(Attr, 0) Then Write('R') Else Write(' ');
If GetBit(Attr, 1) Then Write(' H') Else Write(' ');
If GetBit(Attr, 2) Then Write(' S') Else Write(' ');
If GetBit(Attr, 5) Then Write(' A') Else Write(' ');
If GetBit(Attr, 3) Then Write(' V') Else Write(' ');
If GetBit(Attr, 4) Then Write(' Dir') Else Write(' ');
Write(' ');
End;
Function ReadKey : Char; Assembler;
Asm
Mov ax, 0
Int 16h
End;
Begin
SetAttr := NormalF;
ClearAttr := NormalF;
ParseCommandLine;
Directory := GetFileList;
FilterName(Directory, Path);
Writeln;
If ListIt Then
Begin
Lines := 0;
FilterAttr(Directory, SetAttr);
TempDir := Directory;
If TempDir = Nil Then Writeln('No Files Found');
While TempDir <> Nil Do
Begin
WriteAttr(TempDir^.Attr);
Writeln(TempDir^.Name);
TempDir := TempDir^.Next;
Inc(Lines);
If Lines >= 24 Then
Begin
Write('--Press any key to continue--');
ReadKey;
Writeln;
Lines := 0;
End;
End;
End;
If Not ListIt Then
Begin
TempDir := Directory;
While TempDir <> Nil Do
Begin
TempDir^.Attr := (TempDir^.Attr And Not ClearAttr) Or SetAttr;
SetNewFileAttr(TempDir^.Name, TempDir^.Attr);
TempDir := TempDir^.Next;
End;
If Directory = Nil Then Writeln('No Files Found') Else Writeln('Success!');
End;
DisposeFileList(Directory);
End.