beta-0.89.2
[luatex.git] / source / texk / web2c / lib / eofeoln.c
blob1930df9051af81fe353612001b80c8d9b2b11690
1 /* eofeoln.c: implement Pascal's ideas for end-of-file and end-of-line
2 testing. Public domain. */
4 #include <w2c/config.h>
5 #include "lib.h"
7 /* Return true if we're at the end of FILE, else false. This implements
8 Pascal's `eof' builtin. */
10 boolean
11 eof (FILE *file)
13 register int c;
15 /* If FILE doesn't exist, return true. This happens, for example,
16 when a user does `mft foo.mf' -- there's no change file,
17 so we never open it, so we end up calling this with a null pointer. */
18 if (!file)
19 return true;
21 /* Maybe we're already at the end? */
22 if (feof (file))
23 return true;
25 if ((c = getc (file)) == EOF)
26 return true;
28 /* We weren't at the end. Back up. */
29 (void) ungetc (c, file);
31 return false;
35 /* Return true on end-of-line in FILE or at the end of FILE, else false. */
36 /* Accept both CR and LF as end-of-line. */
38 boolean
39 eoln (FILE *file)
41 register int c;
43 if (feof (file))
44 return true;
46 c = getc (file);
48 if (c != EOF)
49 (void) ungetc (c, file);
51 return c == '\n' || c == '\r' || c == EOF;
54 /* Consume input up and including the first eol encountered. */
55 /* Handle CRLF as a single end-of-line. */
57 void
58 readln (FILE *f)
60 int c;
61 while ((c = getc (f)) != '\n' && c != '\r' && c != EOF)
63 if (c == '\r' && (c = getc (f)) != '\n' && c != EOF)
64 ungetc (c, f);