sysdir.inc 3.8 KB

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