xref: /netbsd-src/lib/libc/locale/wcsftime.c (revision 8b770def4169590693ea3deae87d8f4cc1743d95)
1*8b770defSjoerg /*	 $NetBSD: wcsftime.c,v 1.5 2013/08/19 20:41:15 joerg Exp $	*/
2eb808713Schristos /*-
3eb808713Schristos  * Copyright (c) 2002 Tim J. Robbins
4eb808713Schristos  * All rights reserved.
5eb808713Schristos  *
6eb808713Schristos  * Redistribution and use in source and binary forms, with or without
7eb808713Schristos  * modification, are permitted provided that the following conditions
8eb808713Schristos  * are met:
9eb808713Schristos  * 1. Redistributions of source code must retain the above copyright
10eb808713Schristos  *    notice, this list of conditions and the following disclaimer.
11eb808713Schristos  * 2. Redistributions in binary form must reproduce the above copyright
12eb808713Schristos  *    notice, this list of conditions and the following disclaimer in the
13eb808713Schristos  *    documentation and/or other materials provided with the distribution.
14eb808713Schristos  *
15eb808713Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16eb808713Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17eb808713Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18eb808713Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19eb808713Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20eb808713Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21eb808713Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22eb808713Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23eb808713Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24eb808713Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25eb808713Schristos  * SUCH DAMAGE.
26eb808713Schristos  */
27eb808713Schristos 
28eb808713Schristos #include <sys/cdefs.h>
29*8b770defSjoerg __RCSID("$NetBSD: wcsftime.c,v 1.5 2013/08/19 20:41:15 joerg Exp $");
30eb808713Schristos 
319c11b124Sjoerg #define __SETLOCALE_SOURCE__
329c11b124Sjoerg #include "namespace.h"
33eb808713Schristos #include <errno.h>
34eb808713Schristos #include <limits.h>
359c11b124Sjoerg #include <locale.h>
36eb808713Schristos #include <stdlib.h>
37eb808713Schristos #include <time.h>
38eb808713Schristos #include <wchar.h>
39eb808713Schristos 
409c11b124Sjoerg #include "setlocale_local.h"
419c11b124Sjoerg 
__weak_alias(wcsftime_l,_wcsftime_l)429c11b124Sjoerg __weak_alias(wcsftime_l, _wcsftime_l)
439c11b124Sjoerg 
44eb808713Schristos /*
45eb808713Schristos  * Convert date and time to a wide-character string.
46eb808713Schristos  *
47eb808713Schristos  * This is the wide-character counterpart of strftime(). So that we do not
48eb808713Schristos  * have to duplicate the code of strftime(), we convert the format string to
49eb808713Schristos  * multibyte, call strftime(), then convert the result back into wide
50eb808713Schristos  * characters.
51eb808713Schristos  *
52eb808713Schristos  * This technique loses in the presence of stateful multibyte encoding if any
53eb808713Schristos  * of the conversions in the format string change conversion state. When
54eb808713Schristos  * stateful encoding is implemented, we will need to reset the state between
55eb808713Schristos  * format specifications in the format string.
56eb808713Schristos  */
57eb808713Schristos size_t
58eb808713Schristos wcsftime(wchar_t *wcs, size_t maxsize,
59eb808713Schristos     const wchar_t *format, const struct tm *timeptr)
60eb808713Schristos {
619c11b124Sjoerg 	return wcsftime_l(wcs, maxsize, format, timeptr, _current_locale());
629c11b124Sjoerg }
639c11b124Sjoerg 
649c11b124Sjoerg size_t
wcsftime_l(wchar_t * wcs,size_t maxsize,const wchar_t * format,const struct tm * timeptr,locale_t loc)659c11b124Sjoerg wcsftime_l(wchar_t *wcs, size_t maxsize,
669c11b124Sjoerg     const wchar_t *format, const struct tm *timeptr, locale_t loc)
679c11b124Sjoerg {
68eb808713Schristos 	char *dst, *dstp, *sformat;
69eb808713Schristos 	size_t n, sflen;
70eb808713Schristos 	int sverrno;
71eb808713Schristos 
72eb808713Schristos 	sformat = dst = NULL;
73eb808713Schristos 
74eb808713Schristos 	/*
75eb808713Schristos 	 * Convert the supplied format string to a multibyte representation
76eb808713Schristos 	 * for strftime(), which only handles single-byte characters.
77eb808713Schristos 	 */
789c11b124Sjoerg 	sflen = wcstombs_l(NULL, format, 0, loc);
79eb808713Schristos 	if (sflen == (size_t)-1)
80eb808713Schristos 		goto error;
81eb808713Schristos 	if ((sformat = malloc(sflen + 1)) == NULL)
82eb808713Schristos 		goto error;
839c11b124Sjoerg 	wcstombs_l(sformat, format, sflen + 1, loc);
84eb808713Schristos 
85eb808713Schristos 	/*
86eb808713Schristos 	 * Allocate memory for longest multibyte sequence that will fit
87eb808713Schristos 	 * into the caller's buffer and call strftime() to fill it.
88eb808713Schristos 	 * Then, copy and convert the result back into wide characters in
89eb808713Schristos 	 * the caller's buffer.
90eb808713Schristos 	 */
919c11b124Sjoerg 	if (SIZE_T_MAX / MB_CUR_MAX_L(loc) <= maxsize) {
92dddcbe5bStnozaki 		/* maxsize is preposterously large - avoid int. overflow. */
93eb808713Schristos 		errno = EINVAL;
94eb808713Schristos 		goto error;
95eb808713Schristos 	}
96*8b770defSjoerg 	dst = malloc(maxsize * MB_CUR_MAX_L(loc));
97dbae970bStnozaki 	if (dst == NULL)
98eb808713Schristos 		goto error;
999c11b124Sjoerg 	if (strftime_l(dst, maxsize, sformat, timeptr, loc) == 0)
100eb808713Schristos 		goto error;
101eb808713Schristos 	dstp = dst;
1029c11b124Sjoerg 	n = mbstowcs_l(wcs, dstp, maxsize, loc);
103dbae970bStnozaki 	if (n == (size_t)-2 || n == (size_t)-1)
104eb808713Schristos 		goto error;
105eb808713Schristos 
106eb808713Schristos 	free(sformat);
107eb808713Schristos 	free(dst);
108eb808713Schristos 	return n;
109eb808713Schristos 
110eb808713Schristos error:
111eb808713Schristos 	sverrno = errno;
112eb808713Schristos 	free(sformat);
113eb808713Schristos 	free(dst);
114eb808713Schristos 	errno = sverrno;
115eb808713Schristos 	return 0;
116eb808713Schristos }
117