sysdir.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. {$ifdef usegetcwd}
  69. buf : array[0..254] of char;
  70. {$else}
  71. cwdinfo : stat;
  72. rootinfo : stat;
  73. thedir,dummy : string[255];
  74. dirstream : pdir;
  75. d : pdirent;
  76. name : string[255];
  77. thisdir : stat;
  78. tmp : string[255];
  79. {$endif}
  80. begin
  81. dir:='';
  82. {$ifdef usegetcwd}
  83. if Fpgetcwd(@buf,sizeof(buf))<>nil then
  84. dir:=strpas(buf);
  85. {$else}
  86. thedir:='';
  87. dummy:='';
  88. { get root directory information }
  89. tmp := '/'+#0;
  90. if Fpstat(@tmp[1],rootinfo)<0 then
  91. Exit;
  92. repeat
  93. tmp := dummy+'.'+#0;
  94. { get current directory information }
  95. if Fpstat(@tmp[1],cwdinfo)<0 then
  96. Exit;
  97. tmp:=dummy+'..'+#0;
  98. { open directory stream }
  99. { try to find the current inode number of the cwd }
  100. dirstream:=Fpopendir(@tmp[1]);
  101. if dirstream=nil then
  102. exit;
  103. repeat
  104. name:='';
  105. d:=Fpreaddir(dirstream);
  106. { no more entries to read ... }
  107. if not assigned(d) then
  108. break;
  109. tmp:=dummy+'../'+strpas(d^.d_name) + #0;
  110. if (Fpstat(@tmp[1],thisdir)=0) then
  111. begin
  112. { found the entry for this directory name }
  113. if (cwdinfo.st_dev=thisdir.st_dev) and (cwdinfo.st_ino=thisdir.st_ino) then
  114. begin
  115. { are the filenames of type '.' or '..' ? }
  116. { then do not set the name. }
  117. if (not ((d^.d_name[0]='.') and ((d^.d_name[1]=#0) or
  118. ((d^.d_name[1]='.') and (d^.d_name[2]=#0))))) then
  119. name:='/'+strpas(d^.d_name);
  120. end;
  121. end;
  122. until (name<>'');
  123. if Fpclosedir(dirstream)<0 then
  124. Exit;
  125. thedir:=name+thedir;
  126. dummy:=dummy+'../';
  127. if ((cwdinfo.st_dev=rootinfo.st_dev) and (cwdinfo.st_ino=rootinfo.st_ino)) then
  128. begin
  129. if thedir='' then
  130. dir:='/'
  131. else
  132. dir:=thedir;
  133. exit;
  134. end;
  135. until false;
  136. {$endif}
  137. end;
  138. {
  139. $Log$
  140. Revision 1.3 2005-02-14 17:13:31 peter
  141. * truncate log
  142. Revision 1.2 2005/02/13 20:01:38 peter
  143. * include file cleanup
  144. Revision 1.1 2005/02/07 22:04:55 peter
  145. * moved to unix
  146. Revision 1.1 2005/02/06 13:06:20 peter
  147. * moved file and dir functions to sysfile/sysdir
  148. * win32 thread in systemunit
  149. }