sysdir.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Main OS dependant body of the system unit, loosely modelled
  4. after POSIX. *BSD version (Linux version is near identical)
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {*****************************************************************************
  12. Directory Handling
  13. *****************************************************************************}
  14. const
  15. { read/write search permission for everyone }
  16. MODE_MKDIR = S_IWUSR OR S_IRUSR OR
  17. S_IWGRP OR S_IRGRP OR
  18. S_IWOTH OR S_IROTH OR
  19. S_IXUSR OR S_IXGRP OR S_IXOTH;
  20. {$IFDEF FPC_UNICODE_RTL}
  21. Procedure Do_MkDir(s: rawbytestring);[IOCheck];
  22. Begin
  23. If (S='') or (InOutRes <> 0) then
  24. exit;
  25. If Fpmkdir(pchar(s), MODE_MKDIR)<0 Then
  26. Errno2Inoutres
  27. Else
  28. InOutRes:=0;
  29. End;
  30. Procedure Do_MkDir(s: unicodestring);[IOCheck];
  31. Var
  32. R : RawByteString;
  33. begin
  34. R:=ToSingleByteFileSystemEncodedFileName(S);
  35. Do_MkDir(R);
  36. end;
  37. {$ELSE}
  38. Procedure do_MkDir(p: pchar;len:sizeuint);[IOCheck, public, alias : 'FPC_SYS_MKDIR'];
  39. Begin
  40. If (P=nil) or (p[0]=#0) or (InOutRes <> 0) then
  41. exit;
  42. If Fpmkdir(p, MODE_MKDIR)<0 Then
  43. Errno2Inoutres
  44. Else
  45. InOutRes:=0;
  46. End;
  47. {$ENDIF}
  48. // len is not passed to the *nix functions because the unix API doesn't
  49. // use length safeguards for these functions. (probably because there
  50. // already is a length limit due to PATH_MAX)
  51. {$IFDEF FPC_UNICODE_RTL}
  52. Procedure Do_RmDir(s: rawbytestring);[IOCheck];
  53. begin
  54. If (S='') or (InOutRes <> 0) then
  55. exit;
  56. if (s='.') then
  57. InOutRes := 16;
  58. If Fprmdir(pchar(S))<0 Then
  59. Errno2Inoutres
  60. Else
  61. InOutRes:=0;
  62. End;
  63. Procedure Do_RmDir(s: unicodestring);[IOCheck];
  64. Var
  65. R : RawByteString;
  66. begin
  67. R:=ToSingleByteFileSystemEncodedFileName(S);
  68. Do_RMDir(R);
  69. end;
  70. {$ELSE}
  71. Procedure do_RMDir(p: pchar;len:sizeuint);[IOCheck, public, alias : 'FPC_SYS_RMDIR'];
  72. Begin
  73. if (len=1) and (p^ = '.') then
  74. InOutRes := 16;
  75. If not assigned(p) or (len=0) or (InOutRes <> 0) then
  76. exit;
  77. If Fprmdir(p)<0 Then
  78. Errno2Inoutres
  79. Else
  80. InOutRes:=0;
  81. End;
  82. {$ENDIF}
  83. {$IFDEF FPC_UNICODE_RTL}
  84. Procedure do_ChDir(s: rawbytestring);[IOCheck];
  85. Begin
  86. If (s='') or (InOutRes <> 0) then
  87. exit;
  88. If Fpchdir(pchar(s))<0 Then
  89. Errno2Inoutres
  90. Else
  91. InOutRes:=0;
  92. { file not exists is path not found under tp7 }
  93. if InOutRes=2 then
  94. InOutRes:=3;
  95. End;
  96. Procedure Do_ChDir(s: unicodestring);[IOCheck];
  97. Var
  98. R : RawByteString;
  99. begin
  100. R:=ToSingleByteFileSystemEncodedFileName(S);
  101. Do_ChDir(R);
  102. end;
  103. {$ELSE}
  104. Procedure do_ChDir(s: pchar;len:sizeuint);[IOCheck, public, alias : 'FPC_SYS_CHDIR'];
  105. Begin
  106. If not assigned(s) or (len=0) or (InOutRes <> 0) then
  107. exit;
  108. If Fpchdir(s)<0 Then
  109. Errno2Inoutres
  110. Else
  111. InOutRes:=0;
  112. end;
  113. {$ENDIF}
  114. // !! for now we use getcwd, unless we are fpc_use_libc.
  115. // !! the old code is _still needed_ since the syscall sometimes doesn't work
  116. // !! on special filesystems like NFS etc.
  117. // !! In the libc versions, the alt code is already integrated in the libc code.
  118. // !! Also significantly boosted buffersize. This will make failure of the
  119. // !! dos legacy api's better visibile due to cut-off path, instead of "empty"
  120. {$IFDEF FPC_UNICODE_RTL}
  121. procedure do_getdir(drivenr : byte;var dir : rawbytestring);
  122. {$ELSE}
  123. procedure getdir(drivenr : byte;var dir : shortstring);
  124. {$ENDIF}
  125. var
  126. buf : array[0..2047] of char;
  127. cwdinfo : stat;
  128. rootinfo : stat;
  129. thedir,dummy : string[255];
  130. dirstream : pdir;
  131. d : pdirent;
  132. name : string[255];
  133. thisdir : stat;
  134. tmp : string[255];
  135. begin
  136. dir:='';
  137. if Fpgetcwd(@buf[0],sizeof(buf))<>nil then
  138. dir:=strpas(buf)
  139. {$ifndef FPC_USE_LIBC}
  140. else
  141. begin
  142. thedir:='';
  143. dummy:='';
  144. { get root directory information }
  145. tmp := '/'+#0;
  146. if Fpstat(@tmp[1],rootinfo)<0 then
  147. Exit;
  148. repeat
  149. tmp := dummy+'.'+#0;
  150. { get current directory information }
  151. if Fpstat(@tmp[1],cwdinfo)<0 then
  152. Exit;
  153. tmp:=dummy+'..'+#0;
  154. { open directory stream }
  155. { try to find the current inode number of the cwd }
  156. dirstream:=Fpopendir(@tmp[1]);
  157. if dirstream=nil then
  158. exit;
  159. repeat
  160. name:='';
  161. d:=Fpreaddir(dirstream);
  162. { no more entries to read ... }
  163. if not assigned(d) then
  164. break;
  165. tmp:=dummy+'../'+strpas(d^.d_name) + #0;
  166. if (Fpstat(@tmp[1],thisdir)=0) then
  167. begin
  168. { found the entry for this directory name }
  169. if (cwdinfo.st_dev=thisdir.st_dev) and (cwdinfo.st_ino=thisdir.st_ino) then
  170. begin
  171. { are the filenames of type '.' or '..' ? }
  172. { then do not set the name. }
  173. if (not ((d^.d_name[0]='.') and ((d^.d_name[1]=#0) or
  174. ((d^.d_name[1]='.') and (d^.d_name[2]=#0))))) then
  175. name:='/'+strpas(d^.d_name);
  176. end;
  177. end;
  178. until (name<>'');
  179. if Fpclosedir(dirstream)<0 then
  180. Exit;
  181. thedir:=name+thedir;
  182. dummy:=dummy+'../';
  183. if ((cwdinfo.st_dev=rootinfo.st_dev) and (cwdinfo.st_ino=rootinfo.st_ino)) then
  184. begin
  185. if thedir='' then
  186. dir:='/'
  187. else
  188. dir:=thedir;
  189. exit;
  190. end;
  191. until false;
  192. end;
  193. {$endif}
  194. end;
  195. {$IFDEF FPC_UNICODE_RTL}
  196. procedure do_getdir(drivenr : byte;var dir : unicodestring);
  197. Var
  198. S : rawbytestring;
  199. begin
  200. do_getdir(drivenr,S);
  201. dir:=S;
  202. end;
  203. {$ENDIF}