fget(w)s(): Don't drop characters when buffer is not large enough.

This commit is contained in:
Uwe Bonnes 2003-10-07 05:22:04 +00:00 committed by Alexandre Julliard
parent 16fa009085
commit 7b8b62e61a
1 changed files with 16 additions and 22 deletions

View File

@ -1714,27 +1714,24 @@ int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
*/ */
char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file) char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
{ {
int cc; int cc = MSVCRT_EOF;
char * buf_start = s; char * buf_start = s;
TRACE(":file(%p) fd (%d) str (%p) len (%d)\n", TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
file,file->_file,s,size); file,file->_file,s,size);
for(cc = MSVCRT_fgetc(file); cc != MSVCRT_EOF && cc != '\n'; while ((size >1) && (cc = MSVCRT_fgetc(file)) != MSVCRT_EOF && cc != '\n')
cc = MSVCRT_fgetc(file))
/* _read already handled the translation */
{ {
if (--size <= 0) break;
*s++ = (char)cc; *s++ = (char)cc;
size --;
} }
if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/ if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/
{ {
TRACE(":nothing read\n"); TRACE(":nothing read\n");
return 0; return NULL;
} }
if (cc == '\n') if ((cc != MSVCRT_EOF) && (size > 1))
if (--size > 0) *s++ = cc;
*s++ = '\n';
*s = '\0'; *s = '\0';
TRACE(":got '%s'\n", debugstr_a(buf_start)); TRACE(":got '%s'\n", debugstr_a(buf_start));
return buf_start; return buf_start;
@ -1797,29 +1794,26 @@ MSVCRT_wint_t MSVCRT_getwchar(void)
*/ */
MSVCRT_wchar_t *MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file) MSVCRT_wchar_t *MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
{ {
int cc; int cc = MSVCRT_WEOF;
MSVCRT_wchar_t * buf_start = s; MSVCRT_wchar_t * buf_start = s;
TRACE(":file(%p) fd (%d) str (%p) len (%d)\n", TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
file,file->_file,s,size); file,file->_file,s,size);
for(cc = MSVCRT_fgetwc(file); cc != MSVCRT_WEOF && cc != L'\n'; while ((size >1) && (cc = MSVCRT_fgetwc(file)) != MSVCRT_WEOF && cc != '\n')
cc = MSVCRT_fgetwc(file))
/* _read already handled the translation */
{ {
if (--size <= 0) break; *s++ = (char)cc;
*s++ = cc; size --;
} }
if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/ if ((cc == MSVCRT_WEOF) && (s == buf_start)) /* If nothing read, return 0*/
{ {
TRACE(":nothing read\n"); TRACE(":nothing read\n");
return 0; return NULL;
} }
if (cc == L'\n') if ((cc != MSVCRT_WEOF) && (size > 1))
if (--size > 0) *s++ = cc;
*s++ = '\n'; *s = 0;
*s = '\0'; TRACE(":got %s\n", debugstr_w(buf_start));
/* TRACE(":got '%s'\n", buf_start); */
return buf_start; return buf_start;
} }