/* 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
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;
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
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;
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