ttyname.inc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Peter Vreman
  4. member of the Free Pascal development team.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. A generic implementation of ttyname functionality.
  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. function TTYName(Handle:cint):string;
  13. {
  14. Return the name of the current tty described by handle f.
  15. returns empty string in case of an error.
  16. }
  17. var
  18. mydev : dev_t;
  19. myino : ino_t;
  20. st : stat;
  21. function mysearch(n:string): boolean;
  22. {searches recursively for the device in the directory given by n,
  23. returns true if found and sets the name of the device in ttyname}
  24. var dirstream : pdir;
  25. d : pdirent;
  26. name : string;
  27. st : stat;
  28. begin
  29. dirstream:=fpopendir(n);
  30. if (dirstream=nil) then
  31. exit(false);
  32. d:=fpReaddir(dirstream^);
  33. while (d<>nil) do
  34. begin
  35. name:=n+'/'+strpas(@(d^.d_name));
  36. // fpstat(name,st);
  37. if fpstat(name,st)=0 then
  38. begin
  39. if (fpS_ISDIR(st.st_mode)) and { if it is a directory }
  40. (strpas(@(d^.d_name))<>'.') and { but not ., .. and fd subdirs }
  41. (strpas(@(d^.d_name))<>'..') and
  42. (strpas(@(d^.d_name))<>'') and
  43. (strpas(@(d^.d_name))<>'fd') then
  44. begin {we found a directory, search inside it}
  45. if mysearch(name) then
  46. begin {the device is here}
  47. fpclosedir(dirstream^); {then don't continue searching}
  48. mysearch:=true;
  49. exit;
  50. end;
  51. end
  52. {$ifndef beos}
  53. else if (ino_t(d^.d_fileno)=myino) and (st.st_dev=mydev) then
  54. begin
  55. fpclosedir(dirstream^);
  56. ttyname:=name;
  57. mysearch:=true;
  58. exit;
  59. end;
  60. {$endif}
  61. end;
  62. d:=fpReaddir(dirstream^);
  63. end;
  64. fpclosedir(dirstream^);
  65. mysearch:=false;
  66. end;
  67. begin
  68. TTYName:='';
  69. if (fpfstat(handle,st)=-1) or (isatty (handle)<>1) then
  70. exit;
  71. {$ifndef beos}
  72. mydev:=st.st_dev;
  73. myino:=st.st_ino;
  74. {$endif}
  75. mysearch('/dev');
  76. end;
  77. function TTYName(var F:Text):string;
  78. {
  79. Idem as previous, only now for text variables;
  80. }
  81. begin
  82. TTYName:=TTYName(textrec(f).handle);
  83. end;