Add explicit locale versions for the printf family. Replace asprintf,

snprintf and sprintf with simple wrappers around the corresponding
va_list functions to reduce code duplication.
This commit is contained in:
joerg 2013-04-19 15:22:24 +00:00
parent 3c1c8515c7
commit 2561b63430
23 changed files with 421 additions and 342 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: stdio.h,v 1.83 2013/04/17 09:07:32 joerg Exp $ */ /* $NetBSD: stdio.h,v 1.84 2013/04/19 15:22:24 joerg Exp $ */
/*- /*-
* Copyright (c) 1990, 1993 * Copyright (c) 1990, 1993
@ -527,6 +527,40 @@ int dprintf(int, const char * __restrict, ...)
FILE *fmemopen(void * __restrict, size_t, const char * __restrict); FILE *fmemopen(void * __restrict, size_t, const char * __restrict);
#endif #endif
#if (_POSIX_C_SOURCE - 0) >= 200809L || defined(_NETBSD_SOURCE)
# ifndef __LOCALE_T_DECLARED
typedef struct _locale *locale_t;
# define __LOCALE_T_DECLARED
# endif
int fprintf_l(FILE * __restrict, locale_t, const char * __restrict, ...)
__printflike(3, 4);
int vfprintf_l(FILE * __restrict, locale_t, const char * __restrict,
__va_list) __printflike(3, 0);
int printf_l(locale_t, const char * __restrict, ...)
__printflike(2, 3);
int vprintf_l(locale_t, const char * __restrict, __va_list)
__printflike(2, 0);
int asprintf_l(char ** __restrict, locale_t, const char * __restrict, ...)
__printflike(3, 4);
int vasprintf_l(char ** __restrict, locale_t, const char * __restrict,
__va_list)
__printflike(3, 0);
int vdprintf_l(int, locale_t, const char * __restrict, __va_list)
__printflike(3, 0);
int dprintf_l(int, locale_t, const char * __restrict, ...)
__printflike(3, 4);
int snprintf_l(char * __restrict, size_t, locale_t,
const char * __restrict, ...) __printflike(4, 5);
int vsnprintf_l(char * __restrict, size_t, locale_t,
const char * __restrict, __va_list) __printflike(4, 0);
#ifndef __AUDIT__
int sprintf_l(char * __restrict, locale_t, const char * __restrict, ...)
__printflike(3, 4);
int vsprintf_l(char * __restrict, locale_t, const char * __restrict,
__va_list) __printflike(3, 0);
#endif
#endif
#if _FORTIFY_SOURCE > 0 #if _FORTIFY_SOURCE > 0
#include <ssp/stdio.h> #include <ssp/stdio.h>
#endif #endif

View File

@ -1,4 +1,4 @@
/* $NetBSD: wchar.h,v 1.34 2013/04/18 23:24:26 joerg Exp $ */ /* $NetBSD: wchar.h,v 1.35 2013/04/19 15:22:24 joerg Exp $ */
/*- /*-
* Copyright (c)1999 Citrus Project, * Copyright (c)1999 Citrus Project,
@ -247,6 +247,16 @@ size_t wcrtomb_l(char * __restrict, wchar_t, mbstate_t * __restrict, locale_t);
size_t wcsrtombs_l(char * __restrict, const wchar_t ** __restrict, size_t, size_t wcsrtombs_l(char * __restrict, const wchar_t ** __restrict, size_t,
mbstate_t * __restrict, locale_t); mbstate_t * __restrict, locale_t);
int wctob_l(wint_t, locale_t); int wctob_l(wint_t, locale_t);
int fwprintf_l(FILE * __restrict, locale_t, const wchar_t * __restrict, ...);
int swprintf_l(wchar_t * __restrict, size_t n, locale_t,
const wchar_t * __restrict, ...);
int vfwprintf_l(FILE * __restrict, locale_t,
const wchar_t * __restrict, __va_list);
int vswprintf_l(wchar_t * __restrict, size_t, locale_t,
const wchar_t * __restrict, __va_list);
int vwprintf_l(locale_t, const wchar_t * __restrict, __va_list);
int wprintf_l(locale_t, const wchar_t * __restrict, ...);
#endif /* _NETBSD_SOURCE */ #endif /* _NETBSD_SOURCE */
#endif /* !_WCHAR_H_ */ #endif /* !_WCHAR_H_ */

View File

