Introduce unlocked versions of fputwc() and fgetwc() called __fputwc()

and __fgetwc() which can be used when we know the file is locked.
This commit is contained in:
Tim J. Robbins 2002-09-20 13:20:41 +00:00
parent f90c382c0c
commit 8f030a44b8
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=103676
3 changed files with 45 additions and 19 deletions

View File

@ -38,13 +38,14 @@ __FBSDID("$FreeBSD$");
static __inline wint_t __fgetwc_nbf(FILE *);
/*
* Non-MT-safe version.
*/
wint_t
fgetwc(FILE *fp)
__fgetwc(FILE *fp)
{
wint_t wc;
FLOCKFILE(fp);
ORIENT(fp, 1);
if (MB_CUR_MAX == 1) {
/*
* Assume we're using a single-byte locale. A safer test
@ -53,11 +54,26 @@ fgetwc(FILE *fp)
wc = (wint_t)__sgetc(fp);
} else
wc = __fgetwc_nbf(fp);
FUNLOCKFILE(fp);
return (wc);
}
/*
* MT-safe version.
*/
wint_t
fgetwc(FILE *fp)
{
wint_t r;
FLOCKFILE(fp);
ORIENT(fp, 1);
r = __fgetwc(fp);
FUNLOCKFILE(fp);
return (r);
}
static __inline wint_t
__fgetwc_nbf(FILE *fp)
{
@ -85,10 +101,8 @@ __fgetwc_nbf(FILE *fp)
break;
}
FUNLOCKFILE(fp);
while (n-- != 0)
ungetc((unsigned char)buf[n], fp);
FLOCKFILE(fp);
__ungetc((unsigned char)buf[n], fp);
errno = EILSEQ;
return (WEOF);
}

View File

@ -37,16 +37,16 @@ __FBSDID("$FreeBSD$");
#include "libc_private.h"
#include "local.h"
/*
* Non-MT-safe version.
*/
wint_t
fputwc(wchar_t wc, FILE *fp)
__fputwc(wchar_t wc, FILE *fp)
{
char buf[MB_LEN_MAX];
mbstate_t mbs;
size_t i, len;
FLOCKFILE(fp);
ORIENT(fp, 1);
if (MB_LEN_MAX == 1 && wc > 0 && wc <= UCHAR_MAX) {
/*
* Assume single-byte locale with no special encoding.
@ -57,19 +57,29 @@ fputwc(wchar_t wc, FILE *fp)
len = 1;
} else {
memset(&mbs, 0, sizeof(mbs));
if ((len = wcrtomb(buf, wc, &mbs)) == (size_t)-1) {
FUNLOCKFILE(fp);
if ((len = wcrtomb(buf, wc, &mbs)) == (size_t)-1)
return (WEOF);
}
}
for (i = 0; i < len; i++)
if (__sputc((unsigned char)buf[i], fp) == EOF) {
FUNLOCKFILE(fp);
if (__sputc((unsigned char)buf[i], fp) == EOF)
return (WEOF);
}
FUNLOCKFILE(fp);
return ((wint_t)wc);
}
/*
* MT-safe version.
*/
wint_t
fputwc(wchar_t wc, FILE *fp)
{
wint_t r;
FLOCKFILE(fp);
ORIENT(fp, 1);
r = __fputwc(wc, fp);
FUNLOCKFILE(fp);
return (r);
}

View File

@ -53,6 +53,8 @@ extern fpos_t _sseek(FILE *, fpos_t, int);
extern int _ftello(FILE *, fpos_t *);
extern int _fseeko(FILE *, off_t, int, int);
extern int __fflush(FILE *fp);
extern wint_t __fgetwc(FILE *);
extern wint_t __fputwc(wchar_t, FILE *);
extern int __sflush(FILE *);
extern FILE *__sfp(void);
extern int __srefill(FILE *);