]> arthur.barton.de Git - netatalk.git/blob - libatalk/util/socket.c
5843d1d632a1b923af5efd4bae0a295fa4400ea7
[netatalk.git] / libatalk / util / socket.c
1 /*
2    $Id: socket.c,v 1.1 2009-10-26 12:35:56 franklahm Exp $
3    Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9  
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 */
15
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif /* HAVE_CONFIG_H */
19
20 #include <unistd.h>
21 #include <fcntl.h>
22
23 int setnonblock(int fd, int cmd)
24 {
25     int ofdflags;
26     int fdflags;
27
28     if ((fdflags = ofdflags = fcntl(fd, F_GETFL, 0)) == -1)
29         return -1;
30
31     if (cmd)
32         fdflags |= O_NONBLOCK;
33     else
34         fdflags &= ~O_NONBLOCK;
35
36     if (fdflags != ofdflags)
37         if (fcntl(fd, F_SETFL, fdflags) == -1)
38             return -1;
39
40     return 0;
41 }