Make it possible to build libevents standalone.
[Samba.git] / source4 / lib / events / events_util.c
blob74e11473f31ee73a95f5cd6c627391d9f3760828
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"
24 /**
25 return the number of elements in a string list
27 static size_t str_list_length(const char **list)
29 size_t ret;
30 for (ret=0;list && list[ret];ret++) /* noop */ ;
31 return ret;
34 /**
35 add an entry to a string list
37 const char **str_list_add(const char **list, const char *s)
39 size_t len = str_list_length(list);
40 const char **ret;
42 ret = talloc_realloc(NULL, list, const char *, len+2);
43 if (ret == NULL) return NULL;
45 ret[len] = talloc_strdup(ret, s);
46 if (ret[len] == NULL) return NULL;
48 ret[len+1] = NULL;
50 return ret;
53 /**
54 compare two timeval structures.
55 Return -1 if tv1 < tv2
56 Return 0 if tv1 == tv2
57 Return 1 if tv1 > tv2
59 static int timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
61 if (tv1->tv_sec > tv2->tv_sec) return 1;
62 if (tv1->tv_sec < tv2->tv_sec) return -1;
63 if (tv1->tv_usec > tv2->tv_usec) return 1;
64 if (tv1->tv_usec < tv2->tv_usec) return -1;
65 return 0;
68 /**
69 return a zero timeval
71 struct timeval timeval_zero(void)
73 struct timeval tv;
74 tv.tv_sec = 0;
75 tv.tv_usec = 0;
76 return tv;
79 /**
80 return true if a timeval is zero
82 bool timeval_is_zero(const struct timeval *tv)
84 return tv->tv_sec == 0 && tv->tv_usec == 0;
87 /**
88 return a timeval for the current time
90 struct timeval timeval_current(void)
92 struct timeval tv;
93 GetTimeOfDay(&tv);
94 return tv;
97 /**
98 return a timeval struct with the given elements
100 struct timeval timeval_set(uint32_t secs, uint32_t usecs)
102 struct timeval tv;
103 tv.tv_sec = secs;
104 tv.tv_usec = usecs;
105 return tv;
109 return the difference between two timevals as a timeval
110 if tv1 comes after tv2, then return a zero timeval
111 (this is *tv2 - *tv1)
113 struct timeval timeval_until(const struct timeval *tv1,
114 const struct timeval *tv2)
116 struct timeval t;
117 if (timeval_compare(tv1, tv2) >= 0) {
118 return timeval_zero();
120 t.tv_sec = tv2->tv_sec - tv1->tv_sec;
121 if (tv1->tv_usec > tv2->tv_usec) {
122 t.tv_sec--;
123 t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
124 } else {
125 t.tv_usec = tv2->tv_usec - tv1->tv_usec;
127 return t;