osposix.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. {
  2. Copyright (c) 2001 by Carl Eric Codere
  3. Implements POSIX 1003.1 conforming interface
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  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. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  15. MA 02111-1301 USA.
  16. ****************************************************************************
  17. }
  18. {$Linklib c}
  19. function int_fork : pid_t; cdecl; external name 'fork';
  20. function int_execve(const path : pchar; const argv : ppchar; const envp: ppchar): cint; cdecl; external name 'execve';
  21. function int_waitpid(pid : pid_t; var stat_loc : cint; options: cint): pid_t; cdecl; external name 'waitpid';
  22. function int_uname(var name: utsname): cint; cdecl; external name 'uname';
  23. procedure sys_exit(status : cint); cdecl; external name '_exit';
  24. function int_opendir(const dirname : pchar): pdir; cdecl; external name 'opendir';
  25. function int_readdir(dirp : pdir) : pdirent;cdecl; external name 'readdir';
  26. function int_closedir(dirp : pdir): cint; cdecl; external name 'closedir';
  27. function int_chdir(const path : pchar): cint; cdecl; external name 'chdir';
  28. function int_open(const path: pchar; flags : cint; mode: mode_t):cint; cdecl; external name 'open';
  29. function int_mkdir(const path : pchar; mode: mode_t):cint; cdecl; external name 'mkdir';
  30. function int_unlink(const path: pchar): cint; cdecl; external name 'unlink';
  31. function int_rmdir(const path : pchar): cint; cdecl; external name 'rmdir';
  32. function int_rename(const old : pchar; const newpath: pchar): cint; cdecl;external name 'rename';
  33. function int_access(const pathname : pchar; amode : cint): cint; cdecl; external name 'access';
  34. function int_close(fd : cint): cint; cdecl; external name 'close';
  35. function int_read(fd: cint; buf: pchar; nbytes : size_t): ssize_t; cdecl; external name 'read';
  36. function int_write(fd: cint;const buf:pchar; nbytes : size_t): ssize_t; cdecl; external name 'write';
  37. function int_lseek(fd : cint; offset : off_t; whence : cint): off_t; cdecl; external name 'lseek';
  38. function int_time(var tloc:time_t): time_t; cdecl; external name 'time';
  39. function int_ftruncate(fd : cint; flength : off_t): cint; cdecl; external name 'ftruncate';
  40. function int_sigaction(sig: cint; var act : sigactionrec; var oact : sigactionrec): cint; cdecl; external name 'sigaction';
  41. function int_fstat(fd : cint; var sb : stat): cint; cdecl; external name 'fstat';
  42. function int_stat(const path: pchar; var buf : stat): cint; cdecl; external name 'stat';
  43. function sys_fork : pid_t;
  44. begin
  45. sys_fork := int_fork;
  46. if sys_fork <> - 1 then
  47. begin
  48. errno := 0; { reset errno when the call succeeds, contrary to libc }
  49. end;
  50. end;
  51. function sys_execve(const path : pchar; const argv : ppchar; const envp: ppchar): cint;
  52. begin
  53. sys_execve := int_execve(path, argv, envp);
  54. if sys_execve <> - 1 then
  55. begin
  56. errno := 0; { reset errno when the call succeeds, contrary to libc }
  57. end;
  58. end;
  59. function sys_waitpid(pid : pid_t; var stat_loc : cint; options: cint): pid_t;
  60. begin
  61. sys_waitpid := int_waitpid(pid, stat_loc, options);
  62. if sys_waitpid <> - 1 then
  63. begin
  64. errno := 0; { reset errno when the call succeeds, contrary to libc }
  65. end;
  66. end;
  67. function sys_uname(var name: utsname): cint;
  68. begin
  69. sys_uname := int_uname(name);
  70. if sys_uname <> - 1 then
  71. begin
  72. errno := 0; { reset errno when the call succeeds, contrary to libc }
  73. end;
  74. end;
  75. function sys_opendir(const dirname : pchar): pdir;
  76. begin
  77. sys_opendir := int_opendir(dirname);
  78. if sys_opendir <> nil then
  79. begin
  80. errno := 0; { reset errno when the call succeeds, contrary to libc }
  81. end;
  82. end;
  83. function sys_readdir(dirp : pdir) : pdirent;
  84. begin
  85. sys_readdir := int_readdir(dirp);
  86. if sys_readdir <> nil then
  87. begin
  88. errno := 0; { reset errno when the call succeeds, contrary to libc }
  89. end;
  90. end;
  91. function sys_closedir(dirp : pdir): cint;
  92. begin
  93. sys_closedir := int_closedir(dirp);
  94. if sys_closedir <> -1 then
  95. begin
  96. errno := 0; { reset errno when the call succeeds, contrary to libc }
  97. end;
  98. end;
  99. function sys_chdir(const path : pchar): cint;
  100. begin
  101. sys_chdir := int_chdir(path);
  102. if sys_chdir <> -1 then
  103. begin
  104. errno := 0; { reset errno when the call succeeds, contrary to libc }
  105. end;
  106. end;
  107. function sys_open(const path: pchar; flags : cint; mode: mode_t):cint;
  108. begin
  109. sys_open:= int_open(path, flags, mode);
  110. if sys_open <> -1 then
  111. begin
  112. errno := 0; { reset errno when the call succeeds, contrary to libc }
  113. end;
  114. end;
  115. function sys_mkdir(const path : pchar; mode: mode_t):cint;
  116. begin
  117. sys_mkdir:= int_mkdir(path, mode);
  118. if sys_mkdir <> -1 then
  119. begin
  120. errno := 0; { reset errno when the call succeeds, contrary to libc }
  121. end;
  122. end;
  123. function sys_unlink(const path: pchar): cint;
  124. begin
  125. sys_unlink := int_unlink(path);
  126. if sys_unlink <> -1 then
  127. begin
  128. errno := 0; { reset errno when the call succeeds, contrary to libc }
  129. end;
  130. end;
  131. function sys_rmdir(const path : pchar): cint;
  132. begin
  133. sys_rmdir := int_rmdir(path);
  134. if sys_rmdir <> -1 then
  135. begin
  136. errno := 0; { reset errno when the call succeeds, contrary to libc }
  137. end;
  138. end;
  139. function sys_rename(const old : pchar; const newpath: pchar): cint;
  140. begin
  141. sys_rename := int_rename(old, newpath);
  142. if sys_rename <> -1 then
  143. begin
  144. errno := 0; { reset errno when the call succeeds, contrary to libc }
  145. end;
  146. end;
  147. function sys_access(const pathname : pchar; amode : cint): cint;
  148. begin
  149. sys_access := int_access(pathname, amode);
  150. if sys_access <> -1 then
  151. begin
  152. errno := 0; { reset errno when the call succeeds, contrary to libc }
  153. end;
  154. end;
  155. function sys_close(fd : cint): cint;
  156. begin
  157. sys_close := int_close(fd);
  158. if sys_close <> -1 then
  159. begin
  160. errno := 0; { reset errno when the call succeeds, contrary to libc }
  161. end;
  162. end;
  163. function sys_read(fd: cint; buf: pchar; nbytes : size_t): ssize_t;
  164. begin
  165. sys_read := int_read(fd, buf, nbytes);
  166. if sys_read <> -1 then
  167. begin
  168. errno := 0; { reset errno when the call succeeds, contrary to libc }
  169. end;
  170. end;
  171. function sys_write(fd: cint;const buf:pchar; nbytes : size_t): ssize_t;
  172. begin
  173. sys_write := int_write(fd, buf, nbytes);
  174. if sys_write <> -1 then
  175. begin
  176. errno := 0; { reset errno when the call succeeds, contrary to libc }
  177. end;
  178. end;
  179. function sys_lseek(fd : cint; offset : off_t; whence : cint): off_t;
  180. begin
  181. sys_lseek := int_lseek(fd, offset, whence);
  182. if sys_lseek <> -1 then
  183. begin
  184. errno := 0; { reset errno when the call succeeds, contrary to libc }
  185. end;
  186. end;
  187. function sys_time(var tloc:time_t): time_t;
  188. begin
  189. sys_time := int_time(tloc);
  190. if sys_time <> -1 then
  191. begin
  192. errno := 0; { reset errno when the call succeeds, contrary to libc }
  193. end;
  194. end;
  195. function sys_ftruncate(fd : cint; flength : off_t): cint;
  196. begin
  197. sys_ftruncate := int_ftruncate(fd, flength);
  198. if sys_ftruncate <> -1 then
  199. begin
  200. errno := 0; { reset errno when the call succeeds, contrary to libc }
  201. end;
  202. end;
  203. function sys_sigaction(sig: cint; var act : sigactionrec; var oact : sigactionrec): cint;
  204. begin
  205. sys_sigaction := int_sigaction(sig, act, oact);
  206. if sys_sigaction <> -1 then
  207. begin
  208. errno := 0; { reset errno when the call succeeds, contrary to libc }
  209. end;
  210. end;
  211. function sys_fstat(fd : cint; var sb : stat): cint;
  212. begin
  213. sys_fstat := int_fstat(fd, sb);
  214. if sys_fstat <> -1 then
  215. begin
  216. errno := 0; { reset errno when the call succeeds, contrary to libc }
  217. end;
  218. end;
  219. function sys_stat(const path: pchar; var buf : stat): cint;
  220. begin
  221. sys_stat := int_stat(path, buf);
  222. if sys_stat <> -1 then
  223. begin
  224. errno := 0; { reset errno when the call succeeds, contrary to libc }
  225. end;
  226. end;
  227. const
  228. _S_IFMT = $F000; (* Type of file *)
  229. _S_IFIFO = $1000; (* FIFO *)
  230. _S_IFCHR = $2000; (* Character special *)
  231. _S_IFDIR = $4000; (* Directory *)
  232. _S_IFNAM = $5000; (* Special named file *)
  233. _S_IFBLK = $6000; (* Block special *)
  234. _S_IFREG = $8000; (* Regular *)
  235. _S_IFLNK = $A000; (* Symbolic link *)
  236. _S_IFSOCK = $C000; (* Socket *)
  237. function S_ISDIR(m : mode_t): boolean;
  238. begin
  239. if (m and _S_IFMT) = _S_IFDIR then
  240. S_ISDIR := true
  241. else
  242. S_ISDIR := false;
  243. end;
  244. function S_ISCHR(m : mode_t): boolean;
  245. begin
  246. if (m and _S_IFMT) = _S_IFCHR then
  247. S_ISCHR := true
  248. else
  249. S_ISCHR := false;
  250. end;
  251. function S_ISBLK(m : mode_t): boolean;
  252. begin
  253. if (m and _S_IFMT) = _S_IFBLK then
  254. S_ISBLK := true
  255. else
  256. S_ISBLK := false;
  257. end;
  258. function S_ISREG(m : mode_t): boolean;
  259. begin
  260. if (m and _S_IFMT) = _S_IFREG then
  261. S_ISREG := true
  262. else
  263. S_ISREG := false;
  264. end;
  265. function S_ISFIFO(m : mode_t): boolean;
  266. begin
  267. if (m and _S_IFMT) = _S_IFIFO then
  268. S_ISFIFO := true
  269. else
  270. S_ISFIFO := false;
  271. end;
  272. function wifexited(status : cint): cint;
  273. begin
  274. wifexited := longint((status and $FF) = 0);
  275. end;
  276. function wexitstatus(status : cint): cint;
  277. begin
  278. wexitstatus := (((status) shr 8) and $FF);
  279. end;
  280. function wstopsig(status : cint): cint;
  281. begin
  282. wstopsig := (((status) shr 8) and $FF);
  283. end;
  284. function wifsignaled(status : cint): cint;
  285. begin
  286. if ((status and $FF) <> 0) and ((status and $FF00)=0) then
  287. wifsignaled := 1
  288. else
  289. wifsignaled := 0;
  290. end;