2
0

sysdir.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. Procedure Do_MkDir(s: rawbytestring);[IOCheck];
  21. Begin
  22. If (S='') or (InOutRes <> 0) then
  23. exit;
  24. If Fpmkdir(pchar(s), MODE_MKDIR)<0 Then
  25. Errno2Inoutres
  26. Else
  27. InOutRes:=0;
  28. End;
  29. {$IFDEF FPC_UNICODE_RTL}
  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. {$ENDIF}
  38. // len is not passed to the *nix functions because the unix API doesn't
  39. // use length safeguards for these functions. (probably because there
  40. // already is a length limit due to PATH_MAX)
  41. Procedure Do_RmDir(s: rawbytestring);[IOCheck];
  42. begin
  43. if (s='.') then
  44. InOutRes := 16;
  45. If (S='') or (InOutRes <> 0) then
  46. exit;
  47. If Fprmdir(pchar(S))<0 Then
  48. Errno2Inoutres
  49. Else
  50. InOutRes:=0;
  51. End;
  52. {$IFDEF FPC_UNICODE_RTL}
  53. Procedure Do_RmDir(s: unicodestring);[IOCheck];
  54. Var
  55. R : RawByteString;
  56. begin
  57. R:=ToSingleByteFileSystemEncodedFileName(S);
  58. Do_RMDir(R);
  59. end;
  60. {$ENDIF}
  61. Procedure do_ChDir(s: rawbytestring);[IOCheck];
  62. Begin
  63. If (s='') or (InOutRes <> 0) then
  64. exit;
  65. If Fpchdir(pchar(s))<0 Then
  66. Errno2Inoutres
  67. Else
  68. InOutRes:=0;
  69. { file not exists is path not found under tp7 }
  70. if InOutRes=2 then
  71. InOutRes:=3;
  72. End;
  73. {$IFDEF FPC_UNICODE_RTL}
  74. Procedure Do_ChDir(s: unicodestring);[IOCheck];
  75. Var
  76. R : RawByteString;
  77. begin
  78. R:=ToSingleByteFileSystemEncodedFileName(S);
  79. Do_ChDir(R);
  80. end;
  81. {$ENDIF}
  82. // !! for now we use getcwd, unless we are fpc_use_libc.
  83. // !! the old code is _still needed_ since the syscall sometimes doesn't work
  84. // !! on special filesystems like NFS etc.
  85. // !! In the libc versions, the alt code is already integrated in the libc code.
  86. // !! Also significantly boosted buffersize. This will make failure of the
  87. // !! dos legacy api's better visibile due to cut-off path, instead of "empty"
  88. procedure do_getdir(drivenr : byte;var dir : rawbytestring);
  89. var
  90. buf : array[0..2047] of char;
  91. cwdinfo : stat;
  92. rootinfo : stat;
  93. thedir,dummy : string[255];
  94. dirstream : pdir;
  95. d : pdirent;
  96. name : string[255];
  97. thisdir : stat;
  98. tmp : string[255];
  99. begin
  100. dir:='';
  101. if Fpgetcwd(@buf[0],sizeof(buf))<>nil then
  102. dir:=strpas(buf)
  103. {$ifndef FPC_USE_LIBC}
  104. else
  105. begin
  106. thedir:='';
  107. dummy:='';
  108. { get root directory information }
  109. tmp := '/'+#0;
  110. if Fpstat(@tmp[1],rootinfo)<0 then
  111. Exit;
  112. repeat
  113. tmp := dummy+'.'+#0;
  114. { get current directory information }
  115. if Fpstat(@tmp[1],cwdinfo)<0 then
  116. Exit;
  117. tmp:=dummy+'..'+#0;
  118. { open directory stream }
  119. { try to find the current inode number of the cwd }
  120. dirstream:=Fpopendir(@tmp[1]);
  121. if dirstream=nil then
  122. exit;
  123. repeat
  124. name:='';
  125. d:=Fpreaddir(dirstream);
  126. { no more entries to read ... }
  127. if not assigned(d) then
  128. break;
  129. tmp:=dummy+'../'+strpas(d^.d_name) + #0;
  130. if (Fpstat(@tmp[1],thisdir)=0) then
  131. begin
  132. { found the entry for this directory name }
  133. if (cwdinfo.st_dev=thisdir.st_dev) and (cwdinfo.st_ino=thisdir.st_ino) then
  134. begin
  135. { are the filenames of type '.' or '..' ? }
  136. { then do not set the name. }
  137. if (not ((d^.d_name[0]='.') and ((d^.d_name[1]=#0) or
  138. ((d^.d_name[1]='.') and (d^.d_name[2]=#0))))) then
  139. name:='/'+strpas(d^.d_name);
  140. end;
  141. end;
  142. until (name<>'');
  143. if Fpclosedir(dirstream)<0 then
  144. Exit;
  145. thedir:=name+thedir;
  146. dummy:=dummy+'../';
  147. if ((cwdinfo.st_dev=rootinfo.st_dev) and (cwdinfo.st_ino=rootinfo.st_ino)) then
  148. begin
  149. if thedir='' then
  150. dir:='/'
  151. else
  152. dir:=thedir;
  153. exit;
  154. end;
  155. until false;
  156. end;
  157. {$endif}
  158. end;
  159. {$IFDEF FPC_UNICODE_RTL}
  160. procedure do_getdir(drivenr : byte;var dir : unicodestring);
  161. Var
  162. S : rawbytestring;
  163. begin
  164. do_getdir(drivenr,S);
  165. dir:=S;
  166. end;
  167. {$ENDIF}