OSDN Git Service

Fix and clean up strtotimeval
authorWeichuan Yan <wchyan@marvell.com>
Wed, 26 Mar 2014 03:41:15 +0000 (03:41 +0000)
committerCalin Juravle <calin@google.com>
Thu, 27 Mar 2014 14:57:36 +0000 (14:57 +0000)
- parsing of fractional part was wrong (always parsed as 0)
- return value was also wrong in the presence of fractional parts
- general style clean up

Change-Id: I1935a63db938dbed7cacb4b5646e993a52c27f1a
Signed-off-by: Weichuan Yan <wchyan@marvell.com>
libc/bionic/strtotimeval.c

index 1c132ec..195381b 100644 (file)
 #include <stdlib.h>
 #include <sys/time.h>
 
-char * strtotimeval (const char *str, struct timeval *ts)
-{
-    int n;
-    char *s, *s0;
-    long  fs;  /* Fractional seconds */
+char * strtotimeval(const char *str, struct timeval *ts) {
+  char *s;
+  long fs = 0; /* fractional seconds */
 
-    ts->tv_sec = strtoumax(str, &s, 10);
-    fs = 0;
+  ts->tv_sec = strtoumax(str, &s, 10);
 
-    if ( *s == '.' ) {
-       int  count;
+  if (*s == '.') {
+    s++;
+    int count = 0;
 
-        s0 = s+1;
-
-       /* read up to 6 digits */
-       fs    = 0;
-       count = 0;
-       while ( *s && isdigit(*s) )
-       {
-            if ( ++count < 7 )
-               fs = fs*10 + (*s - '0');
-           s++;
-       }
+    /* read up to 6 digits (microseconds) */
+    while (*s && isdigit(*s)) {
+      if (++count < 7) {
+        fs = fs*10 + (*s - '0');
+      }
+      s++;
+    }
 
-       for ( ; count < 6; count++ )
-           fs *= 10;
+    for (; count < 6; count++) {
+      fs *= 10;
     }
+  }
 
-    ts->tv_usec = fs;
-    return s;
+  ts->tv_usec = fs;
+  return s;
 }