(root)/
coreutils-9.4/
lib/
isapipe.c
       1  /* Test whether a file descriptor is a pipe.
       2  
       3     Copyright (C) 2006, 2008-2023 Free Software Foundation, Inc.
       4  
       5     This file is free software: you can redistribute it and/or modify
       6     it under the terms of the GNU Lesser General Public License as
       7     published by the Free Software Foundation, either version 3 of the
       8     License, or (at your option) any later version.
       9  
      10     This file is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13     GNU Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public License
      16     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      17  
      18  /* Written by Paul Eggert.  */
      19  
      20  #include <config.h>
      21  
      22  #include "isapipe.h"
      23  
      24  #include <errno.h>
      25  
      26  #if defined _WIN32 && ! defined __CYGWIN__
      27  /* Windows platforms.  */
      28  
      29  /* Get GetFileType.  */
      30  # include <windows.h>
      31  
      32  /* Get _get_osfhandle.  */
      33  # if GNULIB_MSVC_NOTHROW
      34  #  include "msvc-nothrow.h"
      35  # else
      36  #  include <io.h>
      37  # endif
      38  
      39  int
      40  isapipe (int fd)
      41  {
      42    HANDLE h = (HANDLE) _get_osfhandle (fd);
      43  
      44    if (h == INVALID_HANDLE_VALUE)
      45      {
      46        errno = EBADF;
      47        return -1;
      48      }
      49  
      50    return (GetFileType (h) == FILE_TYPE_PIPE);
      51  }
      52  
      53  #else
      54  /* Unix platforms.  */
      55  
      56  # include <sys/types.h>
      57  # include <sys/stat.h>
      58  # include <unistd.h>
      59  
      60  /* The maximum link count for pipes; (nlink_t) -1 if not known.  */
      61  # ifndef PIPE_LINK_COUNT_MAX
      62  #  define PIPE_LINK_COUNT_MAX ((nlink_t) (-1))
      63  # endif
      64  
      65  /* Return 1 if FD is a pipe, 0 if not, -1 (setting errno) on error.
      66  
      67     Test fairly strictly whether FD is a pipe.  lseek and checking for
      68     ESPIPE does not suffice, since many non-pipe files cause lseek to
      69     fail with errno == ESPIPE.  */
      70  
      71  int
      72  isapipe (int fd)
      73  {
      74    nlink_t pipe_link_count_max = PIPE_LINK_COUNT_MAX;
      75    bool check_for_fifo = (HAVE_FIFO_PIPES == 1);
      76    struct stat st;
      77    int fstat_result = fstat (fd, &st);
      78  
      79    if (fstat_result != 0)
      80      return fstat_result;
      81  
      82    /* We want something that succeeds only for pipes, but on
      83       POSIX-conforming hosts S_ISFIFO succeeds for both FIFOs and pipes
      84       and we know of no portable, reliable way to distinguish them in
      85       general.  However, in practice pipes always have a link count <=
      86       PIPE_LINK_COUNT_MAX (unless someone attaches them to the file
      87       system name space using fattach, in which case they're not really
      88       pipes any more), so test for that as well.
      89  
      90       On Darwin 7.7, pipes are sockets, so check for those instead.  */
      91  
      92    if (! ((HAVE_FIFO_PIPES == 0 || HAVE_FIFO_PIPES == 1)
      93           && PIPE_LINK_COUNT_MAX != (nlink_t) -1)
      94        && (S_ISFIFO (st.st_mode) | S_ISSOCK (st.st_mode)))
      95      {
      96        int fd_pair[2];
      97        int pipe_result = pipe (fd_pair);
      98        if (pipe_result != 0)
      99          return pipe_result;
     100        else
     101          {
     102            struct stat pipe_st;
     103            int fstat_pipe_result = fstat (fd_pair[0], &pipe_st);
     104            int fstat_pipe_errno = errno;
     105            close (fd_pair[0]);
     106            close (fd_pair[1]);
     107            if (fstat_pipe_result != 0)
     108              {
     109                errno = fstat_pipe_errno;
     110                return fstat_pipe_result;
     111              }
     112            check_for_fifo = (S_ISFIFO (pipe_st.st_mode) != 0);
     113            pipe_link_count_max = pipe_st.st_nlink;
     114          }
     115      }
     116  
     117    return
     118      (st.st_nlink <= pipe_link_count_max
     119       && (check_for_fifo ? S_ISFIFO (st.st_mode) : S_ISSOCK (st.st_mode)));
     120  }
     121  
     122  #endif