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