まつもと ゆきひろです

In message "[ruby-list:23836] IO#reopen, IO#read"
    on 00/07/13, Ueno Katsuhiro <unnie / blue.sky.or.jp> writes:

|これって実装の都合上仕方ないんでしょうか。
|使っている Ruby のバージョンは 1.4.5 です。

--- io.c	2000/06/20 06:08:38	1.5.2.17
+++ io.c	2000/07/14 06:09:21
@@ -384,2 +384,28 @@
 
+static size_t
+io_fread(ptr, len, f)
+    char *ptr;
+    size_t len;
+    FILE *f;
+{
+    size_t n = len;
+    int c;
+
+    while (n--) {
+	if (!READ_DATA_PENDING(f)) {
+	    rb_thread_wait_fd(fileno(f));
+	}
+	TRAP_BEG;
+	c = getc(f);
+	TRAP_END;
+	if (c == EOF) {
+	    *ptr = '\0';
+	    break;
+	}
+	*ptr++ = c;
+    }
+
+    return len - n - 1;
+}
+
 #ifndef S_ISREG
@@ -423,7 +449,5 @@
     str = rb_str_new(0, siz);
+    READ_CHECK(fptr->f);
     for (;;) {
-	READ_CHECK(fptr->f);
-	TRAP_BEG;
-	n = fread(RSTRING(str)->ptr+bytes, 1, siz-bytes, fptr->f);
-	TRAP_END;
+	n = io_fread(RSTRING(str)->ptr+bytes, siz-bytes, fptr->f);
 	if (n == 0 && bytes == 0) {
@@ -444,28 +468,2 @@
 
-static size_t
-io_fread(ptr, len, f)
-    char *ptr;
-    size_t len;
-    FILE *f;
-{
-    size_t n = len;
-    int c;
-
-    while (n--) {
-	if (!READ_DATA_PENDING(f)) {
-	    rb_thread_wait_fd(fileno(f));
-	}
-	TRAP_BEG;
-	c = getc(f);
-	TRAP_END;
-	if (c == EOF) {
-	    *ptr = '\0';
-	    break;
-	}
-	*ptr++ = c;
-    }
-
-    return len - n - 1;
-}
-
 static VALUE
@@ -1729,2 +1727,3 @@
     if (fd < 3) {
+	clearerr(fptr->f);
 	/* need to keep stdio */