Index: include/stdio.h =================================================================== --- include/stdio.h +++ include/stdio.h @@ -302,7 +302,9 @@ char *ctermid(char *); FILE *fdopen(int, const char *); -int fileno(FILE *); +#ifndef __FILENO_C +static __inline int fileno(FILE *); +#endif #endif /* __POSIX_VISIBLE */ #if __POSIX_VISIBLE >= 199209 @@ -328,7 +330,9 @@ void clearerr_unlocked(FILE *); int feof_unlocked(FILE *); int ferror_unlocked(FILE *); -int fileno_unlocked(FILE *); +#ifndef __FILENO_C +static __inline int fileno_unlocked(FILE *); +#endif #endif #if __POSIX_VISIBLE >= 200112 @@ -482,22 +486,44 @@ extern int __isthreaded; +#define __sfileno(p) ((p)->_file) + +#if __POSIX_VISIBLE +#ifndef __FILENO_C +extern int __fileno_mt(FILE *fp); +static __inline int +fileno(FILE *p) +{ + return (!__isthreaded ? __sfileno(p) : (__fileno_mt)(p)); +} +#endif +#endif + +#if __BSD_VISIBLE +/* + * See ISO/IEC 9945-1 ANSI/IEEE Std 1003.1 Second Edition 1996-07-12 + * B.8.2.7 for the rationale behind the *_unlocked() macros. + */ +#ifndef __FILENO_C +static __inline int +fileno_unlocked(FILE *p) +{ + return __sfileno(p); +} +#endif +#endif + #ifndef __cplusplus #define __sfeof(p) (((p)->_flags & __SEOF) != 0) #define __sferror(p) (((p)->_flags & __SERR) != 0) #define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF))) -#define __sfileno(p) ((p)->_file) #define feof(p) (!__isthreaded ? __sfeof(p) : (feof)(p)) #define ferror(p) (!__isthreaded ? __sferror(p) : (ferror)(p)) #define clearerr(p) (!__isthreaded ? __sclearerr(p) : (clearerr)(p)) -#if __POSIX_VISIBLE -#define fileno(p) (!__isthreaded ? __sfileno(p) : (fileno)(p)) -#endif - #define getc(fp) (!__isthreaded ? __sgetc(fp) : (getc)(fp)) #define putc(x, fp) (!__isthreaded ? __sputc(x, fp) : (putc)(x, fp)) @@ -512,7 +538,6 @@ #define feof_unlocked(p) __sfeof(p) #define ferror_unlocked(p) __sferror(p) #define clearerr_unlocked(p) __sclearerr(p) -#define fileno_unlocked(p) __sfileno(p) #endif #if __POSIX_VISIBLE >= 199506 #define getc_unlocked(fp) __sgetc(fp) Index: lib/libc/stdio/fileno.c =================================================================== --- lib/libc/stdio/fileno.c +++ lib/libc/stdio/fileno.c @@ -37,6 +37,7 @@ __FBSDID("$FreeBSD$"); #include "namespace.h" +#define __FILENO_C #include #include "un-namespace.h" #include "libc_private.h" @@ -56,6 +57,16 @@ return (fd); } +/* + * wrapper for fileno() above, this is to be only used inside stdio.h static __inline fileno() wrapper + * to avoid symbol conflict for multithreaded programs (that causes infinite recursion). + */ +extern __inline int +__fileno_mt(FILE *fp) +{ + return fileno(fp); +} + int fileno_unlocked(FILE *fp) {