sysfile.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2005 by Free Pascal development team
  4. Low level file functions
  5. Nintendo DS does not have any drive, so no file handling is needed.
  6. Copyright (c) 2006 by Francesco Lombardi
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {****************************************************************************
  14. Low level File Routines
  15. All these functions can set InOutRes on errors
  16. ****************************************************************************}
  17. { close a file from the handle value }
  18. procedure do_close(handle: THandle);
  19. begin
  20. fclose(P_FILE(Handle));
  21. end;
  22. procedure do_erase(p: pchar);
  23. begin
  24. unlink(p);
  25. end;
  26. procedure do_rename(p1, p2: pchar);
  27. begin
  28. rename(p1, p2);
  29. end;
  30. function do_write(h: THandle; addr: pointer; len: longint) : longint;
  31. begin
  32. result := fwrite(addr, 1, len, P_FILE(h));
  33. end;
  34. function do_read(h: THandle; addr: pointer; len: longint) : longint;
  35. begin
  36. result := fread(addr, 1, len, P_FILE(h));
  37. end;
  38. function do_filepos(handle: THandle): longint;
  39. begin
  40. result := ftell(P_FILE(handle));
  41. end;
  42. procedure do_seek(handle: THandle; pos: longint);
  43. begin
  44. fseek(P_FILE(handle), pos, SEEK_SET);
  45. end;
  46. function do_seekend(handle: THandle): longint;
  47. begin
  48. result := fseek(P_FILE(handle), 0, SEEK_END);
  49. end;
  50. function do_filesize(handle: THandle): longint;
  51. var
  52. res : LONGINT;
  53. statbuf : TStat;
  54. begin
  55. res := fstat(fileno(P_FILE(handle)), statbuf);
  56. if res = 0 then
  57. result := statbuf.st_size
  58. else
  59. result := -1;
  60. end;
  61. { truncate at a given position }
  62. procedure do_truncate(handle: THandle; pos: longint);
  63. begin
  64. ftruncate(fileno(P_FILE(handle)), pos);
  65. end;
  66. procedure do_open(var f; p: pchar; flags: longint);
  67. var
  68. oflags : string[10];
  69. begin
  70. { close first if opened }
  71. if ((flags and $10000) = 0) then
  72. begin
  73. case FileRec(f).mode of
  74. fminput,fmoutput,fminout : Do_Close(FileRec(f).Handle);
  75. fmclosed : ;
  76. else
  77. begin
  78. inoutres:=102; {not assigned}
  79. exit;
  80. end;
  81. end;
  82. end;
  83. { reset file Handle }
  84. FileRec(f).Handle:=UnusedHandle;
  85. { We do the conversion of filemodes here, concentrated on 1 place }
  86. case (flags and 3) of
  87. 0 : begin
  88. oflags := 'rb'#0;
  89. filerec(f).mode := fminput;
  90. end;
  91. 1 : begin
  92. if (flags and $1000)=$1000 then
  93. oflags := 'w+b' else
  94. oflags := 'wb';
  95. filerec(f).mode := fmoutput;
  96. end;
  97. 2 : begin
  98. if (flags and $1000)=$1000 then
  99. oflags := 'w+' else
  100. oflags := 'r+';
  101. filerec(f).mode := fminout;
  102. end;
  103. end;
  104. {if (flags and $1000)=$1000 then
  105. oflags:=oflags or (O_CREAT or O_TRUNC)
  106. else
  107. if (flags and $100)=$100 then
  108. oflags:=oflags or (O_APPEND);}
  109. { empty name is special }
  110. if p[0]=#0 then
  111. begin
  112. case FileRec(f).mode of
  113. fminput :
  114. FileRec(f).Handle:=StdInputHandle;
  115. fminout, { this is set by rewrite }
  116. fmoutput :
  117. FileRec(f).Handle:=StdOutputHandle;
  118. fmappend :
  119. begin
  120. FileRec(f).Handle:=StdOutputHandle;
  121. FileRec(f).mode:=fmoutput; {fool fmappend}
  122. end;
  123. end;
  124. exit;
  125. end;
  126. { real open call }
  127. FileRec(f).Handle := THandle(fopen(p, @oflags[1]));//_open(p,oflags,438);
  128. //WriteLn ('_open (',p,') returned ',ErrNo, 'Handle: ',FileRec(f).Handle);
  129. // errno does not seem to be set on succsess ??
  130. {IF FileRec(f).Handle < 0 THEN
  131. if (ErrNo=Sys_EROFS) and ((OFlags and O_RDWR)<>0) then
  132. begin // i.e. for cd-rom
  133. Oflags:=Oflags and not(O_RDWR);
  134. FileRec(f).Handle := _open(p,oflags,438);
  135. end;}
  136. {
  137. if FileRec(f).Handle = 0 then
  138. Errno2Inoutres
  139. else
  140. InOutRes := 0;
  141. }
  142. end;
  143. function do_isdevice(handle: THandle): boolean;
  144. begin
  145. result := (isatty(fileno(P_FILE(handle))) > 0);
  146. end;