This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
[RFC/patch] New iteration at fixing 9533
- From: Paolo Carlini <pcarlini at unitus dot it>
- To: "libstdc++ at gcc dot gnu dot org" <libstdc++ at gcc dot gnu dot org>
- Cc: Nathan Myers <ncm at cantrip dot org>, Loren James Rittle <rittle at latour dot rsch dot comm dot mot dot com>
- Date: Sat, 22 Mar 2003 20:03:18 +0100
- Subject: [RFC/patch] New iteration at fixing 9533
Hi,
this is the second try.
Tested on x86-linux, testsuite + both 9533 testcases.
Loren, could you possibly test it on your FreeBSD machines?
Comments _welcome_!
Paolo.
//////////
PR libstdc++/9533
* include/bits/fstream.tcc (basic_filebuf<>::open): Don't
call underflow().
(basic_filebuf<>::showmanyc): Use the information provided
by codecvt and __basic_file<>::showmanyc_helper to implement
a non-trivial showmanyc.
* config/io/basic_file_stdio.h
(__basic_file<>::showmanyc_helper): New, declare.
* config/io/basic_file_stdio.cc
(__basic_file<>::showmanyc_helper): Define.
(__basic_file<>::_M_open_mode): Don't set O_NONBLOCK.
(__basic_file<char>::open): Don't call fcntl().
* acinclude.m4 (GLIBCPP_CHECK_S_ISREG_OR_S_IFREG): New macro.
* configure.in: Call here.
* acconfig.h: Add #undefs for the corresponding symbol.
* aclocal.m4: Regenerate.
* configure: Regenerate.
* config.h.in: Regenerate.
diff -urN libstdc++-v3-orig/acconfig.h libstdc++-v3/acconfig.h
--- libstdc++-v3-orig/acconfig.h 2002-11-13 23:49:20.000000000 +0100
+++ libstdc++-v3/acconfig.h 2003-03-22 19:23:05.000000000 +0100
@@ -135,6 +135,9 @@
// Define if the compiler/host combination has __builtin_sqrtl
#undef HAVE___BUILTIN_SQRTL
+// Define if S_ISREG (Posix) or S_IFREG is available in <sys/stat.h>.
+#undef _GLIBCPP_S_ISREG_OR_S_IFREG
+
// Define if LC_MESSAGES is available in <locale.h>.
#undef HAVE_LC_MESSAGES
diff -urN libstdc++-v3-orig/acinclude.m4 libstdc++-v3/acinclude.m4
--- libstdc++-v3-orig/acinclude.m4 2003-03-12 22:34:22.000000000 +0100
+++ libstdc++-v3/acinclude.m4 2003-03-22 19:22:48.000000000 +0100
@@ -2106,6 +2106,30 @@
AC_DEFUN([AC_PROG_LD])
])
+dnl
+dnl Check whether S_ISREG (Posix) or S_IFREG is available in <sys/stat.h>.
+dnl
+
+AC_DEFUN(GLIBCPP_CHECK_S_ISREG_OR_S_IFREG, [
+ AC_CACHE_VAL(glibcpp_cv_S_ISREG, [
+ AC_TRY_LINK([#include <sys/stat.h>],
+ [struct stat buffer; fstat(0, &buffer); S_ISREG(buffer.st_mode); ],
+ [glibcpp_cv_S_ISREG=yes],
+ [glibcpp_cv_S_ISREG=no])
+ ])
+ AC_CACHE_VAL(glibcpp_cv_S_IFREG, [
+ AC_TRY_LINK([#include <sys/stat.h>],
+ [struct stat buffer; fstat(0, &buffer); S_IFREG(buffer.st_mode); ],
+ [glibcpp_cv_S_IFREG=yes],
+ [glibcpp_cv_S_IFREG=no])
+ ])
+ if test x$glibcpp_cv_S_ISREG = xyes; then
+ AC_DEFINE(_GLIBCPP_S_ISREG_OR_S_IFREG, S_ISREG)
+ elif test x$glibcpp_cv_S_IFREG = xyes; then
+ AC_DEFINE(_GLIBCPP_S_ISREG_OR_S_IFREG, S_IFREG)
+ fi
+])
+
# Check whether LC_MESSAGES is available in <locale.h>.
# Ulrich Drepper <drepper at cygnus dot com>, 1995.
diff -urN libstdc++-v3-orig/config/io/basic_file_stdio.cc libstdc++-v3/config/io/basic_file_stdio.cc
--- libstdc++-v3-orig/config/io/basic_file_stdio.cc 2003-03-10 07:55:00.000000000 +0100
+++ libstdc++-v3/config/io/basic_file_stdio.cc 2003-03-22 19:23:40.000000000 +0100
@@ -35,6 +35,18 @@
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
+#include <sys/stat.h>
+#include <poll.h>
+
+#ifdef _GLIBCPP_HAVE_SYS_IOCTL_H
+#define BSD_COMP /* Get FIONREAD on Solaris2. */
+#include <sys/ioctl.h>
+#endif
+
+// Pick up FIONREAD on Solaris 2.5.
+#ifdef _GLIBCPP_HAVE_SYS_FILIO_H
+#include <sys/filio.h>
+#endif
namespace std
{
@@ -76,11 +88,7 @@
if (__testi && !__testo && !__testt && !__testa)
{
strcpy(__c_mode, "r");
-#if defined (O_NONBLOCK)
- __p_mode |= O_RDONLY | O_NONBLOCK;
-#else
__p_mode |= O_RDONLY;
-#endif
}
if (__testi && __testo && !__testt && !__testa)
{
@@ -156,13 +164,6 @@
if ((_M_cfile = fopen(__name, __c_mode)))
{
_M_cfile_created = true;
-
-#if defined (F_SETFL) && defined (O_NONBLOCK)
- // Set input to nonblocking for fifos.
- if (__mode & ios_base::in)
- fcntl(this->fd(), F_SETFL, O_NONBLOCK);
-#endif
-
__ret = this;
}
}
@@ -261,4 +262,36 @@
int
__basic_file<char>::sync()
{ return fflush(_M_cfile); }
+
+ streamsize
+ __basic_file<char>::showmanyc_helper(bool __stdio)
+ {
+#ifdef FIONREAD
+ // Pipes and sockets.
+ int __num = 0;
+ int __r = ioctl(this->fd(), FIONREAD, &__num);
+ if (!__r && __num >= 0)
+ return __num;
+#endif
+
+ // Cheap test.
+ struct pollfd __pfd[1];
+ __pfd[0].fd = this->fd();
+ __pfd[0].events = POLLIN;
+ if (poll(__pfd, 1, 0) <= 0)
+ return 0;
+
+#ifdef _GLIBCPP_S_ISREG_OR_S_IFREG
+ // Regular files.
+ struct stat __buffer;
+ int __ret = fstat(this->fd(), &__buffer);
+ if (!__ret && _GLIBCPP_S_ISREG_OR_S_IFREG(__buffer.st_mode))
+ if (__stdio)
+ return __buffer.st_size - ftell(_M_cfile);
+ else
+ return __buffer.st_size - lseek(this->fd(), 0, ios_base::cur);
+#endif
+ return 0;
+ }
+
} // namespace std
diff -urN libstdc++-v3-orig/config/io/basic_file_stdio.h libstdc++-v3/config/io/basic_file_stdio.h
--- libstdc++-v3-orig/config/io/basic_file_stdio.h 2003-03-09 23:31:44.000000000 +0100
+++ libstdc++-v3/config/io/basic_file_stdio.h 2003-03-22 18:04:12.000000000 +0100
@@ -108,6 +108,9 @@
int
sync();
+
+ streamsize
+ showmanyc_helper(bool __stdio);
};
} // namespace std
diff -urN libstdc++-v3-orig/configure.in libstdc++-v3/configure.in
--- libstdc++-v3-orig/configure.in 2003-03-14 16:12:06.000000000 +0100
+++ libstdc++-v3/configure.in 2003-03-22 18:02:21.000000000 +0100
@@ -411,6 +411,11 @@
GLIBCPP_CHECK_COMPLEX_MATH_SUPPORT
GLIBCPP_CHECK_WCHAR_T_SUPPORT
GLIBCPP_CHECK_STDLIB_SUPPORT
+
+ # For showmanyc_helper().
+ GLIBCPP_CHECK_S_ISREG_OR_S_IFREG
+ AC_CHECK_HEADERS(sys/ioctl.h sys/filio.h)
+
AC_LC_MESSAGES
AC_TRY_COMPILE([
diff -urN libstdc++-v3-orig/include/bits/fstream.tcc libstdc++-v3/include/bits/fstream.tcc
--- libstdc++-v3-orig/include/bits/fstream.tcc 2003-03-17 19:44:44.000000000 +0100
+++ libstdc++-v3/include/bits/fstream.tcc 2003-03-22 18:28:43.000000000 +0100
@@ -96,13 +96,6 @@
// Setup initial position of buffer.
_M_set_indeterminate();
- // Set input buffer to something real.
- // NB: Must open in non-blocking way to do this, or must
- // set the initial position in a different manner than
- // using underflow.
- if (__mode & ios_base::in && _M_buf_allocated)
- this->underflow();
-
if ((__mode & ios_base::ate)
&& this->seekoff(0, ios_base::end, __mode) < 0)
{
@@ -164,9 +157,21 @@
{
streamsize __ret = -1;
bool __testin = this->_M_mode & ios_base::in;
+ const locale __loc = this->getloc();
+ const __codecvt_type& __cvt = use_facet<__codecvt_type>(__loc);
+ // Sync with stdio.
+ bool __sync = this->_M_buf_size == 1;
if (__testin && this->is_open())
- __ret = this->_M_in_end - this->_M_in_cur;
+ {
+ __ret = this->_M_in_end - this->_M_in_cur;
+
+ // For a stateful encoding (-1) the pending sequence might be just
+ // shift and unshift prefixes with no actual character.
+ if (__cvt.encoding() >= 0)
+ __ret += _M_file.showmanyc_helper(__sync) / __cvt.max_length();
+ }
+
_M_last_overflowed = false;
return __ret;
}
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fstream>
#undef NDEBUG
#include <cassert>
int main()
{
using namespace std;
const int count = 10000;
signal(SIGPIPE, SIG_IGN);
unlink("xxx");
if (0 != mkfifo("xxx", S_IRWXU))
{
assert(false);
}
int fval = fork();
if (fval == -1)
{
unlink("xxx");
assert(false);
}
else if (fval == 0)
{
filebuf ofbuf;
ofbuf.open("xxx", ios_base::out);
assert(ofbuf.is_open());
sleep(1);
for (int i = 0; i < count; ++i)
ofbuf.sputc(i % 100);
ofbuf.pubsync();
sleep(1);
ofbuf.close();
exit(0);
}
filebuf ifbuf;
ifbuf.open("xxx", ios_base::in);
assert(ifbuf.is_open());
for (int j = 0; j < count; ++j)
{
int c1 = ifbuf.sbumpc();
printf("%d %d\n", j, c1);
assert(c1 == j % 100);
}
int c6 = ifbuf.sbumpc();
assert(c6 == filebuf::traits_type::eof());
sleep(2);
ifbuf.close();
unlink("xxx");
return 0;
}
#include <fstream>
#include <stdio.h>
#undef NDEBUG
#include <assert.h>
int main()
{
using namespace std;
const char* strlit = "0123456789";
filebuf fbout;
fbout.open("tmp", ios_base::out | ios_base::trunc);
int written = 0;
for (int i = 0; i < BUFSIZ; ++i)
written += fbout.sputn(strlit, 10);
fbout.close();
ifstream in("tmp");
int ia;
int sum = 0;
bool gotsome;
do
{
char buf[100];
int n = in.readsome(buf, sizeof(buf));
ia = in.rdbuf()->in_avail();
gotsome = (n > 0);
sum += n;
}
while (gotsome);
printf("ia = %d\n", ia);
printf("got %d, expected %d, BUFSIZ = %d\n", sum, written, BUFSIZ);
assert(sum == written);
return 0;
}