Index: base/applications/regedit/regproc.c =================================================================== --- base/applications/regedit/regproc.c (revision 62197) +++ base/applications/regedit/regproc.c (working copy) @@ -646,8 +646,9 @@ * * Parameters: * in - input stream to read from + * first_chars - beginning of stream, read due to Unicode check */ -static void processRegLinesA(FILE *in) +static void processRegLinesA(FILE *in, char* first_chars) { LPSTR line = NULL; /* line read from input stream */ ULONG lineSize = REG_VAL_BUF_SIZE; @@ -654,18 +655,24 @@ line = HeapAlloc(GetProcessHeap(), 0, lineSize); CHECK_ENOUGH_MEMORY(line); + memcpy(line, first_chars, 2); while (!feof(in)) { LPSTR s; /* The pointer into line for where the current fgets should read */ - LPSTR check; WCHAR* lineW; s = line; + if(first_chars) + { + s += 2; + first_chars = NULL; + } + for (;;) { size_t size_remaining; - int size_to_get; + int size_to_get, i; char *s_eol; /* various local uses */ /* Do we need to expand the buffer ? */ @@ -691,28 +698,54 @@ */ size_to_get = (size_remaining > INT_MAX ? INT_MAX : size_remaining); - check = fgets (s, size_to_get, in); - - if (check == NULL) + /* Get a single line. note that `i' must be one past the last + * meaningful character in `s' when this loop exits */ + for(i = 0; i < size_to_get-1; ++i) { - if (ferror(in)) + s[i] = fgetc(in); + if(s[i] == EOF) { - perror ("While reading input"); - exit (IO_ERROR); + if(ferror(in)) + { + perror("While reading input"); + exit(IO_ERROR); + } + else + assert(feof(in)); + break; } - else + + if(s[i] == '\r') { - assert (feof(in)); - *s = '\0'; - /* It is not clear to me from the definition that the - * contents of the buffer are well defined on detecting - * an eof without managing to read anything. - */ + /* read the next character iff it's \n */ + if(i+2 >= size_to_get) + { + /* buffer too short, so put back the EOL char to + * read next cycle */ + ungetc('\r', in); + break; + } + s[i+1] = fgetc(in); + if(s[i+1] != '\n') + { + ungetc(s[i+1], in); + i = i+1; + } + else + i = i+2; + break; } + + if(s[i] == '\n') + { + i = i+1; + break; + } } + s[i] = '\0'; /* If we didn't read the eol nor the eof go around for the rest */ - s_eol = strchr (s, '\n'); + s_eol = strpbrk (s, "\r\n"); if (!feof (in) && !s_eol) { s = strchr (s, '\0'); @@ -730,9 +763,9 @@ /* Remove any line feed. Leave s_eol on the \0 */ if (s_eol) { + if (*s_eol == '\r' && *(s_eol+1) == '\n') + *(s_eol+1) = '\0'; *s_eol = '\0'; - if (s_eol > line && *(s_eol-1) == '\r') - *--s_eol = '\0'; } else s_eol = strchr (s, '\0'); @@ -1494,8 +1527,7 @@ } else { - fseek(reg_file, 0, SEEK_SET); - processRegLinesA(reg_file); + processRegLinesA(reg_file, (char*)s); } } return TRUE;