osfile.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. {
  2. $Id$
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 1998 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {
  12. This file implements the system-dependent calls needed for the
  13. classes unit.
  14. }
  15. {
  16. oscalls contains a copy of the needed functions from syscalls.inc
  17. to make things go faster.
  18. }
  19. {$i oscalls.inc}
  20. Function OSCreateFile (Const Filename : string; Mode : Longint) : longint;
  21. {
  22. Creates a file with name FileName, using mode MODE
  23. --> Filename : String giving the path to the file.
  24. Mode : Mode can be
  25. fmCreate : Create file;
  26. fmOpenRead : open for reading;
  27. fmOpenWrite : open for writing;
  28. <-- File Handle, or -1 on error.
  29. }
  30. var
  31. regs : SysCallregs;
  32. NewName : string;
  33. Begin
  34. // Convert mode. The meaning of fmcreate isn't
  35. // entirely clear..
  36. if (mode and fmcreate) = fmcreate then
  37. mode:=OPEN_CREAT or OPEN_RDWR;
  38. //!! This MUST be changed when using ansistrings !!!
  39. NewName:=FileName+#0;
  40. regs.reg2:=longint(@NewName[1]);
  41. regs.reg3:=Mode;
  42. regs.reg4:=438; { 666 octal }
  43. Do_SysCall(SysCall_nr_open,regs);
  44. Result:=regs.reg1;
  45. If Result<0 then Result:=-1;
  46. End;
  47. Function OSReadHandle(Handle : Longint;Var Buffer; Count : Longint): Longint;
  48. {
  49. Read from a handle
  50. --> Handle : file,pipe,etc Handle to read from.
  51. Buffer : Location where to put the read bytes.
  52. Count : Number of bytes that should be read
  53. <-- Number of bytes actually read, or -1 on error.
  54. }
  55. var sr : syscallregs;
  56. begin
  57. sr.reg2:=Handle;
  58. sr.reg3:=Longint(@Buffer);
  59. sr.reg4:=Count;
  60. Do_Syscall (syscall_nr_read,sr);
  61. Result:=sr.reg1;
  62. If Result<-1 then Result:=-1;
  63. end;
  64. Function OSWriteHandle(Handle : Longint;var Buffer; Count : longint) : Longint;
  65. {
  66. Write to a handle
  67. --> Handle : file,pipe,etc Handle to write to.
  68. Buffer : Location where to get the bytes.
  69. Count : Number of bytes that should be written.
  70. <-- Number of bytes actually written, or -1 on error.
  71. }
  72. Var sr: syscallregs;
  73. begin
  74. sr.reg2:=Handle;
  75. sr.reg3:=Longint(@Buffer);
  76. sr.reg4:=Count;
  77. Do_Syscall (syscall_nr_write,sr);
  78. Result:=sr.reg1;
  79. If Result<-1 then Result:=-1;
  80. end;
  81. Function OSSetHandleSize (Handle,Size : Longint) : longint;
  82. {
  83. Set size of handle (for files only)
  84. --> Handle : Handle to set size for.
  85. Size : Size to be set.
  86. <-- 0 on succes or -1 on error.
  87. }
  88. Var sr: syscallregs;
  89. begin
  90. sr.reg2:=Handle;
  91. sr.reg3:=Size;
  92. Do_Syscall (SysCall_nr_Ftruncate,sr);
  93. Result:=sr.reg1;
  94. If Result<-1 then Result:=-1;
  95. end;
  96. Function OSSeekHandle (Handle,OffSet,Origin : longint) : longint;
  97. {
  98. Seek Handle position starting from Origin
  99. --> Handle : Handle of file to do seek on.
  100. Offset : Position to seek.
  101. Origin : Where to start seek:
  102. soFromBeginning
  103. soFromCurrent
  104. soFromEnd
  105. <-- -1 on error, else the new fileposition in bytes from the origin.
  106. }
  107. var sr : syscallregs;
  108. begin
  109. sr.reg2:=Handle;
  110. sr.reg3:=Offset;
  111. sr.reg4:=Origin;
  112. Do_Syscall(Syscall_nr_lseek,sr);
  113. Result:=sr.reg1;
  114. If Result<-1 then Result:=-1;
  115. end;
  116. Function OSCloseHandle (Handle : longint) : longint;
  117. {
  118. Close file associated with HAndle.
  119. --> Handle : Handle of file to do seek on.
  120. <-- 0 on succes, -1 on error.
  121. }
  122. Var sr : syscallregs;
  123. begin
  124. sr.reg2:=Handle;
  125. Do_Syscall (syscall_nr_close,sr);
  126. Result:=sr.reg1;
  127. If Result<-1 then Result:=-1;
  128. end;