xref: /openbsd-src/lib/libc/stdio/vswprintf.c (revision bf198cc6eba0ca1f6d79f71e8e2243d386241fa8)
1*bf198cc6Smillert /*	$OpenBSD: vswprintf.c,v 1.7 2019/01/25 00:19:25 millert Exp $	*/
225963022Sstsp /*	$NetBSD: vswprintf.c,v 1.1 2005/05/14 23:51:02 christos Exp $	*/
325963022Sstsp 
425963022Sstsp /*
5*bf198cc6Smillert  * Copyright (c) 1997 Todd C. Miller <millert@openbsd.org>
625963022Sstsp  * All rights reserved.
725963022Sstsp  *
825963022Sstsp  * Redistribution and use in source and binary forms, with or without
925963022Sstsp  * modification, are permitted provided that the following conditions
1025963022Sstsp  * are met:
1125963022Sstsp  * 1. Redistributions of source code must retain the above copyright
1225963022Sstsp  *    notice, this list of conditions and the following disclaimer.
1325963022Sstsp  * 2. Redistributions in binary form must reproduce the above copyright
1425963022Sstsp  *    notice, this list of conditions and the following disclaimer in the
1525963022Sstsp  *    documentation and/or other materials provided with the distribution.
1625963022Sstsp  * 3. The name of the author may not be used to endorse or promote products
1725963022Sstsp  *    derived from this software without specific prior written permission.
1825963022Sstsp  *
1925963022Sstsp  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
2025963022Sstsp  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
2125963022Sstsp  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
2225963022Sstsp  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2325963022Sstsp  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2425963022Sstsp  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2525963022Sstsp  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2625963022Sstsp  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2725963022Sstsp  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2825963022Sstsp  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2925963022Sstsp  */
3025963022Sstsp 
3125963022Sstsp #include <errno.h>
3225963022Sstsp #include <stdio.h>
3325963022Sstsp #include <stdlib.h>
3425963022Sstsp #include <string.h>
3525963022Sstsp #include <wchar.h>
3625963022Sstsp #include <stdarg.h>
3725963022Sstsp #include "local.h"
3825963022Sstsp 
3925963022Sstsp int
vswprintf(wchar_t * __restrict s,size_t n,const wchar_t * __restrict fmt,__va_list ap)4025963022Sstsp vswprintf(wchar_t * __restrict s, size_t n, const wchar_t * __restrict fmt,
4125963022Sstsp     __va_list ap)
4225963022Sstsp {
4325963022Sstsp 	mbstate_t mbs;
4425963022Sstsp 	FILE f;
4525963022Sstsp 	char *mbp;
4625963022Sstsp 	int ret, sverrno;
4725963022Sstsp 	size_t nwc;
4825963022Sstsp 	struct __sfileext fext;
4925963022Sstsp 
5025963022Sstsp 	if (n == 0) {
5125963022Sstsp 		errno = EINVAL;
5225963022Sstsp 		return (-1);
5325963022Sstsp 	}
5425963022Sstsp 
5525963022Sstsp 	_FILEEXT_SETUP(&f, &fext);
5625963022Sstsp 	f._file = -1;
5725963022Sstsp 	f._flags = __SWR | __SSTR | __SALC;
58fa713987Sderaadt 	f._bf._base = f._p = malloc(128);
5925963022Sstsp 	if (f._bf._base == NULL) {
6025963022Sstsp 		errno = ENOMEM;
6125963022Sstsp 		return (-1);
6225963022Sstsp 	}
6325963022Sstsp 	f._bf._size = f._w = 127;		/* Leave room for the NUL */
6425963022Sstsp 	ret = __vfwprintf(&f, fmt, ap);
6525963022Sstsp 	if (ret < 0) {
6625963022Sstsp 		sverrno = errno;
6725963022Sstsp 		free(f._bf._base);
6825963022Sstsp 		errno = sverrno;
6925963022Sstsp 		return (-1);
7025963022Sstsp 	}
7125963022Sstsp 	if (ret == 0) {
7225963022Sstsp 		s[0] = L'\0';
7325963022Sstsp 		free(f._bf._base);
7425963022Sstsp 		return (0);
7525963022Sstsp 	}
7625963022Sstsp 	*f._p = '\0';
7725963022Sstsp 	mbp = (char *)f._bf._base;
7825963022Sstsp 	/*
7925963022Sstsp 	 * XXX Undo the conversion from wide characters to multibyte that
8025963022Sstsp 	 * fputwc() did in __vfwprintf().
8125963022Sstsp 	 */
8225963022Sstsp 	bzero(&mbs, sizeof(mbs));
8325963022Sstsp 	nwc = mbsrtowcs(s, (const char **)&mbp, n, &mbs);
8425963022Sstsp 	free(f._bf._base);
8525963022Sstsp 	if (nwc == (size_t)-1) {
8625963022Sstsp 		errno = EILSEQ;
8725963022Sstsp 		return (-1);
8825963022Sstsp 	}
8925963022Sstsp 	if (nwc == n) {
9025963022Sstsp 		s[n - 1] = L'\0';
9125963022Sstsp 		errno = EOVERFLOW;
9225963022Sstsp 		return (-1);
9325963022Sstsp 	}
9425963022Sstsp 
9525963022Sstsp 	return (ret);
9625963022Sstsp }
979b9d2a55Sguenther DEF_STRONG(vswprintf);
98