2
$Id: sysutils.pp,v 1.7 2005/02/26 14:38:14 florian Exp $
4
This file is part of the Free Pascal run time library.
5
Copyright (c) 2004 by Karoly Balogh
7
Sysutils unit for MorphOS
9
Based on Amiga version by Carl Eric Codere, and other
12
See the file COPYING.FPC, included in this distribution,
13
for details about the copyright.
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19
**********************************************************************}
29
{ Include platform independent interface part }
32
{ Platform dependent calls }
34
Procedure AddDisk(const path:string);
41
{ Include platform independent implementation part }
45
{ * Include MorphOS specific includes * }
49
{$include doslibd.inc}
50
{$include doslibf.inc}
53
{ * Followings are implemented in the system unit! * }
54
function PathConv(path: shortstring): shortstring; external name 'PATHCONV';
55
procedure AddToList(var l: Pointer; h: LongInt); external name 'ADDTOLIST';
56
procedure RemoveFromList(var l: Pointer; h: LongInt); external name 'REMOVEFROMLIST';
59
MOS_fileList: Pointer; external name 'MOS_FILELIST';
62
{****************************************************************************
64
****************************************************************************}
65
{$I-}{ Required for correct usage of these routines }
68
(****** non portable routines ******)
70
function FileOpen(const FileName: string; Mode: Integer): LongInt;
73
tmpStr : array[0..255] of char;
75
{$WARNING FIX ME! To do: FileOpen Access Modes}
76
tmpStr:=PathConv(FileName)+#0;
77
dosResult:=Open(@tmpStr,MODE_OLDFILE);
81
AddToList(MOS_fileList,dosResult);
87
function FileGetDate(Handle: LongInt) : LongInt;
92
function FileSetDate(Handle, Age: LongInt) : LongInt;
94
// Impossible under unix from FileHandle !!
99
function FileCreate(const FileName: string) : LongInt;
102
tmpStr : array[0..255] of char;
104
tmpStr:=PathConv(FileName)+#0;
105
dosResult:=Open(@tmpStr,MODE_NEWFILE);
109
AddToList(MOS_fileList,dosResult);
111
FileCreate:=dosResult;
115
function FileCreate(const FileName: string; Mode: integer): LongInt;
117
{$WARNING FIX ME! To do: FileCreate Access Modes}
118
FileCreate:=FileCreate(FileName);
122
function FileRead(Handle: LongInt; var Buffer; Count: LongInt): LongInt;
125
if (Count<=0) or (Handle<=0) then exit;
127
FileRead:=dosRead(Handle,@Buffer,Count);
131
function FileWrite(Handle: LongInt; const Buffer; Count: LongInt): LongInt;
134
if (Count<=0) or (Handle<=0) then exit;
136
FileWrite:=dosWrite(Handle,@Buffer,Count);
140
function FileSeek(Handle, FOffset, Origin: LongInt) : LongInt;
145
if (Handle<=0) then exit;
148
fsFromBeginning: seekMode:=OFFSET_BEGINNING;
149
fsFromCurrent : seekMode:=OFFSET_CURRENT;
150
fsFromEnd : seekMode:=OFFSET_END;
153
FileSeek:=dosSeek(Handle, FOffset, seekMode);
156
function FileSeek(Handle: LongInt; FOffset, Origin: Int64): Int64;
158
{$WARNING Need to add 64bit call }
159
FileSeek:=FileSeek(Handle,LongInt(FOffset),LongInt(Origin));
163
procedure FileClose(Handle: LongInt);
165
if (Handle<=0) then exit;
168
RemoveFromList(MOS_fileList,Handle);
172
function FileTruncate(Handle, Size: LongInt): Boolean;
177
if (Handle<=0) then exit;
179
dosResult:=SetFileSize(Handle, Size, OFFSET_BEGINNING);
180
if (dosResult<0) then exit;
186
function DeleteFile(const FileName: string) : Boolean;
188
tmpStr: array[0..255] of char;
190
tmpStr:=PathConv(FileName)+#0;
192
DeleteFile:=dosDeleteFile(@tmpStr);
196
function RenameFile(const OldName, NewName: string): Boolean;
198
tmpOldName, tmpNewName: array[0..255] of char;
200
tmpOldName:=PathConv(OldName)+#0;
201
tmpNewName:=PathConv(NewName)+#0;
203
RenameFile:=dosRename(tmpOldName, tmpNewName);
207
(****** end of non portable routines ******)
210
Function FileAge (Const FileName : String): Longint;
216
dos.GetFTime(F,Time);
217
{ Warning this is not compatible with standard routines
218
since Double are not supported on m68k by default!
224
Function FileExists (Const FileName : String) : Boolean;
230
FileMode := fmOpenRead;
234
If IOResult <> 0 then
244
PDOSSearchRec = ^SearchRec;
246
Function FindFirst (Const Path : String; Attr : Longint; Var Rslt : TSearchRec) : Longint;
248
faSpecial = faHidden or faSysFile or faVolumeID or faDirectory;
255
if Attr and faHidden <> 0 then
256
dosattr := dosattr or Hidden;
257
if Attr and faSysFile <> 0 then
258
dosattr := dosattr or SysFile;
259
if Attr and favolumeID <> 0 then
260
dosattr := dosattr or VolumeID;
261
if Attr and faDirectory <> 0 then
262
dosattr := dosattr or Directory;
264
Rslt.FindHandle := THandle(p);
265
dos.FindFirst(path,dosattr,p^);
266
if DosError <> 0 then
272
Rslt.Name := p^.Name;
273
{ Not compatible with other platforms! }
275
Rslt.Attr := p^.Attr;
276
Rslt.ExcludeAttr := not p^.Attr;
277
Rslt.Size := p^.Size;
283
Function FindNext (Var Rslt : TSearchRec) : Longint;
288
p:= PDOsSearchRec(Rslt.FindHandle);
289
if not assigned(p) then
295
if DosError <> 0 then
301
Rslt.Name := p^.Name;
302
UnpackTime(p^.Time, DT);
303
{ Warning: Not compatible with other platforms }
304
Rslt.time := p^.Time;
305
Rslt.Attr := p^.Attr;
306
Rslt.ExcludeAttr := not p^.Attr;
307
Rslt.Size := p^.Size;
312
Procedure FindClose (Var F : TSearchrec);
317
p:=PDOSSearchRec(f.FindHandle);
318
if not assigned(p) then
323
f.FindHandle := THandle(nil);
326
Function FileGetAttr (Const FileName : String) : Longint;
332
dos.GetFAttr(F,attr);
333
if DosError <> 0 then
340
Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
345
Dos.SetFAttr(F, Attr and $ffff);
346
FileSetAttr := DosError;
351
{****************************************************************************
353
****************************************************************************}
356
The Diskfree and Disksize functions need a file on the specified drive, since this
357
is required for the statfs system call.
358
These filenames are set in drivestr[0..26], and have been preset to :
359
0 - '.' (default drive - hence current dir is ok.)
360
1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
361
2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
362
3 - '/' (C: equivalent of dos is the root partition)
363
4..26 (can be set by you're own applications)
364
! Use AddDisk() to Add new drives !
365
They both return -1 when a failure occurs.
368
FixDriveStr : array[0..3] of pchar=(
376
DriveStr : array[4..26] of pchar;
378
Procedure AddDisk(const path:string);
380
if not (DriveStr[Drives]=nil) then
381
FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
382
GetMem(DriveStr[Drives],length(Path)+1);
383
StrPCopy(DriveStr[Drives],path);
391
Function DiskFree(Drive: Byte): int64;
393
DiskFree := dos.diskFree(Drive);
397
Function DiskSize(Drive: Byte): int64;
399
DiskSize := dos.DiskSize(Drive);
403
Function GetCurrentDir : String;
409
Function SetCurrentDir (Const NewDir : String) : Boolean;
412
result := (IOResult = 0);
416
Function CreateDir (Const NewDir : String) : Boolean;
419
result := (IOResult = 0);
423
Function RemoveDir (Const Dir : String) : Boolean;
426
result := (IOResult = 0);
430
Function DirectoryExists(const Directory: string): Boolean;
434
{ Get old directory }
437
DirectoryExists := (IOResult = 0);
442
{****************************************************************************
444
****************************************************************************}
451
{****************************************************************************
453
****************************************************************************}
455
Procedure GetLocalTime(var SystemTime: TSystemTime);
459
dos.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second,SystemTime.Millisecond);
460
dos.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day, DayOfWeek);
468
{ Fill table entries 0 to 127 }
470
UpperCaseTable[i] := chr(i);
471
for i := 97 to 122 do
472
UpperCaseTable[i] := chr(i - 32);
473
for i := 123 to 191 do
474
UpperCaseTable[i] := chr(i);
475
Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
478
LowerCaseTable[i] := chr(i);
480
LowerCaseTable[i] := chr(i + 32);
481
for i := 91 to 191 do
482
LowerCaseTable[i] := chr(i);
483
Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
487
Procedure InitInternational;
489
InitInternationalGeneric;
493
function SysErrorMessage(ErrorCode: Integer): String;
496
{ Result:=StrError(ErrorCode);}
499
{****************************************************************************
501
****************************************************************************}
503
Function GetEnvironmentVariable(Const EnvVar : String) : String;
506
Result:=Dos.Getenv(shortstring(EnvVar));
508
Function GetEnvironmentVariableCount : Integer;
511
// Result:=FPCCountEnvVar(EnvP);
512
Result:=Dos.envCount;
515
Function GetEnvironmentString(Index : Integer) : String;
518
// Result:=FPCGetEnvStrFromP(Envp,Index);
519
Result:=Dos.EnvStr(Index);
522
function ExecuteProcess (const Path: AnsiString; const ComLine: AnsiString):
525
CommandLine: AnsiString;
529
Dos.Exec (Path, ComLine);
530
if DosError <> 0 then begin
535
CommandLine := Path + ' ' + ComLine;
537
E := EOSError.CreateFmt (SExecuteProcessFailed, [CommandLine, DosError]);
538
E.ErrorCode := DosError;
543
function ExecuteProcess (const Path: AnsiString;
544
const ComLine: array of AnsiString): integer;
546
CommandLine: AnsiString;
551
for I := 0 to High (ComLine) do
552
if Pos (' ', ComLine [I]) <> 0 then
553
CommandLine := CommandLine + ' ' + '"' + ComLine [I] + '"'
555
CommandLine := CommandLine + ' ' + Comline [I];
556
ExecuteProcess := ExecuteProcess (Path, CommandLine);
560
{****************************************************************************
562
****************************************************************************}
566
InitInternational; { Initialize internationalization settings }
571
$Log: sysutils.pp,v $
572
Revision 1.7 2005/02/26 14:38:14 florian
575
Revision 1.6 2005/02/14 17:13:30 peter
578
Revision 1.5 2005/01/30 02:36:14 karoly
581
Revision 1.4 2005/01/12 08:03:42 karoly
582
* Few more Sysutils functions implemented
584
Revision 1.3 2005/01/11 17:44:06 karoly
585
* basic file I/O implemented