Add common.h
[dhcpcd-ui] / src / dhcpcd-curses / common.h
1 /*
2  * dhcpcd - DHCP client daemon
3  * Copyright (c) 2006-2015 Roy Marples <roy@marples.name>
4  * All rights reserved
5
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #ifndef COMMON_H
29 #define COMMON_H
30
31 #include <sys/time.h>
32
33 #include "config.h"
34
35 #define USEC_PER_SEC            1000000L
36 #define USEC_PER_NSEC           1000L
37 #define NSEC_PER_SEC            1000000000L
38 #define MSEC_PER_SEC            1000L
39 #define MSEC_PER_NSEC           1000000L
40
41 /* Some systems don't define timespec macros */
42 #ifndef timespecclear
43 #define timespecclear(tsp)      (tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L)
44 #define timespecisset(tsp)      ((tsp)->tv_sec || (tsp)->tv_nsec)
45 #define timespeccmp(tsp, usp, cmp)                                      \
46         (((tsp)->tv_sec == (usp)->tv_sec) ?                             \
47             ((tsp)->tv_nsec cmp (usp)->tv_nsec) :                       \
48             ((tsp)->tv_sec cmp (usp)->tv_sec))
49 #define timespecadd(tsp, usp, vsp)                                      \
50         do {                                                            \
51                 (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec;          \
52                 (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec;       \
53                 if ((vsp)->tv_nsec >= 1000000000L) {                    \
54                         (vsp)->tv_sec++;                                \
55                         (vsp)->tv_nsec -= 1000000000L;                  \
56                 }                                                       \
57         } while (/* CONSTCOND */ 0)
58 #define timespecsub(tsp, usp, vsp)                                      \
59         do {                                                            \
60                 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;          \
61                 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec;       \
62                 if ((vsp)->tv_nsec < 0) {                               \
63                         (vsp)->tv_sec--;                                \
64                         (vsp)->tv_nsec += 1000000000L;                  \
65                 }                                                       \
66         } while (/* CONSTCOND */ 0)
67 #endif
68
69 #define timespecnorm(tv) do {                                                \
70         while ((tv)->tv_nsec >=  NSEC_PER_SEC) {                             \
71                 (tv)->tv_sec++;                                              \
72                 (tv)->tv_nsec -= NSEC_PER_SEC;                               \
73         }                                                                    \
74 } while (0 /* CONSTCOND */);
75
76 #if __GNUC__ > 2 || defined(__INTEL_COMPILER)
77 # ifndef __dead
78 #  define __dead __attribute__((__noreturn__))
79 # endif
80 # ifndef __packed
81 #  define __packed   __attribute__((__packed__))
82 # endif
83 # ifndef __unused
84 #  define __unused   __attribute__((__unused__))
85 # endif
86 #else
87 # ifndef __dead
88 #  define __dead
89 # endif
90 # ifndef __packed
91 #  define __packed
92 # endif
93 # ifndef __unused
94 #  define __unused
95 # endif
96 #endif
97
98 #endif