]> git.meshlink.io Git - catta/blob - util.c
massive work
[catta] / util.c
1 #include <string.h>
2 #include <unistd.h>
3
4 #include "util.h"
5
6 gchar *flx_get_host_name(void) {
7     char t[256];
8     gethostname(t, sizeof(t));
9     return g_strndup(t, sizeof(t));
10 }
11
12 gchar *flx_normalize_name(const gchar *s) {
13     size_t l;
14     g_assert(s);
15
16     l = strlen(s);
17
18     if (!l)
19         return g_strdup(".");
20
21     if (s[l-1] == '.')
22         return g_strdup(s);
23     
24     return g_strdup_printf("%s.", s);
25 }
26
27 gint flx_timeval_compare(const GTimeVal *a, const GTimeVal *b) {
28     g_assert(a);
29     g_assert(b);
30
31     if (a->tv_sec < b->tv_sec)
32         return -1;
33
34     if (a->tv_sec > b->tv_sec)
35         return 1;
36
37     if (a->tv_usec < b->tv_usec)
38         return -1;
39
40     if (a->tv_usec > b->tv_usec)
41         return 1;
42
43     return 0;
44 }
45
46 glong flx_timeval_diff(const GTimeVal *a, const GTimeVal *b) {
47     g_assert(a);
48     g_assert(b);
49     g_assert(flx_timeval_compare(a, b) >= 0);
50
51     return (a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
52 }