This source file includes following definitions.
- isapipe
- isapipe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include <config.h>
21
22 #include "isapipe.h"
23
24 #include <errno.h>
25
26 #if defined _WIN32 && ! defined __CYGWIN__
27
28
29
30 # include <windows.h>
31
32
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
55
56 # include <stdbool.h>
57 # include <sys/types.h>
58 # include <sys/stat.h>
59 # include <unistd.h>
60
61
62 # ifndef PIPE_LINK_COUNT_MAX
63 # define PIPE_LINK_COUNT_MAX ((nlink_t) (-1))
64 # endif
65
66
67
68
69
70
71
72 int
73 isapipe (int fd)
74 {
75 nlink_t pipe_link_count_max = PIPE_LINK_COUNT_MAX;
76 bool check_for_fifo = (HAVE_FIFO_PIPES == 1);
77 struct stat st;
78 int fstat_result = fstat (fd, &st);
79
80 if (fstat_result != 0)
81 return fstat_result;
82
83
84
85
86
87
88
89
90
91
92
93 if (! ((HAVE_FIFO_PIPES == 0 || HAVE_FIFO_PIPES == 1)
94 && PIPE_LINK_COUNT_MAX != (nlink_t) -1)
95 && (S_ISFIFO (st.st_mode) | S_ISSOCK (st.st_mode)))
96 {
97 int fd_pair[2];
98 int pipe_result = pipe (fd_pair);
99 if (pipe_result != 0)
100 return pipe_result;
101 else
102 {
103 struct stat pipe_st;
104 int fstat_pipe_result = fstat (fd_pair[0], &pipe_st);
105 int fstat_pipe_errno = errno;
106 close (fd_pair[0]);
107 close (fd_pair[1]);
108 if (fstat_pipe_result != 0)
109 {
110 errno = fstat_pipe_errno;
111 return fstat_pipe_result;
112 }
113 check_for_fifo = (S_ISFIFO (pipe_st.st_mode) != 0);
114 pipe_link_count_max = pipe_st.st_nlink;
115 }
116 }
117
118 return
119 (st.st_nlink <= pipe_link_count_max
120 && (check_for_fifo ? S_ISFIFO (st.st_mode) : S_ISSOCK (st.st_mode)));
121 }
122
123 #endif