diff --git a/reactos/lib/sdk/crt/stdio/file.c b/reactos/lib/sdk/crt/stdio/file.c index 154581658f0..e6371e28336 100644 --- a/reactos/lib/sdk/crt/stdio/file.c +++ b/reactos/lib/sdk/crt/stdio/file.c @@ -902,10 +902,18 @@ int CDECL fseek(FILE* file, long offset, int whence) { /* Flush output if needed */ if(file->_flag & _IOWRT) - flush_buffer(file); + flush_buffer(file); if(whence == SEEK_CUR && file->_flag & _IOREAD ) { - offset -= file->_cnt; + offset -= file->_cnt; + if (fdesc[file->_file].wxflag & WX_TEXT) { + /* Black magic correction for CR removal */ + int i; + for (i=0; i_cnt; i++) { + if (file->_ptr[i] == '\n') + offset--; + } + } } /* Discard buffered input */ file->_cnt = 0; @@ -1556,27 +1564,6 @@ int CDECL _rmtmp(void) return num_removed; } -/********************************************************************* - * (internal) remove_cr - * - * Translate all \r\n to \n inplace. - * return the number of \r removed - * Corner cases required by some apps: - * \r\r\n -> \r\n - * BUG: should save state across calls somehow, so CR LF that - * straddles buffer boundary gets recognized properly? - */ -static unsigned int remove_cr(char *buf, unsigned int count) -{ - unsigned int i, j; - - for (i=0, j=0; j < count; j++) - if ((buf[j] != '\r') || ((j+1) < count && buf[j+1] != '\n')) - buf[i++] = buf[j]; - - return count - i; -} - /********************************************************************* * (internal) read_i */ @@ -1604,18 +1591,25 @@ static int read_i(int fd, void *buf, unsigned int count) { if (fdesc[fd].wxflag & WX_TEXT) { - int i; - /* in text mode, a ctrl-z signals EOF */ - for (i=0; i0 && fdesc[fd].wxflag & WX_TEXT) - { - num_read -= remove_cr(buf,num_read); - } return num_read; } @@ -1955,17 +1945,13 @@ int CDECL fgetc(FILE* file) { unsigned char *i; unsigned int j; - do { - if (file->_cnt>0) { - file->_cnt--; - i = (unsigned char *)file->_ptr++; - j = *i; - } else - j = _filbuf(file); - if (!(fdesc[file->_file].wxflag & WX_TEXT) - || ((j != '\r') || (file->_cnt && file->_ptr[0] != '\n'))) - return j; - } while(1); + if (file->_cnt>0) { + file->_cnt--; + i = (unsigned char *)file->_ptr++; + j = *i; + } else + j = _filbuf(file); + return j; } /********************************************************************* @@ -2300,11 +2286,13 @@ int CDECL _flsbuf(int c, FILE* file) } if(file->_bufsiz) { int res=flush_buffer(file); - return res?res : fputc(c, file); + return res?res : fputc(c, file); } else { - unsigned char cc=c; + unsigned char cc=c; int len; - len = _write(file->_file, &cc, 1); + /* set _cnt to 0 for unbuffered FILEs */ + file->_cnt = 0; + len = _write(file->_file, &cc, 1); if (len == 1) return c & 0xff; file->_flag |= _IOERR; return EOF; @@ -2336,8 +2324,6 @@ size_t CDECL fread(void *ptr, size_t size, size_t nmemb, FILE* file) memcpy(ptr, file->_ptr, pcnt); file->_cnt -= pcnt; file->_ptr += pcnt; - if (fdesc[file->_file].wxflag & WX_TEXT) - pcnt -= remove_cr(ptr,pcnt); read += pcnt ; rcnt -= pcnt ; ptr = (char*)ptr + pcnt; @@ -2493,17 +2479,26 @@ int CDECL fsetpos(FILE* file, const fpos_t *pos) */ LONG CDECL ftell(FILE* file) { + /* TODO: just call fgetpos and return lower half of result */ int off=0; long pos; - if(file->_bufsiz) { - if( file->_flag & _IOWRT ) { - off = file->_ptr - file->_base; - } else { - off = -file->_cnt; - } - } pos = _tell(file->_file); - if(pos == -1) return pos; + if(pos == -1) return -1; + if(file->_bufsiz) { + if( file->_flag & _IOWRT ) { + off = file->_ptr - file->_base; + } else { + off = -file->_cnt; + if (fdesc[file->_file].wxflag & WX_TEXT) { + /* Black magic correction for CR removal */ + int i; + for (i=0; i_cnt; i++) { + if (file->_ptr[i] == '\n') + off--; + } + } + } + } return off + pos; } @@ -2512,22 +2507,25 @@ LONG CDECL ftell(FILE* file) */ int CDECL fgetpos(FILE* file, fpos_t *pos) { - /* This code has been lifted form the ftell function */ int off=0; - *pos = _lseeki64(file->_file,0,SEEK_CUR); - - if (*pos == -1) return -1; - + if(*pos == -1) return -1; if(file->_bufsiz) { - if( file->_flag & _IOWRT ) { - off = file->_ptr - file->_base; - } else { - off = -file->_cnt; - } + if( file->_flag & _IOWRT ) { + off = file->_ptr - file->_base; + } else { + off = -file->_cnt; + if (fdesc[file->_file].wxflag & WX_TEXT) { + /* Black magic correction for CR removal */ + int i; + for (i=0; i_cnt; i++) { + if (file->_ptr[i] == '\n') + off--; + } + } + } } *pos += off; - return 0; }