tty-internal.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Internal stuff of the terminal controlling library.
  3. Copyright (C) 2019
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Andrew Borodin <aborodin@vmail.ru>, 2019.
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /** \file
  20. * \brief Source: internal stuff of the terminal controlling library.
  21. */
  22. #include <config.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include "lib/global.h"
  28. #include "tty-internal.h"
  29. /*** global variables ****************************************************************************/
  30. /* pipe to handle SIGWINCH */
  31. int sigwinch_pipe[2];
  32. /*** file scope macro definitions ****************************************************************/
  33. /*** global variables ****************************************************************************/
  34. /*** file scope type declarations ****************************************************************/
  35. /*** file scope variables ************************************************************************/
  36. /* --------------------------------------------------------------------------------------------- */
  37. /*** file scope functions ************************************************************************/
  38. /* --------------------------------------------------------------------------------------------- */
  39. /* --------------------------------------------------------------------------------------------- */
  40. /*** public functions ****************************************************************************/
  41. /* --------------------------------------------------------------------------------------------- */
  42. void
  43. tty_create_winch_pipe (void)
  44. {
  45. int fd_flags;
  46. if (pipe (sigwinch_pipe) == -1)
  47. {
  48. perror (_("Cannot create pipe for SIGWINCH"));
  49. exit (EXIT_FAILURE);
  50. }
  51. /* If we read from an empty pipe, then read(2) will block until data is available.
  52. * If we write to a full pipe, then write(2) blocks until sufficient data has been read
  53. * from the pipe to allow the write to complete..
  54. * Therefore, use nonblocking I/O.
  55. */
  56. fd_flags = fcntl (sigwinch_pipe[0], F_GETFL, NULL);
  57. if (fd_flags != -1)
  58. {
  59. fd_flags |= O_NONBLOCK | O_CLOEXEC;
  60. fd_flags = fcntl (sigwinch_pipe[0], F_SETFL, fd_flags);
  61. }
  62. if (fd_flags == -1)
  63. {
  64. perror (_("Cannot configure write end of SIGWINCH pipe"));
  65. exit (EXIT_FAILURE);
  66. }
  67. fd_flags = fcntl (sigwinch_pipe[1], F_GETFL, NULL);
  68. if (fd_flags != -1)
  69. {
  70. fd_flags |= O_NONBLOCK | O_CLOEXEC;
  71. fd_flags = fcntl (sigwinch_pipe[1], F_SETFL, fd_flags);
  72. }
  73. if (fd_flags == -1)
  74. {
  75. perror (_("Cannot configure read end of SIGWINCH pipe"));
  76. exit (EXIT_FAILURE);
  77. }
  78. }
  79. /* --------------------------------------------------------------------------------------------- */