source: branches/samba-3.5.x/lib/tevent/tevent_util.c

Last change on this file was 464, checked in by Silvan Scherrer, 15 years ago

Samba Server 3.5: changed pipe() to socketpair()

File size: 2.4 KB
Line 
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 "replace.h"
26#include "talloc.h"
27#include "tevent.h"
28#include "tevent_internal.h"
29#include "tevent_util.h"
30#include <fcntl.h>
31#ifdef __OS2__
32#include <sys/filio.h>
33#endif
34
35/**
36 return the number of elements in a string list
37*/
38size_t ev_str_list_length(const char **list)
39{
40 size_t ret;
41 for (ret=0;list && list[ret];ret++) /* noop */ ;
42 return ret;
43}
44
45/**
46 add an entry to a string list
47*/
48const char **ev_str_list_add(const char **list, const char *s)
49{
50 size_t len = ev_str_list_length(list);
51 const char **ret;
52
53 ret = talloc_realloc(NULL, list, const char *, len+2);
54 if (ret == NULL) return NULL;
55
56 ret[len] = talloc_strdup(ret, s);
57 if (ret[len] == NULL) return NULL;
58
59 ret[len+1] = NULL;
60
61 return ret;
62}
63
64
65/**
66 Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
67 else
68 if SYSV use O_NDELAY
69 if BSD use FNDELAY
70**/
71
72int ev_set_blocking(int fd, bool set)
73{
74 int val;
75#ifdef O_NONBLOCK
76#define FLAG_TO_SET O_NONBLOCK
77#else
78#ifdef SYSV
79#define FLAG_TO_SET O_NDELAY
80#else /* BSD */
81#define FLAG_TO_SET FNDELAY
82#endif
83#endif
84
85 if((val = fcntl(fd, F_GETFL, 0)) == -1)
86 return -1;
87#ifndef __OS2__
88 if(set) /* Turn blocking on - ie. clear nonblock flag */
89 val &= ~FLAG_TO_SET;
90 else
91 val |= FLAG_TO_SET;
92 return fcntl( fd, F_SETFL, val);
93#else
94 if(set) /* turn blocking on - ie. clear nonblock flag */
95 val = 0;
96 else
97 val = 1;
98 return os2_ioctl(fd, FIONBIO, (char *) &val, sizeof(val));
99#endif
100
101#undef FLAG_TO_SET
102}
Note: See TracBrowser for help on using the repository browser.