Back to index

radiance  4R0+20100331
fgetline.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: fgetline.c,v 2.7 2004/09/14 02:53:50 greg Exp $";
00003 #endif
00004 /*
00005  * fgetline.c - read line with escaped newlines.
00006  *
00007  *  External symbols declared in rtio.h
00008  */
00009 
00010 #include "copyright.h"
00011 
00012 #include  "rtio.h"
00013 
00014 #ifdef getc_unlocked        /* avoid horrendous overhead of flockfile */
00015 #undef getc
00016 #define getc    getc_unlocked
00017 #endif
00018 
00019 
00020 char *
00021 fgetline(s, n, fp)   /* read in line with escapes, elide final newline */
00022 char  *s;
00023 int  n;
00024 register FILE  *fp;
00025 {
00026        register char  *cp = s;
00027        register int  c = EOF;
00028 
00029        while (--n > 0 && (c = getc(fp)) != EOF) {
00030               if (c == '\r' && (c = getc(fp)) != '\n') {
00031                      ungetc(c, fp);              /* must be Apple file */
00032                      c = '\n';
00033               }
00034               if (c == '\n' && (cp == s || cp[-1] != '\\'))
00035                      break;
00036               *cp++ = c;
00037        }
00038        if (cp == s && c == EOF)
00039               return(NULL);
00040        *cp = '\0';
00041        return(s);
00042 }