libch.inc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 functions unit for Nintendo Wii
  5. Copyright (c) 2011 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 memalign(alignment: integer; size: integer): pointer; cdecl; external;
  44. function memset(s: pointer; c: longint; n: integer): pointer; cdecl; external;
  45. function gmtime(timer: ptime_t): ptm; cdecl; external;
  46. function time(timer: ptime_t): time_t; cdecl; external;
  47. type
  48. TSort = function (const a, b: pointer): integer;
  49. procedure qsort(__base: pointer; __nmemb: integer; __size: integer; __compar: TSort); cdecl; external;
  50. function __errno: plongint; cdecl; export;
  51. type
  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. const
  72. SEEK_SET = 0;
  73. SEEK_CUR = 1;
  74. SEEK_END = 2;
  75. (*
  76. ------------------------------------------------------------------------------
  77. Directory iterator for mantaining state between dir* calls
  78. ------------------------------------------------------------------------------
  79. *)
  80. type
  81. DIR_ITER = record
  82. device: longint;
  83. dirStruct: pointer;
  84. end;
  85. PDIR_ITER = ^DIR_ITER;
  86. stat = packed record
  87. st_dev: longint;
  88. st_ino: longword;
  89. st_mode : longword;
  90. st_nlink : word;
  91. st_uid : word;
  92. st_gid : word;
  93. st_rdev : longint;
  94. st_size : longint;
  95. st_atime : longint;
  96. st_spare1: longint;
  97. st_mtime: longint;
  98. st_spare2: longint;
  99. st_ctime: longint;
  100. st_spare3: longint;
  101. st_blksize: longint;
  102. st_blocks: longint;
  103. st_spare4: array [0..1] of longint;
  104. end;
  105. TStat = stat;
  106. PStat = ^stat;
  107. const
  108. _IFMT = 0170000; // type of file
  109. _IFDIR = 0040000; // directory
  110. _IFCHR = 0020000; // character special
  111. _IFBLK = 0060000; // block special
  112. _IFREG = 0100000; // regular
  113. _IFLNK = 0120000; // symbolic link
  114. _IFSOCK = 0140000; // socket
  115. _IFIFO = 0010000; // fifo
  116. S_BLKSIZE = 1024; // size of a block
  117. S_ISUID = 0004000; // set user id on execution
  118. S_ISGID = 0002000; // set group id on execution
  119. NAME_MAX = 767;
  120. function S_ISBLK(m: longint): boolean; inline;
  121. function S_ISCHR(m: longint): boolean; inline;
  122. function S_ISDIR(m: longint): boolean; inline;
  123. function S_ISFIFO(m: longint): boolean; inline;
  124. function S_ISREG(m: longint): boolean; inline;
  125. function S_ISLNK(m: longint): boolean; inline;
  126. function S_ISSOCK(m: longint): boolean; inline;
  127. type
  128. dirent = record
  129. d_ino: longint;
  130. d_name: array [0..NAME_MAX] of char;
  131. end;
  132. PDirent = ^dirent;
  133. PPDirent = ^PDirent;
  134. DIR = record
  135. position: longint;
  136. dirData: PDIR_ITER;
  137. fileData: dirent;
  138. end;
  139. PDIR = ^DIR;
  140. (* DIR handling *)
  141. function closedir(dirp: PDIR): longint; cdecl; external;
  142. function opendir(const dirname: pchar): PDIR; cdecl; external;
  143. function readdir(dirp: PDIR): PDirent; cdecl; external;
  144. function readdir_r(dirp: PDIR; entry: PDirent; result: PPDirent): longint; cdecl; external;
  145. procedure rewinddir(dirp: PDIR); cdecl; external;
  146. procedure seekdir(dirp: PDIR; loc: longint); cdecl; external;
  147. function telldir(dirp: PDIR): longint; cdecl; external;
  148. function diropen(const path: pchar): PDIR_ITER; cdecl; external;
  149. function dirreset(dirState: PDIR_ITER): longint; cdecl; external;
  150. function dirnext(dirState: PDIR_ITER; filename: pchar; filestat: Pstat): longint; cdecl; external;
  151. function dirclose(dirState: PDIR_ITER): longint; cdecl; external;
  152. (* File handling *)
  153. function fopen(filename: Pchar; modes: Pchar): P_FILE; cdecl; external;
  154. function fread(ptr: pointer; size: longint; n: longint; stream: P_FILE): longint; cdecl; external;
  155. function fread(var ptr; size: longint; n: longint; var stream: _FILE): longint; cdecl; external;
  156. function fwrite(ptr: pointer; size: longint; n: longint; s: P_FILE): longint; cdecl; external;
  157. function fwrite(var ptr; size: longint; n: longint; var s: _FILE): longint; cdecl; external;
  158. function ftell(stream: P_FILE): longint; cdecl; external;
  159. function ftell(var stream: _FILE): longint; cdecl; external;
  160. function fseek(stream: P_FILE; off: longint; whence: longint): longint; cdecl; external;
  161. function fseek(var stream: _FILE; off: longint; whence: longint): longint; cdecl; external;
  162. function fclose(stream: P_FILE): longint; cdecl; external;
  163. function fclose(var stream: _FILE): longint; cdecl; external;
  164. function isatty(fildes: longint): longint; cdecl; external;
  165. function fileno(para1: P_FILE): longint; cdecl; external;
  166. function fileno(var para1: _FILE): longint; cdecl; external;
  167. function fstat(fildes: longint; buf: PStat): longint; cdecl; external;
  168. function fstat(fildes: longint; var buf: TStat): longint; cdecl; external;
  169. function _stat(__file:Pchar; var __buf:Tstat):longint; cdecl; external name 'stat';
  170. function ftruncate(fildes: longint; len: longint): longint; cdecl; external;
  171. function unlink(path: Pchar): longint; cdecl; external;
  172. function rename(para1: Pchar; para2: Pchar): longint; cdecl; external;