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