msvcrt: Fix _read and fgetc.

This commit is contained in:
Duane Clark 2006-07-29 15:27:59 -07:00 committed by Alexandre Julliard
parent 1aa8db05a0
commit b37b967add
2 changed files with 47 additions and 33 deletions

View File

@ -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;
char *bufstart = buf;
@ -1641,17 +1641,11 @@ int CDECL _read(int fd, void *buf, unsigned int count)
{
TRACE(":EOF\n");
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;
if (count > 4)
TRACE("%s\n",debugstr_an(buf,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;
}
else
@ -1666,6 +1660,20 @@ int CDECL _read(int fd, void *buf, unsigned int count)
return all_read;
}
/*********************************************************************
* _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.@)
*/
@ -2109,13 +2117,13 @@ int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
if(file->_flag & MSVCRT__IONBF) {
unsigned char c;
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;
return MSVCRT_EOF;
}
return c;
} else {
file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
file->_cnt = read_i(file->_file, file->_base, file->_bufsiz);
if(file->_cnt<=0) {
file->_flag |= (file->_cnt == 0) ? MSVCRT__IOEOF : MSVCRT__IOERR;
file->_cnt = 0;
@ -2132,12 +2140,21 @@ int CDECL MSVCRT__filbuf(MSVCRT_FILE* file)
*/
int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
{
if (file->_cnt>0) {
file->_cnt--;
return *(unsigned char *)file->_ptr++;
} else {
return MSVCRT__filbuf(file);
}
char *i;
int j;
do {
if (file->_cnt>0) {
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);
}
/*********************************************************************

View File

@ -184,10 +184,7 @@ static void test_readmode( BOOL ascii_mode )
fp++;
ok(fgets(buffer,256,file) !=0,"line 1 fgets failed unexpected in %s\n", IOMODE);
l = ftell(file);
if (ao == -1)
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(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",
lstrlenA(buffer), fp+ao, IOMODE);
/* test a seek back across the buffer boundary */
@ -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(fgets(buffer,256,file) !=0,"second read of line 1 fgets failed unexpected in %s\n", IOMODE);
l = ftell(file);
if (ao == -1)
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(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",
lstrlenA(buffer), fp+ao, IOMODE);
ok(fgets(buffer,256,file) !=0,"line 2 fgets failed unexpected in %s\n", IOMODE);
fp += 2;
l = ftell(file);
if (ao == -1)
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(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",
lstrlenA(buffer), 2+ao, IOMODE);
@ -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);
j=strlen(outbuffer);
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);
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 */
@ -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);
j = fp+10;
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 */
rewind(file);
@ -370,7 +367,7 @@ static void test_file_write_read( void )
"problems with _O_BINARY _write / _read\n");
_close(tempfd);
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");
ok( memcmp(mytext,btext,i-1) == 0,
"problems with _O_BINARY _write / _O_TEXT _read\n");
@ -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);
_lseek(tempfd, -3, FILE_END);
ret = _read(tempfd,btext,2);
todo_wine 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(ret == 1 && *btext == 'e', "_read expected 'e' got \"%.*s\" bad length: %d\n", ret, btext, ret);
ok(tell(tempfd) == 42, "bad position %lu expecting 42\n", tell(tempfd));
_close(tempfd);
ret = unlink(tempf);