libch.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. var
  49. errno: plongint; external name '__errno';
  50. type
  51. {
  52. _FILE = record
  53. firstCluster: longword;
  54. length: longword;
  55. curPos: longword;
  56. curClus: longword; // Current cluster to read from
  57. curSect: integer; // Current sector within cluster
  58. curByte: integer; // Current byte within sector
  59. readBuffer: array [0..511] of byte; // Buffer used for unaligned reads
  60. appClus: longword; // Cluster to append to
  61. appSect: integer; // Sector within cluster for appending
  62. appByte: integer; // Byte within sector for appending
  63. read: boolean; // Can read from file
  64. write: boolean; // Can write to file
  65. append: boolean; // Can append to file
  66. inUse: boolean; // This file is open
  67. dirEntSector: longword; // The sector where the directory entry is stored
  68. dirEntOffset: integer; // The offset within the directory sector
  69. end;
  70. P_FILE = ^_FILE;
  71. }
  72. P_iobuf = ^_iobuf;
  73. _iobuf = record
  74. reserved : longint;
  75. end;
  76. _FILE = _iobuf;
  77. P_FILE = ^_FILE;
  78. PP_FILE = ^P_FILE;
  79. const
  80. SEEK_SET = 0;
  81. SEEK_CUR = 1;
  82. SEEK_END = 2;
  83. R_OK = 1;
  84. W_OK = 2;
  85. X_OK = 4;
  86. F_OK = 8;
  87. L_SET = SEEK_SET;
  88. L_INCR = SEEK_CUR;
  89. L_XTND = SEEK_END;
  90. EFF_ONLY_OK = 8;
  91. STDIN_FILENO = 0;
  92. STDOUT_FILENO = 1;
  93. STDERR_FILENO = 2;
  94. (*
  95. ------------------------------------------------------------------------------
  96. Directory iterator for mantaining state between dir* calls
  97. ------------------------------------------------------------------------------
  98. *)
  99. type
  100. DIR_ITER = record
  101. device: longint;
  102. dirStruct: pointer;
  103. end;
  104. PDIR_ITER = ^DIR_ITER;
  105. stat = record
  106. st_dev: longint;
  107. st_ino: longword;
  108. st_mode : longword;
  109. st_nlink : word;
  110. st_uid : word;
  111. st_gid : word;
  112. st_rdev : longint;
  113. st_size : longint;
  114. st_atime : longint;
  115. st_spare1: longint;
  116. st_mtime: longint;
  117. st_spare2: longint;
  118. st_ctime: longint;
  119. st_spare3: longint;
  120. st_blksize: longint;
  121. st_blocks: longint;
  122. st_spare4: array [0..1] of longint;
  123. end;
  124. TStat = stat;
  125. PStat = ^stat;
  126. const
  127. _IFMT = 0170000; // type of file
  128. _IFDIR = 0040000; // directory
  129. _IFCHR = 0020000; // character special
  130. _IFBLK = 0060000; // block special
  131. _IFREG = 0100000; // regular
  132. _IFLNK = 0120000; // symbolic link
  133. _IFSOCK = 0140000; // socket
  134. _IFIFO = 0010000; // fifo
  135. S_BLKSIZE = 1024; // size of a block
  136. S_ISUID = 0004000; // set user id on execution
  137. S_ISGID = 0002000; // set group id on execution
  138. NAME_MAX = 767;
  139. function S_ISBLK(m: longint): boolean; inline;
  140. function S_ISCHR(m: longint): boolean; inline;
  141. function S_ISDIR(m: longint): boolean; inline;
  142. function S_ISFIFO(m: longint): boolean; inline;
  143. function S_ISREG(m: longint): boolean; inline;
  144. function S_ISLNK(m: longint): boolean; inline;
  145. function S_ISSOCK(m: longint): boolean; inline;
  146. type
  147. dirent = record
  148. d_ino: longint;
  149. d_name: array [0..NAME_MAX] of char;
  150. end;
  151. PDirent = ^dirent;
  152. PPDirent = ^PDirent;
  153. DIR = record
  154. position: longint;
  155. dirData: PDIR_ITER;
  156. fileData: dirent;
  157. end;
  158. PDIR = ^DIR;
  159. (* DIR handling *)
  160. function closedir(dirp: PDIR): longint; cdecl; external;
  161. function opendir(const dirname: pchar): PDIR; cdecl; external;
  162. function readdir(dirp: PDIR): PDirent; cdecl; external;
  163. function readdir_r(dirp: PDIR; entry: PDirent; result: PPDirent): longint; cdecl; external;
  164. procedure rewinddir(dirp: PDIR); cdecl; external;
  165. procedure seekdir(dirp: PDIR; loc: longint); cdecl; external;
  166. function telldir(dirp: PDIR): longint; cdecl; external;
  167. function diropen(const path: pchar): PDIR_ITER; cdecl; external;
  168. function dirreset(dirState: PDIR_ITER): longint; cdecl; external;
  169. function dirnext(dirState: PDIR_ITER; filename: pchar; filestat: Pstat): longint; cdecl; external;
  170. function dirclose(dirState: PDIR_ITER): longint; cdecl; external;
  171. (* File handling *)
  172. function fopen(filename: Pchar; modes: Pchar): P_FILE; cdecl; external;
  173. function fread(ptr: pointer; size: longint; n: longint; stream: P_FILE): longint; cdecl; external;
  174. function fread(var ptr; size: longint; n: longint; var stream: _FILE): longint; cdecl; external;
  175. function fwrite(ptr: pointer; size: longint; n: longint; s: P_FILE): longint; cdecl; external;
  176. function fwrite(var ptr; size: longint; n: longint; var s: _FILE): longint; cdecl; external;
  177. function ftell(stream: P_FILE): longint; cdecl; external;
  178. function ftell(var stream: _FILE): longint; cdecl; external;
  179. function fseek(stream: P_FILE; off: longint; whence: longint): longint; cdecl; external;
  180. function fseek(var stream: _FILE; off: longint; whence: longint): longint; cdecl; external;
  181. function fclose(stream: P_FILE): longint; cdecl; external;
  182. function fclose(var stream: _FILE): longint; cdecl; external;
  183. function isatty(fildes: longint): longint; cdecl; external;
  184. function fileno(para1: P_FILE): longint; cdecl; external;
  185. function fileno(var para1: _FILE): longint; cdecl; external;
  186. function fstat(fildes: longint; buf: PStat): longint; cdecl; external;
  187. function fstat(fildes: longint; var buf: TStat): longint; cdecl; external;
  188. function _stat(__file: Pchar; var __buf: Tstat): longint; cdecl; external name 'stat';
  189. function _stat(path: Pchar; buf: Pstat): longint; cdecl; external name 'stat';
  190. function ftruncate(fildes: longint; len: longint): longint; cdecl; external;
  191. function unlink(path: Pchar): longint; cdecl; external;
  192. function rename(para1: Pchar; para2: Pchar): longint; cdecl; external;
  193. function _open(path: Pchar; oflag: longint): longint; cdecl; external name 'open';
  194. function _open(path: Pchar; oflag, mode: longint): longint; cdecl; external name 'open';
  195. function _read(fildes:longint; buf:pointer; nbytes:dword):longint;cdecl;external name 'read';
  196. function _read(fildes:longint; var buf; nbytes:dword):longint;cdecl;external name 'read';
  197. function _write(fildes:longint; buf:pointer; nbytes:dword):longint;cdecl;external name 'write';
  198. function _write(fildes:longint; var buf; nbytes:dword):longint;cdecl;external name 'write';
  199. function _lseek(fildes:longint; offset:longint; whence:longint):longint;cdecl;external name 'lseek';
  200. function _close(fildes:longint): longint; cdecl; external name 'close';
  201. function _unlink(path:Pchar): longint; cdecl; external name 'unlink';
  202. function _rename(para1: Pchar; para2: Pchar): longint; cdecl; external name 'rename';
  203. function _access(path: Pchar; mode: longint): longint; cdecl; external name 'access';
  204. function _tell(fildes: longint): longint; cdecl; external name 'tell';
  205. function _isatty(fildes: longint): longint; cdecl; external name 'isatty';
  206. function _truncate(fildes: longint; len: longint): longint; cdecl; external name 'truncate';
  207. const
  208. F_GETFL = 1; // get file status flags
  209. F_SETFL = 2; // set file status flags
  210. F_DUPFD = 3; // duplicate file descriptor
  211. F_GETFD = 4; // get file descriptor flags
  212. F_SETFD = 5; // set file descriptor flags
  213. F_SETLK = 6; // set record locking info
  214. F_SETLK64 = 16; // set record locking info (64-bit)
  215. F_GETLK = 7; // get record locking info
  216. F_GETLK64 = 17; // get record locking info (64-bit)
  217. F_SETLKW = 8; // get record locking info; wait if blocked
  218. F_SETLKW64 = 18; // get record locking info (64-bit)
  219. F_CLOEXEC = 9; // close on execute
  220. // values for 'l_type' field of 'struct flock'...
  221. F_RDLCK = 1; // shared or read lock
  222. F_WRLCK = 2; // exclusive or write lock
  223. F_UNLCK = 3; // unlock
  224. // values for 'oflag' in open()...
  225. O_RDONLY =$00000000; // open for read only
  226. O_WRONLY =$00000001; // open for write only
  227. O_RDWR =$00000002; // open for read and write
  228. O_ACCMODE =$00000003; // access flags mask
  229. O_reserved1 =$00000004; // reserved
  230. O_reserved2 =$00000008; // reserved
  231. O_APPEND =$00000010; // writes done at end of file
  232. O_CREAT =$00000020; // create new file
  233. O_TRUNC =$00000040; // truncate existing file
  234. O_EXCL =$00000080; // exclusive open
  235. O_NOCTTY =$00000100; // no controlling terminal--unsupported
  236. O_BINARY =$00000200; // binary file--all files
  237. O_NDELAY =$00000400; // nonblocking flag
  238. O_reserved3 =$00000800; // reserved
  239. O_SYNC =$00001000; // synchronized I/O file integrity
  240. O_DSYNC =$00002000; // synchronized I/O data integrity
  241. O_RSYNC =$00004000; // synchronized read I/O
  242. O_NONBLOCK = O_NDELAY; // alias
  243. FD_CLOEXEC =$00008000; // parent closes after call to process()
  244. O_UPDATE =$00010000; // keep legacy files updated
  245. O_FIFO =$00100000; // opening one end of a FIFO [non-standard]
  246. // value for third argument when 'cmd' is F_SETFL in fcntl()...
  247. FNDELAY = O_NDELAY; // fcntl() non-blocking I/O
  248. // 'shflag' values for sopen()...
  249. SH_DENYRW = $00000010; // deny read/write mode
  250. SH_DENYWR = $00000020; // deny write mode
  251. SH_DENYRD = $00000030; // deny read mode
  252. SH_DENYNO = $00000040; // deny none mode
  253. Const
  254. Sys_EPERM = 1; { Operation not permitted }
  255. Sys_ENOENT = 2; { No such file or directory }
  256. Sys_ESRCH = 3; { No such process }
  257. Sys_EINTR = 4; { Interrupted system call }
  258. Sys_EIO = 5; { I/O error }
  259. Sys_ENXIO = 6; { No such device or address }
  260. Sys_E2BIG = 7; { Arg list too long }
  261. Sys_ENOEXEC = 8; { Exec format error }
  262. Sys_EBADF = 9; { Bad file number }
  263. Sys_ECHILD = 10; { No child processes }
  264. Sys_EAGAIN = 11; { Try again }
  265. Sys_ENOMEM = 12; { Out of memory }
  266. Sys_EACCES = 13; { Permission denied }
  267. Sys_EFAULT = 14; { Bad address }
  268. Sys_ENOTBLK = 15; { Block device required, NOT POSIX! }
  269. Sys_EBUSY = 16; { Device or resource busy }
  270. Sys_EEXIST = 17; { File exists }
  271. Sys_EXDEV = 18; { Cross-device link }
  272. Sys_ENODEV = 19; { No such device }
  273. Sys_ENOTDIR = 20; { Not a directory }
  274. Sys_EISDIR = 21; { Is a directory }
  275. Sys_EINVAL = 22; { Invalid argument }
  276. Sys_ENFILE = 23; { File table overflow }
  277. Sys_EMFILE = 24; { Too many open files }
  278. Sys_ENOTTY = 25; { Not a typewriter }
  279. Sys_ETXTBSY = 26; { Text file busy. The new process was
  280. a pure procedure (shared text) file which was
  281. open for writing by another process, or file
  282. which was open for writing by another process,
  283. or while the pure procedure file was being
  284. executed an open(2) call requested write access
  285. requested write access.}
  286. Sys_EFBIG = 27; { File too large }
  287. Sys_ENOSPC = 28; { No space left on device }
  288. Sys_ESPIPE = 29; { Illegal seek }
  289. Sys_EROFS = 30; { Read-only file system }
  290. Sys_EMLINK = 31; { Too many links }
  291. Sys_EPIPE = 32; { Broken pipe }
  292. Sys_EDOM = 33; { Math argument out of domain of func }
  293. Sys_ERANGE = 34; { Math result not representable }
  294. Sys_EDEADLK = 35; { Resource deadlock would occur }
  295. Sys_ENAMETOOLONG= 36; { File name too long }
  296. Sys_ENOLCK = 37; { No record locks available }
  297. Sys_ENOSYS = 38; { Function not implemented }
  298. Sys_ENOTEMPTY= 39; { Directory not empty }
  299. Sys_ELOOP = 40; { Too many symbolic links encountered }
  300. Sys_EWOULDBLOCK = Sys_EAGAIN; { Operation would block }
  301. Sys_ENOMSG = 42; { No message of desired type }
  302. Sys_EIDRM = 43; { Identifier removed }
  303. Sys_ECHRNG = 44; { Channel number out of range }
  304. Sys_EL2NSYNC= 45; { Level 2 not synchronized }
  305. Sys_EL3HLT = 46; { Level 3 halted }
  306. Sys_EL3RST = 47; { Level 3 reset }
  307. Sys_ELNRNG = 48; { Link number out of range }
  308. Sys_EUNATCH = 49; { Protocol driver not attached }
  309. Sys_ENOCSI = 50; { No CSI structure available }
  310. Sys_EL2HLT = 51; { Level 2 halted }
  311. Sys_EBADE = 52; { Invalid exchange }
  312. Sys_EBADR = 53; { Invalid request descriptor }
  313. Sys_EXFULL = 54; { Exchange full }
  314. Sys_ENOANO = 55; { No anode }
  315. Sys_EBADRQC = 56; { Invalid request code }
  316. Sys_EBADSLT = 57; { Invalid slot }
  317. Sys_EDEADLOCK= 58; { File locking deadlock error }
  318. Sys_EBFONT = 59; { Bad font file format }
  319. Sys_ENOSTR = 60; { Device not a stream }
  320. Sys_ENODATA = 61; { No data available }
  321. Sys_ETIME = 62; { Timer expired }
  322. Sys_ENOSR = 63; { Out of streams resources }
  323. Sys_ENONET = 64; { Machine is not on the network }
  324. Sys_ENOPKG = 65; { Package not installed }
  325. Sys_EREMOTE = 66; { Object is remote }
  326. Sys_ENOLINK = 67; { Link has been severed }
  327. Sys_EADV = 68; { Advertise error }
  328. Sys_ESRMNT = 69; { Srmount error }
  329. Sys_ECOMM = 70; { Communication error on send }
  330. Sys_EPROTO = 71; { Protocol error }
  331. Sys_EMULTIHOP= 72; { Multihop attempted }
  332. Sys_EDOTDOT = 73; { RFS specific error }
  333. Sys_EBADMSG = 74; { Not a data message }
  334. Sys_EOVERFLOW= 75; { Value too large for defined data type }
  335. Sys_ENOTUNIQ= 76; { Name not unique on network }
  336. Sys_EBADFD = 77; { File descriptor in bad state }
  337. Sys_EREMCHG = 78; { Remote address changed }
  338. Sys_ELIBACC = 79; { Can not access a needed shared library }
  339. Sys_ELIBBAD = 80; { Accessing a corrupted shared library }
  340. Sys_ELIBSCN = 81; { .lib section in a.out corrupted }
  341. Sys_ELIBMAX = 82; { Attempting to link in too many shared libraries }
  342. Sys_ELIBEXEC= 83; { Cannot exec a shared library directly }
  343. Sys_EILSEQ = 84; { Illegal byte sequence }
  344. Sys_ERESTART= 85; { Interrupted system call should be restarted }
  345. Sys_ESTRPIPE= 86; { Streams pipe error }
  346. Sys_EUSERS = 87; { Too many users }
  347. Sys_ENOTSOCK= 88; { Socket operation on non-socket }
  348. Sys_EDESTADDRREQ= 89; { Destination address required }
  349. Sys_EMSGSIZE= 90; { Message too long }
  350. Sys_EPROTOTYPE= 91; { Protocol wrong type for socket }
  351. Sys_ENOPROTOOPT= 92; { Protocol not available }
  352. Sys_EPROTONOSUPPORT= 93; { Protocol not supported }
  353. Sys_ESOCKTNOSUPPORT= 94; { Socket type not supported }
  354. Sys_EOPNOTSUPP= 95; { Operation not supported on transport endpoint }
  355. Sys_EPFNOSUPPORT= 96; { Protocol family not supported }
  356. Sys_EAFNOSUPPORT= 97; { Address family not supported by protocol }
  357. Sys_EADDRINUSE= 98; { Address already in use }
  358. Sys_EADDRNOTAVAIL= 99; { Cannot assign requested address }
  359. Sys_ENETDOWN= 100; { Network is down }
  360. Sys_ENETUNREACH= 101; { Network is unreachable }
  361. Sys_ENETRESET= 102; { Network dropped connection because of reset }
  362. Sys_ECONNABORTED= 103; { Software caused connection abort }
  363. Sys_ECONNRESET= 104; { Connection reset by peer }
  364. Sys_ENOBUFS = 105; { No buffer space available }
  365. Sys_EISCONN = 106; { Transport endpoint is already connected }
  366. Sys_ENOTCONN= 107; { Transport endpoint is not connected }
  367. Sys_ESHUTDOWN= 108; { Cannot send after transport endpoint shutdown }
  368. Sys_ETOOMANYREFS= 109; { Too many references: cannot splice }
  369. Sys_ETIMEDOUT= 110; { Connection timed out }
  370. Sys_ECONNREFUSED= 111; { Connection refused }
  371. Sys_EHOSTDOWN= 112; { Host is down }
  372. Sys_EHOSTUNREACH= 113; { No route to host }
  373. Sys_EALREADY= 114; { Operation already in progress }
  374. Sys_EINPROGRESS= 115; { Operation now in progress }
  375. Sys_ESTALE = 116; { Stale NFS file handle }
  376. Sys_EUCLEAN = 117; { Structure needs cleaning }
  377. Sys_ENOTNAM = 118; { Not a XENIX named type file }
  378. Sys_ENAVAIL = 119; { No XENIX semaphores available }
  379. Sys_EISNAM = 120; { Is a named type file }
  380. Sys_EREMOTEIO= 121; { Remote I/O error }
  381. Sys_EDQUOT = 122; { Quota exceeded }
  382. { This value was suggested by Daniel
  383. based on infos from www.linuxassembly.org }
  384. Sys_ERROR_MAX = $fff;