]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic: use the return value of endswith
authorHristo Venev <hristo@venev.name>
Sun, 25 Oct 2015 16:24:39 +0000 (18:24 +0200)
committerHristo Venev <hristo@venev.name>
Sun, 25 Oct 2015 16:46:20 +0000 (18:46 +0200)
It returns the position where the suffix begins, which can be used for
strndup to extract the prefix without calling strlen.

src/basic/calendarspec.c
src/basic/time-util.c

index 987ca81910fe9eb6e57952f5165298c91b20f281..50328e4187fda15ca64480deb13af7442a5189d6 100644 (file)
@@ -650,6 +650,7 @@ fail:
 int calendar_spec_from_string(const char *p, CalendarSpec **spec) {
         CalendarSpec *c;
         int r;
+        const char *utc;
 
         assert(p);
         assert(spec);
@@ -661,9 +662,11 @@ int calendar_spec_from_string(const char *p, CalendarSpec **spec) {
         if (!c)
                 return -ENOMEM;
 
-        c->utc = endswith_no_case(p, "UTC");
-        if (c->utc)
-                p = strndupa(p, strlen(p) - strlen(" UTC"));
+        utc = endswith_no_case(p, " UTC");
+        if (utc) {
+                c->utc = true;
+                p = strndupa(p, utc - p);
+        }
 
         if (strcaseeq(p, "minutely")) {
                 r = const_chain(0, &c->second);
index d117380d52843a6ba8d557cb1e72372188e74ad3..b348ed4204d8f67bbcfc6b43c80708736af5686c 100644 (file)
@@ -479,7 +479,7 @@ int parse_timestamp(const char *t, usec_t *usec) {
         };
 
         const char *k;
-        bool utc;
+        const char *utc;
         struct tm tm, copy;
         time_t x;
         usec_t x_usec, plus = 0, minus = 0, ret;
@@ -529,8 +529,8 @@ int parse_timestamp(const char *t, usec_t *usec) {
 
                 goto finish;
 
-        } else if (endswith(t, " ago")) {
-                t = strndupa(t, strlen(t) - strlen(" ago"));
+        } else if ((k = endswith(t, " ago"))) {
+                t = strndupa(t, k - t);
 
                 r = parse_sec(t, &minus);
                 if (r < 0)
@@ -538,8 +538,8 @@ int parse_timestamp(const char *t, usec_t *usec) {
 
                 goto finish;
 
-        } else if (endswith(t, " left")) {
-                t = strndupa(t, strlen(t) - strlen(" left"));
+        } else if ((k = endswith(t, " left"))) {
+                t = strndupa(t, k - t);
 
                 r = parse_sec(t, &plus);
                 if (r < 0)
@@ -550,7 +550,7 @@ int parse_timestamp(const char *t, usec_t *usec) {
 
         utc = endswith_no_case(t, " UTC");
         if (utc)
-                t = strndupa(t, strlen(t) - strlen(" UTC"));
+                t = strndupa(t, utc - t);
 
         x = ret / USEC_PER_SEC;
         x_usec = 0;