From b5d839c996d7236ed5788296b4d723e67ec4fdb8 Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Tue, 27 Sep 2005 00:00:42 +0000 Subject: [BZ #713] 2005-09-26 Ulrich Drepper [BZ #713] * libio/iofgets.c: Treat N==1 correctly. * libio/iofgets_u.c: Likewise. * libio/iofgetws.c: Likewise. * libio/iofgetws_u.c: Likewise. * stdio-common/Makefile (tests): Add tst-fgets. * stdio-common/tst-fgets.c: New file. --- libio/iofgets_u.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'libio/iofgets_u.c') diff --git a/libio/iofgets_u.c b/libio/iofgets_u.c index ded2a7c..4fb5b1c 100644 --- a/libio/iofgets_u.c +++ b/libio/iofgets_u.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1993,1995,1996,1997,1998,2002 Free Software Foundation, Inc. +/* Copyright (C) 1993,1995-1998,2002,2005 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -40,6 +40,14 @@ fgets_unlocked (buf, n, fp) CHECK_FILE (fp, NULL); if (n <= 0) return NULL; + if (__builtin_expect (n == 1, 0)) + { + /* Another irregular case: since we have to store a NUL byte and + there is only room for exactly one byte, we don't have to + read anything. */ + buf[0] = '\0'; + return buf; + } /* This is very tricky since a file descriptor may be in the non-blocking mode. The error flag doesn't mean much in this case. We return an error only when there is a new error. */ -- cgit v1.1