Hurd: Fix port deallocation on mknod error.
[glibc.git] / time / asctime.c
blobfa1b48bf6e7a0de4c772d21ca2d121b6b3a97730
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, see
17 <http://www.gnu.org/licenses/>. */
19 #include "../locale/localeinfo.h"
20 #include <errno.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <time.h>
25 /* This is defined in locale/C-time.c in the GNU libc. */
26 extern const struct __locale_data _nl_C_LC_TIME attribute_hidden;
27 #define ab_day_name(DAY) (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABDAY_1)+(DAY)].string)
28 #define ab_month_name(MON) (_nl_C_LC_TIME.values[_NL_ITEM_INDEX (ABMON_1)+(MON)].string)
30 static const char format[] = "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n";
31 static char result[ 3+1+ 3+1+20+1+20+1+20+1+20+1+20+1 + 1];
34 static char *
35 asctime_internal (const struct tm *tp, char *buf, size_t buflen)
37 if (tp == NULL)
39 __set_errno (EINVAL);
40 return NULL;
43 /* We limit the size of the year which can be printed. Using the %d
44 format specifier used the addition of 1900 would overflow the
45 number and a negative vaue is printed. For some architectures we
46 could in theory use %ld or an evern larger integer format but
47 this would mean the output needs more space. This would not be a
48 problem if the 'asctime_r' interface would be defined sanely and
49 a buffer size would be passed. */
50 if (__builtin_expect (tp->tm_year > INT_MAX - 1900, 0))
52 eoverflow:
53 __set_errno (EOVERFLOW);
54 return NULL;
57 int n = __snprintf (buf, buflen, format,
58 (tp->tm_wday < 0 || tp->tm_wday >= 7 ?
59 "???" : ab_day_name (tp->tm_wday)),
60 (tp->tm_mon < 0 || tp->tm_mon >= 12 ?
61 "???" : ab_month_name (tp->tm_mon)),
62 tp->tm_mday, tp->tm_hour, tp->tm_min,
63 tp->tm_sec, 1900 + tp->tm_year);
64 if (n < 0)
65 return NULL;
66 if (n >= buflen)
67 goto eoverflow;
69 return buf;
73 /* Like asctime, but write result to the user supplied buffer. The
74 buffer is only guaranteed to be 26 bytes in length. */
75 char *
76 __asctime_r (const struct tm *tp, char *buf)
78 return asctime_internal (tp, buf, 26);
80 weak_alias (__asctime_r, asctime_r)
83 /* Returns a string of the form "Day Mon dd hh:mm:ss yyyy\n"
84 which is the representation of TP in that form. */
85 char *
86 asctime (const struct tm *tp)
88 return asctime_internal (tp, result, sizeof (result));
90 libc_hidden_def (asctime)