From: Zbigniew Jędrzejewski-Szmek Date: Sun, 28 Feb 2016 23:56:07 +0000 (-0500) Subject: Merge pull request #2749 from martinpitt/adjtime X-Git-Tag: v230~308 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=8f462d878c6c14c0ec9582615d18dd6a7d72fa28;p=thirdparty%2Fsystemd.git Merge pull request #2749 from martinpitt/adjtime --- 8f462d878c6c14c0ec9582615d18dd6a7d72fa28 diff --cc src/timedate/timedated.c index 2a10135fbab,4e120564c00..9406d28abec --- a/src/timedate/timedated.c +++ b/src/timedate/timedated.c @@@ -125,21 -125,35 +125,35 @@@ static int context_write_data_local_rtc if (!w) return -ENOMEM; } else { - char *p, *e; + char *p; - char *e = (char*) "\n"; /* default if there are not 3 lines with \n terminator */ ++ const char *e = "\n"; /* default if there is less than 3 lines */ + const char *prepend = ""; size_t a, b; - p = strchr(s, '\n'); - if (!p) - return -EIO; - - p = strchr(p+1, '\n'); - if (!p) - return -EIO; - - p++; - e = strchr(p, '\n'); - if (!e) - return -EIO; + p = strchrnul(s, '\n'); + if (*p == '\0') { + /* only one line, no \n terminator */ + prepend = "\n0\n"; + } else if (p[1] == '\0') { + /* only one line, with \n terminator */ + ++p; + prepend = "0\n"; + } else { + p = strchr(p+1, '\n'); + if (!p) { + /* only two lines, no \n terminator */ + prepend = "\n"; + p = s + strlen(s); + } else { + char *end; + /* third line might have a \n terminator or not */ + p++; + end = strchr(p, '\n'); + /* if we actually have a fourth line, use that as suffix "e", otherwise the default \n */ + if (end) + e = end; + } + } a = p - s; b = strlen(e);