|
| 1 | +{ @author: Sylvain Maltais ([email protected]) |
| 2 | + @created: 2021 |
| 3 | + @website(https://www.gladir.com/freebsd-0) |
| 4 | + @abstract(Target: Turbo Pascal, Free Pascal) |
| 5 | +} |
| 6 | + |
| 7 | + |
| 8 | +Program TAIL(Input,Output); |
| 9 | + |
| 10 | +Uses DOS; |
| 11 | + |
| 12 | +Var |
| 13 | + OptionFlag:(_None,_Lines,_Bytes); |
| 14 | + ForceCheck:Boolean; |
| 15 | + I,CurrLinePos,MaxBuffer,MaxLine:Integer; |
| 16 | + EndPos,CurrEndPos:LongInt; |
| 17 | + ByteReaded:Integer; |
| 18 | + NumLine,Err:Word; |
| 19 | + FindEnd:Boolean; |
| 20 | + FileView:File{$IFDEF FPC}of Byte{$ENDIF}; |
| 21 | + Buffer:Array[0..255]of Byte; |
| 22 | + Info:SearchRec; |
| 23 | + FileName,CurrLine,CurrParam:String; |
| 24 | + |
| 25 | +BEGIN |
| 26 | + OptionFlag:=_None; |
| 27 | + MaxLine:=10; |
| 28 | + MaxBuffer:=32767; |
| 29 | + ForceCheck:=False; |
| 30 | + FileName:=''; |
| 31 | + If(ParamStr(1)='/?')or(ParamStr(1)='--help')or(ParamStr(1)='-h')Then Begin |
| 32 | + WriteLn('TAIL : Cette commande permet d''afficher la fin du fichier.'); |
| 33 | + WriteLn; |
| 34 | + WriteLn('Syntaxe : TAIL [--lines lignes] [--bytes octets] [-f] nomdufichier'); |
| 35 | + WriteLn; |
| 36 | + WriteLn(' -f Ce parametre permet de forcer la surveiller de ', |
| 37 | + 'la fin du fichier sans arret.'); |
| 38 | + WriteLn(' --lines lignes Ce parametre permet d''indiquer le nombre de ', |
| 39 | + 'ligne a lire.'); |
| 40 | + WriteLn(' --bytes octets Ce parametre permet d''indiquer le nombre ', |
| 41 | + 'd''octets a lire.'); |
| 42 | + End |
| 43 | + Else |
| 44 | + If ParamCount > 0 Then Begin |
| 45 | + For I:=1 to ParamCount do Begin |
| 46 | + CurrParam:=ParamStr(I); |
| 47 | + If OptionFlag<>_None Then Begin |
| 48 | + Case OptionFlag of |
| 49 | + _Lines:Val(CurrParam,MaxLine,Err); |
| 50 | + _Bytes:Val(CurrParam,MaxBuffer,Err); |
| 51 | + End; |
| 52 | + OptionFlag:=_None; |
| 53 | + End |
| 54 | + Else |
| 55 | + If CurrParam='-f'Then ForceCheck:=True Else |
| 56 | + If CurrParam='--lines'Then OptionFlag:=_Lines Else |
| 57 | + If CurrParam='--bytes'Then OptionFlag:=_Bytes Else |
| 58 | + If Copy(CurrParam,1,8)='--bytes='Then Begin |
| 59 | + Val(Copy(CurrParam,9,255),MaxBuffer,Err); |
| 60 | + End |
| 61 | + Else |
| 62 | + If Copy(CurrParam,1,8)='--lines='Then Begin |
| 63 | + Val(Copy(CurrParam,9,255),Maxline,Err); |
| 64 | + End |
| 65 | + Else |
| 66 | + If((Length(CurrParam)>=2) and (CurrParam[1]='-')and(CurrParam[2] in['0'..'9']))Then Begin |
| 67 | + Val(Copy(CurrParam,2,255),MaxLine,Err); |
| 68 | + End |
| 69 | + Else |
| 70 | + Begin |
| 71 | + FileName:=ParamStr(I); |
| 72 | + OptionFlag:=_None; |
| 73 | + End; |
| 74 | + End; |
| 75 | + CurrLinePos:=0; |
| 76 | + {$I-}Assign(FileView,FileName); |
| 77 | + Reset(FileView{$IFNDEF FPC},1{$ENDIF});{$I+} |
| 78 | + If IOResult<>0Then Begin |
| 79 | + WriteLn('Fichier introuvable ou impossible a lire !'); |
| 80 | + Halt; |
| 81 | + End; |
| 82 | + EndPos:=FileSize(FileView); |
| 83 | + NumLine:=0;FindEnd:=False; |
| 84 | + If MaxBuffer<>32767Then Begin |
| 85 | + Dec(EndPos,MaxBuffer); |
| 86 | + If EndPos < 0Then EndPos:=0; |
| 87 | + MaxLine := 1000; |
| 88 | + End |
| 89 | + Else |
| 90 | + Repeat |
| 91 | + CurrEndPos:=EndPos-SizeOf(Buffer); |
| 92 | + If CurrEndPos<0Then CurrEndPos:=0; |
| 93 | + Seek(FileView,CurrEndPos); |
| 94 | + BlockRead(FileView,Buffer,SizeOf(Buffer),ByteReaded); |
| 95 | + If ByteReaded<=0Then Break; |
| 96 | + For I:=ByteReaded-1 downto 0do Begin |
| 97 | + If Buffer[I]=10Then Begin |
| 98 | + Inc(NumLine); |
| 99 | + If NumLine>MaxLine Then Begin |
| 100 | + Dec(EndPos,SizeOf(Buffer)-I); |
| 101 | + FindEnd:=True; |
| 102 | + Break; |
| 103 | + End; |
| 104 | + End; |
| 105 | + End; |
| 106 | + If(FindEnd)Then Break; |
| 107 | + Dec(EndPos,ByteReaded); |
| 108 | + Until EndPos<=0; |
| 109 | + Seek(FileView,EndPos); |
| 110 | + While Not EOF(FileView)do Begin |
| 111 | + BlockRead(FileView,Buffer,SizeOf(Buffer),ByteReaded); |
| 112 | + If ByteReaded=0Then Break; |
| 113 | + For I:=0 to ByteReaded-1 do Begin |
| 114 | + Case Buffer[I]of |
| 115 | + 13:Begin |
| 116 | + Inc(CurrLinePos); |
| 117 | + WriteLn; |
| 118 | + End; |
| 119 | + 10:; |
| 120 | + Else Write(Char(Buffer[I])); |
| 121 | + End; |
| 122 | + End; |
| 123 | + If CurrLinePos>=MaxLine Then Break; |
| 124 | + End; |
| 125 | + Close(FileView); |
| 126 | + If(ForceCheck)Then Begin |
| 127 | + Repeat |
| 128 | + FindFirst(FileName,AnyFile,Info); |
| 129 | + If(DosError=0)and(Info.Size>EndPos)Then Begin |
| 130 | + {$I-}Assign(FileView,FileName); |
| 131 | + If(ForceCheck)Then FileMode:=0 + $30 + $80; { Fixe le mode Lecture seulement + Partage } |
| 132 | + Reset(FileView{$IFNDEF FPC},1{$ENDIF});{$I+} |
| 133 | + Seek(FileView,EndPos); |
| 134 | + BlockRead(FileView,Buffer,SizeOf(Buffer),ByteReaded); |
| 135 | + If ByteReaded>0Then For I:=0 to ByteReaded-1 do Begin |
| 136 | + Case Buffer[I]of |
| 137 | + 13:Begin |
| 138 | + Inc(CurrLinePos); |
| 139 | + WriteLn; |
| 140 | + End; |
| 141 | + 10:; |
| 142 | + Else Write(Char(Buffer[I])); |
| 143 | + End; |
| 144 | + End; |
| 145 | + Inc(EndPos,ByteReaded); |
| 146 | + Close(FileView); |
| 147 | + End; |
| 148 | + Until True=False; |
| 149 | + End; |
| 150 | + End |
| 151 | + Else |
| 152 | + WriteLn('Parametre requis !'); |
| 153 | +END. |
0 commit comments