sysdir.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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(Const s: String);[IOCheck];
  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. Var
  22. Buffer: Array[0..255] of Char;
  23. Begin
  24. If (s='') or (InOutRes <> 0) then
  25. exit;
  26. Move(s[1], Buffer, Length(s));
  27. Buffer[Length(s)] := #0;
  28. If Fpmkdir(@buffer[0], MODE_MKDIR)<0 Then
  29. Errno2Inoutres
  30. Else
  31. InOutRes:=0;
  32. End;
  33. Procedure RmDir(Const s: String);[IOCheck];
  34. Var
  35. Buffer: Array[0..255] of Char;
  36. Begin
  37. if (s = '.') then
  38. InOutRes := 16;
  39. If (s='') or (InOutRes <> 0) then
  40. exit;
  41. Move(s[1], Buffer, Length(s));
  42. Buffer[Length(s)] := #0;
  43. If Fprmdir(@buffer[0])<0 Then
  44. Errno2Inoutres
  45. Else
  46. InOutRes:=0;
  47. End;
  48. Procedure ChDir(Const s: String);[IOCheck];
  49. Var
  50. Buffer: Array[0..255] of Char;
  51. Begin
  52. If (s='') or (InOutRes <> 0) then
  53. exit;
  54. Move(s[1], Buffer, Length(s));
  55. Buffer[Length(s)] := #0;
  56. If Fpchdir(@buffer[0])<0 Then
  57. Errno2Inoutres
  58. Else
  59. InOutRes:=0;
  60. { file not exists is path not found under tp7 }
  61. if InOutRes=2 then
  62. InOutRes:=3;
  63. End;
  64. // !! for now we use getcwd, unless we are fpc_use_libc.
  65. // !! the old code is _still needed_ since the syscall sometimes doesn't work
  66. // !! on special filesystems like NFS etc.
  67. // !! In the libc versions, the alt code is already integrated in the libc code.
  68. // !! Also significantly boosted buffersize. This will make failure of the
  69. // !! dos legacy api's better visibile due to cut-off path, instead of "empty"
  70. procedure getdir(drivenr : byte;var dir : shortstring);
  71. var
  72. buf : array[0..2047] of char;
  73. cwdinfo : stat;
  74. rootinfo : stat;
  75. thedir,dummy : string[255];
  76. dirstream : pdir;
  77. d : pdirent;
  78. name : string[255];
  79. thisdir : stat;
  80. tmp : string[255];
  81. begin
  82. dir:='';
  83. if Fpgetcwd(@buf[0],sizeof(buf))<>nil then
  84. dir:=strpas(buf)
  85. {$ifndef FPC_USE_LIBC}
  86. else
  87. begin
  88. thedir:='';
  89. dummy:='';
  90. { get root directory information }
  91. tmp := '/'+#0;
  92. if Fpstat(@tmp[1],rootinfo)<0 then
  93. Exit;
  94. repeat
  95. tmp := dummy+'.'+#0;
  96. { get current directory information }
  97. if Fpstat(@tmp[1],cwdinfo)<0 then
  98. Exit;
  99. tmp:=dummy+'..'+#0;
  100. { open directory stream }
  101. { try to find the current inode number of the cwd }
  102. dirstream:=Fpopendir(@tmp[1]);
  103. if dirstream=nil then
  104. exit;
  105. repeat
  106. name:='';
  107. d:=Fpreaddir(dirstream);
  108. { no more entries to read ... }
  109. if not assigned(d) then
  110. break;
  111. tmp:=dummy+'../'+strpas(d^.d_name) + #0;
  112. if (Fpstat(@tmp[1],thisdir)=0) then
  113. begin
  114. { found the entry for this directory name }
  115. if (cwdinfo.st_dev=thisdir.st_dev) and (cwdinfo.st_ino=thisdir.st_ino) then
  116. begin
  117. { are the filenames of type '.' or '..' ? }
  118. { then do not set the name. }
  119. if (not ((d^.d_name[0]='.') and ((d^.d_name[1]=#0) or
  120. ((d^.d_name[1]='.') and (d^.d_name[2]=#0))))) then
  121. name:='/'+strpas(d^.d_name);
  122. end;
  123. end;
  124. until (name<>'');
  125. if Fpclosedir(dirstream)<0 then
  126. Exit;
  127. thedir:=name+thedir;
  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. dir:=thedir;
  135. exit;
  136. end;
  137. until false;
  138. end;
  139. {$endif}
  140. end;