libch.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 1999-2002 by the Free Pascal development team
  4. libc unit for Nintendo DS
  5. Copyright (c) 2006 by Francesco Lombardi
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *****************************************************************************}
  12. type
  13. time_t = longint;
  14. ptime_t = ^time_t;
  15. Ptm = ^tm;
  16. tm = record
  17. tm_sec: longint;
  18. tm_min: longint;
  19. tm_hour: longint;
  20. tm_mday: longint;
  21. tm_mon: longint;
  22. tm_year: longint;
  23. tm_wday: longint;
  24. tm_yday: longint;
  25. tm_isdst: longint;
  26. end;
  27. (* Some libc functions *)
  28. //function printf(format: Pchar; args: array of const): longint; cdecl; external;
  29. function printf(format: Pchar): longint; cdecl; varargs; external;
  30. //function sprintf(s: Pchar; format: Pchar; args: array of const): longint; cdecl; external;
  31. function sprintf(s: Pchar; format: Pchar): longint; varargs; cdecl; external;
  32. //function iprintf(format: Pchar; args: array of const): longint; cdecl; external;
  33. function iprintf(format: Pchar): longint; varargs; cdecl; external;
  34. //function scanf(format: Pchar; args: array of const): longint; cdecl; external;
  35. function scanf(format: Pchar): longint; cdecl; varargs; external;
  36. //function sscanf(s: Pchar; format: Pchar; args: array of const): longint; cdecl; external;
  37. function sscanf(s: Pchar; format: Pchar): longint; cdecl; varargs; external;
  38. function strcmp(s1: Pchar; s2: Pchar): longint; cdecl; external;
  39. function malloc(size: integer): pointer; cdecl; external;
  40. function realloc(ptr: pointer; size: integer): pointer; cdecl; external;
  41. procedure free(ptr: pointer); cdecl; external;
  42. function memcpy(dest: pointer; src: pointer; n: integer): pointer; cdecl; external;
  43. function gmtime(timer: ptime_t): ptm; cdecl; external;
  44. function time(timer: ptime_t): time_t; cdecl; external;
  45. type
  46. TSort = function (const a, b: pointer): integer;
  47. procedure qsort(__base: pointer; __nmemb: integer; __size: integer; __compar: TSort); cdecl; external;
  48. function __errno: plongint; cdecl; export;
  49. type
  50. _FILE = record
  51. firstCluster: longword;
  52. length: longword;
  53. curPos: longword;
  54. curClus: longword; // Current cluster to read from
  55. curSect: integer; // Current sector within cluster
  56. curByte: integer; // Current byte within sector
  57. readBuffer: array [0..511] of byte; // Buffer used for unaligned reads
  58. appClus: longword; // Cluster to append to
  59. appSect: integer; // Sector within cluster for appending
  60. appByte: integer; // Byte within sector for appending
  61. read: boolean; // Can read from file
  62. write: boolean; // Can write to file
  63. append: boolean; // Can append to file
  64. inUse: boolean; // This file is open
  65. dirEntSector: longword; // The sector where the directory entry is stored
  66. dirEntOffset: integer; // The offset within the directory sector
  67. end;
  68. P_FILE = ^_FILE;
  69. const
  70. SEEK_SET = 0;
  71. SEEK_CUR = 1;
  72. SEEK_END = 2;
  73. R_OK = 1;
  74. W_OK = 2;
  75. X_OK = 4;
  76. F_OK = 8;
  77. L_SET = SEEK_SET;
  78. L_INCR = SEEK_CUR;
  79. L_XTND = SEEK_END;
  80. EFF_ONLY_OK = 8;
  81. STDIN_FILENO = 0;
  82. STDOUT_FILENO = 1;
  83. STDERR_FILENO = 2;
  84. (*
  85. ------------------------------------------------------------------------------
  86. Directory iterator for mantaining state between dir* calls
  87. ------------------------------------------------------------------------------
  88. *)
  89. type
  90. DIR_ITER = record
  91. device: longint;
  92. dirStruct: pointer;
  93. end;
  94. PDIR_ITER = ^DIR_ITER;
  95. stat = record
  96. st_dev: longint;
  97. st_ino: longword;
  98. st_mode : longword;
  99. st_nlink : word;
  100. st_uid : word;
  101. st_gid : word;
  102. st_rdev : longint;
  103. st_size : longint;
  104. st_atime : longint;
  105. st_spare1: longint;
  106. st_mtime: longint;
  107. st_spare2: longint;
  108. st_ctime: longint;
  109. st_spare3: longint;
  110. st_blksize: longint;
  111. st_blocks: longint;
  112. st_spare4: array [0..1] of longint;
  113. end;
  114. TStat = stat;
  115. PStat = ^stat;
  116. const
  117. _IFMT = 0170000; // type of file
  118. _IFDIR = 0040000; // directory
  119. _IFCHR = 0020000; // character special
  120. _IFBLK = 0060000; // block special
  121. _IFREG = 0100000; // regular
  122. _IFLNK = 0120000; // symbolic link
  123. _IFSOCK = 0140000; // socket
  124. _IFIFO = 0010000; // fifo
  125. S_BLKSIZE = 1024; // size of a block
  126. S_ISUID = 0004000; // set user id on execution
  127. S_ISGID = 0002000; // set group id on execution
  128. NAME_MAX = 767;
  129. function S_ISBLK(m: longint): boolean; inline;
  130. function S_ISCHR(m: longint): boolean; inline;
  131. function S_ISDIR(m: longint): boolean; inline;
  132. function S_ISFIFO(m: longint): boolean; inline;
  133. function S_ISREG(m: longint): boolean; inline;
  134. function S_ISLNK(m: longint): boolean; inline;
  135. function S_ISSOCK(m: longint): boolean; inline;
  136. type
  137. dirent = record
  138. d_ino: longint;
  139. d_name: array [0..NAME_MAX] of char;
  140. end;
  141. PDirent = ^dirent;
  142. PPDirent = ^PDirent;
  143. DIR = record
  144. position: longint;
  145. dirData: PDIR_ITER;
  146. fileData: dirent;
  147. end;
  148. PDIR = ^DIR;
  149. (* DIR handling *)
  150. function closedir(dirp: PDIR): longint; cdecl; external;
  151. function opendir(const dirname: pchar): PDIR; cdecl; external;
  152. function readdir(dirp: PDIR): PDirent; cdecl; external;
  153. function readdir_r(dirp: PDIR; entry: PDirent; result: PPDirent): longint; cdecl; external;
  154. procedure rewinddir(dirp: PDIR); cdecl; external;
  155. procedure seekdir(dirp: PDIR; loc: longint); cdecl; external;
  156. function telldir(dirp: PDIR): longint; cdecl; external;
  157. function diropen(const path: pchar): PDIR_ITER; cdecl; external;
  158. function dirreset(dirState: PDIR_ITER): longint; cdecl; external;
  159. function dirnext(dirState: PDIR_ITER; filename: pchar; filestat: Pstat): longint; cdecl; external;
  160. function dirclose(dirState: PDIR_ITER): longint; cdecl; external;
  161. (* File handling *)
  162. function fopen(filename: Pchar; modes: Pchar): P_FILE; cdecl; external;
  163. function fread(ptr: pointer; size: longint; n: longint; stream: P_FILE): longint; cdecl; external;
  164. function fread(var ptr; size: longint; n: longint; var stream: _FILE): longint; cdecl; external;
  165. function fwrite(ptr: pointer; size: longint; n: longint; s: P_FILE): longint; cdecl; external;
  166. function fwrite(var ptr; size: longint; n: longint; var s: _FILE): longint; cdecl; external;
  167. function ftell(stream: P_FILE): longint; cdecl; external;
  168. function ftell(var stream: _FILE): longint; cdecl; external;
  169. function fseek(stream: P_FILE; off: longint; whence: longint): longint; cdecl; external;
  170. function fseek(var stream: _FILE; off: longint; whence: longint): longint; cdecl; external;
  171. function fclose(stream: P_FILE): longint; cdecl; external;
  172. function fclose(var stream: _FILE): longint; cdecl; external;
  173. function isatty(fildes: longint): longint; cdecl; external;
  174. function fileno(para1: P_FILE): longint; cdecl; external;
  175. function fileno(var para1: _FILE): longint; cdecl; external;
  176. function fstat(fildes: longint; buf: PStat): longint; cdecl; external;
  177. function fstat(fildes: longint; var buf: TStat): longint; cdecl; external;
  178. function _stat(__file:Pchar; var __buf:Tstat):longint; cdecl; external name 'stat';
  179. function ftruncate(fildes: longint; len: longint): longint; cdecl; external;
  180. function unlink(path: Pchar): longint; cdecl; external;
  181. function rename(para1: Pchar; para2: Pchar): longint; cdecl; external;
  182. function FpOpen(path: Pchar; oflag: longint): longint; cdecl; external name 'open';
  183. function FpOpen(path: Pchar; oflag, mode: longint): longint; cdecl; external name 'open';
  184. function FpRead(fildes:longint; buf:pointer; nbytes:dword):longint;cdecl;external name 'read';
  185. function FpRead(fildes:longint; var buf; nbytes:dword):longint;cdecl;external name 'read';
  186. function FpWrite(fildes:longint; buf:pointer; nbytes:dword):longint;cdecl;external name 'write';
  187. function FpWrite(fildes:longint; var buf; nbytes:dword):longint;cdecl;external name 'write';
  188. function fplseek(fildes:longint; offset:longint; whence:longint):longint;cdecl;external name 'lseek';
  189. function FpClose(fildes:longint):longint;cdecl;external name 'close';
  190. function FpChsize(fildes:longint; size:dword):longint;cdecl;external name 'chsize';
  191. function FpUnlink(path:Pchar):longint;cdecl;external name 'unlink';
  192. function FpRename(para1: Pchar; para2: Pchar): longint; cdecl; external name 'rename';
  193. function Fpstat(path:Pchar; buf:Pstat):longint;cdecl;external name 'stat';
  194. function Fpstat(path:Pchar; var buf:Tstat):longint;cdecl;external name 'stat';
  195. function FpAccess(path:Pchar; mode:longint):longint;cdecl;external name 'access';
  196. const
  197. F_GETFL = 1; // get file status flags
  198. F_SETFL = 2; // set file status flags
  199. F_DUPFD = 3; // duplicate file descriptor
  200. F_GETFD = 4; // get file descriptor flags
  201. F_SETFD = 5; // set file descriptor flags
  202. F_SETLK = 6; // set record locking info
  203. F_SETLK64 = 16; // set record locking info (64-bit)
  204. F_GETLK = 7; // get record locking info
  205. F_GETLK64 = 17; // get record locking info (64-bit)
  206. F_SETLKW = 8; // get record locking info; wait if blocked
  207. F_SETLKW64 = 18; // get record locking info (64-bit)
  208. F_CLOEXEC = 9; // close on execute
  209. // values for 'l_type' field of 'struct flock'...
  210. F_RDLCK = 1; // shared or read lock
  211. F_WRLCK = 2; // exclusive or write lock
  212. F_UNLCK = 3; // unlock
  213. // values for 'oflag' in open()...
  214. O_RDONLY =$00000000; // open for read only
  215. O_WRONLY =$00000001; // open for write only
  216. O_RDWR =$00000002; // open for read and write
  217. O_ACCMODE =$00000003; // access flags mask
  218. O_reserved1 =$00000004; // reserved
  219. O_reserved2 =$00000008; // reserved
  220. O_APPEND =$00000010; // writes done at end of file
  221. O_CREAT =$00000020; // create new file
  222. O_TRUNC =$00000040; // truncate existing file
  223. O_EXCL =$00000080; // exclusive open
  224. O_NOCTTY =$00000100; // no controlling terminal--unsupported
  225. O_BINARY =$00000200; // binary file--all files
  226. O_NDELAY =$00000400; // nonblocking flag
  227. O_reserved3 =$00000800; // reserved
  228. O_SYNC =$00001000; // synchronized I/O file integrity
  229. O_DSYNC =$00002000; // synchronized I/O data integrity
  230. O_RSYNC =$00004000; // synchronized read I/O
  231. O_NONBLOCK = O_NDELAY; // alias
  232. FD_CLOEXEC =$00008000; // parent closes after call to process()
  233. O_UPDATE =$00010000; // keep legacy files updated
  234. O_FIFO =$00100000; // opening one end of a FIFO [non-standard]
  235. // value for third argument when 'cmd' is F_SETFL in fcntl()...
  236. FNDELAY = O_NDELAY; // fcntl() non-blocking I/O
  237. // 'shflag' values for sopen()...
  238. SH_DENYRW = $00000010; // deny read/write mode
  239. SH_DENYWR = $00000020; // deny write mode
  240. SH_DENYRD = $00000030; // deny read mode
  241. SH_DENYNO = $00000040; // deny none mode