/* readline.c -- read a line of input * * char *line; * FILE *cif; * FNL *inf; * int size; * size = readline(&line, &cif, &inf); * * cif points to a currently open file (or is NULL if there * is none). inf is a list of names of files to be read * when cif is exhausted. readline reads a line, putting * it in space allocated from the heap. A pointer to the * line is placed in line. The terminal newline is replaced * with a '\0'. The number of chars in the line (not including * the terminator) is returned. */ #include #include "common.h" #include "readline.h" char *ealloc(); char *erealloc(); int strlen(); char *igets(); int readline(lp, ci, il) char **lp; FILE **ci; FNL **il; { char *b; /* buffer */ int bs; /* buffer size */ char *s; /* current char pointer */ int sp; /* space left */ int sl; b = ealloc(MINBUF); bs = MINBUF; s = b; sp = bs; for(;;) { if (NULL == igets(s, sp, ci, il)) return(EOF); sl = strlen(s); s += sl; sp -= sl; if ('\n' == s[-1]) break; if (1 >= sp) { bs += MINBUF; b = erealloc(b, bs); sp += MINBUF; s = b + bs - sp; } } s[-1] = '\0'; *lp = b; return(bs - sp - 1); }