libch.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. Ptimeval = ^Ttimeval;
  28. Ttimeval = record
  29. tv_sec : longint;
  30. tv_usec : longint;
  31. end;
  32. Timeval = TTimeval;
  33. (* Some libc functions *)
  34. //function printf(format: Pchar; args: array of const): longint; cdecl; external;
  35. function printf(format: Pchar): longint; cdecl; varargs; external;
  36. //function sprintf(s: Pchar; format: Pchar; args: array of const): longint; cdecl; external;
  37. function sprintf(s: Pchar; format: Pchar): longint; varargs; cdecl; external;
  38. //function iprintf(format: Pchar; args: array of const): longint; cdecl; external;
  39. function iprintf(format: Pchar): longint; varargs; cdecl; external;
  40. //function scanf(format: Pchar; args: array of const): longint; cdecl; external;
  41. function scanf(format: Pchar): longint; cdecl; varargs; external;
  42. //function sscanf(s: Pchar; format: Pchar; args: array of const): longint; cdecl; external;
  43. function sscanf(s: Pchar; format: Pchar): longint; cdecl; varargs; external;
  44. function strcmp(s1: Pchar; s2: Pchar): longint; cdecl; external;
  45. function malloc(size: integer): pointer; cdecl; external;
  46. function realloc(ptr: pointer; size: integer): pointer; cdecl; external;
  47. procedure free(ptr: pointer); cdecl; external;
  48. function memcpy(dest: pointer; src: pointer; n: integer): pointer; cdecl; external;
  49. function memalign(alignment: integer; size: integer): pointer; cdecl; external;
  50. function memset(s: pointer; c: longint; n: integer): pointer; cdecl; external;
  51. function gmtime(timer: ptime_t): ptm; cdecl; external;
  52. function time(timer: ptime_t): time_t; cdecl; external;
  53. type
  54. TSort = function (const a, b: pointer): integer;
  55. procedure qsort(__base: pointer; __nmemb: integer; __size: integer; __compar: TSort); cdecl; external;
  56. function __errno: plongint; cdecl; export;
  57. type
  58. _FILE = record
  59. firstCluster: longword;
  60. length: longword;
  61. curPos: longword;
  62. curClus: longword; // Current cluster to read from
  63. curSect: integer; // Current sector within cluster
  64. curByte: integer; // Current byte within sector
  65. readBuffer: array [0..511] of byte; // Buffer used for unaligned reads
  66. appClus: longword; // Cluster to append to
  67. appSect: integer; // Sector within cluster for appending
  68. appByte: integer; // Byte within sector for appending
  69. read: boolean; // Can read from file
  70. write: boolean; // Can write to file
  71. append: boolean; // Can append to file
  72. inUse: boolean; // This file is open
  73. dirEntSector: longword; // The sector where the directory entry is stored
  74. dirEntOffset: integer; // The offset within the directory sector
  75. end;
  76. P_FILE = ^_FILE;
  77. const
  78. SEEK_SET = 0;
  79. SEEK_CUR = 1;
  80. SEEK_END = 2;
  81. (*
  82. ------------------------------------------------------------------------------
  83. Directory iterator for mantaining state between dir* calls
  84. ------------------------------------------------------------------------------
  85. *)
  86. type
  87. DIR_ITER = record
  88. device: longint;
  89. dirStruct: pointer;
  90. end;
  91. PDIR_ITER = ^DIR_ITER;
  92. stat = packed record
  93. st_dev: longint;
  94. st_ino: longword;
  95. st_mode : longword;
  96. st_nlink : word;
  97. st_uid : word;
  98. st_gid : word;
  99. st_rdev : longint;
  100. st_size : longint;
  101. st_atime : longint;
  102. st_spare1: longint;
  103. st_mtime: longint;
  104. st_spare2: longint;
  105. st_ctime: longint;
  106. st_spare3: longint;
  107. st_blksize: longint;
  108. st_blocks: longint;
  109. st_spare4: array [0..1] of longint;
  110. end;
  111. TStat = stat;
  112. PStat = ^stat;
  113. const
  114. _IFMT = 0170000; // type of file
  115. _IFDIR = 0040000; // directory
  116. _IFCHR = 0020000; // character special
  117. _IFBLK = 0060000; // block special
  118. _IFREG = 0100000; // regular
  119. _IFLNK = 0120000; // symbolic link
  120. _IFSOCK = 0140000; // socket
  121. _IFIFO = 0010000; // fifo
  122. S_BLKSIZE = 1024; // size of a block
  123. S_ISUID = 0004000; // set user id on execution
  124. S_ISGID = 0002000; // set group id on execution
  125. NAME_MAX = 767;
  126. function S_ISBLK(m: longint): boolean; inline;
  127. function S_ISCHR(m: longint): boolean; inline;
  128. function S_ISDIR(m: longint): boolean; inline;
  129. function S_ISFIFO(m: longint): boolean; inline;
  130. function S_ISREG(m: longint): boolean; inline;
  131. function S_ISLNK(m: longint): boolean; inline;
  132. function S_ISSOCK(m: longint): boolean; inline;
  133. type
  134. dirent = record
  135. d_ino: longint;
  136. d_name: array [0..NAME_MAX] of char;
  137. end;
  138. PDirent = ^dirent;
  139. PPDirent = ^PDirent;
  140. DIR = record
  141. position: longint;
  142. dirData: PDIR_ITER;
  143. fileData: dirent;
  144. end;
  145. PDIR = ^DIR;
  146. (* DIR handling *)
  147. function closedir(dirp: PDIR): longint; cdecl; external;
  148. function opendir(const dirname: pchar): PDIR; cdecl; external;
  149. function readdir(dirp: PDIR): PDirent; cdecl; external;
  150. function readdir_r(dirp: PDIR; entry: PDirent; result: PPDirent): longint; cdecl; external;
  151. procedure rewinddir(dirp: PDIR); cdecl; external;
  152. procedure seekdir(dirp: PDIR; loc: longint); cdecl; external;
  153. function telldir(dirp: PDIR): longint; cdecl; external;
  154. function diropen(const path: pchar): PDIR_ITER; cdecl; external;
  155. function dirreset(dirState: PDIR_ITER): longint; cdecl; external;
  156. function dirnext(dirState: PDIR_ITER; filename: pchar; filestat: Pstat): longint; cdecl; external;
  157. function dirclose(dirState: PDIR_ITER): longint; cdecl; external;
  158. (* File handling *)
  159. function fopen(filename: Pchar; modes: Pchar): P_FILE; cdecl; external;
  160. function fread(ptr: pointer; size: longint; n: longint; stream: P_FILE): longint; cdecl; external;
  161. function fread(var ptr; size: longint; n: longint; var stream: _FILE): longint; cdecl; external;
  162. function fwrite(ptr: pointer; size: longint; n: longint; s: P_FILE): longint; cdecl; external;
  163. function fwrite(var ptr; size: longint; n: longint; var s: _FILE): longint; cdecl; external;
  164. function ftell(stream: P_FILE): longint; cdecl; external;
  165. function ftell(var stream: _FILE): longint; cdecl; external;
  166. function fseek(stream: P_FILE; off: longint; whence: longint): longint; cdecl; external;
  167. function fseek(var stream: _FILE; off: longint; whence: longint): longint; cdecl; external;
  168. function fclose(stream: P_FILE): longint; cdecl; external;
  169. function fclose(var stream: _FILE): longint; cdecl; external;
  170. function isatty(fildes: longint): longint; cdecl; external;
  171. function fileno(para1: P_FILE): longint; cdecl; external;
  172. function fileno(var para1: _FILE): longint; cdecl; external;
  173. function fstat(fildes: longint; buf: PStat): longint; cdecl; external;
  174. function fstat(fildes: longint; var buf: TStat): longint; cdecl; external;
  175. function _stat(__file:Pchar; var __buf:Tstat):longint; cdecl; external name 'stat';
  176. function ftruncate(fildes: longint; len: longint): longint; cdecl; external;
  177. function unlink(path: Pchar): longint; cdecl; external;
  178. function rename(para1: Pchar; para2: Pchar): longint; cdecl; external;