1 /***
2   This file is part of libdaemon.
3 
4   Copyright 2003-2008 Lennart Poettering
5 
6   libdaemon is free software; you can redistribute it and/or modify
7   it under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation, either version 2.1 of the
9   License, or (at your option) any later version.
10 
11   libdaemon is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15 
16   You should have received a copy of the GNU Lesser General Public
17   License along with libdaemon. If not, see
18   <http://www.gnu.org/licenses/>.
19 ***/
20 
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 
25 #include <fcntl.h>
26 
27 #include "dnonblock.h"
28 
daemon_nonblock(int fd,int b)29 int daemon_nonblock(int fd, int b) {
30     int a, c;
31 
32     if ((a = fcntl(fd, F_GETFL)) < 0)
33         return -1;
34 
35     if (b)
36         c = a | O_NONBLOCK;
37     else
38         c = a & ~O_NONBLOCK;
39 
40     if (c == a)
41         return 0;
42 
43     return fcntl(fd, F_SETFL, c);
44 }
45