posix.pp 13 KB

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