summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/lib/eofeoln.c
blob: c584f5da138465a676e124c3e418e450ad5ea49d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* eofeoln.c: implement Pascal's ideas for end-of-file and end-of-line
   testing.  Public domain. */

#include "config.h"
#include "lib.h"

/* Return true if we're at the end of FILE, else false.  This implements
   Pascal's `eof' builtin.  */

boolean
eof (FILE *file)
{
  register int c;

  /* If FILE doesn't exist, return false. This happens when, for
     example, when a user does `mft foo.mf' -- there's no change file,
     so we never open it, so we end up calling this with a null pointer. */
  if (!file)
    return true;
    
  /* Maybe we're already at the end?  */
  if (feof (file))
    return true;

  if ((c = getc (file)) == EOF)
    return true;

  /* We weren't at the end.  Back up.  */
  (void) ungetc (c, file);

  return false;
}


/* Return true on end-of-line in FILE or at the end of FILE, else false.  */
/* Accept both CR and LF as end-of-line. */

boolean
eoln (FILE *file)
{
  register int c;

  if (feof (file))
    return true;
  
  c = getc (file);
  
  if (c != EOF)
    (void) ungetc (c, file);
    
  return c == '\n' || c == '\r' || c == EOF;
}

/* Consume input up and including the first eol encountered. */
/* Handle CRLF as a single end-of-line. */

void
readln (FILE *f)
{
    int c;
    while ((c = getc (f)) != '\n' && c != '\r' && c != EOF)
        ;
    if (c == '\r' && (c = getc (f)) != '\n' && c != EOF)
        ungetc (c, f);
}