made _read() non-greedy - it now returns as soon as any amount of data has been read...
[reactos.git] / reactos / lib / msvcrt / io / read.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: lib/msvcrt/io/read.c
5 * PURPOSE: Reads a file
6 * PROGRAMER: Boudewijn Dekker
7 * UPDATE HISTORY:
8 * 28/12/1998: Created
9 * 03/05/2002: made _read() non-greedy - it now returns as soon as
10 * any amount of data has been read. It's the expected
11 * behavior for line-buffered streams (KJK::Hyperion)
12 */
13 #include <windows.h>
14 #include <msvcrt/io.h>
15 #include <msvcrt/internal/file.h>
16
17 #define NDEBUG
18 #include <msvcrt/msvcrtdbg.h>
19
20 size_t _read(int _fd, void *_buf, size_t _nbyte)
21 {
22 DWORD _rbyte = 0, nbyte = _nbyte;
23 char *bufp = (char*)_buf;
24 HANDLE hfile;
25 int istext;
26
27 DPRINT("_read(fd %d, buf %x, nbyte %d)\n", _fd, _buf, _nbyte);
28
29 /* null read */
30 if(_nbyte == 0)
31 return 0;
32
33 hfile = _get_osfhandle(_fd);
34 istext = __fileno_getmode(_fd) & O_TEXT;
35
36 /* read data */
37 if (!ReadFile(hfile, bufp, nbyte, &_rbyte, NULL))
38 {
39 /* failure */
40 return -1;
41 }
42
43 /* text mode */
44 if (_rbyte && istext)
45 {
46 int cr = 0;
47 DWORD count = _rbyte;
48
49 /* repeat for all bytes in the buffer */
50 for(; count; bufp++, count--)
51 {
52 /* carriage return */
53 if (*bufp == '\r')
54 cr++;
55 /* shift characters back, to ignore carriage returns */
56 else if (cr != 0)
57 *(bufp - cr) = *bufp;
58
59 }
60
61 /* ignore the carriage returns */
62 _rbyte -= cr;
63 }
64
65 DPRINT("%d\n", _rbyte);
66 return _rbyte;
67 }