|
From: KJK::Hyperion <no...@li...> - 2002-05-03 20:38:14
|
At 10.16 03/05/2002, you wrote:
[...]
>BUT _read (in msvcrt\io\read.c) doesn't want to return until it's read the
>size requested (filebuf asks for 4096), even if enter is pressed
[...]
proposed patch for read.c (fixed logical flaw, general cleanup, added some
comments. Some perplexities about the handling of carriage returns in text
files):
Index: io/read.c
===================================================================
RCS file: /CVS/ReactOS/reactos/lib/msvcrt/io/read.c,v
retrieving revision 1.4
diff -u -r1.4 read.c
--- io/read.c 1 Apr 2002 21:50:56 -0000 1.4
+++ io/read.c 3 May 2002 20:03:46 -0000
@@ -16,38 +16,49 @@
size_t _read(int _fd, void *_buf, size_t _nbyte)
{
- DWORD _rbyte = 0, nbyte = _nbyte, count;
- int cr;
+ DWORD _rbyte = 0, nbyte = _nbyte;
char *bufp = (char*)_buf;
+ HANDLE hfile;
+ int istext;
DPRINT("_read(fd %d, buf %x, nbyte %d)\n", _fd, _buf, _nbyte);
- while (nbyte)
+ /* null read */
+ if(_nbyte == 0)
+ return 0;
+
+ hfile = _get_osfhandle(_fd);
+ istext = __fileno_getmode(_fd) & O_TEXT;
+
+ /* read data */
+ if (!ReadFile(hfile, bufp, nbyte, &_rbyte, NULL))
{
- if (!ReadFile(_get_osfhandle(_fd), bufp, nbyte, &_rbyte, NULL))
- {
- return -1;
- }
- if (_rbyte == 0)
- break;
- if (__fileno_getmode(_fd) & O_TEXT)
+ /* failure */
+ return -1;
+ }
+
+ /* text mode */
+ if (_rbyte && istext)
+ {
+ int cr = 0;
+ DWORD count = _rbyte;
+
+ /* repeat for all bytes in the buffer */
+ for(; count; bufp++, count--)
{
- cr = 0;
- count = _rbyte;
- while (count)
- {
- if (*bufp == '\r')
- cr++;
- else if (cr != 0)
- *(bufp - cr) = *bufp;
- bufp++;
- count--;
- }
- _rbyte -= cr;
- bufp -= cr;
+ /* carriage return */
+ if (*bufp == '\r')
+ cr++;
+ /* shift characters back, to ignore carriage returns */
+ else if (cr != 0)
+ *(bufp - cr) = *bufp;
+
}
- nbyte -= _rbyte;
+
+ /* ignore the carriage returns */
+ _rbyte -= cr;
}
- DPRINT("%d\n", _nbyte - nbyte);
- return _nbyte - nbyte;
+
+ DPRINT("%d\n", _rbyte);
+ return _rbyte;
}
|