--- ray/src/common/fgetline.c 1989/10/04 15:48:15 1.1 +++ ray/src/common/fgetline.c 2004/02/11 22:11:33 2.5 @@ -1,37 +1,37 @@ -/* Copyright (c) 1989 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: fgetline.c,v 2.5 2004/02/11 22:11:33 greg Exp $"; #endif - /* - * fgetline.c - read line with escaped newlines + * fgetline.c - read line with escaped newlines. * - * 10/4/89 + * External symbols declared in rtio.h */ -#include +#include "copyright.h" +#include "rtio.h" + char * fgetline(s, n, fp) /* read in line with escapes, elide final newline */ char *s; -register int n; -FILE *fp; +int n; +register FILE *fp; { - register char *cp; - register int c; + register char *cp = s; + register int c = EOF; - cp = s; - while (--n > 0 && (c = getc(fp)) != EOF && c != '\n') { - if (c == '\\') { - if ((c = getc(fp)) == EOF) - break; + while (--n > 0 && (c = getc(fp)) != EOF) { + 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) + if (cp == s && c == EOF) return(NULL); - *cp++ = '\0'; + *cp = '\0'; return(s); }