sysfile.inc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. procedure NDS2PASErr(Err: longint);
  18. begin
  19. if Err = 0 then { Else it will go through all the cases }
  20. exit;
  21. case Err of
  22. Sys_ENFILE,
  23. Sys_EMFILE : Inoutres := 4;
  24. Sys_ENOENT : Inoutres := 2;
  25. Sys_EBADF : Inoutres := 6;
  26. Sys_ENOMEM,
  27. Sys_EFAULT : Inoutres := 217;
  28. Sys_EINVAL : Inoutres := 218;
  29. Sys_EPIPE,
  30. Sys_EINTR,
  31. Sys_EIO,
  32. Sys_EAGAIN,
  33. Sys_ENOSPC : Inoutres := 101;
  34. Sys_ENAMETOOLONG,
  35. Sys_ELOOP,
  36. Sys_ENOTDIR : Inoutres := 3;
  37. Sys_EROFS,
  38. Sys_EEXIST,
  39. Sys_EACCES : Inoutres := 5;
  40. Sys_EBUSY : Inoutres := 162
  41. else begin
  42. Writeln(stderr, 'NDS2PASErr: unknown error ', err);
  43. flush(stderr);
  44. Inoutres := Err;
  45. end;
  46. end;
  47. END;
  48. procedure Errno2Inoutres;
  49. begin
  50. NDS2PASErr(errno^);
  51. end;
  52. procedure SetFileError(var Err: longint);
  53. begin
  54. if Err >= 0 then
  55. InOutRes := 0
  56. else begin
  57. Err := errno^;
  58. NDS2PASErr(Err);
  59. Err := 0;
  60. end;
  61. end;
  62. { close a file from the handle value }
  63. procedure do_close(handle: THandle);
  64. var
  65. res: longint;
  66. begin
  67. //fclose(P_FILE(Handle));
  68. res := _close(handle);
  69. if res <> 0 then
  70. SetFileError(res)
  71. else
  72. InOutRes := 0;
  73. end;
  74. procedure do_erase(p: pchar; pchangeable: boolean);
  75. var
  76. res: longint;
  77. begin
  78. //unlink(p);
  79. res := _unlink(p);
  80. if res <> 0 then
  81. SetFileError(res)
  82. else
  83. InOutRes := 0;
  84. end;
  85. procedure do_rename(p1, p2: pchar; p1changeable, p2changeable: boolean);
  86. var
  87. res: longint;
  88. begin
  89. //rename(p1, p2);
  90. res := _rename(p1, p2);
  91. if res <> 0 then
  92. SetFileError(res)
  93. else
  94. InOutRes := 0;
  95. end;
  96. function do_write(h: THandle; addr: pointer; len: longint) : longint;
  97. var
  98. res: longint;
  99. begin
  100. //result := fwrite(addr, 1, len, P_FILE(h));
  101. res := _write(h, addr, len);
  102. if res > 0 then
  103. InOutRes := 0
  104. else
  105. SetFileError(res);
  106. do_write := res;
  107. end;
  108. function do_read(h: THandle; addr: pointer; len: longint) : longint;
  109. var
  110. res: longint;
  111. begin
  112. //result := fread(addr, 1, len, P_FILE(h));
  113. res := _read(h, addr, len);
  114. if res > 0 then
  115. InOutRes := 0
  116. else
  117. SetFileError(res);
  118. do_read := res;
  119. end;
  120. function do_filepos(handle: THandle): longint;
  121. var
  122. res: longint;
  123. begin
  124. InOutRes := 0;
  125. //result := ftell(P_FILE(handle));
  126. res := _tell(handle);
  127. if res < 0 then
  128. SetFileError(res)
  129. else
  130. InOutRes := 0;
  131. do_filepos := res;
  132. end;
  133. procedure do_seek(handle: THandle; pos: longint);
  134. var
  135. res: longint;
  136. begin
  137. //fseek(P_FILE(handle), pos, SEEK_SET);
  138. _lseek(handle, pos, SEEK_SET);
  139. if res < 0 then
  140. SetFileError(res)
  141. else
  142. InOutRes := 0;
  143. end;
  144. function do_seekend(handle: THandle): longint;
  145. var
  146. res: longint;
  147. begin
  148. //result := fseek(P_FILE(handle), 0, SEEK_END);
  149. res := _lseek(handle, 0, SEEK_END);
  150. if res < 0 then
  151. SetFileError(res)
  152. else
  153. InOutRes := 0;
  154. do_seekend := res;
  155. end;
  156. function do_filesize(handle: THandle): longint;
  157. var
  158. res : longint;
  159. statbuf : TStat;
  160. begin
  161. //res := fstat(fileno(P_FILE(handle)), statbuf);
  162. res := fstat(handle, statbuf);
  163. if res = 0 then
  164. begin
  165. InOutRes := 0;
  166. result := statbuf.st_size
  167. end else
  168. begin
  169. SetFileError(Res);
  170. do_filesize := -1;
  171. end;
  172. end;
  173. { truncate at a given position }
  174. procedure do_truncate(handle: THandle; pos: longint);
  175. var
  176. res : longint;
  177. begin
  178. //ftruncate(fileno(P_FILE(handle)), pos);
  179. res := _truncate(handle, pos);
  180. if res <> 0 then
  181. SetFileError(res)
  182. else
  183. InOutRes := 0;
  184. end;
  185. procedure do_open(var f;p:pchar;flags:longint; pchangeable: boolean);
  186. {
  187. filerec and textrec have both handle and mode as the first items so
  188. they could use the same routine for opening/creating.
  189. when (flags and $10) the file will be append
  190. when (flags and $100) the file will be truncate/rewritten
  191. when (flags and $1000) there is no check for close (needed for textfiles)
  192. }
  193. var
  194. oflags: longint;
  195. begin
  196. { close first if opened }
  197. if ((flags and $10000)=0) then
  198. begin
  199. case FileRec(f).mode of
  200. fminput,fmoutput,fminout : Do_Close(FileRec(f).Handle);
  201. fmclosed : ;
  202. else
  203. begin
  204. inoutres:=102; {not assigned}
  205. exit;
  206. end;
  207. end;
  208. end;
  209. { reset file Handle }
  210. FileRec(f).Handle:=UnusedHandle;
  211. { We do the conversion of filemodes here, concentrated on 1 place }
  212. case (flags and 3) of
  213. 0 : begin
  214. oflags := O_RDONLY;
  215. filerec(f).mode := fminput;
  216. end;
  217. 1 : begin
  218. oflags := O_WRONLY;
  219. filerec(f).mode := fmoutput;
  220. end;
  221. 2 : begin
  222. oflags := O_RDWR;
  223. filerec(f).mode := fminout;
  224. end;
  225. end;
  226. if (flags and $1000) = $1000 then
  227. oflags := oflags or (O_CREAT or O_TRUNC)
  228. else
  229. if (flags and $100) = $100 then
  230. oflags := oflags or (O_APPEND);
  231. { empty name is special }
  232. if p[0] = #0 then
  233. begin
  234. case FileRec(f).mode of
  235. fminput:
  236. FileRec(f).Handle := StdInputHandle;
  237. fminout, { this is set by rewrite }
  238. fmoutput :
  239. FileRec(f).Handle := StdOutputHandle;
  240. fmappend :
  241. begin
  242. FileRec(f).Handle := StdOutputHandle;
  243. FileRec(f).mode := fmoutput; {fool fmappend}
  244. end;
  245. end;
  246. exit;
  247. end;
  248. { real open call }
  249. errno^ := 0;
  250. FileRec(f).Handle := _open(p, oflags, 438);
  251. { open somtimes returns > -1 but errno was set }
  252. if (errno^ <> 0) or (longint(FileRec(f).Handle) < 0) then
  253. if (errno^ = Sys_EROFS) and ((OFlags and O_RDWR) <> 0) then
  254. begin // i.e. for cd-rom
  255. Oflags := Oflags and not(O_RDWR);
  256. FileRec(f).Handle := _open(p,oflags,438);
  257. end;
  258. if (errno^ <> 0) or (longint(FileRec(f).Handle) < 0) then
  259. begin
  260. Errno2Inoutres;
  261. FileRec(f).mode:=fmclosed;
  262. end
  263. else
  264. InOutRes := 0;
  265. end;
  266. function do_isdevice(handle: THandle): boolean;
  267. begin
  268. //result := (isatty(fileno(P_FILE(handle))) > 0);
  269. do_isdevice := (_isatty(handle) > 0);
  270. end;