1*84d9c625SLionel Sambuc /* $NetBSD: wcsftime.c,v 1.5 2013/08/19 20:41:15 joerg Exp $ */
22fe8fb19SBen Gras /*-
32fe8fb19SBen Gras * Copyright (c) 2002 Tim J. Robbins
42fe8fb19SBen Gras * All rights reserved.
52fe8fb19SBen Gras *
62fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
72fe8fb19SBen Gras * modification, are permitted provided that the following conditions
82fe8fb19SBen Gras * are met:
92fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
102fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
112fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
122fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
132fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
142fe8fb19SBen Gras *
152fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
162fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
172fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
182fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
192fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
202fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
212fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
222fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
232fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
242fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
252fe8fb19SBen Gras * SUCH DAMAGE.
262fe8fb19SBen Gras */
272fe8fb19SBen Gras
282fe8fb19SBen Gras #include <sys/cdefs.h>
29*84d9c625SLionel Sambuc __RCSID("$NetBSD: wcsftime.c,v 1.5 2013/08/19 20:41:15 joerg Exp $");
302fe8fb19SBen Gras
31*84d9c625SLionel Sambuc #define __SETLOCALE_SOURCE__
32*84d9c625SLionel Sambuc #include "namespace.h"
332fe8fb19SBen Gras #include <errno.h>
342fe8fb19SBen Gras #include <limits.h>
35*84d9c625SLionel Sambuc #include <locale.h>
362fe8fb19SBen Gras #include <stdlib.h>
372fe8fb19SBen Gras #include <time.h>
382fe8fb19SBen Gras #include <wchar.h>
392fe8fb19SBen Gras
40*84d9c625SLionel Sambuc #include "setlocale_local.h"
41*84d9c625SLionel Sambuc
__weak_alias(wcsftime_l,_wcsftime_l)42*84d9c625SLionel Sambuc __weak_alias(wcsftime_l, _wcsftime_l)
43*84d9c625SLionel Sambuc
442fe8fb19SBen Gras /*
452fe8fb19SBen Gras * Convert date and time to a wide-character string.
462fe8fb19SBen Gras *
472fe8fb19SBen Gras * This is the wide-character counterpart of strftime(). So that we do not
482fe8fb19SBen Gras * have to duplicate the code of strftime(), we convert the format string to
492fe8fb19SBen Gras * multibyte, call strftime(), then convert the result back into wide
502fe8fb19SBen Gras * characters.
512fe8fb19SBen Gras *
522fe8fb19SBen Gras * This technique loses in the presence of stateful multibyte encoding if any
532fe8fb19SBen Gras * of the conversions in the format string change conversion state. When
542fe8fb19SBen Gras * stateful encoding is implemented, we will need to reset the state between
552fe8fb19SBen Gras * format specifications in the format string.
562fe8fb19SBen Gras */
572fe8fb19SBen Gras size_t
582fe8fb19SBen Gras wcsftime(wchar_t *wcs, size_t maxsize,
592fe8fb19SBen Gras const wchar_t *format, const struct tm *timeptr)
602fe8fb19SBen Gras {
61*84d9c625SLionel Sambuc return wcsftime_l(wcs, maxsize, format, timeptr, _current_locale());
62*84d9c625SLionel Sambuc }
63*84d9c625SLionel Sambuc
64*84d9c625SLionel Sambuc size_t
wcsftime_l(wchar_t * wcs,size_t maxsize,const wchar_t * format,const struct tm * timeptr,locale_t loc)65*84d9c625SLionel Sambuc wcsftime_l(wchar_t *wcs, size_t maxsize,
66*84d9c625SLionel Sambuc const wchar_t *format, const struct tm *timeptr, locale_t loc)
67*84d9c625SLionel Sambuc {
682fe8fb19SBen Gras char *dst, *dstp, *sformat;
692fe8fb19SBen Gras size_t n, sflen;
702fe8fb19SBen Gras int sverrno;
712fe8fb19SBen Gras
722fe8fb19SBen Gras sformat = dst = NULL;
732fe8fb19SBen Gras
742fe8fb19SBen Gras /*
752fe8fb19SBen Gras * Convert the supplied format string to a multibyte representation
762fe8fb19SBen Gras * for strftime(), which only handles single-byte characters.
772fe8fb19SBen Gras */
78*84d9c625SLionel Sambuc sflen = wcstombs_l(NULL, format, 0, loc);
792fe8fb19SBen Gras if (sflen == (size_t)-1)
802fe8fb19SBen Gras goto error;
812fe8fb19SBen Gras if ((sformat = malloc(sflen + 1)) == NULL)
822fe8fb19SBen Gras goto error;
83*84d9c625SLionel Sambuc wcstombs_l(sformat, format, sflen + 1, loc);
842fe8fb19SBen Gras
852fe8fb19SBen Gras /*
862fe8fb19SBen Gras * Allocate memory for longest multibyte sequence that will fit
872fe8fb19SBen Gras * into the caller's buffer and call strftime() to fill it.
882fe8fb19SBen Gras * Then, copy and convert the result back into wide characters in
892fe8fb19SBen Gras * the caller's buffer.
902fe8fb19SBen Gras */
91*84d9c625SLionel Sambuc if (SIZE_T_MAX / MB_CUR_MAX_L(loc) <= maxsize) {
922fe8fb19SBen Gras /* maxsize is preposterously large - avoid int. overflow. */
932fe8fb19SBen Gras errno = EINVAL;
942fe8fb19SBen Gras goto error;
952fe8fb19SBen Gras }
96*84d9c625SLionel Sambuc dst = malloc(maxsize * MB_CUR_MAX_L(loc));
972fe8fb19SBen Gras if (dst == NULL)
982fe8fb19SBen Gras goto error;
99*84d9c625SLionel Sambuc if (strftime_l(dst, maxsize, sformat, timeptr, loc) == 0)
1002fe8fb19SBen Gras goto error;
1012fe8fb19SBen Gras dstp = dst;
102*84d9c625SLionel Sambuc n = mbstowcs_l(wcs, dstp, maxsize, loc);
1032fe8fb19SBen Gras if (n == (size_t)-2 || n == (size_t)-1)
1042fe8fb19SBen Gras goto error;
1052fe8fb19SBen Gras
1062fe8fb19SBen Gras free(sformat);
1072fe8fb19SBen Gras free(dst);
1082fe8fb19SBen Gras return n;
1092fe8fb19SBen Gras
1102fe8fb19SBen Gras error:
1112fe8fb19SBen Gras sverrno = errno;
1122fe8fb19SBen Gras free(sformat);
1132fe8fb19SBen Gras free(dst);
1142fe8fb19SBen Gras errno = sverrno;
1152fe8fb19SBen Gras return 0;
1162fe8fb19SBen Gras }
117