maint: update all copyright year number ranges
[gnulib.git] / lib / parse-duration.c
index 91f5291..6012ed8 100644 (file)
@@ -1,5 +1,5 @@
 /* Parse a time duration and return a seconds count
-   Copyright (C) 2008 Free Software Foundation, Inc.
+   Copyright (C) 2008-2013 Free Software Foundation, Inc.
    Written by Bruce Korb <bkorb@gnu.org>, 2008.
 
    This program is free software: you can redistribute it and/or modify
@@ -26,7 +26,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include "xalloc.h"
 
 #ifndef NUL
 #define NUL '\0'
@@ -55,14 +54,14 @@ typedef enum {
 #define TIME_MAX        0x7FFFFFFF
 
 /* Wrapper around strtoul that does not require a cast.  */
-static unsigned long inline
+static unsigned long
 str_const_to_ul (cch_t * str, cch_t ** ppz, int base)
 {
   return strtoul (str, (char **)ppz, base);
 }
 
 /* Wrapper around strtol that does not require a cast.  */
-static long inline
+static long
 str_const_to_l (cch_t * str, cch_t ** ppz, int base)
 {
   return strtol (str, (char **)ppz, base);
@@ -71,7 +70,7 @@ str_const_to_l (cch_t * str, cch_t ** ppz, int base)
 /* Returns BASE + VAL * SCALE, interpreting BASE = BAD_TIME
    with errno set as an error situation, and returning BAD_TIME
    with errno set in an error situation.  */
-static time_t inline
+static time_t
 scale_n_add (time_t base, time_t val, int scale)
 {
   if (base == BAD_TIME)
@@ -381,7 +380,7 @@ parse_time (cch_t * pz)
 }
 
 /* Returns a substring of the given string, with spaces at the beginning and at
-   the end destructively removed.  */
+   the end destructively removed, per SNOBOL.  */
 static char *
 trim (char * pz)
 {
@@ -406,13 +405,20 @@ trim (char * pz)
 static time_t
 parse_period (cch_t * in_pz)
 {
-  char * pz   = xstrdup (in_pz);
-  char * pT   = strchr (pz, 'T');
+  char * pT;
   char * ps;
+  char * pz   = strdup (in_pz);
   void * fptr = pz;
   time_t res  = 0;
 
-  if (pT != NUL)
+  if (pz == NULL)
+    {
+      errno = ENOMEM;
+      return BAD_TIME;
+    }
+
+  pT = strchr (pz, 'T');
+  if (pT != NULL)
     {
       *(pT++) = NUL;
       pz = trim (pz);
@@ -452,7 +458,7 @@ parse_period (cch_t * in_pz)
 }
 
 static time_t
-parse_non_iso8601(cch_t * pz)
+parse_non_iso8601 (cch_t * pz)
 {
   whats_done_t whatd_we_do = NOTHING_IS_DONE;
 
@@ -566,38 +572,24 @@ parse_non_iso8601(cch_t * pz)
 time_t
 parse_duration (char const * pz)
 {
-  time_t res = 0;
-
   while (isspace ((unsigned char)*pz))
     pz++;
 
-  do {
-    if (*pz == 'P')
-      {
-        res = parse_period (pz + 1);
-        if (res == BAD_TIME)
-          break;
-        return res;
-      }
-
-    if (*pz == 'T')
-      {
-        res = parse_time (pz + 1);
-        if (res == BAD_TIME)
-          break;
-        return res;
-      }
-
-    if (! isdigit ((unsigned char)*pz))
-      break;
+  switch (*pz)
+    {
+    case 'P':
+      return parse_period (pz + 1);
 
-    res = parse_non_iso8601 (pz);
-    if (res != BAD_TIME)
-      return res;
+    case 'T':
+      return parse_time (pz + 1);
 
-  } while (0);
+    default:
+      if (isdigit ((unsigned char)*pz))
+        return parse_non_iso8601 (pz);
 
-  return BAD_TIME;
+      errno = EINVAL;
+      return BAD_TIME;
+    }
 }
 
 /*