]> git.meshlink.io Git - catta/blob - util.c
0fcedc71c0fee844ba99015c4b5a6fcd421bb883
[catta] / util.c
1 #include <string.h>
2 #include <unistd.h>
3 #include <fcntl.h>
4 #include <errno.h>
5
6 #include "util.h"
7
8 gchar *flx_get_host_name(void) {
9     char t[256];
10     gethostname(t, sizeof(t));
11     return g_strndup(t, sizeof(t));
12 }
13
14 gchar *flx_normalize_name(const gchar *s) {
15     size_t l;
16     g_assert(s);
17
18     l = strlen(s);
19
20     if (!l)
21         return g_strdup(".");
22
23     if (s[l-1] == '.')
24         return g_strdup(s);
25     
26     return g_strdup_printf("%s.", s);
27 }
28
29 gint flx_timeval_compare(const GTimeVal *a, const GTimeVal *b) {
30     g_assert(a);
31     g_assert(b);
32
33     if (a->tv_sec < b->tv_sec)
34         return -1;
35
36     if (a->tv_sec > b->tv_sec)
37         return 1;
38
39     if (a->tv_usec < b->tv_usec)
40         return -1;
41
42     if (a->tv_usec > b->tv_usec)
43         return 1;
44
45     return 0;
46 }
47
48 glong flx_timeval_diff(const GTimeVal *a, const GTimeVal *b) {
49     g_assert(a);
50     g_assert(b);
51
52     if (flx_timeval_compare(a, b) < 0)
53         return flx_timeval_diff(b, a);
54
55     return ((glong) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
56 }
57
58
59 gint flx_set_cloexec(gint fd) {
60     gint n;
61
62     g_assert(fd >= 0);
63     
64     if ((n = fcntl(fd, F_GETFD)) < 0)
65         return -1;
66
67     if (n & FD_CLOEXEC)
68         return 0;
69
70     return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
71 }
72
73 gint flx_set_nonblock(gint fd) {
74     gint n;
75
76     g_assert(fd >= 0);
77
78     if ((n = fcntl(fd, F_GETFL)) < 0)
79         return -1;
80
81     if (n & O_NONBLOCK)
82         return 0;
83
84     return fcntl(fd, F_SETFL, n|O_NONBLOCK);
85 }
86
87 gint flx_wait_for_write(gint fd) {
88     fd_set fds;
89     gint r;
90     
91     FD_ZERO(&fds);
92     FD_SET(fd, &fds);
93     
94     if ((r = select(fd+1, NULL, &fds, NULL, NULL)) < 0) {
95         g_message("select() failed: %s", strerror(errno));
96
97         return -1;
98     }
99     
100     g_assert(r > 0);
101
102     return 0;
103 }
104
105 GTimeVal *flx_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
106     g_assert(tv);
107
108     g_get_current_time(tv);
109
110     if (msec)
111         g_time_val_add(tv, msec*1000);
112
113     if (jitter)
114         g_time_val_add(tv, g_random_int_range(0, jitter) * 1000);
115         
116     return tv;
117 }
118
119 gint flx_age(const GTimeVal *a) {
120     GTimeVal now;
121     
122     g_assert(a);
123
124     g_get_current_time(&now);
125
126     return flx_timeval_diff(&now, a);
127 }
128
129 gboolean flx_domain_equal(const gchar *a, const gchar *b) {
130     g_assert(a);
131     g_assert(b);
132
133     for (;;) {
134         if (*a == 0 && *b == 0)
135             return TRUE;
136         
137         if (*a == 0 && *b == '.' && *(b+1) == 0)
138             return TRUE;
139
140         if (*a == '.' && *(a+1) == 0 && *b == 0)
141             return TRUE;
142
143         if (*a != *b)
144             return FALSE;
145
146         a++;
147         b++;
148     }
149 }
150
151 guint flx_domain_hash(const gchar *p) {
152     char t[256];
153     strncpy(t, p, sizeof(t)-1);
154     t[sizeof(t)-1] = 0;
155
156     return g_int_hash(t);
157 }
158