io-write-fix.diff

hongli (Hongli Lai), 01/06/2010 03:37 am

Download (1.3 kB)

b/io.c
468 468
{
469 469
    long len, n, r, l, offset = 0;
470 470
    FILE *f = GetWriteFile(fptr);
471
#if BSD_STDIO
472
    off_t cur_offset;
473
#endif
471 474

  
472 475
    len = RSTRING(str)->len;
473 476
    if ((n = len) <= 0) return n;
......
488 491
	r = write(fileno(f), RSTRING(str)->ptr+offset, l);
489 492
        TRAP_END;
490 493
#if BSD_STDIO
491
	fseeko(f, lseek(fileno(f), (off_t)0, SEEK_CUR), SEEK_SET);
494
	if (r > 0) {
495
	    cur_offset = lseek(fileno(f), (off_t)0, SEEK_CUR);
496
	    if (cur_offset != -1) {
497
		if (fseeko(f, cur_offset, SEEK_SET) == -1)
498
		    /* Raise fseeko's error. */
499
		    r = -1;
500
	    }
501
	    else if (errno != ESPIPE) {
502
		/* Raise lseek's error. */
503
		r = -1;
504
	    }
505
	    /* else if (errno == ESPIPE) ignore error */
506
	}
492 507
#endif
493 508
        if (r == n) return len;
494 509
        if (0 <= r) {
b/test/io/test_write.rb
1
require 'test/unit'
2

  
3
class TestIOWrite < Test::Unit::TestCase
4
  def test_write
5
    a, b = IO.pipe
6
    begin
7
      a.close
8
      assert_raises(Errno::EPIPE) do
9
        b.write("hi")
10
      end
11
    ensure
12
      a.close if !a.closed?
13
      b.close if !b.closed?
14
    end
15
  end
16
end