sysdir.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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, 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 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 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. { // $define usegetcwd}
  65. procedure getdir(drivenr : byte;var dir : shortstring);
  66. var
  67. {$ifdef usegetcwd}
  68. buf : array[0..254] of char;
  69. {$else}
  70. cwdinfo : stat;
  71. rootinfo : stat;
  72. thedir,dummy : string[255];
  73. dirstream : pdir;
  74. d : pdirent;
  75. name : string[255];
  76. thisdir : stat;
  77. tmp : string[255];
  78. {$endif}
  79. begin
  80. dir:='';
  81. {$ifdef usegetcwd}
  82. if Fpgetcwd(@buf,sizeof(buf))<>nil then
  83. dir:=strpas(buf);
  84. {$else}
  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;