93f6492560165b697185f4fb7a069cc2c8213a5e
[sfrench/samba-autobuild/.git] / source4 / lib / events / events_util.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Andrew Tridgell 2005
5    Copyright (C) Jelmer Vernooij 2005
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "replace.h"
22 #include "talloc.h"
23 #include "events.h"
24 #include "events_internal.h"
25 #include <fcntl.h>
26
27 /**
28   return the number of elements in a string list
29 */
30 static size_t str_list_length(const char **list)
31 {
32         size_t ret;
33         for (ret=0;list && list[ret];ret++) /* noop */ ;
34         return ret;
35 }
36
37 /**
38   add an entry to a string list
39 */
40 const char **ev_str_list_add(const char **list, const char *s)
41 {
42         size_t len = str_list_length(list);
43         const char **ret;
44
45         ret = talloc_realloc(NULL, list, const char *, len+2);
46         if (ret == NULL) return NULL;
47
48         ret[len] = talloc_strdup(ret, s);
49         if (ret[len] == NULL) return NULL;
50
51         ret[len+1] = NULL;
52
53         return ret;
54 }
55
56
57 /**
58  Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
59  else
60   if SYSV use O_NDELAY
61   if BSD use FNDELAY
62 **/
63
64 int ev_set_blocking(int fd, bool set)
65 {
66         int val;
67 #ifdef O_NONBLOCK
68 #define FLAG_TO_SET O_NONBLOCK
69 #else
70 #ifdef SYSV
71 #define FLAG_TO_SET O_NDELAY
72 #else /* BSD */
73 #define FLAG_TO_SET FNDELAY
74 #endif
75 #endif
76
77         if((val = fcntl(fd, F_GETFL, 0)) == -1)
78                 return -1;
79         if(set) /* Turn blocking on - ie. clear nonblock flag */
80                 val &= ~FLAG_TO_SET;
81         else
82                 val |= FLAG_TO_SET;
83         return fcntl( fd, F_SETFL, val);
84 #undef FLAG_TO_SET
85 }