2
0

posix.pp 13 KB

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