1/*++
2/* NAME
3/*	peekfd 3
4/* SUMMARY
5/*	determine amount of data ready to read
6/* SYNOPSIS
7/*	#include <iostuff.h>
8/*
9/*	ssize_t	peekfd(fd)
10/*	int	fd;
11/* DESCRIPTION
12/*	peekfd() attempts to find out how many bytes are available to
13/*	be read from the named file descriptor. The result value is
14/*	the number of available bytes.
15/* DIAGNOSTICS
16/*	peekfd() returns -1 in case of trouble. The global \fIerrno\fR
17/*	variable reflects the nature of the problem.
18/* BUGS
19/*	On some systems, non-blocking read() may fail even after a
20/*	positive return from peekfd(). The smtp-sink program works
21/*	around this by using the readable() function instead.
22/* LICENSE
23/* .ad
24/* .fi
25/*	The Secure Mailer license must be distributed with this software.
26/* AUTHOR(S)
27/*	Wietse Venema
28/*	IBM T.J. Watson Research
29/*	P.O. Box 704
30/*	Yorktown Heights, NY 10598, USA
31/*--*/
32
33/* System library. */
34
35#include <sys_defs.h>
36#include <sys/ioctl.h>
37#ifdef FIONREAD_IN_SYS_FILIO_H
38#include <sys/filio.h>
39#endif
40#ifdef FIONREAD_IN_TERMIOS_H
41#include <termios.h>
42#endif
43#include <unistd.h>
44
45/* Utility library. */
46
47#include "iostuff.h"
48
49/* peekfd - return amount of data ready to read */
50
51ssize_t peekfd(int fd)
52{
53
54    /*
55     * Anticipate a series of system-dependent code fragments.
56     */
57#ifdef FIONREAD
58    int     count;
59
60    return (ioctl(fd, FIONREAD, (char *) &count) < 0 ? -1 : count);
61#else
62#error "don't know how to look ahead"
63#endif
64}
65