From: Dan Streetman Date: Wed, 30 Jun 2021 11:17:22 +0000 (-0400) Subject: time: split get_timezone() into main function and zone1970.tab function X-Git-Tag: v249-rc3~11^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=09a54a862b8f45cff087eb4eabbd283d354afc90;p=thirdparty%2Fsystemd.git time: split get_timezone() into main function and zone1970.tab function This allows for adding another function to read from a different timezone source, which is added in the next commit. --- diff --git a/src/basic/time-util.c b/src/basic/time-util.c index c6bdf01d122..c874cfec894 100644 --- a/src/basic/time-util.c +++ b/src/basic/time-util.c @@ -1262,7 +1262,7 @@ int parse_nsec(const char *t, nsec_t *nsec) { return 0; } -int get_timezones(char ***ret) { +static int get_timezones_from_zone1970_tab(char ***ret) { _cleanup_fclose_ FILE *f = NULL; _cleanup_strv_free_ char **zones = NULL; int r; @@ -1270,34 +1270,48 @@ int get_timezones(char ***ret) { assert(ret); f = fopen("/usr/share/zoneinfo/zone1970.tab", "re"); - if (f) { - for (;;) { - _cleanup_free_ char *line = NULL, *cc = NULL, *co = NULL, *tz = NULL; + if (!f) + return -errno; - r = read_line(f, LONG_LINE_MAX, &line); - if (r < 0) - return r; - if (r == 0) - break; + for (;;) { + _cleanup_free_ char *line = NULL, *cc = NULL, *co = NULL, *tz = NULL; - const char *p = line; + r = read_line(f, LONG_LINE_MAX, &line); + if (r < 0) + return r; + if (r == 0) + break; - /* Line format is: - * 'country codes' 'coordinates' 'timezone' 'comments' */ - r = extract_many_words(&p, NULL, 0, &cc, &co, &tz, NULL); - if (r < 0) - continue; + const char *p = line; - /* Lines that start with # are comments. */ - if (*cc == '#') - continue; + /* Line format is: + * 'country codes' 'coordinates' 'timezone' 'comments' */ + r = extract_many_words(&p, NULL, 0, &cc, &co, &tz, NULL); + if (r < 0) + continue; - r = strv_extend(&zones, tz); - if (r < 0) - return r; - } - } else if (errno != ENOENT) - return -errno; + /* Lines that start with # are comments. */ + if (*cc == '#') + continue; + + r = strv_extend(&zones, tz); + if (r < 0) + return r; + } + + *ret = TAKE_PTR(zones); + return 0; +} + +int get_timezones(char ***ret) { + _cleanup_strv_free_ char **zones = NULL; + int r; + + assert(ret); + + r = get_timezones_from_zone1970_tab(&zones); + if (r < 0 && r != -ENOENT) + return r; /* Always include UTC */ r = strv_extend(&zones, "UTC");