include/tkz/tkz_local.h

filter: Statementstmt 94.9% 75 / 79 Branchbran 75.5% 71 / 94 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.3% 146 / 173 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/engine.xs ↑ Summary Zone.xs »
1 #ifndef TKZ_LOCAL_H
2 #define TKZ_LOCAL_H
3  
4 /* This process's zone, found by name and never by reading the system's
5 * zone data: the TZ variable, then the name /etc/localtime links to, then
6 * /etc/timezone, then /etc/sysconfig/clock, then on Windows the zone key
7 * name mapped through CLDR's table. UTC when nothing names a zone that is
8 * compiled in.
9 *
10 * TZ is passed in, never read with getenv: under PERL_IMPLICIT_SYS getenv
11 * does not see %ENV. NULL means TZ is not set; an empty TZ means UTC, as
12 * it does to the C library. */
13  
14 #ifdef WIN32
15 # include <windows.h>
16 #else
17 # include <unistd.h>
18 #endif
19 #include <stdio.h>
20  
21 /* The zone a path names, if it holds ".../zoneinfo/Area/City". */
22 static int
23 3 tkz_zone_from_path(const char *p, size_t len)
24 {
25 size_t i;
26 static const char key[] = "zoneinfo/";
27 67 ▶ for (i = len; i-- > 0; ) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = len; i-- > 0; ) { 67 0 67
28 67 ▶ if (i + sizeof(key) - 1 <= len && memcmp(p + i, key, sizeof(key) - 1) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (i + sizeof(key) - 1 <= len && memcmp(p + i, key, sizeof(key) - 1) == 0) { 43 24 67
if (i + sizeof(key) - 1 <= len && memcmp(p + i, key, sizeof(key) - 1) == 0) { 3 40 43
29 3 size_t start = i + sizeof(key) - 1;
30 3 return tkz_lookup_name(p + start, len - start);
31 }
32 }
33 0 return -1;
34 }
35  
36 static int
37 11 tkz_zone_from_tz(const char *tz, size_t len)
38 {
39 int id;
40 11 ▶ if (len && tz[0] == ':') {
Branch: 2 branches
BranchTrueFalseTotal
if (len && tz[0] == ':') { 9 2 11
if (len && tz[0] == ':') { 1 8 9
41 1 tz++;
42 1 len--;
43 }
44 11 ▶ if (len == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) 2 9 11
45 2 return tkz_lookup_name("Etc/UTC", 7);
46 9 ▶ if (tz[0] == '/')
Branch: 1 branch
BranchTrueFalseTotal
if (tz[0] == '/') 1 8 9
47 1 return tkz_zone_from_path(tz, len);
48 8 id = tkz_lookup_name(tz, len);
49 8 return id;
50 }
51  
52 /* The first line of a file, trimmed of space; 0 if there is none. */
53 static size_t
54 3 tkz_first_line(const char *path, char *buf, size_t size)
55 {
56 3 FILE *fh = fopen(path, "r");
57 size_t n;
58 3 ▶ if (!fh)
Branch: 1 branch
BranchTrueFalseTotal
if (!fh) 1 2 3
59 1 return 0;
60 2 ▶ if (!fgets(buf, (int)size, fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fgets(buf, (int)size, fh)) { 0 2 2
61 0 fclose(fh);
62 0 return 0;
63 }
64 2 fclose(fh);
65 2 n = strlen(buf);
66 8 ▶ while (n && (buf[n - 1] == '\n' || buf[n - 1] == '\r' || buf[n - 1] == ' ' || buf[n - 1] == '\t'))
Branch: 5 branches
BranchTrueFalseTotal
while (n && (buf[n - 1] == '\n' || buf[n - 1] == '\r' || buf[n - 1] == ' ' || buf[n - 1] == '\t')) 8 0 8
while (n && (buf[n - 1] == '\n' || buf[n - 1] == '\r' || buf[n - 1] == ' ' || buf[n - 1] == '\t')) 2 6 8
while (n && (buf[n - 1] == '\n' || buf[n - 1] == '\r' || buf[n - 1] == ' ' || buf[n - 1] == '\t')) 0 6 6
while (n && (buf[n - 1] == '\n' || buf[n - 1] == '\r' || buf[n - 1] == ' ' || buf[n - 1] == '\t')) 4 2 6
while (n && (buf[n - 1] == '\n' || buf[n - 1] == '\r' || buf[n - 1] == ' ' || buf[n - 1] == '\t')) 0 2 2
67 6 buf[--n] = '\0';
68 6 ▶ while (n && (buf[0] == ' ' || buf[0] == '\t')) {
Branch: 3 branches
BranchTrueFalseTotal
while (n && (buf[0] == ' ' || buf[0] == '\t')) { 6 0 6
while (n && (buf[0] == ' ' || buf[0] == '\t')) { 4 2 6
while (n && (buf[0] == ' ' || buf[0] == '\t')) { 0 2 2
69 4 memmove(buf, buf + 1, n);
70 4 n--;
71 }
72 2 return n;
73 }
74  
75 /* ZONE="Europe/London" in a Red Hat style /etc/sysconfig/clock. */
76 static int
77 1 tkz_zone_from_clock_file(const char *path)
78 {
79 1 FILE *fh = fopen(path, "r");
80 char line[256];
81 1 int id = -1;
82 1 ▶ if (!fh)
Branch: 1 branch
BranchTrueFalseTotal
if (!fh) 0 1 1
83 0 return -1;
84 4 ▶ while (id < 0 && fgets(line, sizeof line, fh)) {
Branch: 2 branches
BranchTrueFalseTotal
while (id < 0 && fgets(line, sizeof line, fh)) { 3 1 4
while (id < 0 && fgets(line, sizeof line, fh)) { 3 0 3
85 3 char *p = line, *q;
86 3 ▶ while (*p == ' ' || *p == '\t') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (*p == ' ' || *p == '\t') p++; 0 3 3
while (*p == ' ' || *p == '\t') p++; 0 3 3
87 3 ▶ if (strncmp(p, "ZONE=", 5) && strncmp(p, "TIMEZONE=", 9))
Branch: 2 branches
BranchTrueFalseTotal
if (strncmp(p, "ZONE=", 5) && strncmp(p, "TIMEZONE=", 9)) 2 1 3
if (strncmp(p, "ZONE=", 5) && strncmp(p, "TIMEZONE=", 9)) 2 0 2
88 2 continue;
89 1 p = strchr(p, '=') + 1;
90 1 ▶ if (*p == '"' || *p == '\'') p++;
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '"' || *p == '\'') p++; 0 1 1
if (*p == '"' || *p == '\'') p++; 0 0 0
91 1 q = p;
92 13 ▶ while (*q && *q != '"' && *q != '\'' && *q != '\n' && *q != '\r' && *q != ' ')
Branch: 6 branches
BranchTrueFalseTotal
while (*q && *q != '"' && *q != '\'' && *q != '\n' && *q != '\r' && *q != ' ') 13 0 13
while (*q && *q != '"' && *q != '\'' && *q != '\n' && *q != '\r' && *q != ' ') 12 1 13
while (*q && *q != '"' && *q != '\'' && *q != '\n' && *q != '\r' && *q != ' ') 12 0 12
while (*q && *q != '"' && *q != '\'' && *q != '\n' && *q != '\r' && *q != ' ') 12 0 12
while (*q && *q != '"' && *q != '\'' && *q != '\n' && *q != '\r' && *q != ' ') 12 0 12
while (*q && *q != '"' && *q != '\'' && *q != '\n' && *q != '\r' && *q != ' ') 12 0 12
93 12 q++;
94 1 id = tkz_lookup_name(p, (size_t)(q - p));
95 }
96 1 fclose(fh);
97 1 return id;
98 }
99  
100 static int
101 7 tkz_windows_lookup(const char *name)
102 {
103 7 int lo = 0, hi = TKZ_WINDOWS_COUNT - 1;
104 47 ▶ while (lo <= hi) {
Branch: 1 branch
BranchTrueFalseTotal
while (lo <= hi) { 44 3 47
105 44 int mid = lo + (hi - lo) / 2;
106 44 int c = strcmp(tkz_windows_names + tkz_windows[mid].name, name);
107 44 ▶ if (c == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c == 0) { 4 40 44
108 4 const char *zn = tkz_names + tkz_zones[tkz_windows[mid].zone].name;
109 4 return tkz_lookup_name(zn, strlen(zn));
110 }
111 40 ▶ if (c < 0) lo = mid + 1; else hi = mid - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0) lo = mid + 1; else hi = mid - 1; 19 21 40
112 }
113 3 return -1;
114 }
115  
116 /* Each source tried in turn, with the files and the Windows time zone key
117 * name passed in so a test can stand in its own on any platform. NULL
118 * skips a source; the system is never asked here. */
119 static int
120 20 tkz_local_zone_from(const char *tz, size_t tzlen,
121 const char *localtime_link, const char *timezone_file,
122 const char *clock_file, const char *windows_key)
123 {
124 int id;
125 char buf[1024];
126 20 ▶ if (tz && (id = tkz_zone_from_tz(tz, tzlen)) >= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (tz && (id = tkz_zone_from_tz(tz, tzlen)) >= 0) 11 9 20
if (tz && (id = tkz_zone_from_tz(tz, tzlen)) >= 0) 10 1 11
127 10 return id;
128 #ifndef WIN32
129 10 ▶ if (localtime_link) {
Branch: 1 branch
BranchTrueFalseTotal
if (localtime_link) { 4 6 10
130 4 ssize_t n = readlink(localtime_link, buf, sizeof buf - 1);
131 4 ▶ if (n > 0 && (id = tkz_zone_from_path(buf, (size_t)n)) >= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (n > 0 && (id = tkz_zone_from_path(buf, (size_t)n)) >= 0) 2 2 4
if (n > 0 && (id = tkz_zone_from_path(buf, (size_t)n)) >= 0) 2 0 2
132 2 return id;
133 }
134 #else
135 (void)localtime_link;
136 #endif
137 8 ▶ if (timezone_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (timezone_file) { 3 5 8
138 3 size_t n = tkz_first_line(timezone_file, buf, sizeof buf);
139 3 ▶ if (n && (id = tkz_lookup_name(buf, n)) >= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (n && (id = tkz_lookup_name(buf, n)) >= 0) 2 1 3
if (n && (id = tkz_lookup_name(buf, n)) >= 0) 2 0 2
140 2 return id;
141 }
142 6 ▶ if (clock_file && (id = tkz_zone_from_clock_file(clock_file)) >= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (clock_file && (id = tkz_zone_from_clock_file(clock_file)) >= 0) 1 5 6
if (clock_file && (id = tkz_zone_from_clock_file(clock_file)) >= 0) 1 0 1
143 1 return id;
144 5 ▶ if (windows_key && (id = tkz_windows_lookup(windows_key)) >= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (windows_key && (id = tkz_windows_lookup(windows_key)) >= 0) 3 2 5
if (windows_key && (id = tkz_windows_lookup(windows_key)) >= 0) 1 2 3
145 1 return id;
146 4 return tkz_lookup_name("Etc/UTC", 7);
147 }
148  
149 /* The system's time zone key name on Windows, in buf; NULL elsewhere or
150 * when Windows has none. */
151 static const char *
152 3 tkz_windows_key_name(char *buf, size_t size)
153 {
154 #ifdef WIN32
155 DYNAMIC_TIME_ZONE_INFORMATION dz;
156 size_t i;
157 if (GetDynamicTimeZoneInformation(&dz) == TIME_ZONE_ID_INVALID)
158 return NULL;
159 for (i = 0; i + 1 < size && i < 128 && dz.TimeZoneKeyName[i]; i++)
160 buf[i] = dz.TimeZoneKeyName[i] < 128 ? (char)dz.TimeZoneKeyName[i] : '?';
161 buf[i] = '\0';
162 return i ? buf : NULL;
163 #else
164 (void)buf;
165 (void)size;
166 3 return NULL;
167 #endif
168 }
169  
170 static int
171 3 tkz_local_zone(const char *tz, size_t tzlen)
172 {
173 char key[256];
174 3 return tkz_local_zone_from(tz, tzlen, "/etc/localtime", "/etc/timezone",
175 "/etc/sysconfig/clock",
176 tkz_windows_key_name(key, sizeof key));
177 }
178  
179 #endif
« xs/engine.xs ↑ Summary Zone.xs »