* isapipe.c (isapipe): Rename local s/fd/fd_pair/ to avoid shadowing
[gnulib.git] / lib / isapipe.c
1 /* Test whether a file descriptor is a pipe.
2
3    Copyright (C) 2006 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program 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 General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 /* Written by Paul Eggert.  */
20
21 #include <config.h>
22
23 #include "isapipe.h"
24
25 #include <errno.h>
26 #include <stdbool.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30
31 #include "stat-macros.h"
32
33 /* Whether pipes are FIFOs; -1 if not known.  */
34 #ifndef HAVE_FIFO_PIPES
35 # define HAVE_FIFO_PIPES (-1)
36 #endif
37
38 /* The maximum link count for pipes; (nlink_t) -1 if not known.  */
39 #ifndef PIPE_LINK_COUNT_MAX
40 # define PIPE_LINK_COUNT_MAX ((nlink_t) (-1))
41 #endif
42
43 /* Return 1 if FD is a pipe, 0 if not, -1 (setting errno) on error.
44
45    Test fairly strictly whether FD is a pipe.  lseek and checking for
46    ESPIPE does not suffice, since many non-pipe files cause lseek to
47    fail with errno == ESPIPE.  */
48
49 int
50 isapipe (int fd)
51 {
52   nlink_t pipe_link_count_max = PIPE_LINK_COUNT_MAX;
53   bool check_for_fifo = (HAVE_FIFO_PIPES == 1);
54   struct stat st;
55   int fstat_result = fstat (fd, &st);
56
57   if (fstat_result != 0)
58     return fstat_result;
59
60   /* We want something that succeeds only for pipes, but on
61      POSIX-conforming hosts S_ISFIFO succeeds for both FIFOs and pipes
62      and we know of no portable, reliable way to distinguish them in
63      general.  However, in practice pipes always have a link count <=
64      PIPE_LINK_COUNT_MAX (unless someone attaches them to the file
65      system name space using fattach, in which case they're not really
66      pipes any more), so test for that as well.
67
68      On Darwin 7.7, pipes are sockets, so check for those instead.  */
69
70   if (! ((HAVE_FIFO_PIPES == 0 || HAVE_FIFO_PIPES == 1)
71          && PIPE_LINK_COUNT_MAX != (nlink_t) -1)
72       && (S_ISFIFO (st.st_mode) | S_ISSOCK (st.st_mode)))
73     {
74       int fd_pair[2];
75       int pipe_result = pipe (fd_pair);
76       if (pipe_result != 0)
77         return pipe_result;
78       else
79         {
80           struct stat pipe_st;
81           int fstat_pipe_result = fstat (fd_pair[0], &pipe_st);
82           int fstat_pipe_errno = errno;
83           close (fd_pair[0]);
84           close (fd_pair[1]);
85           if (fstat_pipe_result != 0)
86             {
87               errno = fstat_pipe_errno;
88               return fstat_pipe_result;
89             }
90           check_for_fifo = (S_ISFIFO (pipe_st.st_mode) != 0);
91           pipe_link_count_max = pipe_st.st_nlink;
92         }
93     }
94
95   return
96     (st.st_nlink <= pipe_link_count_max
97      && (check_for_fifo ? S_ISFIFO (st.st_mode) : S_ISSOCK (st.st_mode)));
98 }