Use constants rather than string comparison for a saner API.
[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 #ifndef TIMEVAL_TO_TIMESPEC
42 #define TIMEVAL_TO_TIMESPEC(tv, ts) do {                                \
43         (ts)->tv_sec = (tv)->tv_sec;                                    \
44         (ts)->tv_nsec = (tv)->tv_usec * USEC_PER_NSEC;                  \
45 } while (0 /* CONSTCOND */)
46 #endif
47
48 /* Some systems don't define timespec macros */
49 #ifndef timespecclear
50 #define timespecclear(tsp)      (tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L)
51 #define timespecisset(tsp)      ((tsp)->tv_sec || (tsp)->tv_nsec)
52 #define timespeccmp(tsp, usp, cmp)                                      \
53         (((tsp)->tv_sec == (usp)->tv_sec) ?                             \
54             ((tsp)->tv_nsec cmp (usp)->tv_nsec) :                       \
55             ((tsp)->tv_sec cmp (usp)->tv_sec))
56 #define timespecadd(tsp, usp, vsp)                                      \
57         do {                                                            \
58                 (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec;          \
59                 (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec;       \
60                 if ((vsp)->tv_nsec >= 1000000000L) {                    \
61                         (vsp)->tv_sec++;                                \
62                         (vsp)->tv_nsec -= 1000000000L;                  \
63                 }                                                       \
64         } while (/* CONSTCOND */ 0)
65 #define timespecsub(tsp, usp, vsp)                                      \
66         do {                                                            \
67                 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;          \
68                 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec;       \
69                 if ((vsp)->tv_nsec < 0) {                               \
70                         (vsp)->tv_sec--;                                \
71                         (vsp)->tv_nsec += 1000000000L;                  \
72                 }                                                       \
73         } while (/* CONSTCOND */ 0)
74 #endif
75
76 #define timespecnorm(tv) do {                                                \
77         while ((tv)->tv_nsec >=  NSEC_PER_SEC) {                             \
78                 (tv)->tv_sec++;                                              \
79                 (tv)->tv_nsec -= NSEC_PER_SEC;                               \
80         }                                                                    \
81 } while (0 /* CONSTCOND */);
82
83 #if __GNUC__ > 2 || defined(__INTEL_COMPILER)
84 # ifndef __dead
85 #  define __dead __attribute__((__noreturn__))
86 # endif
87 # ifndef __packed
88 #  define __packed   __attribute__((__packed__))
89 # endif
90 # ifndef __unused
91 #  define __unused   __attribute__((__unused__))
92 # endif
93 #else
94 # ifndef __dead
95 #  define __dead
96 # endif
97 # ifndef __packed
98 #  define __packed
99 # endif
100 # ifndef __unused
101 #  define __unused
102 # endif
103 #endif
104
105 #endif