]>
Commit | Line | Data |
---|---|---|
2aa62f2b | 1 | /* $NetBSD: wcsftime.c,v 1.2 2006/10/04 14:19:16 tnozaki Exp $ */\r |
2 | /*-\r | |
3 | * Copyright (c) 2002 Tim J. Robbins\r | |
4 | * All rights reserved.\r | |
5 | *\r | |
6 | * Redistribution and use in source and binary forms, with or without\r | |
7 | * modification, are permitted provided that the following conditions\r | |
8 | * are met:\r | |
9 | * 1. Redistributions of source code must retain the above copyright\r | |
10 | * notice, this list of conditions and the following disclaimer.\r | |
11 | * 2. Redistributions in binary form must reproduce the above copyright\r | |
12 | * notice, this list of conditions and the following disclaimer in the\r | |
13 | * documentation and/or other materials provided with the distribution.\r | |
14 | *\r | |
15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND\r | |
16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r | |
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r | |
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE\r | |
19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r | |
20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r | |
21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r | |
22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r | |
23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r | |
24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r | |
25 | * SUCH DAMAGE.\r | |
26 | */\r | |
27 | #include <LibConfig.h>\r | |
28 | #include <sys/EfiCdefs.h>\r | |
29 | #if defined(LIBC_SCCS) && !defined(lint)\r | |
30 | #if 0\r | |
31 | __FBSDID("$FreeBSD: src/lib/libc/locale/wcsftime.c,v 1.4 2004/04/07 09:47:56 tjr Exp $");\r | |
32 | #else\r | |
33 | __RCSID("$NetBSD: wcsftime.c,v 1.2 2006/10/04 14:19:16 tnozaki Exp $");\r | |
34 | #endif\r | |
35 | #endif /* LIBC_SCCS and not lint */\r | |
36 | \r | |
37 | #include <errno.h>\r | |
38 | #include <limits.h>\r | |
39 | #include <stdlib.h>\r | |
40 | #include <time.h>\r | |
41 | #include <wchar.h>\r | |
42 | #include <machine/int_limits.h>\r | |
43 | \r | |
44 | /*\r | |
45 | * Convert date and time to a wide-character string.\r | |
46 | *\r | |
47 | * This is the wide-character counterpart of strftime(). So that we do not\r | |
48 | * have to duplicate the code of strftime(), we convert the format string to\r | |
49 | * multibyte, call strftime(), then convert the result back into wide\r | |
50 | * characters.\r | |
51 | *\r | |
52 | * This technique loses in the presence of stateful multibyte encoding if any\r | |
53 | * of the conversions in the format string change conversion state. When\r | |
54 | * stateful encoding is implemented, we will need to reset the state between\r | |
55 | * format specifications in the format string.\r | |
56 | */\r | |
57 | size_t\r | |
58 | wcsftime(wchar_t *wcs, size_t maxsize,\r | |
59 | const wchar_t *format, const struct tm *timeptr)\r | |
60 | {\r | |
61 | char *dst, *dstp, *sformat;\r | |
62 | size_t n, sflen;\r | |
63 | int sverrno;\r | |
64 | \r | |
65 | sformat = dst = NULL;\r | |
66 | \r | |
67 | /*\r | |
68 | * Convert the supplied format string to a multibyte representation\r | |
69 | * for strftime(), which only handles single-byte characters.\r | |
70 | */\r | |
71 | sflen = wcstombs(NULL, format, 0);\r | |
72 | if (sflen == (size_t)-1)\r | |
73 | goto error;\r | |
74 | if ((sformat = malloc(sflen + 1)) == NULL)\r | |
75 | goto error;\r | |
76 | wcstombs(sformat, format, sflen + 1);\r | |
77 | \r | |
78 | /*\r | |
79 | * Allocate memory for longest multibyte sequence that will fit\r | |
80 | * into the caller's buffer and call strftime() to fill it.\r | |
81 | * Then, copy and convert the result back into wide characters in\r | |
82 | * the caller's buffer.\r | |
83 | */\r | |
84 | if (SIZE_T_MAX / MB_CUR_MAX <= maxsize) {\r | |
85 | /* maxsize is preposturously large - avoid int. overflow. */\r | |
86 | errno = EINVAL;\r | |
87 | goto error;\r | |
88 | }\r | |
89 | dst = malloc(maxsize * MB_CUR_MAX);\r | |
90 | if (dst == NULL)\r | |
91 | goto error;\r | |
92 | if (strftime(dst, maxsize, sformat, timeptr) == 0)\r | |
93 | goto error;\r | |
94 | dstp = dst;\r | |
95 | n = mbstowcs(wcs, dstp, maxsize);\r | |
96 | if (n == (size_t)-2 || n == (size_t)-1)\r | |
97 | goto error;\r | |
98 | \r | |
99 | free(sformat);\r | |
100 | free(dst);\r | |
101 | return n;\r | |
102 | \r | |
103 | error:\r | |
104 | sverrno = errno;\r | |
105 | free(sformat);\r | |
106 | free(dst);\r | |
107 | errno = sverrno;\r | |
108 | return 0;\r | |
109 | }\r |