From: Zbigniew Jędrzejewski-Szmek Date: Mon, 22 Mar 2021 08:20:47 +0000 (+0100) Subject: shared/calendarspec: constify parameter and simplify assignments to variable X-Git-Tag: v248-2~36^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=462f15d92d35f812d7d77edd486ca63236cffe83;p=thirdparty%2Fsystemd.git shared/calendarspec: constify parameter and simplify assignments to variable The scope of start & stop is narrowed down, and they are assigned only once. No functional change, but I think the code is easier to read this way. Also add a comment to make the code easier to read. --- diff --git a/src/shared/calendarspec.c b/src/shared/calendarspec.c index feb43efdcda..5c666412946 100644 --- a/src/shared/calendarspec.c +++ b/src/shared/calendarspec.c @@ -1101,7 +1101,7 @@ int calendar_spec_from_string(const char *p, CalendarSpec **spec) { return 0; } -static int find_end_of_month(struct tm *tm, bool utc, int day) { +static int find_end_of_month(const struct tm *tm, bool utc, int day) { struct tm t = *tm; t.tm_mon++; @@ -1114,28 +1114,39 @@ static int find_end_of_month(struct tm *tm, bool utc, int day) { return t.tm_mday; } -static int find_matching_component(const CalendarSpec *spec, const CalendarComponent *c, - struct tm *tm, int *val) { - const CalendarComponent *p = c; - int start, stop, d = -1; +static int find_matching_component( + const CalendarSpec *spec, + const CalendarComponent *c, + const struct tm *tm, /* tm is only used for end-of-month calculations */ + int *val) { + + int d = -1, r; bool d_set = false; - int r; assert(val); + /* Finds the *earliest* matching time specified by one of the CalendarCompoment items in chain c. + * If no matches can be found, returns -ENOENT. + * Otherwise, updates *val to the matching time. 1 is returned if *val was changed, 0 otherwise. + */ + if (!c) return 0; + bool end_of_month = spec->end_of_month && c == spec->day; + while (c) { - start = c->start; - stop = c->stop; + int start, stop; - if (spec->end_of_month && p == spec->day) { - start = find_end_of_month(tm, spec->utc, start); - stop = find_end_of_month(tm, spec->utc, stop); + if (end_of_month) { + start = find_end_of_month(tm, spec->utc, c->start); + stop = find_end_of_month(tm, spec->utc, c->stop); if (stop > 0) SWAP_TWO(start, stop); + } else { + start = c->start; + stop = c->stop; } if (start >= *val) {