Home | History | Annotate | Line # | Download | only in stdio
vswprintf.c revision 1.3
      1  1.3  christos /*	$NetBSD: vswprintf.c,v 1.3 2012/03/15 18:22:31 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 1997 Todd C. Miller <Todd.Miller (at) courtesan.com>
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that the following conditions
      9  1.1  christos  * are met:
     10  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  christos  *    documentation and/or other materials provided with the distribution.
     15  1.1  christos  * 3. The name of the author may not be used to endorse or promote products
     16  1.1  christos  *    derived from this software without specific prior written permission.
     17  1.1  christos  *
     18  1.1  christos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     19  1.1  christos  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
     20  1.1  christos  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
     21  1.1  christos  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     22  1.1  christos  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     23  1.1  christos  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     24  1.1  christos  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     25  1.1  christos  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26  1.1  christos  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     27  1.1  christos  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1  christos  */
     29  1.1  christos 
     30  1.1  christos #include <sys/cdefs.h>
     31  1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     32  1.1  christos #if 0
     33  1.1  christos __FBSDID("$FreeBSD: src/lib/libc/stdio/vswprintf.c,v 1.6 2005/02/21 19:41:44 fjoe Exp $");
     34  1.1  christos #else
     35  1.3  christos __RCSID("$NetBSD: vswprintf.c,v 1.3 2012/03/15 18:22:31 christos Exp $");
     36  1.1  christos #endif
     37  1.1  christos #endif /* LIBC_SCCS and not lint */
     38  1.1  christos 
     39  1.1  christos #include <errno.h>
     40  1.1  christos #include <stdio.h>
     41  1.1  christos #include <stdlib.h>
     42  1.1  christos #include <wchar.h>
     43  1.1  christos #include <stdarg.h>
     44  1.1  christos #include "reentrant.h"
     45  1.1  christos #include "local.h"
     46  1.1  christos 
     47  1.1  christos int
     48  1.1  christos vswprintf(wchar_t * __restrict s, size_t n, const wchar_t * __restrict fmt,
     49  1.1  christos     va_list ap)
     50  1.1  christos {
     51  1.1  christos 	static const mbstate_t initial;
     52  1.1  christos 	mbstate_t mbs;
     53  1.1  christos 	FILE f;
     54  1.1  christos 	char *mbp;
     55  1.1  christos 	int ret, sverrno;
     56  1.1  christos 	size_t nwc;
     57  1.1  christos 	struct __sfileext fext;
     58  1.1  christos 
     59  1.1  christos 	if (n == 0) {
     60  1.1  christos 		errno = EINVAL;
     61  1.3  christos 		return -1;
     62  1.1  christos 	}
     63  1.1  christos 
     64  1.1  christos 	_FILEEXT_SETUP(&f, &fext);
     65  1.1  christos 	f._file = -1;
     66  1.1  christos 	f._flags = __SWR | __SSTR | __SALC;
     67  1.1  christos 	f._bf._base = f._p = (unsigned char *)malloc(128);
     68  1.1  christos 	if (f._bf._base == NULL) {
     69  1.1  christos 		errno = ENOMEM;
     70  1.3  christos 		return -1;
     71  1.1  christos 	}
     72  1.1  christos 	f._bf._size = f._w = 127;		/* Leave room for the NUL */
     73  1.1  christos 	ret = __vfwprintf_unlocked(&f, fmt, ap);
     74  1.1  christos 	if (ret < 0) {
     75  1.1  christos 		sverrno = errno;
     76  1.1  christos 		free(f._bf._base);
     77  1.1  christos 		errno = sverrno;
     78  1.3  christos 		return -1;
     79  1.1  christos 	}
     80  1.1  christos 	*f._p = '\0';
     81  1.1  christos 	mbp = (char *)f._bf._base;
     82  1.1  christos 	/*
     83  1.1  christos 	 * XXX Undo the conversion from wide characters to multibyte that
     84  1.1  christos 	 * fputwc() did in __vfwprintf().
     85  1.1  christos 	 */
     86  1.1  christos 	mbs = initial;
     87  1.2  christos 	nwc = mbsrtowcs(s, (void *)&mbp, n, &mbs);
     88  1.1  christos 	free(f._bf._base);
     89  1.1  christos 	if (nwc == (size_t)-1) {
     90  1.1  christos 		errno = EILSEQ;
     91  1.3  christos 		return -1;
     92  1.1  christos 	}
     93  1.1  christos 	if (nwc == n) {
     94  1.1  christos 		s[n - 1] = L'\0';
     95  1.1  christos 		errno = EOVERFLOW;
     96  1.3  christos 		return -1;
     97  1.1  christos 	}
     98  1.1  christos 
     99  1.3  christos 	return ret;
    100  1.1  christos }
    101