summaryrefslogtreecommitdiff
path: root/src/basic/time-util.c
diff options
context:
space:
mode:
authorDan Streetman <ddstreet@canonical.com>2021-06-29 09:13:22 -0400
committerDan Streetman <ddstreet@canonical.com>2021-06-30 13:42:58 -0400
commit31097e2b996ed463ca97d3df618a614c875386c5 (patch)
tree233aa196ee46ae06251726144689919122b386b7 /src/basic/time-util.c
parent58551e6ebc465227d0add8c714f9f38213b6878a (diff)
downloadsystemd-31097e2b996ed463ca97d3df618a614c875386c5.tar.gz
time: simplify get_timezones()
The function can be simplified by using extract_many_words() and strv_extend()
Diffstat (limited to 'src/basic/time-util.c')
-rw-r--r--src/basic/time-util.c56
1 files changed, 19 insertions, 37 deletions
diff --git a/src/basic/time-util.c b/src/basic/time-util.c
index 48e4d8dea8..c6bdf01d12 100644
--- a/src/basic/time-util.c
+++ b/src/basic/time-util.c
@@ -1265,23 +1265,14 @@ int parse_nsec(const char *t, nsec_t *nsec) {
int get_timezones(char ***ret) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_strv_free_ char **zones = NULL;
- size_t n_zones = 0;
int r;
assert(ret);
- zones = strv_new("UTC");
- if (!zones)
- return -ENOMEM;
-
- n_zones = 1;
-
f = fopen("/usr/share/zoneinfo/zone1970.tab", "re");
if (f) {
for (;;) {
- _cleanup_free_ char *line = NULL, *w = NULL;
- char *p;
- size_t k;
+ _cleanup_free_ char *line = NULL, *cc = NULL, *co = NULL, *tz = NULL;
r = read_line(f, LONG_LINE_MAX, &line);
if (r < 0)
@@ -1289,43 +1280,34 @@ int get_timezones(char ***ret) {
if (r == 0)
break;
- p = strstrip(line);
+ const char *p = line;
- if (isempty(p) || *p == '#')
+ /* Line format is:
+ * 'country codes' 'coordinates' 'timezone' 'comments' */
+ r = extract_many_words(&p, NULL, 0, &cc, &co, &tz, NULL);
+ if (r < 0)
continue;
- /* Skip over country code */
- p += strcspn(p, WHITESPACE);
- p += strspn(p, WHITESPACE);
-
- /* Skip over coordinates */
- p += strcspn(p, WHITESPACE);
- p += strspn(p, WHITESPACE);
-
- /* Found timezone name */
- k = strcspn(p, WHITESPACE);
- if (k <= 0)
+ /* Lines that start with # are comments. */
+ if (*cc == '#')
continue;
- w = strndup(p, k);
- if (!w)
- return -ENOMEM;
-
- if (!GREEDY_REALLOC(zones, n_zones + 2))
- return -ENOMEM;
-
- zones[n_zones++] = TAKE_PTR(w);
- zones[n_zones] = NULL;
+ r = strv_extend(&zones, tz);
+ if (r < 0)
+ return r;
}
-
- strv_sort(zones);
- strv_uniq(zones);
-
} else if (errno != ENOENT)
return -errno;
- *ret = TAKE_PTR(zones);
+ /* Always include UTC */
+ r = strv_extend(&zones, "UTC");
+ if (r < 0)
+ return -ENOMEM;
+ strv_sort(zones);
+ strv_uniq(zones);
+
+ *ret = TAKE_PTR(zones);
return 0;
}