sysdir.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. const
  17. { read/write search permission for everyone }
  18. MODE_MKDIR = S_IWUSR OR S_IRUSR OR
  19. S_IWGRP OR S_IRGRP OR
  20. S_IWOTH OR S_IROTH OR
  21. S_IXUSR OR S_IXGRP OR S_IXOTH;
  22. Var
  23. Buffer: Array[0..255] of Char;
  24. Begin
  25. If (s='') or (InOutRes <> 0) then
  26. exit;
  27. Move(s[1], Buffer, Length(s));
  28. Buffer[Length(s)] := #0;
  29. If Fpmkdir(@buffer, MODE_MKDIR)<0 Then
  30. Errno2Inoutres
  31. Else
  32. InOutRes:=0;
  33. End;
  34. Procedure RmDir(Const s: String);[IOCheck];
  35. Var
  36. Buffer: Array[0..255] of Char;
  37. Begin
  38. if (s = '.') then
  39. InOutRes := 16;
  40. If (s='') or (InOutRes <> 0) then
  41. exit;
  42. Move(s[1], Buffer, Length(s));
  43. Buffer[Length(s)] := #0;
  44. If Fprmdir(@buffer)<0 Then
  45. Errno2Inoutres
  46. Else
  47. InOutRes:=0;
  48. End;
  49. Procedure ChDir(Const s: String);[IOCheck];
  50. Var
  51. Buffer: Array[0..255] of Char;
  52. Begin
  53. If (s='') or (InOutRes <> 0) then
  54. exit;
  55. Move(s[1], Buffer, Length(s));
  56. Buffer[Length(s)] := #0;
  57. If Fpchdir(@buffer)<0 Then
  58. Errno2Inoutres
  59. Else
  60. InOutRes:=0;
  61. { file not exists is path not found under tp7 }
  62. if InOutRes=2 then
  63. InOutRes:=3;
  64. End;
  65. { // $define usegetcwd}
  66. procedure getdir(drivenr : byte;var dir : shortstring);
  67. var
  68. {$ifndef usegetcwd}
  69. cwdinfo : stat;
  70. rootinfo : stat;
  71. thedir,dummy : string[255];
  72. dirstream : pdir;
  73. d : pdirent;
  74. name : string[255];
  75. thisdir : stat;
  76. {$endif}
  77. tmp : string[255];
  78. begin
  79. {$ifdef usegetcwd}
  80. Fpgetcwd(@tmp[1],4096);
  81. dir:=tmp;
  82. {$else}
  83. dir:='';
  84. thedir:='';
  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. name:='';
  103. d:=Fpreaddir(dirstream);
  104. { no more entries to read ... }
  105. if not assigned(d) then
  106. break;
  107. tmp:=dummy+'../'+strpas(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. name:='/'+strpas(d^.d_name);
  118. end;
  119. end;
  120. until (name<>'');
  121. if Fpclosedir(dirstream)<0 then
  122. Exit;
  123. thedir:=name+thedir;
  124. dummy:=dummy+'../';
  125. if ((cwdinfo.st_dev=rootinfo.st_dev) and (cwdinfo.st_ino=rootinfo.st_ino)) then
  126. begin
  127. if thedir='' then
  128. dir:='/'
  129. else
  130. dir:=thedir;
  131. exit;
  132. end;
  133. until false;
  134. {$endif}
  135. end;
  136. {
  137. $Log$
  138. Revision 1.1 2005-02-07 22:04:55 peter
  139. * moved to unix
  140. Revision 1.1 2005/02/06 13:06:20 peter
  141. * moved file and dir functions to sysfile/sysdir
  142. * win32 thread in systemunit
  143. }