sysdir.inc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. Procedure MkDir(s: pchar;len:sizeuint);[IOCheck, public, alias : 'FPC_SYS_MKDIR'];
  15. const
  16. { read/write search permission for everyone }
  17. MODE_MKDIR = S_IWUSR OR S_IRUSR OR
  18. S_IWGRP OR S_IRGRP OR
  19. S_IWOTH OR S_IROTH OR
  20. S_IXUSR OR S_IXGRP OR S_IXOTH;
  21. // len is not passed to the *nix functions because the unix API doesn't
  22. // use length safeguards for these functions. (probably because there
  23. // already is a length limit due to PATH_MAX)
  24. Begin
  25. If not assigned(s) or (len=0) or (InOutRes <> 0) then
  26. exit;
  27. If Fpmkdir(s, MODE_MKDIR)<0 Then
  28. Errno2Inoutres
  29. Else
  30. InOutRes:=0;
  31. End;
  32. Procedure RmDir(s: pchar;len:sizeuint);[IOCheck, public, alias : 'FPC_SYS_RMDIR'];
  33. Begin
  34. if (len=1) and (s^ = '.') then
  35. InOutRes := 16;
  36. If not assigned(s) or (len=0) or (InOutRes <> 0) then
  37. exit;
  38. If Fprmdir(s)<0 Then
  39. Errno2Inoutres
  40. Else
  41. InOutRes:=0;
  42. End;
  43. Procedure ChDir(s: pchar;len:sizeuint);[IOCheck, public, alias : 'FPC_SYS_CHDIR'];
  44. Begin
  45. If not assigned(s) or (len=0) or (InOutRes <> 0) then
  46. exit;
  47. If Fpchdir(s)<0 Then
  48. Errno2Inoutres
  49. Else
  50. InOutRes:=0;
  51. { file not exists is path not found under tp7 }
  52. if InOutRes=2 then
  53. InOutRes:=3;
  54. End;
  55. // !! for now we use getcwd, unless we are fpc_use_libc.
  56. // !! the old code is _still needed_ since the syscall sometimes doesn't work
  57. // !! on special filesystems like NFS etc.
  58. // !! In the libc versions, the alt code is already integrated in the libc code.
  59. // !! Also significantly boosted buffersize. This will make failure of the
  60. // !! dos legacy api's better visibile due to cut-off path, instead of "empty"
  61. procedure do_getdir(drivenr : byte;var dir : rawbytestring);
  62. var
  63. buf : array[0..2047] of char;
  64. {$ifndef FPC_USE_LIBC}
  65. cwdinfo : stat;
  66. rootinfo : stat;
  67. thedir,dummy : rawbytestring;
  68. dirstream : pdir;
  69. d : pdirent;
  70. thisdir : stat;
  71. tmp : rawbytestring;
  72. {$endif FPC_USE_LIBC}
  73. begin
  74. dir:='';
  75. if Fpgetcwd(@buf[0],sizeof(buf))<>nil then
  76. begin
  77. dir:=buf;
  78. { the returned result by the OS is in the DefaultFileSystemCodePage ->
  79. no conversion }
  80. setcodepage(dir,DefaultFileSystemCodePage,false);
  81. end
  82. {$ifndef FPC_USE_LIBC}
  83. else
  84. begin
  85. dummy:='';
  86. { get root directory information }
  87. tmp := '/'+#0;
  88. if Fpstat(@tmp[1],rootinfo)<0 then
  89. Exit;
  90. repeat
  91. tmp := dummy+'.'+#0;
  92. { get current directory information }
  93. if Fpstat(@tmp[1],cwdinfo)<0 then
  94. Exit;
  95. tmp:=dummy+'..'+#0;
  96. { open directory stream }
  97. { try to find the current inode number of the cwd }
  98. dirstream:=Fpopendir(@tmp[1]);
  99. if dirstream=nil then
  100. exit;
  101. repeat
  102. thedir:='';
  103. d:=Fpreaddir(dirstream);
  104. { no more entries to read ... }
  105. if not assigned(d) then
  106. break;
  107. tmp:=dummy+'../'+d^.d_name + #0;
  108. if (Fpstat(@tmp[1],thisdir)=0) then
  109. begin
  110. { found the entry for this directory name }
  111. if (cwdinfo.st_dev=thisdir.st_dev) and (cwdinfo.st_ino=thisdir.st_ino) then
  112. begin
  113. { are the filenames of type '.' or '..' ? }
  114. { then do not set the name. }
  115. if (not ((d^.d_name[0]='.') and ((d^.d_name[1]=#0) or
  116. ((d^.d_name[1]='.') and (d^.d_name[2]=#0))))) then
  117. { d^.d_name is an array[0..x] of char -> will be assigned the
  118. ansi code page on conversion to ansistring -> also typecast
  119. '/' to ansistring rather than rawbytestring so code pages match
  120. (will be unconditionally set to DefaultFileSystemCodePage at
  121. the end without conversion) }
  122. thedir:=ansistring('/')+d^.d_name;
  123. end;
  124. end;
  125. until (thedir<>'');
  126. if Fpclosedir(dirstream)<0 then
  127. Exit;
  128. dummy:=dummy+'../';
  129. if ((cwdinfo.st_dev=rootinfo.st_dev) and (cwdinfo.st_ino=rootinfo.st_ino)) then
  130. begin
  131. if thedir='' then
  132. dir:='/'
  133. else
  134. begin
  135. dir:=thedir;
  136. { try to ensure that "dir" has a refcount of 1, so that setcodepage
  137. doesn't have to create a deep copy }
  138. thedir:='';
  139. end;
  140. { the returned result by the OS is in the DefaultFileSystemCodePage ->
  141. no conversion }
  142. setcodepage(dir,DefaultFileSystemCodePage,false);
  143. exit;
  144. end;
  145. until false;
  146. end;
  147. {$endif}
  148. end;