posix.pp 14 KB

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