Fix wrong copying processing for last bytes in x86-32 wcscpy
[glibc.git] / time / asctime.c
blob8cf266a3ff7c7a7aa4c89e38805396f0baa60f22
1 /* Copyright (C) 1991,1993,1995-1997,2000,2002,2005,2010
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include "../locale/localeinfo.h"
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <time.h>
26 /* This is defined in locale/C-time.c in the GNU libc. */
27 extern const struct __locale_data _nl_C_LC_TIME attribute_hidden;
28 #define ab_day_name(DAY) (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)+(DAY)].string)
29 #define ab_month_name(MON) (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)+(MON)].string)
31 static const char format[] = "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n";
32 static char result[ 3+1+ 3+1+20+1+20+1+20+1+20+1+20+1 + 1];
35 static char *
36 asctime_internal (const struct tm *tp, char *buf, size_t buflen)
38 if (tp == NULL)
40 __set_errno (EINVAL);
41 return NULL;
44 /* We limit the size of the year which can be printed. Using the %d
45 format specifier used the addition of 1900 would overflow the
46 number and a negative vaue is printed. For some architectures we
47 could in theory use %ld or an evern larger integer format but
48 this would mean the output needs more space. This would not be a
49 problem if the 'asctime_r' interface would be defined sanely and
50 a buffer size would be passed. */
51 if (__builtin_expect (tp->tm_year > INT_MAX - 1900, 0))
53 eoverflow:
54 __set_errno (EOVERFLOW);
55 return NULL;
58 int n = __snprintf (buf, buflen, format,
59 (tp->tm_wday < 0 || tp->tm_wday >= 7 ?
60 "???" : ab_day_name (tp->tm_wday)),
61 (tp->tm_mon < 0 || tp->tm_mon >= 12 ?
62 "???" : ab_month_name (tp->tm_mon)),
63 tp->tm_mday, tp->tm_hour, tp->tm_min,
64 tp->tm_sec, 1900 + tp->tm_year);
65 if (n < 0)
66 return NULL;
67 if (n >= buflen)
68 goto eoverflow;
70 return buf;
74 /* Like asctime, but write result to the user supplied buffer. The
75 buffer is only guaranteed to be 26 bytes in length. */
76 char *
77 __asctime_r (const struct tm *tp, char *buf)
79 return asctime_internal (tp, buf, 26);
81 weak_alias (__asctime_r, asctime_r)
84 /* Returns a string of the form "Day Mon dd hh:mm:ss yyyy\n"
85 which is the representation of TP in that form. */
86 char *
87 asctime (const struct tm *tp)
89 return asctime_internal (tp, result, sizeof (result));
91 libc_hidden_def (asctime)