ttyname.inc 2.7 KB

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