selftest: add my copyright
[Samba/gbeck.git] / lib / tevent / tevent_util.c
blob9f70cf2349ab4a2e211ddb1e8bc16437b94d44cd
1 /*
2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2005
5 Copyright (C) Jelmer Vernooij 2005
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.
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.
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/>.
21 #include "replace.h"
22 #include "talloc.h"
23 #include "tevent.h"
24 #include "tevent_internal.h"
25 #include "tevent_util.h"
26 #include <fcntl.h>
28 /**
29 return the number of elements in a string list
31 size_t ev_str_list_length(const char **list)
33 size_t ret;
34 for (ret=0;list && list[ret];ret++) /* noop */ ;
35 return ret;
38 /**
39 add an entry to a string list
41 const char **ev_str_list_add(const char **list, const char *s)
43 size_t len = ev_str_list_length(list);
44 const char **ret;
46 ret = talloc_realloc(NULL, list, const char *, len+2);
47 if (ret == NULL) return NULL;
49 ret[len] = talloc_strdup(ret, s);
50 if (ret[len] == NULL) return NULL;
52 ret[len+1] = NULL;
54 return ret;
58 /**
59 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
60 else
61 if SYSV use O_NDELAY
62 if BSD use FNDELAY
63 **/
65 int ev_set_blocking(int fd, bool set)
67 int val;
68 #ifdef O_NONBLOCK
69 #define FLAG_TO_SET O_NONBLOCK
70 #else
71 #ifdef SYSV
72 #define FLAG_TO_SET O_NDELAY
73 #else /* BSD */
74 #define FLAG_TO_SET FNDELAY
75 #endif
76 #endif
78 if((val = fcntl(fd, F_GETFL, 0)) == -1)
79 return -1;
80 if(set) /* Turn blocking on - ie. clear nonblock flag */
81 val &= ~FLAG_TO_SET;
82 else
83 val |= FLAG_TO_SET;
84 return fcntl( fd, F_SETFL, val);
85 #undef FLAG_TO_SET