1 #ifndef COMPAT_H
2 #define COMPAT_H
3 
4 #define _GNU_SOURCE
5 
6 /* ISO C */
7 #include <assert.h>
8 #include <complex.h>
9 #include <ctype.h>
10 #include <errno.h>
11 #include <fenv.h>
12 #include <float.h>
13 #include <inttypes.h>
14 #include <iso646.h>
15 #include <limits.h>
16 #include <locale.h>
17 #include <math.h>
18 #include <setjmp.h>
19 #include <signal.h>
20 #include <stdarg.h>
21 #include <stdbool.h>
22 #include <stddef.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <tgmath.h>
28 #include <time.h>
29 #include <wchar.h>
30 #include <wctype.h>
31 
32 #include <unistd.h>
33 #include <sys/time.h>
34 #include <sys/types.h>
35 #include <pwd.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <arpa/inet.h>
39 #include <sys/queue.h>
40 #include <netinet/tcp.h>
41 #include <netinet/in.h>
42 
43 #define ARRAY_SIZE(x)      (sizeof(x) / sizeof(x[1]))
44 #define COMPILER_ASSERT(X) (void)sizeof(char[(X) ? 1 : -1])
45 
46 /* Test for backtrace() */
47 #if defined(__APPLE__) || (defined(__linux__) && defined(__GLIBC__))
48 #define HAVE_BACKTRACE 1
49 #endif
50 
51 #if defined(__linux__) || defined(__OpenBSD__)
52 #define _XOPEN_SOURCE 700
53 /*
54  *  * On NetBSD, _XOPEN_SOURCE undefines _NETBSD_SOURCE and
55  *   * thus hides inet_aton etc.
56  *    */
57 #elif !defined(__NetBSD__)
58 #define _XOPEN_SOURCE
59 #endif
60 
61 // It converts <x>[dhms] to seconds.
62 // If format is invalid, or x is not a positive integer, returns -1.
63 static inline int
seconds_from_string(char * s,int * seconds)64 seconds_from_string(char *s, int *seconds)
65 {
66     int x   = 0;
67     char *p = s;
68     int mul = 24 * 3600;
69     while (*p != '\0') {
70         if (isdigit(*p)) {
71             p++;
72             continue;
73         } else {
74             if (*p == 'd') {
75                 mul = 24 * 3600;
76                 break;
77             } else if (*p == 'h') {
78                 mul = 3600;
79                 break;
80             } else if (*p == 'm') {
81                 mul = 60;
82                 break;
83             } else if (*p == 's') {
84                 mul = 1;
85                 break;
86             } else {
87                 return -1;
88             }
89         }
90     }
91     // *p or *(p+1) should be '\0'.
92     if (*p != '\0' && *(p + 1) != '\0') {
93         return -2;
94     }
95     errno = 0;
96     x     = strtol(s, NULL, 10);
97     if (errno != 0) {
98         return -3;
99     }
100     *seconds = x * mul;
101     return 0;
102 }
103 
104 #endif
105