Commit b37b967a authored by Duane Clark's avatar Duane Clark Committed by Alexandre Julliard

msvcrt: Fix _read and fgetc.

parent 1aa8db05
...@@ -1615,9 +1615,9 @@ static unsigned int remove_cr(char *buf, unsigned int count) ...@@ -1615,9 +1615,9 @@ static unsigned int remove_cr(char *buf, unsigned int count)
} }
/********************************************************************* /*********************************************************************
* _read (MSVCRT.@) * (internal) read_i
*/ */
int CDECL _read(int fd, void *buf, unsigned int count) static int read_i(int fd, void *buf, unsigned int count)
{ {
DWORD num_read, all_read = 0; DWORD num_read, all_read = 0;
char *bufstart = buf; char *bufstart = buf;
...@@ -1641,17 +1641,11 @@ int CDECL _read(int fd, void *buf, unsigned int count) ...@@ -1641,17 +1641,11 @@ int CDECL _read(int fd, void *buf, unsigned int count)
{ {
TRACE(":EOF\n"); TRACE(":EOF\n");
MSVCRT_fdesc[fd].wxflag |= WX_ATEOF; MSVCRT_fdesc[fd].wxflag |= WX_ATEOF;
if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
num_read -= remove_cr(bufstart+all_read,num_read);
all_read += num_read; all_read += num_read;
if (count > 4) if (count > 4)
TRACE("%s\n",debugstr_an(buf,all_read)); TRACE("%s\n",debugstr_an(buf,all_read));
return all_read; return all_read;
} }
if (MSVCRT_fdesc[fd].wxflag & WX_TEXT)
{
num_read -= remove_cr(bufstart+all_read,num_read);
}
all_read += num_read; all_read += num_read;
} }
else else
...@@ -1667,6 +1661,20 @@ int CDECL _read(int fd, void *buf, unsigned int count) ...@@ -1667,6 +1661,20 @@ int CDECL _read(int fd, void *buf, unsigned int count)
} }
/********************************************************************* /*********************************************************************
* _read (MSVCRT.@)
*/
int CDECL _read(int fd, void *buf, unsigned int count)
{
int num_read;
num_read = read_i(fd, buf, count);
if (num_read>0 && MSVCRT_fdesc[fd].wxflag & WX_TEXT)
{
num_read -= remove_cr(buf,num_read);
}
return num_read;
}
/*********************************************************************
* _getw (MSVCRT.@) * _getw (MSVCRT.@)
*/ */
int CDECL MSVCRT__getw(MSVCRT_FILE* file) int CDECL MSVCRT__getw(MSVCRT_FILE* file)
...@@ -2109,13 +2117,13 @@ int CDECL MSVCRT__filbuf(MSVCRT_FILE* file) ...@@ -2109,13 +2117,13 @@ int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
if(file->_flag & MSVCRT__IONBF) { if(file->_flag & MSVCRT__IONBF) {
unsigned char c; unsigned char c;
int r; int r;
if ((r = _read(file->_file,&c,1)) != 1) { if ((r = read_i(file->_file,&c,1)) != 1) {
file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR; file->_flag |= (r == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
return MSVCRT_EOF; return MSVCRT_EOF;
} }
return c; return c;
} else { } else {
file->_cnt = _read(file->_file, file->_base, file->_bufsiz); file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
if(file->_cnt<=0) { if(file->_cnt<=0) {
file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR; file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
file->_cnt = 0; file->_cnt = 0;
...@@ -2132,12 +2140,21 @@ int CDECL MSVCRT__filbuf(MSVCRT_FILE* file) ...@@ -2132,12 +2140,21 @@ int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
*/ */
int CDECL MSVCRT_fgetc(MSVCRT_FILE* file) int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
{ {
if (file->_cnt>0) { char *i;
file->_cnt--; int j;
return *(unsigned char *)file->_ptr++; do {
} else { if (file->_cnt>0) {
return MSVCRT__filbuf(file); file->_cnt--;
} i = file->_ptr++;
j = *i;
} else {
j = MSVCRT__filbuf(file);
if (j == MSVCRT_EOF)
return j;
}
if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) || (j != '\r'))
return j;
} while(1);
} }
/********************************************************************* /*********************************************************************
......
...@@ -184,10 +184,7 @@ static void test_readmode( BOOL ascii_mode ) ...@@ -184,10 +184,7 @@ static void test_readmode( BOOL ascii_mode )
fp++; fp++;
ok(fgets(buffer,256,file) !=0,"line 1 fgets failed unexpected in %s\n", IOMODE); ok(fgets(buffer,256,file) !=0,"line 1 fgets failed unexpected in %s\n", IOMODE);
l = ftell(file); l = ftell(file);
if (ao == -1) ok(l == pl+fp,"line 1 ftell got %ld should be %d in %s\n", l, pl+fp, IOMODE);
todo_wine ok(l == pl+fp,"line 1 ftell got %ld should be %d in %s\n", l, pl+fp, IOMODE);
else
ok(l == pl+fp,"line 1 ftell got %ld should be %d in %s\n", l, pl+fp, IOMODE);
ok(lstrlenA(buffer) == fp+ao,"line 1 fgets got size %d should be %d in %s\n", ok(lstrlenA(buffer) == fp+ao,"line 1 fgets got size %d should be %d in %s\n",
lstrlenA(buffer), fp+ao, IOMODE); lstrlenA(buffer), fp+ao, IOMODE);
/* test a seek back across the buffer boundary */ /* test a seek back across the buffer boundary */
...@@ -197,19 +194,13 @@ static void test_readmode( BOOL ascii_mode ) ...@@ -197,19 +194,13 @@ static void test_readmode( BOOL ascii_mode )
ok(l == pl,"ftell after seek got %ld should be %d in %s\n", l, pl, IOMODE); ok(l == pl,"ftell after seek got %ld should be %d in %s\n", l, pl, IOMODE);
ok(fgets(buffer,256,file) !=0,"second read of line 1 fgets failed unexpected in %s\n", IOMODE); ok(fgets(buffer,256,file) !=0,"second read of line 1 fgets failed unexpected in %s\n", IOMODE);
l = ftell(file); l = ftell(file);
if (ao == -1) ok(l == pl+fp,"second read of line 1 ftell got %ld should be %d in %s\n", l, pl+fp, IOMODE);
todo_wine ok(l == pl+fp,"second read of line 1 ftell got %ld should be %d in %s\n", l, pl+fp, IOMODE);
else
ok(l == pl+fp,"second read of line 1 ftell got %ld should be %d in %s\n", l, pl+fp, IOMODE);
ok(lstrlenA(buffer) == fp+ao,"second read of line 1 fgets got size %d should be %d in %s\n", ok(lstrlenA(buffer) == fp+ao,"second read of line 1 fgets got size %d should be %d in %s\n",
lstrlenA(buffer), fp+ao, IOMODE); lstrlenA(buffer), fp+ao, IOMODE);
ok(fgets(buffer,256,file) !=0,"line 2 fgets failed unexpected in %s\n", IOMODE); ok(fgets(buffer,256,file) !=0,"line 2 fgets failed unexpected in %s\n", IOMODE);
fp += 2; fp += 2;
l = ftell(file); l = ftell(file);
if (ao == -1) ok(l == pl+fp,"line 2 ftell got %ld should be %d in %s\n", l, pl+fp, IOMODE);
todo_wine ok(l == pl+fp,"line 2 ftell got %ld should be %d in %s\n", l, pl+fp, IOMODE);
else
ok(l == pl+fp,"line 2 ftell got %ld should be %d in %s\n", l, pl+fp, IOMODE);
ok(lstrlenA(buffer) == 2+ao,"line 2 fgets got size %d should be %d in %s\n", ok(lstrlenA(buffer) == 2+ao,"line 2 fgets got size %d should be %d in %s\n",
lstrlenA(buffer), 2+ao, IOMODE); lstrlenA(buffer), 2+ao, IOMODE);
...@@ -219,7 +210,10 @@ static void test_readmode( BOOL ascii_mode ) ...@@ -219,7 +210,10 @@ static void test_readmode( BOOL ascii_mode )
ok(fgets(buffer,MSVCRT_BUFSIZ-6,file) !=0,"padding line fgets failed unexpected in %s\n", IOMODE); ok(fgets(buffer,MSVCRT_BUFSIZ-6,file) !=0,"padding line fgets failed unexpected in %s\n", IOMODE);
j=strlen(outbuffer); j=strlen(outbuffer);
i=fread(buffer,1,256,file); i=fread(buffer,1,256,file);
ok(i==j+6+ao*4,"fread failed, expected %d got %d in %s\n", j+6+ao*4, i, IOMODE); if (ao == -1)
todo_wine todo_wine ok(i==j+6+ao*4,"fread failed, expected %d got %d in %s\n", j+6+ao*4, i, IOMODE);
else
ok(i==j+6+ao*4,"fread failed, expected %d got %d in %s\n", j+6+ao*4, i, IOMODE);
l = ftell(file); l = ftell(file);
ok(l == pl+j+1,"ftell after fread got %ld should be %d in %s\n", l, pl+j+1, IOMODE); ok(l == pl+j+1,"ftell after fread got %ld should be %d in %s\n", l, pl+j+1, IOMODE);
/* fread should return the requested number of bytes if available */ /* fread should return the requested number of bytes if available */
...@@ -228,7 +222,10 @@ static void test_readmode( BOOL ascii_mode ) ...@@ -228,7 +222,10 @@ static void test_readmode( BOOL ascii_mode )
ok(fgets(buffer,MSVCRT_BUFSIZ-6,file) !=0,"padding line fgets failed unexpected in %s\n", IOMODE); ok(fgets(buffer,MSVCRT_BUFSIZ-6,file) !=0,"padding line fgets failed unexpected in %s\n", IOMODE);
j = fp+10; j = fp+10;
i=fread(buffer,1,j,file); i=fread(buffer,1,j,file);
ok(i==j,"fread failed, expected %d got %d in %s\n", j, i, IOMODE); if (ao == -1)
todo_wine ok(i==j,"fread failed, expected %d got %d in %s\n", j, i, IOMODE);
else
ok(i==j,"fread failed, expected %d got %d in %s\n", j, i, IOMODE);
/* test some additional functions */ /* test some additional functions */
rewind(file); rewind(file);
...@@ -370,7 +367,7 @@ static void test_file_write_read( void ) ...@@ -370,7 +367,7 @@ static void test_file_write_read( void )
"problems with _O_BINARY _write / _read\n"); "problems with _O_BINARY _write / _read\n");
_close(tempfd); _close(tempfd);
tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */ tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
todo_wine ok(_read(tempfd,btext,i) == i-1, ok(_read(tempfd,btext,i) == i-1,
"_read _O_TEXT got bad length\n"); "_read _O_TEXT got bad length\n");
ok( memcmp(mytext,btext,i-1) == 0, ok( memcmp(mytext,btext,i-1) == 0,
"problems with _O_BINARY _write / _O_TEXT _read\n"); "problems with _O_BINARY _write / _O_TEXT _read\n");
...@@ -413,8 +410,8 @@ static void test_file_write_read( void ) ...@@ -413,8 +410,8 @@ static void test_file_write_read( void )
ok(ret == 1 && *btext == '\n', "_read expected '\\n' got bad length: %d\n", ret); ok(ret == 1 && *btext == '\n', "_read expected '\\n' got bad length: %d\n", ret);
_lseek(tempfd, -3, FILE_END); _lseek(tempfd, -3, FILE_END);
ret = _read(tempfd,btext,2); ret = _read(tempfd,btext,2);
todo_wine ok(ret == 1 && *btext == 'e', "_read expected 'e' got \"%.*s\" bad length: %d\n", ret, btext, ret); ok(ret == 1 && *btext == 'e', "_read expected 'e' got \"%.*s\" bad length: %d\n", ret, btext, ret);
todo_wine ok(tell(tempfd) == 42, "bad position %lu expecting 42\n", tell(tempfd)); ok(tell(tempfd) == 42, "bad position %lu expecting 42\n", tell(tempfd));
_close(tempfd); _close(tempfd);
ret = unlink(tempf); ret = unlink(tempf);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment