Commit | Line | Data |
---|---|---|
b74bc1d6 JG |
1 | /* |
2 | * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License, version 2 only, as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | */ | |
17 | ||
18 | #include <common/time.h> | |
19 | #include <common/macros.h> | |
20 | #include <stddef.h> | |
21 | #include <stdint.h> | |
22 | #include <limits.h> | |
23 | #include <errno.h> | |
24 | #include <pthread.h> | |
10945cfa JG |
25 | #include <locale.h> |
26 | #include <string.h> | |
27 | ||
28 | static bool utf8_output_supported; | |
29 | ||
30 | LTTNG_HIDDEN | |
31 | bool locale_supports_utf8(void) | |
32 | { | |
33 | return utf8_output_supported; | |
34 | } | |
b74bc1d6 JG |
35 | |
36 | LTTNG_HIDDEN | |
37 | int timespec_to_ms(struct timespec ts, unsigned long *ms) | |
38 | { | |
39 | unsigned long res, remain_ms; | |
40 | ||
41 | if (ts.tv_sec > ULONG_MAX / MSEC_PER_SEC) { | |
42 | errno = EOVERFLOW; | |
43 | return -1; /* multiplication overflow */ | |
44 | } | |
45 | res = ts.tv_sec * MSEC_PER_SEC; | |
46 | remain_ms = ULONG_MAX - res; | |
47 | if (ts.tv_nsec / NSEC_PER_MSEC > remain_ms) { | |
48 | errno = EOVERFLOW; | |
49 | return -1; /* addition overflow */ | |
50 | } | |
51 | res += ts.tv_nsec / NSEC_PER_MSEC; | |
52 | *ms = res; | |
53 | return 0; | |
54 | } | |
55 | ||
56 | LTTNG_HIDDEN | |
57 | struct timespec timespec_abs_diff(struct timespec t1, struct timespec t2) | |
58 | { | |
59 | uint64_t ts1 = (uint64_t) t1.tv_sec * (uint64_t) NSEC_PER_SEC + | |
60 | (uint64_t) t1.tv_nsec; | |
61 | uint64_t ts2 = (uint64_t) t2.tv_sec * (uint64_t) NSEC_PER_SEC + | |
62 | (uint64_t) t2.tv_nsec; | |
63 | uint64_t diff = max(ts1, ts2) - min(ts1, ts2); | |
64 | struct timespec res; | |
65 | ||
66 | res.tv_sec = diff / (uint64_t) NSEC_PER_SEC; | |
67 | res.tv_nsec = diff % (uint64_t) NSEC_PER_SEC; | |
68 | return res; | |
69 | } | |
10945cfa JG |
70 | |
71 | static | |
72 | void __attribute__((constructor)) init_locale_utf8_support(void) | |
73 | { | |
74 | const char *program_locale = setlocale(LC_ALL, NULL); | |
75 | const char *lang = getenv("LANG"); | |
76 | ||
77 | if (program_locale && strstr(program_locale, "utf8")) { | |
78 | utf8_output_supported = true; | |
79 | } else if (strstr(lang, "utf8")) { | |
80 | utf8_output_supported = true; | |
81 | } | |
82 | } |