@ -1,4 +1,4 @@
/* $NetBSD: namespace.h,v 1.161 2013/04/18 23:24:26 joerg Exp $ */ /* $NetBSD: namespace.h,v 1.162 2013/04/19 15:22:24 joerg Exp $ */
/*- /*-
* Copyright (c) 1997-2004 The NetBSD Foundation, Inc. * Copyright (c) 1997-2004 The NetBSD Foundation, Inc.
@ -168,6 +168,7 @@
#define arc4random_uniform _arc4random_uniform #define arc4random_uniform _arc4random_uniform
#define asctime_r _asctime_r #define asctime_r _asctime_r
#define asprintf _asprintf #define asprintf _asprintf
#define asprintf_l _asprintf_l
#define atoll _atoll #define atoll _atoll
#define authnone_create _authnone_create #define authnone_create _authnone_create
#define authunix_create _authunix_create #define authunix_create _authunix_create
@ -240,6 +241,7 @@
#define difftime _difftime #define difftime _difftime
#define dirname _dirname #define dirname _dirname
#define dn_expand _dn_expand #define dn_expand _dn_expand
#define dprintf_l _dprintf_l
#define drand48 _drand48 #define drand48 _drand48
#define endfsent _endfsent #define endfsent _endfsent
#define endgrent _endgrent #define endgrent _endgrent
@ -276,6 +278,7 @@
#define funlockfile _funlockfile #define funlockfile _funlockfile
#define fnmatch _fnmatch #define fnmatch _fnmatch
#define fparseln _fparseln #define fparseln _fparseln
#define fprintf_l _fprintf_l
#define fpgetmask _fpgetmask #define fpgetmask _fpgetmask
#define fpgetprec _fpgetprec #define fpgetprec _fpgetprec
#define fpgetround _fpgetround #define fpgetround _fpgetround
@ -295,6 +298,7 @@
#define fts_open _fts_open #define fts_open _fts_open
#define fts_read _fts_read #define fts_read _fts_read
#define fts_set _fts_set #define fts_set _fts_set
#define fwprintf_l _fwprintf_l
#define gai_strerror _gai_strerror #define gai_strerror _gai_strerror
#define get_myaddress _get_myaddress #define get_myaddress _get_myaddress
#define getaddrinfo _getaddrinfo #define getaddrinfo _getaddrinfo
@ -497,6 +501,7 @@
#define posix2time _posix2time #define posix2time _posix2time
#define posix2time_z _posix2time_z #define posix2time_z _posix2time_z
#define pread _pread #define pread _pread
#define printf_l _printf_l
#define pselect _pselect #define pselect _pselect
#define psignal _psignal #define psignal _psignal
#define pthread_atfork _pthread_atfork #define pthread_atfork _pthread_atfork
@ -593,7 +598,9 @@
#ifndef snprintf #ifndef snprintf
#define snprintf _snprintf #define snprintf _snprintf
#endif #endif
#define snprintf_l _snprintf_l
#define snprintf_ss _snprintf_ss #define snprintf_ss _snprintf_ss
#define sprintf_l _sprintf_l
#define sradixsort _sradixsort #define sradixsort _sradixsort
#define srand48 _srand48 #define srand48 _srand48
#define srandom _srandom #define srandom _srandom
@ -644,6 +651,7 @@
#define svcudp_create _svcudp_create #define svcudp_create _svcudp_create
#define svcudp_enablecache _svcudp_enablecache #define svcudp_enablecache _svcudp_enablecache
#define sysarch _sys_sysarch #define sysarch _sys_sysarch
#define swprintf_l _swprintf_l
#define sysctl _sysctl #define sysctl _sysctl
#define sysctlbyname _sysctlbyname #define sysctlbyname _sysctlbyname
#define sysctlgetmibinfo _sysctlgetmibinfo #define sysctlgetmibinfo _sysctlgetmibinfo
@ -687,12 +695,22 @@
#define uuid_create_nil _uuid_create_nil #define uuid_create_nil _uuid_create_nil
#define uuid_is_nil _uuid_is_nil #define uuid_is_nil _uuid_is_nil
#define valloc _valloc #define valloc _valloc
#define vdprintf _vdprintf #define vasprintf _vasprintf
#define vasprintf_l _vasprintf_l
#define vdprintf _vdprintf #define vdprintf _vdprintf
#ifndef vsnprintf #ifndef vsnprintf
#define vsnprintf _vsnprintf #define vsnprintf _vsnprintf
#endif #endif
#define vdprintf_l _vdprintf_l
#define vdprintf_l _vdprintf_l
#define vfprintf_l _vfprintf_l
#define vfwprintf_l _vfwprintf_l
#define vprintf_l _vprintf_l
#define vsnprintf_l _vsnprintf_l
#define vsnprintf_ss _vsnprintf_ss #define vsnprintf_ss _vsnprintf_ss
#define vsprintf_l _vsprintf_l
#define vswprintf_l _vswprintf_l
#define vwprintf_l _vwprintf_l
#define vsyslog _vsyslog #define vsyslog _vsyslog
#define vsyslog_r _vsyslog_r #define vsyslog_r _vsyslog_r
#define vsyslog_ss _vsyslog_ss #define vsyslog_ss _vsyslog_ss
@ -716,6 +734,7 @@
#define wcstold_l _wcstold_l #define wcstold_l _wcstold_l
#define wcwidth _wcwidth #define wcwidth _wcwidth
#define wcwidth_l _wcwidth_l #define wcwidth_l _wcwidth_l
#define wprintf_l _wprintf_l
#define xdr_accepted_reply _xdr_accepted_reply #define xdr_accepted_reply _xdr_accepted_reply
#define xdr_array _xdr_array #define xdr_array _xdr_array
#define xdr_authunix_parms _xdr_authunix_parms #define xdr_authunix_parms _xdr_authunix_parms

View File

@ -1,12 +1,12 @@
# from: @(#)Makefile.inc 5.7 (Berkeley) 6/27/91 # from: @(#)Makefile.inc 5.7 (Berkeley) 6/27/91
# $NetBSD: Makefile.inc,v 1.41 2012/03/27 15:05:42 christos Exp $ # $NetBSD: Makefile.inc,v 1.42 2013/04/19 15:22:25 joerg Exp $
# stdio sources # stdio sources
.PATH: ${.CURDIR}/stdio .PATH: ${.CURDIR}/stdio
CPPFLAGS+=-DWIDE_DOUBLE CPPFLAGS+=-DWIDE_DOUBLE
SRCS+= asprintf.c clrerr.c dprintf.c fclose.c fdopen.c feof.c ferror.c \ SRCS+= clrerr.c dprintf.c fclose.c fdopen.c feof.c ferror.c \
fflush.c fgetc.c fgetln.c fgetpos.c fgets.c fgetstr.c fgetwc.c \ fflush.c fgetc.c fgetln.c fgetpos.c fgets.c fgetstr.c fgetwc.c \
fgetwln.c fgetws.c fileno.c findfp.c flags.c flockfile.c fopen.c \ fgetwln.c fgetws.c fileno.c findfp.c flags.c flockfile.c fopen.c \
fparseln.c fprintf.c fpurge.c fputc.c fputs.c fputwc.c fputws.c \ fparseln.c fprintf.c fpurge.c fputc.c fputs.c fputwc.c fputws.c \
@ -15,7 +15,7 @@ SRCS+= asprintf.c clrerr.c dprintf.c fclose.c fdopen.c feof.c ferror.c \
getc.c getchar.c getdelim.c getline.c gettemp.c getw.c getwc.c \ getc.c getchar.c getdelim.c getline.c gettemp.c getw.c getwc.c \
getwchar.c makebuf.c mkdtemp.c mkstemp.c perror.c printf.c putc.c \ getwchar.c makebuf.c mkdtemp.c mkstemp.c perror.c printf.c putc.c \
putchar.c puts.c putw.c putwc.c putwchar.c refill.c remove.c rewind.c \ putchar.c puts.c putw.c putwc.c putwchar.c refill.c remove.c rewind.c \
rget.c scanf.c setbuf.c setbuffer.c setvbuf.c snprintf.c snprintf_ss.c \ rget.c scanf.c setbuf.c setbuffer.c setvbuf.c snprintf_ss.c \
sscanf.c stdio.c swprintf.c swscanf.c tmpfile.c ungetc.c ungetwc.c \ sscanf.c stdio.c swprintf.c swscanf.c tmpfile.c ungetc.c ungetwc.c \
vasprintf.c vdprintf.c vfprintf.c vfscanf.c vfwprintf.c vfwscanf.c \ vasprintf.c vdprintf.c vfprintf.c vfscanf.c vfwprintf.c vfwscanf.c \
vprintf.c vscanf.c vsnprintf.c vsnprintf_ss.c vsscanf.c vswprintf.c \ vprintf.c vscanf.c vsnprintf.c vsnprintf_ss.c vsscanf.c vswprintf.c \
@ -23,7 +23,7 @@ SRCS+= asprintf.c clrerr.c dprintf.c fclose.c fdopen.c feof.c ferror.c \
SRCS+= fmemopen.c SRCS+= fmemopen.c
.if !defined(AUDIT) .if !defined(AUDIT)
SRCS+= gets.c sprintf.c vsprintf.c tempnam.c tmpnam.c mktemp.c SRCS+= gets.c vsprintf.c tempnam.c tmpnam.c mktemp.c
.endif .endif
# namespace purity wrappers # namespace purity wrappers

View File

@ -1,85 +0,0 @@
/* $NetBSD: asprintf.c,v 1.19 2012/03/15 18:22:30 christos Exp $ */
/*
* Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: asprintf.c,v 1.19 2012/03/15 18:22:30 christos Exp $");
#endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "reentrant.h"
#include "local.h"
#ifdef __weak_alias
__weak_alias(asprintf, _asprintf)
#endif
int
asprintf(char **str, char const *fmt, ...)
{
int ret;
va_list ap;
FILE f;
struct __sfileext fext;
unsigned char *_base;
_DIAGASSERT(str != NULL);
_FILEEXT_SETUP(&f, &fext);
f._file = -1;
f._flags = __SWR | __SSTR | __SALC;
f._bf._base = f._p = malloc((size_t)128);
if (f._bf._base == NULL)
goto err;
f._bf._size = f._w = 127; /* Leave room for the NUL */
va_start(ap, fmt);
ret = __vfprintf_unlocked(&f, fmt, ap);
va_end(ap);
if (ret < 0)
goto err;
*f._p = '\0';
_base = realloc(f._bf._base, (size_t)ret + 1);
if (_base == NULL)
goto err;
*str = (char *)_base;
return ret;
err:
if (f._bf._base)
free(f._bf._base);
*str = NULL;
errno = ENOMEM;
return -1;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: dprintf.c,v 1.1 2010/09/06 14:52:55 christos Exp $ */ /* $NetBSD: dprintf.c,v 1.2 2013/04/19 15:22:25 joerg Exp $ */
/*- /*-
* Copyright (c) 1990, 1993 * Copyright (c) 1990, 1993
@ -34,7 +34,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint) #if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: dprintf.c,v 1.1 2010/09/06 14:52:55 christos Exp $"); __RCSID("$NetBSD: dprintf.c,v 1.2 2013/04/19 15:22:25 joerg Exp $");
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include "namespace.h" #include "namespace.h"
@ -46,6 +46,8 @@ __RCSID("$NetBSD: dprintf.c,v 1.1 2010/09/06 14:52:55 christos Exp $");
#include "reentrant.h" #include "reentrant.h"
#include "local.h" #include "local.h"
__weak_alias(dprintf_l, _dprintf_l)
int int
dprintf(int fd, const char * __restrict fmt, ...) dprintf(int fd, const char * __restrict fmt, ...)
{ {
@ -57,3 +59,15 @@ dprintf(int fd, const char * __restrict fmt, ...)
va_end(ap); va_end(ap);
return ret; return ret;
} }
int
dprintf_l(int fd, locale_t loc, const char * __restrict fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = vdprintf_l(fd, loc, fmt, ap);
va_end(ap);
return ret;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: fprintf.c,v 1.12 2012/03/15 18:22:30 christos Exp $ */ /* $NetBSD: fprintf.c,v 1.13 2013/04/19 15:22:25 joerg Exp $ */
/*- /*-
* Copyright (c) 1990, 1993 * Copyright (c) 1990, 1993
@ -37,10 +37,11 @@
#if 0 #if 0
static char sccsid[] = "@(#)fprintf.c 8.1 (Berkeley) 6/4/93"; static char sccsid[] = "@(#)fprintf.c 8.1 (Berkeley) 6/4/93";
#else #else
__RCSID("$NetBSD: fprintf.c,v 1.12 2012/03/15 18:22:30 christos Exp $"); __RCSID("$NetBSD: fprintf.c,v 1.13 2013/04/19 15:22:25 joerg Exp $");
#endif #endif
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <stdarg.h> #include <stdarg.h>
@ -60,3 +61,17 @@ fprintf(FILE *fp, const char *fmt, ...)
va_end(ap); va_end(ap);
return ret; return ret;
} }
__weak_alias(fprintf_l, _fprintf_l)
int
fprintf_l(FILE *fp, locale_t loc, const char *fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = vfprintf_l(fp, loc, fmt, ap);
va_end(ap);
return ret;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: fwprintf.c,v 1.2 2012/03/15 18:22:30 christos Exp $ */ /* $NetBSD: fwprintf.c,v 1.3 2013/04/19 15:22:25 joerg Exp $ */
/*- /*-
* Copyright (c) 2002 Tim J. Robbins * Copyright (c) 2002 Tim J. Robbins
@ -31,14 +31,17 @@
#if 0 #if 0
__FBSDID("$FreeBSD: src/lib/libc/stdio/fwprintf.c,v 1.1 2002/09/21 13:00:30 tjr Exp $"); __FBSDID("$FreeBSD: src/lib/libc/stdio/fwprintf.c,v 1.1 2002/09/21 13:00:30 tjr Exp $");
#else #else
__RCSID("$NetBSD: fwprintf.c,v 1.2 2012/03/15 18:22:30 christos Exp $"); __RCSID("$NetBSD: fwprintf.c,v 1.3 2013/04/19 15:22:25 joerg Exp $");
#endif #endif
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <wchar.h> #include <wchar.h>
__weak_alias(fwprintf_l, _fwprintf_l)
int int
fwprintf(FILE * __restrict fp, const wchar_t * __restrict fmt, ...) fwprintf(FILE * __restrict fp, const wchar_t * __restrict fmt, ...)
{ {
@ -51,3 +54,16 @@ fwprintf(FILE * __restrict fp, const wchar_t * __restrict fmt, ...)
return ret; return ret;
} }
int
fwprintf_l(FILE * __restrict fp, locale_t loc, const wchar_t * __restrict fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = vfwprintf_l(fp, loc, fmt, ap);
va_end(ap);
return ret;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: local.h,v 1.34 2012/03/27 15:05:42 christos Exp $ */ /* $NetBSD: local.h,v 1.35 2013/04/19 15:22:25 joerg Exp $ */
/*- /*-
* Copyright (c) 1990, 1993 * Copyright (c) 1990, 1993
@ -67,8 +67,8 @@ extern int __svfscanf(FILE * __restrict, const char * __restrict,
va_list) __scanflike(2, 0); va_list) __scanflike(2, 0);
extern int __svfscanf_unlocked(FILE * __restrict, const char * __restrict, extern int __svfscanf_unlocked(FILE * __restrict, const char * __restrict,
va_list) __scanflike(2, 0); va_list) __scanflike(2, 0);
extern int __vfprintf_unlocked(FILE * __restrict, const char * __restrict, extern int __vfprintf_unlocked_l(FILE * __restrict, locale_t,
va_list) __printflike(2, 0); const char * __restrict, va_list) __printflike(3, 0);
extern int __sdidinit; extern int __sdidinit;
@ -81,7 +81,7 @@ extern wint_t __fputwc_unlock(wchar_t, FILE *);
extern ssize_t __getdelim(char **__restrict, size_t *__restrict, int, extern ssize_t __getdelim(char **__restrict, size_t *__restrict, int,
FILE *__restrict); FILE *__restrict);
extern char *__fgetstr(FILE * __restrict, size_t * __restrict, int); extern char *__fgetstr(FILE * __restrict, size_t * __restrict, int);
extern int __vfwprintf_unlocked(FILE *, const wchar_t *, va_list); extern int __vfwprintf_unlocked_l(FILE *, locale_t, const wchar_t *, va_list);
extern int __vfwscanf_unlocked(FILE * __restrict, extern int __vfwscanf_unlocked(FILE * __restrict,
const wchar_t * __restrict, va_list); const wchar_t * __restrict, va_list);

View File

@ -1,4 +1,4 @@
/* $NetBSD: printf.c,v 1.12 2012/03/15 18:22:30 christos Exp $ */ /* $NetBSD: printf.c,v 1.13 2013/04/19 15:22:25 joerg Exp $ */
/*- /*-
* Copyright (c) 1990, 1993 * Copyright (c) 1990, 1993
@ -37,15 +37,18 @@
#if 0 #if 0
static char sccsid[] = "@(#)printf.c 8.1 (Berkeley) 6/4/93"; static char sccsid[] = "@(#)printf.c 8.1 (Berkeley) 6/4/93";
#else #else
__RCSID("$NetBSD: printf.c,v 1.12 2012/03/15 18:22:30 christos Exp $"); __RCSID("$NetBSD: printf.c,v 1.13 2013/04/19 15:22:25 joerg Exp $");
#endif #endif
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
__weak_alias(printf_l, _printf_l)
int int
printf(char const *fmt, ...) printf(char const *fmt, ...)
{ {
@ -57,3 +60,15 @@ printf(char const *fmt, ...)
va_end(ap); va_end(ap);
return ret; return ret;
} }
int
printf_l(locale_t loc, char const *fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = vfprintf_l(stdout, loc, fmt, ap);
va_end(ap);
return ret;
}

View File

@ -1,95 +0,0 @@
/* $NetBSD: snprintf.c,v 1.24 2012/03/15 18:22:30 christos Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Chris Torek.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
static char sccsid[] = "@(#)snprintf.c 8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: snprintf.c,v 1.24 2012/03/15 18:22:30 christos Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include "reentrant.h"
#include "local.h"
#if defined(_FORTIFY_SOURCE) && !defined(__lint__)
#undef snprintf
#define snprintf _snprintf
#endif
#ifdef __weak_alias
__weak_alias(snprintf,_snprintf)
#endif
int
snprintf(char *str, size_t n, char const *fmt, ...)
{
int ret;
va_list ap;
FILE f;
struct __sfileext fext;
unsigned char dummy[1];
_DIAGASSERT(n == 0 || str != NULL);
_DIAGASSERT(fmt != NULL);
if ((int)n < 0) {
errno = EINVAL;
return -1;
}
va_start(ap, fmt);
_FILEEXT_SETUP(&f, &fext);
f._file = -1;
f._flags = __SWR | __SSTR;
if (n == 0) {
f._bf._base = f._p = dummy;
f._bf._size = f._w = 0;
} else {
f._bf._base = f._p = (unsigned char *)str;
_DIAGASSERT(__type_fit(int, n - 1));
f._bf._size = f._w = (int)(n - 1);
}
ret = __vfprintf_unlocked(&f, fmt, ap);
*f._p = 0;
va_end(ap);
return ret;
}

View File

@ -1,78 +0,0 @@
/* $NetBSD: sprintf.c,v 1.16 2012/03/15 18:22:30 christos Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Chris Torek.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
static char sccsid[] = "@(#)sprintf.c 8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: sprintf.c,v 1.16 2012/03/15 18:22:30 christos Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include "reentrant.h"
#include "local.h"
#ifdef _FORTIFY_SOURCE
#undef sprintf
#endif
int
sprintf(char *str, char const *fmt, ...)
{
int ret;
va_list ap;
FILE f;
struct __sfileext fext;
_DIAGASSERT(str != NULL);
_DIAGASSERT(fmt != NULL);
_FILEEXT_SETUP(&f, &fext);
f._file = -1;
f._flags = __SWR | __SSTR;
f._bf._base = f._p = (unsigned char *)str;
f._bf._size = f._w = INT_MAX;
va_start(ap, fmt);
ret = __vfprintf_unlocked(&f, fmt, ap);
va_end(ap);
*f._p = 0;
return ret;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: swprintf.c,v 1.2 2012/03/15 18:22:30 christos Exp $ */ /* $NetBSD: swprintf.c,v 1.3 2013/04/19 15:22:25 joerg Exp $ */
/*- /*-
* Copyright (c) 2002 Tim J. Robbins * Copyright (c) 2002 Tim J. Robbins
@ -31,14 +31,17 @@
#if 0 #if 0
__FBSDID("$FreeBSD: src/lib/libc/stdio/swprintf.c,v 1.1 2002/09/21 13:00:30 tjr Exp $"); __FBSDID("$FreeBSD: src/lib/libc/stdio/swprintf.c,v 1.1 2002/09/21 13:00:30 tjr Exp $");
#else #else
__RCSID("$NetBSD: swprintf.c,v 1.2 2012/03/15 18:22:30 christos Exp $"); __RCSID("$NetBSD: swprintf.c,v 1.3 2013/04/19 15:22:25 joerg Exp $");
#endif #endif
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <wchar.h> #include <wchar.h>
__weak_alias(swprintf_l, _swprintf_l)
int int
swprintf(wchar_t * __restrict s, size_t n, const wchar_t * __restrict fmt, ...) swprintf(wchar_t * __restrict s, size_t n, const wchar_t * __restrict fmt, ...)
{ {
@ -51,3 +54,17 @@ swprintf(wchar_t * __restrict s, size_t n, const wchar_t * __restrict fmt, ...)
return ret; return ret;
} }
int
swprintf_l(wchar_t * __restrict s, size_t n, locale_t loc,
const wchar_t * __restrict fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = vswprintf_l(s, n, loc, fmt, ap);
va_end(ap);
return ret;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: vasprintf.c,v 1.14 2012/03/15 18:22:30 christos Exp $ */ /* $NetBSD: vasprintf.c,v 1.15 2013/04/19 15:22:25 joerg Exp $ */
/* /*
* Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
@ -29,18 +29,28 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint) #if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: vasprintf.c,v 1.14 2012/03/15 18:22:30 christos Exp $"); __RCSID("$NetBSD: vasprintf.c,v 1.15 2013/04/19 15:22:25 joerg Exp $");
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <locale.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "reentrant.h" #include "reentrant.h"
#include "setlocale_local.h"
#include "local.h" #include "local.h"
__weak_alias(asprintf, _asprintf)
__weak_alias(asprintf_l, _asprintf_l)
__weak_alias(vasprintf, _vasprintf)
__weak_alias(vasprintf_l, _vasprintf_l)
int int
vasprintf(char **str, const char *fmt, va_list ap) vasprintf_l(char **str, locale_t loc, const char *fmt, va_list ap)
{ {
int ret; int ret;
FILE f; FILE f;
@ -57,7 +67,7 @@ vasprintf(char **str, const char *fmt, va_list ap)
if (f._bf._base == NULL) if (f._bf._base == NULL)
goto err; goto err;
f._bf._size = f._w = 127; /* Leave room for the NUL */ f._bf._size = f._w = 127; /* Leave room for the NUL */
ret = __vfprintf_unlocked(&f, fmt, ap); ret = __vfprintf_unlocked_l(&f, loc, fmt, ap);
if (ret == -1) if (ret == -1)
goto err; goto err;
*f._p = '\0'; *f._p = '\0';
@ -74,3 +84,33 @@ err:
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
} }
int
vasprintf(char **str, const char *fmt, va_list ap)
{
return vasprintf_l(str, *_current_locale(), fmt, ap);
}
int
asprintf_l(char **str, locale_t loc, char const *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = vasprintf_l(str, loc, fmt, ap);
va_end(ap);
return ret;
}
int
asprintf(char **str, char const *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = vasprintf(str, fmt, ap);
va_end(ap);
return ret;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: vdprintf.c,v 1.2 2011/07/17 20:54:34 joerg Exp $ */ /* $NetBSD: vdprintf.c,v 1.3 2013/04/19 15:22:25 joerg Exp $ */
/*- /*-
* Copyright (c) 1990, 1993 * Copyright (c) 1990, 1993
@ -34,7 +34,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint) #if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: vdprintf.c,v 1.2 2011/07/17 20:54:34 joerg Exp $"); __RCSID("$NetBSD: vdprintf.c,v 1.3 2013/04/19 15:22:25 joerg Exp $");
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include "namespace.h" #include "namespace.h"
@ -43,19 +43,22 @@ __RCSID("$NetBSD: vdprintf.c,v 1.2 2011/07/17 20:54:34 joerg Exp $");
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <locale.h>
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <limits.h> #include <limits.h>
#include "reentrant.h" #include "reentrant.h"
#include "setlocale_local.h"
#include "local.h" #include "local.h"
#ifdef __weak_alias #ifdef __weak_alias
__weak_alias(vdprintf,_vdprintf) __weak_alias(vdprintf,_vdprintf)
__weak_alias(vdprintf_l,_vdprintf_l)
#endif #endif
int int
vdprintf(int fd, const char * __restrict fmt, va_list ap) vdprintf_l(int fd, locale_t loc, const char * __restrict fmt, va_list ap)
{ {
FILE f; FILE f;
struct __sfileext fext; struct __sfileext fext;
@ -109,8 +112,14 @@ vdprintf(int fd, const char * __restrict fmt, va_list ap)
f._seek = NULL; f._seek = NULL;
f._close = NULL; f._close = NULL;
if ((ret = vfprintf(&f, fmt, ap)) < 0) if ((ret = vfprintf_l(&f, loc, fmt, ap)) < 0)
return ret; return ret;
return fflush(&f) ? EOF : ret; return fflush(&f) ? EOF : ret;
} }
int
vdprintf(int fd, const char * __restrict fmt, va_list ap)
{
return vdprintf_l(fd, *_current_locale(), fmt, ap);
}

View File

@ -1,2 +1,5 @@
#define NARROW #define NARROW
#include "vfwprintf.c" #include "vfwprintf.c"
__weak_alias(vfprintf_l, _vfprintf_l)

View File

@ -1,4 +1,4 @@
/* $NetBSD: vfwprintf.c,v 1.30 2012/03/27 15:05:42 christos Exp $ */ /* $NetBSD: vfwprintf.c,v 1.31 2013/04/19 15:22:25 joerg Exp $ */
/*- /*-
* Copyright (c) 1990, 1993 * Copyright (c) 1990, 1993
@ -38,7 +38,7 @@
static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93"; static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93";
__FBSDID("$FreeBSD: src/lib/libc/stdio/vfwprintf.c,v 1.27 2007/01/09 00:28:08 imp Exp $"); __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwprintf.c,v 1.27 2007/01/09 00:28:08 imp Exp $");
#else #else
__RCSID("$NetBSD: vfwprintf.c,v 1.30 2012/03/27 15:05:42 christos Exp $"); __RCSID("$NetBSD: vfwprintf.c,v 1.31 2013/04/19 15:22:25 joerg Exp $");
#endif #endif
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
@ -64,6 +64,7 @@ __RCSID("$NetBSD: vfwprintf.c,v 1.30 2012/03/27 15:05:42 christos Exp $");
#include <wctype.h> #include <wctype.h>
#include "reentrant.h" #include "reentrant.h"
#include "setlocale_local.h"
#include "local.h" #include "local.h"
#include "extern.h" #include "extern.h"
#include "fvwrite.h" #include "fvwrite.h"
@ -73,7 +74,7 @@ __RCSID("$NetBSD: vfwprintf.c,v 1.30 2012/03/27 15:05:42 christos Exp $");
#define CHAR_T wchar_t #define CHAR_T wchar_t
#define STRLEN(a) wcslen(a) #define STRLEN(a) wcslen(a)
#define MEMCHR(a, b, c) wmemchr(a, b, c) #define MEMCHR(a, b, c) wmemchr(a, b, c)
#define SCONV(a, b) __mbsconv(a, b) #define SCONV(a, b, loc) __mbsconv(a, b, loc)
#define STRCONST(a) L ## a #define STRCONST(a) L ## a
#define WDECL(a, b) a ## w ## b #define WDECL(a, b) a ## w ## b
#define END_OF_FILE WEOF #define END_OF_FILE WEOF
@ -83,7 +84,7 @@ __RCSID("$NetBSD: vfwprintf.c,v 1.30 2012/03/27 15:05:42 christos Exp $");
#define CHAR_T char #define CHAR_T char
#define STRLEN(a) strlen(a) #define STRLEN(a) strlen(a)
#define MEMCHR(a, b, c) memchr(a, b, c) #define MEMCHR(a, b, c) memchr(a, b, c)
#define SCONV(a, b) __wcsconv(a, b) #define SCONV(a, b, loc) __wcsconv(a, b, loc)
#define STRCONST(a) a #define STRCONST(a) a
#define WDECL(a, b) a ## b #define WDECL(a, b) a ## b
#define END_OF_FILE EOF #define END_OF_FILE EOF
@ -131,16 +132,20 @@ enum typeid {
T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR
}; };
static int __sbprintf(FILE *, const CHAR_T *, va_list); #ifdef NARROW
__printflike(3, 0)
#endif
static int __sbprintf(FILE *, locale_t, const CHAR_T *, va_list);
static CHAR_T *__ujtoa(uintmax_t, CHAR_T *, int, int, const char *, int, static CHAR_T *__ujtoa(uintmax_t, CHAR_T *, int, int, const char *, int,
char, const char *); char, const char *);
static CHAR_T *__ultoa(u_long, CHAR_T *, int, int, const char *, int, static CHAR_T *__ultoa(u_long, CHAR_T *, int, int, const char *, int,
char, const char *); char, const char *);
#ifndef NARROW #ifndef NARROW
static CHAR_T *__mbsconv(char *, int); static CHAR_T *__mbsconv(char *, int, locale_t);
static wint_t __xfputwc(CHAR_T, FILE *); static wint_t __xfputwc(CHAR_T, FILE *, locale_t);
#else #else
static char *__wcsconv(wchar_t *, int); static char *__wcsconv(wchar_t *, int, locale_t);
static int __sprint(FILE *, struct __suio *); static int __sprint(FILE *, struct __suio *);
#endif #endif
static int __find_arguments(const CHAR_T *, va_list, union arg **); static int __find_arguments(const CHAR_T *, va_list, union arg **);
@ -152,7 +157,7 @@ static int __grow_type_table(size_t, enum typeid **, size_t *);
* worries about ungetc buffers and so forth. * worries about ungetc buffers and so forth.
*/ */
static int static int
__sbprintf(FILE *fp, const CHAR_T *fmt, va_list ap) __sbprintf(FILE *fp, locale_t loc, const CHAR_T *fmt, va_list ap)
{ {
int ret; int ret;
FILE fake; FILE fake;
@ -178,7 +183,7 @@ __sbprintf(FILE *fp, const CHAR_T *fmt, va_list ap)
fake._lbfsize = 0; /* not actually used, but Just In Case */ fake._lbfsize = 0; /* not actually used, but Just In Case */
/* do the work, then copy any error status */ /* do the work, then copy any error status */
ret = WDECL(__vf,printf_unlocked)(&fake, fmt, ap); ret = WDECL(__vf,printf_unlocked_l)(&fake, loc, fmt, ap);
if (ret >= 0 && fflush(&fake)) if (ret >= 0 && fflush(&fake))
ret = END_OF_FILE; ret = END_OF_FILE;
if (fake._flags & __SERR) if (fake._flags & __SERR)
@ -192,7 +197,7 @@ __sbprintf(FILE *fp, const CHAR_T *fmt, va_list ap)
* File must already be locked. * File must already be locked.
*/ */
static wint_t static wint_t
__xfputwc(wchar_t wc, FILE *fp) __xfputwc(wchar_t wc, FILE *fp, locale_t loc)
{ {
static const mbstate_t initial; static const mbstate_t initial;
mbstate_t mbs; mbstate_t mbs;
@ -205,7 +210,7 @@ __xfputwc(wchar_t wc, FILE *fp)
return __fputwc_unlock(wc, fp); return __fputwc_unlock(wc, fp);
mbs = initial; mbs = initial;
if ((len = wcrtomb(buf, wc, &mbs)) == (size_t)-1) { if ((len = wcrtomb_l(buf, wc, &mbs, loc)) == (size_t)-1) {
fp->_flags |= __SERR; fp->_flags |= __SERR;
return END_OF_FILE; return END_OF_FILE;
} }
@ -412,7 +417,7 @@ __ujtoa(uintmax_t val, CHAR_T *endp, int base, int octzero,
* that the multibyte char. string ends in a null character. * that the multibyte char. string ends in a null character.
*/ */
static wchar_t * static wchar_t *
__mbsconv(char *mbsarg, int prec) __mbsconv(char *mbsarg, int prec, locale_t loc)
{ {
static const mbstate_t initial; static const mbstate_t initial;
mbstate_t mbs; mbstate_t mbs;
@ -436,7 +441,7 @@ __mbsconv(char *mbsarg, int prec)
insize = nchars = nconv = 0; insize = nchars = nconv = 0;
mbs = initial; mbs = initial;
while (nchars != (size_t)prec) { while (nchars != (size_t)prec) {
nconv = mbrlen(p, MB_CUR_MAX, &mbs); nconv = mbrlen_l(p, MB_CUR_MAX_L(loc), &mbs, loc);
if (nconv == 0 || nconv == (size_t)-1 || if (nconv == 0 || nconv == (size_t)-1 ||
nconv == (size_t)-2) nconv == (size_t)-2)
break; break;
@ -462,7 +467,7 @@ __mbsconv(char *mbsarg, int prec)
mbs = initial; mbs = initial;
nconv = 0; nconv = 0;
while (insize != 0) { while (insize != 0) {
nconv = mbrtowc(wcp, p, insize, &mbs); nconv = mbrtowc_l(wcp, p, insize, &mbs, loc);
if (nconv == 0 || nconv == (size_t)-1 || nconv == (size_t)-2) if (nconv == 0 || nconv == (size_t)-1 || nconv == (size_t)-2)
break; break;
wcp++; wcp++;
@ -485,7 +490,7 @@ __mbsconv(char *mbsarg, int prec)
* string ends is null-terminated. * string ends is null-terminated.
*/ */
static char * static char *
__wcsconv(wchar_t *wcsarg, int prec) __wcsconv(wchar_t *wcsarg, int prec, locale_t loc)
{ {
static const mbstate_t initial; static const mbstate_t initial;
mbstate_t mbs; mbstate_t mbs;
@ -498,7 +503,7 @@ __wcsconv(wchar_t *wcsarg, int prec)
if (prec < 0) { if (prec < 0) {
p = wcsarg; p = wcsarg;
mbs = initial; mbs = initial;
nbytes = wcsrtombs(NULL, (void *)&p, 0, &mbs); nbytes = wcsrtombs_l(NULL, (void *)&p, 0, &mbs, loc);
if (nbytes == (size_t)-1) if (nbytes == (size_t)-1)
return NULL; return NULL;
} else { } else {
@ -514,7 +519,7 @@ __wcsconv(wchar_t *wcsarg, int prec)
p = wcsarg; p = wcsarg;
mbs = initial; mbs = initial;
for (;;) { for (;;) {
clen = wcrtomb(buf, *p++, &mbs); clen = wcrtomb_l(buf, *p++, &mbs, loc);
if (clen == 0 || clen == (size_t)-1 || if (clen == 0 || clen == (size_t)-1 ||
nbytes + clen > (size_t)prec) nbytes + clen > (size_t)prec)
break; break;
@ -528,8 +533,8 @@ __wcsconv(wchar_t *wcsarg, int prec)
/* Fill the output buffer. */ /* Fill the output buffer. */
p = wcsarg; p = wcsarg;
mbs = initial; mbs = initial;
if ((nbytes = wcsrtombs(convbuf, (void *)&p, if ((nbytes = wcsrtombs_l(convbuf, (void *)&p,
nbytes, &mbs)) == (size_t)-1) { nbytes, &mbs, loc)) == (size_t)-1) {
free(convbuf); free(convbuf);
return NULL; return NULL;
} }
@ -547,7 +552,19 @@ WDECL(vf,printf)(FILE * __restrict fp, const CHAR_T * __restrict fmt0, va_list a
int ret; int ret;
FLOCKFILE(fp); FLOCKFILE(fp);
ret = WDECL(__vf,printf_unlocked)(fp, fmt0, ap); ret = WDECL(__vf,printf_unlocked_l)(fp, *_current_locale(), fmt0, ap);
FUNLOCKFILE(fp);
return ret;
}
int
WDECL(vf,printf_l)(FILE * __restrict fp, locale_t loc, const CHAR_T * __restrict fmt0,
va_list ap)
{
int ret;
FLOCKFILE(fp);
ret = WDECL(__vf,printf_unlocked_l)(fp, loc, fmt0, ap);
FUNLOCKFILE(fp); FUNLOCKFILE(fp);
return ret; return ret;
} }
@ -600,7 +617,7 @@ static char *cvt(double, int, int, char *, int *, int, int *);
* Non-MT-safe version * Non-MT-safe version
*/ */
int int
WDECL(__vf,printf_unlocked)(FILE *fp, const CHAR_T *fmt0, va_list ap) WDECL(__vf,printf_unlocked_l)(FILE *fp, locale_t loc, const CHAR_T *fmt0, va_list ap)
{ {
CHAR_T *fmt; /* format string */ CHAR_T *fmt; /* format string */
int ch; /* character from fmt */ int ch; /* character from fmt */
@ -688,6 +705,9 @@ WDECL(__vf,printf_unlocked)(FILE *fp, const CHAR_T *fmt0, va_list ap)
static const char xdigs_lower[16] = "0123456789abcdef"; static const char xdigs_lower[16] = "0123456789abcdef";
static const char xdigs_upper[16] = "0123456789ABCDEF"; static const char xdigs_upper[16] = "0123456789ABCDEF";
if (loc == NULL)
loc = _C_locale;
/* /*
* BEWARE, these `goto error' on error, PRINT uses `n2' and * BEWARE, these `goto error' on error, PRINT uses `n2' and
* PAD uses `n'. * PAD uses `n'.
@ -695,7 +715,7 @@ WDECL(__vf,printf_unlocked)(FILE *fp, const CHAR_T *fmt0, va_list ap)
#ifndef NARROW #ifndef NARROW
#define PRINT(ptr, len) do { \ #define PRINT(ptr, len) do { \
for (n3 = 0; n3 < (len); n3++) \ for (n3 = 0; n3 < (len); n3++) \
__xfputwc((ptr)[n3], fp); \ __xfputwc((ptr)[n3], fp, loc); \
} while (/*CONSTCOND*/0) } while (/*CONSTCOND*/0)
#define FLUSH() #define FLUSH()
#else #else
@ -809,7 +829,7 @@ WDECL(__vf,printf_unlocked)(FILE *fp, const CHAR_T *fmt0, va_list ap)
thousands_sep = '\0'; thousands_sep = '\0';
grouping = NULL; grouping = NULL;
#ifndef NO_FLOATING_POINT #ifndef NO_FLOATING_POINT
decimal_point = localeconv()->decimal_point; decimal_point = localeconv_l(loc)->decimal_point;
expsize = 0; /* XXXGCC -Wuninitialized [sh3,m68000] */ expsize = 0; /* XXXGCC -Wuninitialized [sh3,m68000] */
#endif #endif
convbuf = NULL; convbuf = NULL;
@ -822,7 +842,7 @@ WDECL(__vf,printf_unlocked)(FILE *fp, const CHAR_T *fmt0, va_list ap)
/* optimise fprintf(stderr) (and other unbuffered Unix files) */ /* optimise fprintf(stderr) (and other unbuffered Unix files) */
if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
__sfileno(fp) != -1) __sfileno(fp) != -1)
return __sbprintf(fp, fmt0, ap); return __sbprintf(fp, loc, fmt0, ap);
fmt = (CHAR_T *)__UNCONST(fmt0); fmt = (CHAR_T *)__UNCONST(fmt0);
argtable = NULL; argtable = NULL;
@ -903,8 +923,8 @@ reswitch: switch (ch) {
goto rflag; goto rflag;
case '\'': case '\'':
flags |= GROUPING; flags |= GROUPING;
thousands_sep = *(localeconv()->thousands_sep); thousands_sep = *(localeconv_l(loc)->thousands_sep);
grouping = localeconv()->grouping; grouping = localeconv_l(loc)->grouping;
/* If the locale doesn't define the above, use sane /* If the locale doesn't define the above, use sane
* defaults - otherwise silly things happen! */ * defaults - otherwise silly things happen! */
if (thousands_sep == 0) if (thousands_sep == 0)
@ -992,8 +1012,8 @@ reswitch: switch (ch) {
size_t mbseqlen; size_t mbseqlen;
mbs = initial; mbs = initial;
mbseqlen = wcrtomb(buf, mbseqlen = wcrtomb_l(buf,
(wchar_t)GETARG(wint_t), &mbs); (wchar_t)GETARG(wint_t), &mbs, loc);
if (mbseqlen == (size_t)-1) { if (mbseqlen == (size_t)-1) {
fp->_flags |= __SERR; fp->_flags |= __SERR;
goto error; goto error;
@ -1007,7 +1027,7 @@ reswitch: switch (ch) {
if (flags & LONGINT) if (flags & LONGINT)
*buf = (wchar_t)GETARG(wint_t); *buf = (wchar_t)GETARG(wint_t);
else else
*buf = (wchar_t)btowc(GETARG(int)); *buf = (wchar_t)btowc_l(GETARG(int), loc);
size = 1; size = 1;
#endif #endif
result = buf; result = buf;
@ -1074,7 +1094,7 @@ reswitch: switch (ch) {
if (convbuf != NULL) if (convbuf != NULL)
free(convbuf); free(convbuf);
#ifndef NARROW #ifndef NARROW
result = convbuf = __mbsconv(dtoaresult, -1); result = convbuf = __mbsconv(dtoaresult, -1, loc);
#else #else
/*XXX inefficient*/ /*XXX inefficient*/
result = convbuf = strdup(dtoaresult); result = convbuf = strdup(dtoaresult);
@ -1123,7 +1143,7 @@ fp_begin:
if (convbuf != NULL) if (convbuf != NULL)
free(convbuf); free(convbuf);
#ifndef NARROW #ifndef NARROW
result = convbuf = __mbsconv(dtoaresult, -1); result = convbuf = __mbsconv(dtoaresult, -1, loc);
#else #else
/*XXX inefficient*/ /*XXX inefficient*/
result = convbuf = strdup(dtoaresult); result = convbuf = strdup(dtoaresult);
@ -1195,7 +1215,7 @@ fp_common:
if (convbuf != NULL) if (convbuf != NULL)
free(convbuf); free(convbuf);
#ifndef NARROW #ifndef NARROW
result = convbuf = __mbsconv(dtoaresult, -1); result = convbuf = __mbsconv(dtoaresult, -1, loc);
#else #else
/*XXX inefficient*/ /*XXX inefficient*/
result = convbuf = strdup(dtoaresult); result = convbuf = strdup(dtoaresult);
@ -1323,7 +1343,7 @@ fp_common:
if ((mc = GETARG(MCHAR_T *)) == NULL) if ((mc = GETARG(MCHAR_T *)) == NULL)
result = STRCONST("(null)"); result = STRCONST("(null)");
else { else {
convbuf = SCONV(mc, prec); convbuf = SCONV(mc, prec, loc);
if (convbuf == NULL) { if (convbuf == NULL) {
fp->_flags |= __SERR; fp->_flags |= __SERR;
goto error; goto error;

View File

@ -1,4 +1,4 @@
/* $NetBSD: vprintf.c,v 1.12 2012/03/15 18:22:31 christos Exp $ */ /* $NetBSD: vprintf.c,v 1.13 2013/04/19 15:22:25 joerg Exp $ */
/*- /*-
* Copyright (c) 1990, 1993 * Copyright (c) 1990, 1993
@ -37,15 +37,18 @@
#if 0 #if 0
static char sccsid[] = "@(#)vprintf.c 8.1 (Berkeley) 6/4/93"; static char sccsid[] = "@(#)vprintf.c 8.1 (Berkeley) 6/4/93";
#else #else
__RCSID("$NetBSD: vprintf.c,v 1.12 2012/03/15 18:22:31 christos Exp $"); __RCSID("$NetBSD: vprintf.c,v 1.13 2013/04/19 15:22:25 joerg Exp $");
#endif #endif
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
__weak_alias(vprintf_l, _vprintf_l)
int int
vprintf(const char *fmt, va_list ap) vprintf(const char *fmt, va_list ap)
{ {
@ -54,3 +57,10 @@ vprintf(const char *fmt, va_list ap)
return vfprintf(stdout, fmt, ap); return vfprintf(stdout, fmt, ap);
} }
int
vprintf_l(locale_t loc, const char *fmt, va_list ap)
{
return vfprintf_l(stdout, loc, fmt, ap);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: vsnprintf.c,v 1.25 2012/03/15 18:22:31 christos Exp $ */ /* $NetBSD: vsnprintf.c,v 1.26 2013/04/19 15:22:25 joerg Exp $ */
/*- /*-
* Copyright (c) 1990, 1993 * Copyright (c) 1990, 1993
@ -37,7 +37,7 @@
#if 0 #if 0
static char sccsid[] = "@(#)vsnprintf.c 8.1 (Berkeley) 6/4/93"; static char sccsid[] = "@(#)vsnprintf.c 8.1 (Berkeley) 6/4/93";
#else #else
__RCSID("$NetBSD: vsnprintf.c,v 1.25 2012/03/15 18:22:31 christos Exp $"); __RCSID("$NetBSD: vsnprintf.c,v 1.26 2013/04/19 15:22:25 joerg Exp $");
#endif #endif
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
@ -45,21 +45,28 @@ __RCSID("$NetBSD: vsnprintf.c,v 1.25 2012/03/15 18:22:31 christos Exp $");
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <locale.h>
#include <stdio.h> #include <stdio.h>
#include "reentrant.h" #include "reentrant.h"
#include "setlocale_local.h"
#include "local.h" #include "local.h"
#if defined(_FORTIFY_SOURCE) && !defined(__lint__) #if defined(_FORTIFY_SOURCE) && !defined(__lint__)
#undef vsnprintf #undef vsnprintf
#define vsnprintf _vsnprintf #define vsnprintf _vsnprintf
#undef snprintf
#define snprintf _snprintf
#endif #endif
#ifdef __weak_alias #ifdef __weak_alias
__weak_alias(vsnprintf,_vsnprintf) __weak_alias(vsnprintf,_vsnprintf)
__weak_alias(vsnprintf_l,_vsnprintf_l)
__weak_alias(snprintf,_snprintf)
__weak_alias(snprintf_l,_snprintf_l)
#endif #endif
int int
vsnprintf(char *str, size_t n, const char *fmt, va_list ap) vsnprintf_l(char *str, size_t n, locale_t loc, const char *fmt, va_list ap)
{ {
int ret; int ret;
FILE f; FILE f;
@ -85,7 +92,37 @@ vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
_DIAGASSERT(__type_fit(int, n - 1)); _DIAGASSERT(__type_fit(int, n - 1));
f._bf._size = f._w = (int)(n - 1); f._bf._size = f._w = (int)(n - 1);
} }
ret = __vfprintf_unlocked(&f, fmt, ap); ret = __vfprintf_unlocked_l(&f, loc, fmt, ap);
*f._p = 0; *f._p = 0;
return ret; return ret;
} }
int
vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
{
return vsnprintf_l(str, n, *_current_locale(), fmt, ap);
}
int
snprintf(char *str, size_t n, const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = vsnprintf(str, n, fmt, ap);
va_end(ap);
return ret;
}
int
snprintf_l(char *str, size_t n, locale_t loc, const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = vsnprintf_l(str, n, loc, fmt, ap);
va_end(ap);
return ret;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: vsprintf.c,v 1.17 2012/03/15 18:22:31 christos Exp $ */ /* $NetBSD: vsprintf.c,v 1.18 2013/04/19 15:22:25 joerg Exp $ */
/*- /*-
* Copyright (c) 1990, 1993 * Copyright (c) 1990, 1993
@ -37,23 +37,31 @@
#if 0 #if 0
static char sccsid[] = "@(#)vsprintf.c 8.1 (Berkeley) 6/4/93"; static char sccsid[] = "@(#)vsprintf.c 8.1 (Berkeley) 6/4/93";
#else #else
__RCSID("$NetBSD: vsprintf.c,v 1.17 2012/03/15 18:22:31 christos Exp $"); __RCSID("$NetBSD: vsprintf.c,v 1.18 2013/04/19 15:22:25 joerg Exp $");
#endif #endif
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <limits.h> #include <limits.h>
#include <locale.h>
#include <stdio.h> #include <stdio.h>
#include "reentrant.h" #include "reentrant.h"
#include "setlocale_local.h"
#include "local.h" #include "local.h"
#ifdef _FORTIFY_SOURCE #ifdef _FORTIFY_SOURCE
#undef vsprintf #undef vsprintf
#undef sprintf
#endif #endif
__weak_alias(sprintf_l, _sprintf_l)
__weak_alias(vsprintf_l, _vsprintf_l)
int int
vsprintf(char *str, const char *fmt, va_list ap) vsprintf_l(char *str, locale_t loc, const char *fmt, va_list ap)
{ {
int ret; int ret;
FILE f; FILE f;
@ -67,7 +75,39 @@ vsprintf(char *str, const char *fmt, va_list ap)
f._flags = __SWR | __SSTR; f._flags = __SWR | __SSTR;
f._bf._base = f._p = (unsigned char *)str; f._bf._base = f._p = (unsigned char *)str;
f._bf._size = f._w = INT_MAX; f._bf._size = f._w = INT_MAX;
ret = __vfprintf_unlocked(&f, fmt, ap); ret = __vfprintf_unlocked_l(&f, loc, fmt, ap);
*f._p = 0; *f._p = 0;
return ret; return ret;
} }
int
vsprintf(char *str, const char *fmt, va_list ap)
{
return vsprintf_l(str, *_current_locale(), fmt, ap);
}
int
sprintf_l(char *str, locale_t loc, char const *fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = vsprintf_l(str, loc, fmt, ap);
va_end(ap);
return ret;
}
int
sprintf(char *str, char const *fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = vsprintf(str, fmt, ap);
va_end(ap);
return ret;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: vswprintf.c,v 1.3 2012/03/15 18:22:31 christos Exp $ */ /* $NetBSD: vswprintf.c,v 1.4 2013/04/19 15:22:25 joerg Exp $ */
/* /*
* Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
@ -32,21 +32,27 @@
#if 0 #if 0
__FBSDID("$FreeBSD: src/lib/libc/stdio/vswprintf.c,v 1.6 2005/02/21 19:41:44 fjoe Exp $"); __FBSDID("$FreeBSD: src/lib/libc/stdio/vswprintf.c,v 1.6 2005/02/21 19:41:44 fjoe Exp $");
#else #else
__RCSID("$NetBSD: vswprintf.c,v 1.3 2012/03/15 18:22:31 christos Exp $"); __RCSID("$NetBSD: vswprintf.c,v 1.4 2013/04/19 15:22:25 joerg Exp $");
#endif #endif
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <errno.h> #include <errno.h>
#include <locale.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <wchar.h> #include <wchar.h>
#include <stdarg.h> #include <stdarg.h>
#include "reentrant.h" #include "reentrant.h"
#include "setlocale_local.h"
#include "local.h" #include "local.h"
__weak_alias(vswprintf_l, _vswprintf_l)
int int
vswprintf(wchar_t * __restrict s, size_t n, const wchar_t * __restrict fmt, vswprintf_l(wchar_t * __restrict s, size_t n, locale_t loc,
va_list ap) const wchar_t * __restrict fmt, va_list ap)
{ {
static const mbstate_t initial; static const mbstate_t initial;
mbstate_t mbs; mbstate_t mbs;
@ -70,7 +76,7 @@ vswprintf(wchar_t * __restrict s, size_t n, const wchar_t * __restrict fmt,
return -1; return -1;
} }
f._bf._size = f._w = 127; /* Leave room for the NUL */ f._bf._size = f._w = 127; /* Leave room for the NUL */
ret = __vfwprintf_unlocked(&f, fmt, ap); ret = __vfwprintf_unlocked_l(&f, loc, fmt, ap);
if (ret < 0) { if (ret < 0) {
sverrno = errno; sverrno = errno;
free(f._bf._base); free(f._bf._base);
@ -84,7 +90,7 @@ vswprintf(wchar_t * __restrict s, size_t n, const wchar_t * __restrict fmt,
* fputwc() did in __vfwprintf(). * fputwc() did in __vfwprintf().
*/ */
mbs = initial; mbs = initial;
nwc = mbsrtowcs(s, (void *)&mbp, n, &mbs); nwc = mbsrtowcs_l(s, (void *)&mbp, n, &mbs, loc);
free(f._bf._base); free(f._bf._base);
if (nwc == (size_t)-1) { if (nwc == (size_t)-1) {
errno = EILSEQ; errno = EILSEQ;
@ -98,3 +104,10 @@ vswprintf(wchar_t * __restrict s, size_t n, const wchar_t * __restrict fmt,
return ret; return ret;
} }
int
vswprintf(wchar_t * __restrict s, size_t n, const wchar_t * __restrict fmt,
va_list ap)
{
return vswprintf_l(s, n, *_current_locale(), fmt, ap);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: vwprintf.c,v 1.2 2012/03/15 18:22:31 christos Exp $ */ /* $NetBSD: vwprintf.c,v 1.3 2013/04/19 15:22:25 joerg Exp $ */
/*- /*-
* Copyright (c) 2002 Tim J. Robbins * Copyright (c) 2002 Tim J. Robbins
@ -31,14 +31,23 @@
#if 0 #if 0
__FBSDID("$FreeBSD: src/lib/libc/stdio/vwprintf.c,v 1.1 2002/09/21 13:00:30 tjr Exp $"); __FBSDID("$FreeBSD: src/lib/libc/stdio/vwprintf.c,v 1.1 2002/09/21 13:00:30 tjr Exp $");
#else #else
__RCSID("$NetBSD: vwprintf.c,v 1.2 2012/03/15 18:22:31 christos Exp $"); __RCSID("$NetBSD: vwprintf.c,v 1.3 2013/04/19 15:22:25 joerg Exp $");
#endif #endif
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <wchar.h> #include <wchar.h>
__weak_alias(vwprintf_l, _vwprintf_l)
int
vwprintf_l(locale_t loc, const wchar_t * __restrict fmt, va_list ap)
{
return vfwprintf_l(stdout, loc, fmt, ap);
}
int int
vwprintf(const wchar_t * __restrict fmt, va_list ap) vwprintf(const wchar_t * __restrict fmt, va_list ap)
{ {

View File

@ -1,4 +1,4 @@
/* $NetBSD: wprintf.c,v 1.2 2012/03/15 18:22:31 christos Exp $ */ /* $NetBSD: wprintf.c,v 1.3 2013/04/19 15:22:25 joerg Exp $ */
/*- /*-
* Copyright (c) 2002 Tim J. Robbins * Copyright (c) 2002 Tim J. Robbins
@ -31,14 +31,17 @@
#if 0 #if 0
__FBSDID("$FreeBSD: src/lib/libc/stdio/wprintf.c,v 1.1 2002/09/21 13:00:30 tjr Exp $"); __FBSDID("$FreeBSD: src/lib/libc/stdio/wprintf.c,v 1.1 2002/09/21 13:00:30 tjr Exp $");
#else #else
__RCSID("$NetBSD: wprintf.c,v 1.2 2012/03/15 18:22:31 christos Exp $"); __RCSID("$NetBSD: wprintf.c,v 1.3 2013/04/19 15:22:25 joerg Exp $");
#endif #endif
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <wchar.h> #include <wchar.h>
__weak_alias(wprintf_l, _wprintf_l)
int int
wprintf(const wchar_t * __restrict fmt, ...) wprintf(const wchar_t * __restrict fmt, ...)
{ {
@ -51,3 +54,16 @@ wprintf(const wchar_t * __restrict fmt, ...)
return ret; return ret;
} }
int
wprintf_l(locale_t loc, const wchar_t * __restrict fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = vfwprintf_l(stdout, loc, fmt, ap);
va_end(ap);
return ret;
}