File: readline.c

package info (click to toggle)
macutils 2.0b3-17
  • links: PTS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,256 kB
  • sloc: ansic: 12,737; makefile: 661
file content (40 lines) | stat: -rwxr-xr-x 703 bytes parent folder | download | duplicates (7)
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
#include "globals.h"
#include "readline.h"

char line[1024];	/* Allow a lot! */

/* Read a line.  Allow termination by CR or LF or both.  Also allow for
   a non-terminated line at end-of-file.  Returns 1 if a line is read,
   0 otherwise. */
int readline()
{
    int ptr = 0, c;

    while(1) {
	if(was_macbin && to_read-- <= 0) {
	    c = EOF;
	} else {
	    c = getc(ifp);
	}
	if(c == EOF || c == '\n' || c == '\r' || ptr == 1023) {
	    break;
	}
	line[ptr++] = c;
    }
    line[ptr++] = 0;
    if(c == EOF) {
	if(ptr == 1) {
	    return 0;
	} else {
	    return 1;
	}
    }
    c = getc(ifp);
    if(c != '\n' || c != '\r') {
	(void)ungetc(c, ifp);
    } else {
	to_read--;
    }
    return 1;
}