Blame time/asctime.c

Packit 6c4009
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include "../locale/localeinfo.h"
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <limits.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <time.h>
Packit 6c4009
Packit 6c4009
/* This is defined in locale/C-time.c in the GNU libc.  */
Packit 6c4009
extern const struct __locale_data _nl_C_LC_TIME attribute_hidden;
Packit 6c4009
#define ab_day_name(DAY) (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)+(DAY)].string)
Packit 6c4009
#define ab_month_name(MON) (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)+(MON)].string)
Packit 6c4009
Packit 6c4009
static const char format[] = "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n";
Packit 6c4009
static char result[		 3+1+ 3+1+20+1+20+1+20+1+20+1+20+1 + 1];
Packit 6c4009
Packit 6c4009
Packit 6c4009
static char *
Packit 6c4009
asctime_internal (const struct tm *tp, char *buf, size_t buflen)
Packit 6c4009
{
Packit 6c4009
  if (tp == NULL)
Packit 6c4009
    {
Packit 6c4009
      __set_errno (EINVAL);
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* We limit the size of the year which can be printed.  Using the %d
Packit 6c4009
     format specifier used the addition of 1900 would overflow the
Packit 6c4009
     number and a negative vaue is printed.  For some architectures we
Packit 6c4009
     could in theory use %ld or an evern larger integer format but
Packit 6c4009
     this would mean the output needs more space.  This would not be a
Packit 6c4009
     problem if the 'asctime_r' interface would be defined sanely and
Packit 6c4009
     a buffer size would be passed.  */
Packit 6c4009
  if (__glibc_unlikely (tp->tm_year > INT_MAX - 1900))
Packit 6c4009
    {
Packit 6c4009
    eoverflow:
Packit 6c4009
      __set_errno (EOVERFLOW);
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int n = __snprintf (buf, buflen, format,
Packit 6c4009
		      (tp->tm_wday < 0 || tp->tm_wday >= 7 ?
Packit 6c4009
		       "???" : ab_day_name (tp->tm_wday)),
Packit 6c4009
		      (tp->tm_mon < 0 || tp->tm_mon >= 12 ?
Packit 6c4009
		       "???" : ab_month_name (tp->tm_mon)),
Packit 6c4009
		      tp->tm_mday, tp->tm_hour, tp->tm_min,
Packit 6c4009
		      tp->tm_sec, 1900 + tp->tm_year);
Packit 6c4009
  if (n < 0)
Packit 6c4009
    return NULL;
Packit 6c4009
  if (n >= buflen)
Packit 6c4009
    goto eoverflow;
Packit 6c4009
Packit 6c4009
  return buf;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Like asctime, but write result to the user supplied buffer.  The
Packit 6c4009
   buffer is only guaranteed to be 26 bytes in length.  */
Packit 6c4009
char *
Packit 6c4009
__asctime_r (const struct tm *tp, char *buf)
Packit 6c4009
{
Packit 6c4009
  return asctime_internal (tp, buf, 26);
Packit 6c4009
}
Packit 6c4009
weak_alias (__asctime_r, asctime_r)
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Returns a string of the form "Day Mon dd hh:mm:ss yyyy\n"
Packit 6c4009
   which is the representation of TP in that form.  */
Packit 6c4009
char *
Packit 6c4009
asctime (const struct tm *tp)
Packit 6c4009
{
Packit 6c4009
  return asctime_internal (tp, result, sizeof (result));
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (asctime)