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