Avoid some possibly wrong configure test results.
[gnulib.git] / m4 / isapipe.m4
bloba23563328e24a63bdb3428bbdc16fac07fa8775b
1 # isapipe.m4
2 # serial 1
3 dnl Copyright (C) 2006, 2009-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 # Test whether a file descriptor is a pipe.
10 dnl Written by Paul Eggert.
12 AC_DEFUN([gl_ISAPIPE],
14   # OpenVMS has isapipe already, so check for it.
15   AC_CHECK_FUNCS([isapipe])
16   if test $ac_cv_func_isapipe = yes; then
17     HAVE_ISAPIPE=1
18   else
19     HAVE_ISAPIPE=0
20   fi
23 # Prerequisites of lib/isapipe.c.
24 AC_DEFUN([gl_PREREQ_ISAPIPE],
26   AC_CACHE_CHECK([whether pipes are FIFOs (and for their link count)],
27     [gl_cv_pipes_are_fifos],
28     [AC_RUN_IFELSE(
29        [AC_LANG_SOURCE(
30           [[#include <stdio.h>
31             #include <sys/types.h>
32             #include <sys/stat.h>
33             #include <unistd.h>
34             #ifndef S_ISFIFO
35              #define S_ISFIFO(m) 0
36             #endif
37             #ifndef S_ISSOCK
38              #define S_ISSOCK(m) 0
39             #endif
40             int
41             main (int argc, char **argv)
42             {
43               int fd[2];
44               struct stat st;
45               if (pipe (fd) != 0)
46                 return 1;
47               if (fstat (fd[0], &st) != 0)
48                 return 2;
49               if (2 <= argc && argv[1][0] == '-')
50                 {
51                   char const *yesno = (S_ISFIFO (st.st_mode) ? "yes" : "no");
52                   if (st.st_nlink <= 1)
53                     {
54                       long int i = st.st_nlink;
55                       if (i != st.st_nlink)
56                         return 3;
57                       printf ("%s (%ld)\n", yesno, i);
58                     }
59                   else
60                     {
61                       unsigned long int i = st.st_nlink;
62                       if (i != st.st_nlink)
63                         return 4;
64                       printf ("%s (%lu)\n", yesno, i);
65                     }
66                 }
67               else
68                 {
69                   if (! S_ISFIFO (st.st_mode) && ! S_ISSOCK (st.st_mode))
70                     return 5;
71                 }
72               return 0;
73             }]])],
74        [gl_cv_pipes_are_fifos=`./conftest$ac_exeext -`
75         test -z "$gl_cv_pipes_are_fifos" && gl_cv_pipes_are_fifos=no],
76        [gl_cv_pipes_are_fifos=unknown],
77        [gl_cv_pipes_are_fifos=cross-compiling])])
79   case $gl_cv_pipes_are_fifos in #(
80   'yes ('*')')
81     AC_DEFINE([HAVE_FIFO_PIPES], [1],
82       [Define to 1 if pipes are FIFOs, 0 if sockets.  Leave undefined
83        if not known.]);; #(
84   'no ('*')')
85     AC_DEFINE([HAVE_FIFO_PIPES], [0]);;
86   esac
88   case $gl_cv_pipes_are_fifos in #(
89   *'('*')')
90     AC_DEFINE_UNQUOTED([PIPE_LINK_COUNT_MAX],
91       [`expr "$gl_cv_pipes_are_fifos" : '.*\((.*)\)'`],
92       [Define to the maximum link count that a true pipe can have.]);;
93   esac