annotate lib/isapipe.c @ 17935:0ad1f4c9eed5 default tip

tests: support stderr verification with returns_() * tests/init.sh (returns_): Disable tracing for this wrapper function, so that stderr of the wrapped command is unchanged, allowing for verification of the contents.
author Pádraig Brady <P@draigBrady.com>
date Mon, 16 Feb 2015 17:20:39 +0000
parents ab58d4870664
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7233
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
1 /* Test whether a file descriptor is a pipe.
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
2
17848
ab58d4870664 version-etc: new year
Paul Eggert <eggert@cs.ucla.edu>
parents: 17587
diff changeset
3 Copyright (C) 2006, 2008-2015 Free Software Foundation, Inc.
7233
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
4
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7581
diff changeset
5 This program is free software: you can redistribute it and/or modify
7233
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7581
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7581
diff changeset
8 (at your option) any later version.
7233
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
9
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
13 GNU General Public License for more details.
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
14
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7581
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
7233
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
17
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
18 /* Written by Paul Eggert. */
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
19
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
20 #include <config.h>
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
21
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
22 #include "isapipe.h"
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
23
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
24 #include <errno.h>
9953
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
25
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
26 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
27 /* Windows platforms. */
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
28
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
29 /* Get GetFileType. */
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
30 # include <windows.h>
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
31
15752
b86e9061a6d0 New module 'msvc-nothrow'. Makes _get_osfhandle safe on MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
32 /* Get _get_osfhandle. */
b86e9061a6d0 New module 'msvc-nothrow'. Makes _get_osfhandle safe on MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
33 # include "msvc-nothrow.h"
b86e9061a6d0 New module 'msvc-nothrow'. Makes _get_osfhandle safe on MSVC 9.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
34
9953
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
35 int
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
36 isapipe (int fd)
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
37 {
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
38 HANDLE h = (HANDLE) _get_osfhandle (fd);
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
39
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
40 if (h == INVALID_HANDLE_VALUE)
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
41 {
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
42 errno = EBADF;
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
43 return -1;
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
44 }
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
45
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
46 return (GetFileType (h) == FILE_TYPE_PIPE);
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
47 }
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
48
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
49 #else
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
50 /* Unix platforms. */
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
51
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
52 # include <stdbool.h>
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
53 # include <sys/types.h>
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
54 # include <sys/stat.h>
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
55 # include <unistd.h>
7233
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
56
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
57 /* The maximum link count for pipes; (nlink_t) -1 if not known. */
9953
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
58 # ifndef PIPE_LINK_COUNT_MAX
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
59 # define PIPE_LINK_COUNT_MAX ((nlink_t) (-1))
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
60 # endif
7233
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
61
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
62 /* Return 1 if FD is a pipe, 0 if not, -1 (setting errno) on error.
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
63
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
64 Test fairly strictly whether FD is a pipe. lseek and checking for
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
65 ESPIPE does not suffice, since many non-pipe files cause lseek to
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
66 fail with errno == ESPIPE. */
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
67
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
68 int
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
69 isapipe (int fd)
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
70 {
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
71 nlink_t pipe_link_count_max = PIPE_LINK_COUNT_MAX;
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
72 bool check_for_fifo = (HAVE_FIFO_PIPES == 1);
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
73 struct stat st;
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
74 int fstat_result = fstat (fd, &st);
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
75
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
76 if (fstat_result != 0)
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
77 return fstat_result;
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
78
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
79 /* We want something that succeeds only for pipes, but on
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
80 POSIX-conforming hosts S_ISFIFO succeeds for both FIFOs and pipes
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
81 and we know of no portable, reliable way to distinguish them in
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
82 general. However, in practice pipes always have a link count <=
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
83 PIPE_LINK_COUNT_MAX (unless someone attaches them to the file
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
84 system name space using fattach, in which case they're not really
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
85 pipes any more), so test for that as well.
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
86
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
87 On Darwin 7.7, pipes are sockets, so check for those instead. */
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
88
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
89 if (! ((HAVE_FIFO_PIPES == 0 || HAVE_FIFO_PIPES == 1)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9953
diff changeset
90 && PIPE_LINK_COUNT_MAX != (nlink_t) -1)
7233
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
91 && (S_ISFIFO (st.st_mode) | S_ISSOCK (st.st_mode)))
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
92 {
7236
c05b41ab8e3b * isapipe.c (isapipe): Rename local s/fd/fd_pair/ to avoid shadowing
Paul Eggert <eggert@cs.ucla.edu>
parents: 7233
diff changeset
93 int fd_pair[2];
c05b41ab8e3b * isapipe.c (isapipe): Rename local s/fd/fd_pair/ to avoid shadowing
Paul Eggert <eggert@cs.ucla.edu>
parents: 7233
diff changeset
94 int pipe_result = pipe (fd_pair);
7233
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
95 if (pipe_result != 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9953
diff changeset
96 return pipe_result;
7233
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
97 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9953
diff changeset
98 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9953
diff changeset
99 struct stat pipe_st;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9953
diff changeset
100 int fstat_pipe_result = fstat (fd_pair[0], &pipe_st);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9953
diff changeset
101 int fstat_pipe_errno = errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9953
diff changeset
102 close (fd_pair[0]);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9953
diff changeset
103 close (fd_pair[1]);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9953
diff changeset
104 if (fstat_pipe_result != 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9953
diff changeset
105 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9953
diff changeset
106 errno = fstat_pipe_errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9953
diff changeset
107 return fstat_pipe_result;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9953
diff changeset
108 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9953
diff changeset
109 check_for_fifo = (S_ISFIFO (pipe_st.st_mode) != 0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9953
diff changeset
110 pipe_link_count_max = pipe_st.st_nlink;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9953
diff changeset
111 }
7233
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
112 }
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
113
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
114 return
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
115 (st.st_nlink <= pipe_link_count_max
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
116 && (check_for_fifo ? S_ISFIFO (st.st_mode) : S_ISSOCK (st.st_mode)));
ec85aace506a * modules/isapipe: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
117 }
9953
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
118
5605642e7756 Port to native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
119 #endif