chiark / gitweb /
Infrastructure: Switch testing over to Autotest.
[mLib] / sys / tv.h
1 /* -*-c-*-
2  *
3  * Manipulation of timeval structures
4  *
5  * (c) 1998 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the mLib utilities library.
11  *
12  * mLib is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Library General Public License as
14  * published by the Free Software Foundation; either version 2 of the
15  * License, or (at your option) any later version.
16  *
17  * mLib is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with mLib; if not, write to the Free
24  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25  * MA 02111-1307, USA.
26  */
27
28 #ifndef MLIB_TV_H
29 #define MLIB_TV_H
30
31 #ifdef __cplusplus
32   extern "C" {
33 #endif
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #include <sys/time.h>
38
39 /*----- A macro to make reading easier ------------------------------------*/
40
41 #define MILLION 1000000
42
43 /*----- Functions provided ------------------------------------------------*/
44
45 /* --- @tv_add@ --- *
46  *
47  * Arguments:   @struct timeval *dst@ = destination block
48  *              @const struct timeval *a, *b@ = source blocks
49  *
50  * Returns:     ---
51  *
52  * Use:         Adds two timevals.
53  */
54
55 extern void tv_add(struct timeval */*dst*/,
56                    const struct timeval */*a*/,
57                    const struct timeval */*b*/);
58
59 #define TV_ADD(dst, a, b) TV_ADDL(dst, a, (b)->tv_sec, (b)->tv_usec)
60
61 /* --- @tv_addl@ --- *
62  *
63  * Arguments:   @struct timeval *dst@ = destination block
64  *              @const struct timeval *a@ = source blocks
65  *              @time_t sec@, @unsigned long usec@ = time to add
66  *
67  * Returns:     ---
68  *
69  * Use:         Adds a literal time in seconds and microseconds.
70  */
71
72 extern void tv_addl(struct timeval */*dst*/,
73                     const struct timeval */*a*/,
74                     time_t /*sec*/, unsigned long /*usec*/);
75
76 #define TV_ADDL(dst, a, sec, usec) do {                                 \
77   (dst)->tv_sec = (a)->tv_sec + (sec);                                  \
78   (dst)->tv_usec = (a)->tv_usec + (usec);                               \
79   if ((dst)->tv_usec >= MILLION) {                                      \
80     (dst)->tv_usec -= MILLION;                                          \
81     (dst)->tv_sec++;                                                    \
82   }                                                                     \
83 } while (0)
84
85 /* --- @tv_sub@ --- *
86  *
87  * Arguments:   @struct timeval *dst@ = destination block
88  *              @const struct timeval *a, *b@ = source blocks
89  *
90  * Returns:     ---
91  *
92  * Use:         Subtracts two timevals.
93  */
94
95 extern void tv_sub(struct timeval */*dst*/,
96                    const struct timeval */*a*/,
97                    const struct timeval */*b*/);
98
99 #define TV_SUB(dst, a, b) TV_SUBL(dst, a, (b)->tv_sec, (b)->tv_usec)
100
101 /* --- @tv_subl@ --- *
102  *
103  * Arguments:   @struct timeval *dst@ = destination block
104  *              @const struct timeval *a@ = source blocks
105  *              @time_t sec@, @unsigned long usec@ = time to subtract
106  *
107  * Returns:     ---
108  *
109  * Use:         Subtracts a literal time in seconds and microseconds.
110  */
111
112 extern void tv_subl(struct timeval */*dst*/,
113                     const struct timeval */*a*/,
114                     time_t /*sec*/, unsigned long /*usec*/);
115
116 #define TV_SUBL(dst, a, sec, usec) do {                                 \
117   (dst)->tv_sec = (a)->tv_sec - (sec);                                  \
118   if ((a)->tv_usec >= (usec))                                           \
119     (dst)->tv_usec = (a)->tv_usec - (usec);                             \
120   else {                                                                \
121     (dst)->tv_usec = (a)->tv_usec + MILLION - (usec);                   \
122     (dst)->tv_sec--;                                                    \
123   }                                                                     \
124 } while (0)
125
126 /* --- @tv_cmp@ --- *
127  *
128  * Arguments:   @const struct timeval *a, *b@ = source blocks
129  *
130  * Returns:     Less than, equal to, or greater than zero.
131  *
132  * Use:         Compares two timevals.
133  */
134
135 extern int tv_cmp(const struct timeval */*a*/,
136                   const struct timeval */*b*/);
137
138 #define TV_CMP(a, op, b) ((a)->tv_sec == (b)->tv_sec ?                  \
139                             (a)->tv_usec op (b)->tv_usec :              \
140                             (a)->tv_sec op (b)->tv_sec)
141
142 /*----- That's all, folks -------------------------------------------------*/
143
144 #ifdef __cplusplus
145   }
146 #endif
147
148 #endif