2
$Id: sysutils.pp,v 1.9 2003/11/26 20:00:19 florian Exp $
3
This file is part of the Free Pascal run time library.
4
Copyright (c) 1999-2001 by Florian Klaempfl
5
member of the Free Pascal development team
7
Sysutils unit for POSIX compliant systems
9
See the file COPYING.FPC, included in this distribution,
10
for details about the copyright.
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
**********************************************************************}
25
{ Include platform independent interface part }
28
{ Platform dependent calls }
30
Procedure AddDisk(const path:string);
37
{ Include platform independent implementation part }
42
{****************************************************************************
44
****************************************************************************}
47
{ read/write permission for everyone }
48
MODE_OPEN = S_IWUSR OR S_IRUSR OR
53
Function FileOpen (Const FileName : string; Mode : Integer) : Longint;
61
fmOpenRead : Flags:=Flags or O_RDONLY;
62
fmOpenWrite : Flags:=Flags or O_WRONLY;
63
fmOpenReadWrite : Flags:=Flags or O_RDWR;
65
FileHandle:=sys_Open (pchar(FileName),Flags,MODE_OPEN);
66
if (ErrNo=Sys_EROFS) and ((Flags and O_RDWR)<>0) then
68
Flags:=Flags and not(O_RDWR);
69
FileHandle:=sys_open(pchar(FileName),Flags,MODE_OPEN);
71
FileOpen := longint(FileHandle);
73
{ if there was an error, then don't do anything }
74
if FileHandle = -1 then
76
{ now check if the file can actually be used }
77
{ by verifying the locks on the file }
78
lock.l_whence := SEEK_SET;
79
lock.l_start := 0; { from start of file }
80
lock.l_len := 0; { to END of file }
81
if sys_fcntl(FileHandle, F_GETLK, @lock)<>-1 then
83
{ if another process has created a lock on this file }
85
if (lock.l_type = F_WRLCK) then
88
sys_close(FileHandle);
93
if (lock.l_type = F_RDLK) and
94
((Flags = O_RDWR) or Flags = O_WRONLY)) then
97
sys_close(FileHandle);
102
{ now actually set the lock: }
103
{ only the following are simulated with sysutils : }
104
{ - fmShareDenywrite (get exclusive lock) }
105
{ - fmShareExclusive (get exclusive lock) }
106
if ((Mode and fmShareDenyWrite)<>0) or
107
((Mode and fmShareExclusive)<>0) then
109
lock.l_whence := SEEK_SET;
110
lock.l_start := 0; { from stat of file }
111
lock.l_len := 0; { to END of file }
112
lock.l_type := F_WRLCK; { exclusive lock }
113
if sys_fcntl(FileHandle, F_SETLK, @lock)=-1 then
115
sys_close(FileHandel);
124
Function FileCreate (Const FileName : String) : Longint;
127
FileCreate:=sys_Open(pchar(FileName),O_RDWR or O_CREAT or O_TRUNC,MODE_OPEN);
131
Function FileRead (Handle : Longint; Var Buffer; Count : longint) : Longint;
135
FileRead:=sys_read(Handle,pchar(@Buffer),Count);
136
until ErrNo<>Sys_EINTR;
137
If FileRead = -1 then
142
Function FileWrite (Handle : Longint; const Buffer; Count : Longint) : Longint;
146
FileWrite:=sys_write(Handle,pchar(@Buffer),Count);
147
until ErrNo<>Sys_EINTR;
148
if FileWrite = -1 then
153
Function FileSeek (Handle,FOffset,Origin : Longint) : Longint;
159
{ from beginning of file }
160
0 : whence := SEEK_SET;
161
{ from current position }
162
1 : whence := SEEK_CUR;
164
2 : whence := SEEK_END;
168
FileSeek := sys_lseek(Handle,FOffset,whence);
174
Procedure FileClose (Handle : Longint);
180
Function FileTruncate (Handle,Size: Longint) : boolean;
183
if sys_ftruncate(Handle,Size)=0 then
186
FileTruncate := false;
190
Function FileAge (Const FileName : String): Longint;
197
dos.GetFTime(F,Time);
203
Function FileExists (Const FileName : String) : Boolean;
208
if sys_stat(pchar(filename),Info)<>0 then
215
Function UNIXToWinAttr (FN : Pchar; Const Info : Stat) : Longint;
219
If S_ISDIR(Info.st_mode) then
220
Result:=Result or faDirectory ;
221
If (FN[0]='.') and (not (FN[1] in [#0,'.'])) then
222
Result:=Result or faHidden;
223
if (info.st_mode and S_IWUSR)=0 then
224
Result:=Result or fareadonly;
225
If S_ISREG(Info.st_Mode) Then
226
Result:=Result or faSysFile;
233
PDOSSearchRec = ^SearchRec;
235
Function FindFirst (Const Path : String; Attr : Longint; Var Rslt : TSearchRec) : Longint;
238
faSpecial = faHidden or faSysFile or faVolumeID or faDirectory;
244
if Attr and faHidden <> 0 then
245
dosattr := dosattr or Hidden;
246
if Attr and faSysFile <> 0 then
247
dosattr := dosattr or SysFile;
248
if Attr and favolumeID <> 0 then
249
dosattr := dosattr or VolumeID;
250
if Attr and faDirectory <> 0 then
251
dosattr := dosattr or faDirectory;
253
Rslt.FindHandle := THandle(p);
254
dos.FindFirst(path,dosattr,p^);
255
if DosError <> 0 then
261
Rslt.Name := p^.Name;
262
Rslt.Time := p^.Time;
263
Rslt.Attr := p^.Attr;
264
Rslt.ExcludeAttr := not p^.Attr;
265
Rslt.Size := p^.Size;
271
Function FindNext (Var Rslt : TSearchRec) : Longint;
275
p:= PDOsSearchRec(Rslt.FindHandle);
276
if not assigned(p) then
282
if DosError <> 0 then
288
Rslt.Name := p^.Name;
289
Rslt.Time := p^.Time;
290
Rslt.Attr := p^.Attr;
291
Rslt.ExcludeAttr := not p^.Attr;
292
Rslt.Size := p^.Size;
298
Procedure FindClose (Var F : TSearchrec);
304
p:=PDOSSearchRec(f.FindHandle);
305
if not assigned(p) then
310
f.FindHandle := THandle(nil);
313
Function FileGetDate (Handle : Longint) : Longint;
318
If sys_FStat(Handle,Info)<>0 then
321
Result:=Info.st_mtime;
325
Function FileSetDate (Handle,Age : Longint) : Longint;
328
// Impossible under unix from FileHandle !!
333
Function FileGetAttr (Const FileName : String) : Longint;
338
If sys_stat (pchar(FileName),Info)<>0 then
341
Result:=UNIXToWinAttr(Pchar(FileName),Info);
345
Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
352
Function DeleteFile (Const FileName : String) : Boolean;
354
if sys_unlink(pchar(FileName))=0 then
360
Function RenameFile (Const OldName, NewName : String) : Boolean;
363
{ you can directly typecast and ansistring to a pchar }
364
if sys_rename(pchar(OldName),pchar(NewName))=0 then
373
{****************************************************************************
375
****************************************************************************}
378
The Diskfree and Disksize functions need a file on the specified drive, since this
379
is required for the statfs system call.
380
These filenames are set in drivestr[0..26], and have been preset to :
381
0 - '.' (default drive - hence current dir is ok.)
382
1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
383
2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
384
3 - '/' (C: equivalent of dos is the root partition)
385
4..26 (can be set by you're own applications)
386
! Use AddDisk() to Add new drives !
387
They both return -1 when a failure occurs.
390
FixDriveStr : array[0..3] of pchar=(
398
DriveStr : array[4..26] of pchar;
400
Procedure AddDisk(const path:string);
402
if not (DriveStr[Drives]=nil) then
403
FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
404
GetMem(DriveStr[Drives],length(Path)+1);
405
StrPCopy(DriveStr[Drives],path);
413
Function DiskFree(Drive: Byte): int64;
415
DiskFree := dos.diskFree(Drive);
420
Function DiskSize(Drive: Byte): int64;
422
DiskSize := dos.DiskSize(Drive);
428
Function GetCurrentDir : String;
434
Function SetCurrentDir (Const NewDir : String) : Boolean;
437
result := (IOResult = 0);
441
Function CreateDir (Const NewDir : String) : Boolean;
444
result := (IOResult = 0);
448
Function RemoveDir (Const Dir : String) : Boolean;
451
result := (IOResult = 0);
455
Function DirectoryExists(const Directory: string): Boolean;
461
l:=sys_Stat(pchar(Directory),Info);
463
Result:=S_ISDIR(info.st_mode)
469
{****************************************************************************
471
****************************************************************************}
478
{****************************************************************************
480
****************************************************************************}
482
Procedure GetLocalTime(var SystemTime: TSystemTime);
486
dos.GetTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second,SystemTime.Millisecond);
487
dos.GetDate(SystemTime.Year, SystemTime.Month, SystemTime.Day, DayOfWeek);
495
{ Fill table entries 0 to 127 }
497
UpperCaseTable[i] := chr(i);
498
for i := 97 to 122 do
499
UpperCaseTable[i] := chr(i - 32);
500
for i := 123 to 191 do
501
UpperCaseTable[i] := chr(i);
502
Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
505
LowerCaseTable[i] := chr(i);
507
LowerCaseTable[i] := chr(i + 32);
508
for i := 91 to 191 do
509
LowerCaseTable[i] := chr(i);
510
Move (CPISO88591LCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
514
Procedure InitInternational;
519
function SysErrorMessage(ErrorCode: Integer): String;
522
{ Result:=StrError(ErrorCode);}
525
{****************************************************************************
527
****************************************************************************}
529
Function GetEnvironmentVariable(Const EnvVar : String) : String;
532
Result:=Dos.Getenv(shortstring(EnvVar));
536
{****************************************************************************
538
****************************************************************************}
541
InitExceptions; { Initialize exceptions. OS independent }
542
InitInternational; { Initialize internationalization settings }
547
$Log: sysutils.pp,v $
548
Revision 1.9 2003/11/26 20:00:19 florian
549
* error handling for Variants improved
551
Revision 1.8 2003/10/25 23:43:59 hajny
552
* THandle in sysutils common using System.THandle
554
Revision 1.7 2003/10/09 20:13:19 florian
555
* more type alias updates as suggested by DarekM
557
Revision 1.6 2003/04/01 15:57:41 peter
558
* made THandle platform dependent and unique type
560
Revision 1.5 2003/03/29 15:36:58 hajny
561
* DirectoryExists merged from the fixes branch
563
Revision 1.4 2003/03/29 15:16:26 hajny
564
* dummy DirectoryExists added
566
Revision 1.3 2002/09/07 16:01:26 peter
567
* old logs removed and tabs fixed
569
Revision 1.2 2002/08/10 13:42:36 marco
570
* Fixes Posix dir copied to devel branch
572
Revision 1.1.2.5 2002/04/28 07:28:43 carl
575
Revision 1.1.2.4 2002/03/03 08:47:37 carl
576
+ FindFirst / FindNext implemented
578
Revision 1.1.2.3 2002/01/22 07:41:11 michael
579
+ Fixed FileSearch bug in Win32 and made FIleSearch platform independent
b'\\ No newline at end of file'