xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/non_blocking.c (revision 41fbaed053f8fbfdf9d2a4ee0a7386a3c83f8505)
1 /*	$NetBSD: non_blocking.c,v 1.1.1.1 2009/06/23 10:09:00 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	non_blocking 3
6 /* SUMMARY
7 /*	set/clear non-blocking flag
8 /* SYNOPSIS
9 /*	#include <iostuff.h>
10 /*
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.
15 /*
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).
23 /*
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 /*--*/
37 
38 /* System interfaces. */
39 
40 #include "sys_defs.h"
41 #include <fcntl.h>
42 
43 /* Utility library. */
44 
45 #include "msg.h"
46 #include "iostuff.h"
47 
48 /* Backwards compatibility */
49 #ifndef O_NONBLOCK
50 #define PATTERN	FNDELAY
51 #else
52 #define PATTERN	O_NONBLOCK
53 #endif
54 
55 /* non_blocking - set/clear non-blocking flag */
56 
non_blocking(fd,on)57 int     non_blocking(fd, on)
58 int     fd;
59 int     on;
60 {
61     int     flags;
62 
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);
68 }
69