]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - time/getdate.c
Fix mktime localtime offset confusion
[thirdparty/glibc.git] / time / getdate.c
index 8c9d16f83378777c5929742fae3b95d9cf71dd6c..e568149cdf63883a45b9dc95c7bbf7c50663f8ef 100644 (file)
@@ -1,5 +1,5 @@
 /* Convert a string representation of time to a time value.
-   Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+   Copyright (C) 1997-2018 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
 
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-   02111-1307 USA.  */
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
 
 #include <limits.h>
 #include <stdio.h>
 #include <stdio_ext.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
 #include <sys/stat.h>
+#include <ctype.h>
+#include <alloca.h>
 
 #define TM_YEAR_BASE 1900
 
@@ -44,7 +46,7 @@ static int check_mday (int year, int mon, int mday);
      6  memory allication failed (not enough memory available),
      7  there is no line in the template that matches the input,
      8  invalid input specification Example: February 31 or a time is
-        specified that can not be represented in a time_t (representing
+       specified that can not be represented in a time_t (representing
        the time in seconds since 00:00:00 UTC, January 1, 1970) */
 int getdate_err;
 
@@ -129,13 +131,51 @@ __getdate_r (const char *string, struct tm *tp)
     return 2;
 
   /* Open the template file.  */
-  fp = fopen (datemsk, "r");
+  fp = fopen (datemsk, "rce");
   if (fp == NULL)
     return 2;
 
   /* No threads reading this stream.  */
   __fsetlocking (fp, FSETLOCKING_BYCALLER);
 
+  /* Skip leading whitespace.  */
+  while (isspace (*string))
+    string++;
+
+  size_t inlen, oldlen;
+
+  oldlen = inlen = strlen (string);
+
+  /* Skip trailing whitespace.  */
+  while (inlen > 0 && isspace (string[inlen - 1]))
+    inlen--;
+
+  char *instr = NULL;
+
+  if (inlen < oldlen)
+    {
+      bool using_malloc = false;
+
+      if (__libc_use_alloca (inlen + 1))
+       instr = alloca (inlen + 1);
+      else
+       {
+         instr = malloc (inlen + 1);
+         if (instr == NULL)
+           {
+             fclose (fp);
+             return 6;
+           }
+         using_malloc = true;
+       }
+      memcpy (instr, string, inlen);
+      instr[inlen] = '\0';
+      string = instr;
+
+      if (!using_malloc)
+       instr = NULL;
+    }
+
   line = NULL;
   len = 0;
   do
@@ -158,13 +198,15 @@ __getdate_r (const char *string, struct tm *tp)
       if (result && *result == '\0')
        break;
     }
-  while (!feof_unlocked (fp));
+  while (!__feof_unlocked (fp));
+
+  free (instr);
 
   /* Free the buffer.  */
   free (line);
 
   /* Check for errors. */
-  if (ferror_unlocked (fp))
+  if (__ferror_unlocked (fp))
     {
       fclose (fp);
       return 5;
@@ -212,28 +254,31 @@ __getdate_r (const char *string, struct tm *tp)
       tp->tm_sec = tm.tm_sec;
     }
 
+  /* Fill in the gaps.  */
+  if (tp->tm_hour == INT_MIN)
+    tp->tm_hour = 0;
+  if (tp->tm_min == INT_MIN)
+    tp->tm_min = 0;
+  if (tp->tm_sec == INT_MIN)
+    tp->tm_sec = 0;
+
   /* If no date is given, today is assumed if the given hour is
      greater than the current hour and tomorrow is assumed if
      it is less.  */
   if (tp->tm_hour >= 0 && tp->tm_hour <= 23
-      && tp->tm_year == INT_MIN && tp->tm_mon == INT_MIN
+      && tp->tm_mon == INT_MIN
       && tp->tm_mday == INT_MIN && tp->tm_wday == INT_MIN)
     {
-      tp->tm_year = tm.tm_year;
       tp->tm_mon = tm.tm_mon;
       tp->tm_mday = tm.tm_mday + ((tp->tm_hour - tm.tm_hour) < 0 ? 1 : 0);
       mday_ok = 1;
     }
 
-  /* Fill in the gaps.  */
+  /* More fillers.  */
   if (tp->tm_year == INT_MIN)
     tp->tm_year = tm.tm_year;
-  if (tp->tm_hour == INT_MIN)
-    tp->tm_hour = 0;
-  if (tp->tm_min == INT_MIN)
-    tp->tm_min = 0;
-  if (tp->tm_sec == INT_MIN)
-    tp->tm_sec = 0;
+  if (tp->tm_mon == INT_MIN)
+    tp->tm_mon = tm.tm_mon;
 
   /* Check if the day of month is within range, and if the time can be
      represented in a time_t.  We make use of the fact that the mktime