1/*++
2/* NAME
3/*	non_blocking 3
4/* SUMMARY
5/*	set/clear non-blocking flag
6/* SYNOPSIS
7/*	#include <iostuff.h>
8/*
9/*	int	non_blocking(int fd, int on)
10/* DESCRIPTION
11/*	the \fInon_blocking\fR() function manipulates the non-blocking
12/*	flag for the specified open file, and returns the old setting.
13/*
14/*	Arguments:
15/* .IP fd
16/*	A file descriptor.
17/* .IP on
18/*	For non-blocking I/O, specify a non-zero value (or use the
19/*	NON_BLOCKING constant); for blocking I/O, specify zero
20/*	(or use the BLOCKING constant).
21/*
22/*	The result is non-zero when the non-blocking flag was enabled.
23/* DIAGNOSTICS
24/*	All errors are fatal.
25/* LICENSE
26/* .ad
27/* .fi
28/*	The Secure Mailer license must be distributed with this software.
29/* AUTHOR(S)
30/*	Wietse Venema
31/*	IBM T.J. Watson Research
32/*	P.O. Box 704
33/*	Yorktown Heights, NY 10598, USA
34/*--*/
35
36/* System interfaces. */
37
38#include "sys_defs.h"
39#include <fcntl.h>
40
41/* Utility library. */
42
43#include "msg.h"
44#include "iostuff.h"
45
46/* Backwards compatibility */
47#ifndef O_NONBLOCK
48#define PATTERN	FNDELAY
49#else
50#define PATTERN	O_NONBLOCK
51#endif
52
53/* non_blocking - set/clear non-blocking flag */
54
55int     non_blocking(fd, on)
56int     fd;
57int     on;
58{
59    int     flags;
60
61    if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
62	msg_fatal("fcntl: get flags: %m");
63    if (fcntl(fd, F_SETFL, on ? flags | PATTERN : flags & ~PATTERN) < 0)
64	msg_fatal("fcntl: set non-blocking flag %s: %m", on ? "on" : "off");
65    return ((flags & PATTERN) != 0);
66}
67