sysdir.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Main OS dependant body of the system unit, loosely modelled
  5. after POSIX. *BSD version (Linux version is near identical)
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {*****************************************************************************
  13. Directory Handling
  14. *****************************************************************************}
  15. Procedure MkDir(Const s: String);[IOCheck];
  16. Var
  17. Buffer: Array[0..255] of Char;
  18. Begin
  19. If (s='') or (InOutRes <> 0) then
  20. exit;
  21. Move(s[1], Buffer, Length(s));
  22. Buffer[Length(s)] := #0;
  23. If Fpmkdir(@buffer, MODE_MKDIR)<0 Then
  24. Errno2Inoutres
  25. Else
  26. InOutRes:=0;
  27. End;
  28. Procedure RmDir(Const s: String);[IOCheck];
  29. Var
  30. Buffer: Array[0..255] of Char;
  31. Begin
  32. if (s = '.') then
  33. InOutRes := 16;
  34. If (s='') or (InOutRes <> 0) then
  35. exit;
  36. Move(s[1], Buffer, Length(s));
  37. Buffer[Length(s)] := #0;
  38. If Fprmdir(@buffer)<0 Then
  39. Errno2Inoutres
  40. Else
  41. InOutRes:=0;
  42. End;
  43. Procedure ChDir(Const s: String);[IOCheck];
  44. Var
  45. Buffer: Array[0..255] of Char;
  46. Begin
  47. If (s='') or (InOutRes <> 0) then
  48. exit;
  49. Move(s[1], Buffer, Length(s));
  50. Buffer[Length(s)] := #0;
  51. If Fpchdir(@buffer)<0 Then
  52. Errno2Inoutres
  53. Else
  54. InOutRes:=0;
  55. { file not exists is path not found under tp7 }
  56. if InOutRes=2 then
  57. InOutRes:=3;
  58. End;
  59. { // $define usegetcwd}
  60. procedure getdir(drivenr : byte;var dir : shortstring);
  61. var
  62. {$ifndef usegetcwd}
  63. cwdinfo : stat;
  64. rootinfo : stat;
  65. thedir,dummy : string[255];
  66. dirstream : pdir;
  67. d : pdirent;
  68. name : string[255];
  69. thisdir : stat;
  70. {$endif}
  71. tmp : string[255];
  72. begin
  73. {$ifdef usegetcwd}
  74. Fpgetcwd(@tmp[1],4096);
  75. dir:=tmp;
  76. {$else}
  77. dir:='';
  78. thedir:='';
  79. dummy:='';
  80. { get root directory information }
  81. tmp := '/'+#0;
  82. if Fpstat(@tmp[1],rootinfo)<0 then
  83. Exit;
  84. repeat
  85. tmp := dummy+'.'+#0;
  86. { get current directory information }
  87. if Fpstat(@tmp[1],cwdinfo)<0 then
  88. Exit;
  89. tmp:=dummy+'..'+#0;
  90. { open directory stream }
  91. { try to find the current inode number of the cwd }
  92. dirstream:=Fpopendir(@tmp[1]);
  93. if dirstream=nil then
  94. exit;
  95. repeat
  96. name:='';
  97. d:=Fpreaddir(dirstream);
  98. { no more entries to read ... }
  99. if not assigned(d) then
  100. break;
  101. tmp:=dummy+'../'+strpas(d^.d_name) + #0;
  102. if (Fpstat(@tmp[1],thisdir)=0) then
  103. begin
  104. { found the entry for this directory name }
  105. if (cwdinfo.st_dev=thisdir.st_dev) and (cwdinfo.st_ino=thisdir.st_ino) then
  106. begin
  107. { are the filenames of type '.' or '..' ? }
  108. { then do not set the name. }
  109. if (not ((d^.d_name[0]='.') and ((d^.d_name[1]=#0) or
  110. ((d^.d_name[1]='.') and (d^.d_name[2]=#0))))) then
  111. name:='/'+strpas(d^.d_name);
  112. end;
  113. end;
  114. until (name<>'');
  115. if Fpclosedir(dirstream)<0 then
  116. Exit;
  117. thedir:=name+thedir;
  118. dummy:=dummy+'../';
  119. if ((cwdinfo.st_dev=rootinfo.st_dev) and (cwdinfo.st_ino=rootinfo.st_ino)) then
  120. begin
  121. if thedir='' then
  122. dir:='/'
  123. else
  124. dir:=thedir;
  125. exit;
  126. end;
  127. until false;
  128. {$endif}
  129. end;
  130. {
  131. $Log$
  132. Revision 1.1 2005-02-06 13:06:20 peter
  133. * moved file and dir functions to sysfile/sysdir
  134. * win32 thread in systemunit
  135. }