--- ray/src/common/fgetline.c 2003/06/27 06:53:21 2.4 +++ ray/src/common/fgetline.c 2020/07/29 18:19:31 2.10 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: fgetline.c,v 2.4 2003/06/27 06:53:21 greg Exp $"; +static const char RCSid[] = "$Id: fgetline.c,v 2.10 2020/07/29 18:19:31 greg Exp $"; #endif /* * fgetline.c - read line with escaped newlines. @@ -13,23 +13,31 @@ static const char RCSid[] = "$Id: fgetline.c,v 2.4 200 char * -fgetline(s, n, fp) /* read in line with escapes, elide final newline */ -char *s; -int n; -register FILE *fp; +fgetline( /* read in line with escapes, elide final newline */ + char *s, + int n, + FILE *fp +) { - register char *cp = s; - register int c = EOF; + char *cp = s; + int c = EOF; while (--n > 0 && (c = getc(fp)) != EOF) { - if (c == '\r') - continue; + if (c == '\r' && (c = getc(fp)) != '\n') { + ungetc(c, fp); /* must be Apple file */ + c = '\n'; + } if (c == '\n' && (cp == s || cp[-1] != '\\')) break; *cp++ = c; } - if (cp == s && c == EOF) + if ((cp == s) & (c == EOF)) return(NULL); *cp = '\0'; +#if defined(_WIN32) || defined(_WIN64) + /* remove escaped newlines */ + for (cp = s; (cp = strchr(cp, '\\')) != NULL && cp[1] == '\n'; ) + memmove(cp, cp+2, strlen(cp+2)+1); +#endif return(s); }