wcsftime.c revision 187312
150276Speter/*- 2166124Srafan * Copyright (c) 2002 Tim J. Robbins 350276Speter * All rights reserved. 450276Speter * 550276Speter * Redistribution and use in source and binary forms, with or without 650276Speter * modification, are permitted provided that the following conditions 750276Speter * are met: 850276Speter * 1. Redistributions of source code must retain the above copyright 950276Speter * notice, this list of conditions and the following disclaimer. 1050276Speter * 2. Redistributions in binary form must reproduce the above copyright 1150276Speter * notice, this list of conditions and the following disclaimer in the 1250276Speter * documentation and/or other materials provided with the distribution. 1350276Speter * 1450276Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1550276Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1650276Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1750276Speter * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1850276Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1950276Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2050276Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2150276Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2250276Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2350276Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2450276Speter * SUCH DAMAGE. 2550276Speter */ 2650276Speter 2750276Speter#include <sys/cdefs.h> 2850276Speter__FBSDID("$FreeBSD: head/lib/libc/locale/wcsftime.c 187312 2009-01-15 20:45:59Z rdivacky $"); 29166124Srafan 3050276Speter#include <errno.h> 31166124Srafan#include <limits.h> 3250276Speter#include <stdlib.h> 3350276Speter#include <time.h> 3450276Speter#include <wchar.h> 3550276Speter 3650276Speter/* 37166124Srafan * Convert date and time to a wide-character string. 38166124Srafan * 3950276Speter * This is the wide-character counterpart of strftime(). So that we do not 4050276Speter * have to duplicate the code of strftime(), we convert the format string to 4150276Speter * multibyte, call strftime(), then convert the result back into wide 4250276Speter * characters. 4350276Speter * 4450276Speter * This technique loses in the presence of stateful multibyte encoding if any 4550276Speter * of the conversions in the format string change conversion state. When 4650276Speter * stateful encoding is implemented, we will need to reset the state between 4750276Speter * format specifications in the format string. 4850276Speter */ 4950276Spetersize_t 5050276Speterwcsftime(wchar_t * __restrict wcs, size_t maxsize, 5150276Speter const wchar_t * __restrict format, const struct tm * __restrict timeptr) 5250276Speter{ 5350276Speter static const mbstate_t initial; 5450276Speter mbstate_t mbs; 5550276Speter char *dst, *sformat; 56166124Srafan const char *dstp; 57166124Srafan const wchar_t *formatp; 5850276Speter size_t n, sflen; 5950276Speter int sverrno; 6050276Speter 6150276Speter sformat = dst = NULL; 6250276Speter 6350276Speter /* 6450276Speter * Convert the supplied format string to a multibyte representation 6550276Speter * for strftime(), which only handles single-byte characters. 66 */ 67 mbs = initial; 68 formatp = format; 69 sflen = wcsrtombs(NULL, &formatp, 0, &mbs); 70 if (sflen == (size_t)-1) 71 goto error; 72 if ((sformat = malloc(sflen + 1)) == NULL) 73 goto error; 74 mbs = initial; 75 wcsrtombs(sformat, &formatp, sflen + 1, &mbs); 76 77 /* 78 * Allocate memory for longest multibyte sequence that will fit 79 * into the caller's buffer and call strftime() to fill it. 80 * Then, copy and convert the result back into wide characters in 81 * the caller's buffer. 82 */ 83 if (SIZE_T_MAX / MB_CUR_MAX <= maxsize) { 84 /* maxsize is prepostorously large - avoid int. overflow. */ 85 errno = EINVAL; 86 goto error; 87 } 88 if ((dst = malloc(maxsize * MB_CUR_MAX)) == NULL) 89 goto error; 90 if (strftime(dst, maxsize, sformat, timeptr) == 0) 91 goto error; 92 dstp = dst; 93 mbs = initial; 94 n = mbsrtowcs(wcs, &dstp, maxsize, &mbs); 95 if (n == (size_t)-2 || n == (size_t)-1 || dstp != NULL) 96 goto error; 97 98 free(sformat); 99 free(dst); 100 return (n); 101 102error: 103 sverrno = errno; 104 free(sformat); 105 free(dst); 106 errno = sverrno; 107 return (0); 108} 109