From 821c8b9aacac0dde42d6d9a957ca7534ae3fedfc Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Mon, 9 Jul 2012 12:13:16 +0000 Subject: [PATCH] * libc/stdio/fileno.c (fileno): Check if f is a valid stream. If not, return -1 and set errno to EBADF per POSIX. --- newlib/ChangeLog | 8 ++++++++ newlib/libc/stdio/fileno.c | 9 ++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/newlib/ChangeLog b/newlib/ChangeLog index 072c832fe..f8521f47b 100644 --- a/newlib/ChangeLog +++ b/newlib/ChangeLog @@ -1,3 +1,11 @@ +2012-07-09 Corinna Vinschen + + * libc/stdio/fileno.c (fileno): Check if f is a valid stream. If not, + return -1 and set errno to EBADF per POSIX. + +2012-07-06 Corinna Vinschen + + 2012-07-06 Corinna Vinschen Allow building of Cygwin using Mingw64 SDK headers: diff --git a/newlib/libc/stdio/fileno.c b/newlib/libc/stdio/fileno.c index 818f1a1ca..be8f3d66a 100644 --- a/newlib/libc/stdio/fileno.c +++ b/newlib/libc/stdio/fileno.c @@ -47,6 +47,7 @@ Supporting OS subroutines required: none. #include <_ansi.h> #include +#include #include "local.h" int @@ -56,7 +57,13 @@ _DEFUN(fileno, (f), int result; CHECK_INIT (_REENT, f); _newlib_flockfile_start (f); - result = __sfileno (f); + if (f->_flags) + result = __sfileno (f); + else + { + result = -1; + _REENT->_errno = EBADF; + } _newlib_flockfile_end (f); return result; }