| 1 |
#ifndef lint |
| 2 |
static const char RCSid[] = "$Id: fgetline.c,v 2.6 2004/04/29 14:36:49 greg Exp $"; |
| 3 |
#endif |
| 4 |
/* |
| 5 |
* fgetline.c - read line with escaped newlines. |
| 6 |
* |
| 7 |
* External symbols declared in rtio.h |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "copyright.h" |
| 11 |
|
| 12 |
#include "rtio.h" |
| 13 |
|
| 14 |
#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ |
| 15 |
#undef getc |
| 16 |
#define getc getc_unlocked |
| 17 |
#endif |
| 18 |
|
| 19 |
|
| 20 |
char * |
| 21 |
fgetline(s, n, fp) /* read in line with escapes, elide final newline */ |
| 22 |
char *s; |
| 23 |
int n; |
| 24 |
register FILE *fp; |
| 25 |
{ |
| 26 |
register char *cp = s; |
| 27 |
register int c = EOF; |
| 28 |
|
| 29 |
while (--n > 0 && (c = getc(fp)) != EOF) { |
| 30 |
if (c == '\r' && (c = getc(fp)) != '\n') { |
| 31 |
ungetc(c, fp); /* must be Apple file */ |
| 32 |
c = '\n'; |
| 33 |
} |
| 34 |
if (c == '\n' && (cp == s || cp[-1] != '\\')) |
| 35 |
break; |
| 36 |
*cp++ = c; |
| 37 |
} |
| 38 |
if (cp == s && c == EOF) |
| 39 |
return(NULL); |
| 40 |
*cp = '\0'; |
| 41 |
return(s); |
| 42 |
} |