/* igets.c -- read a line of sort input * * char lbuf[LLEN]; * FILE *current; * FNL *names; * if (NULL == igets(lbuf, LLEN, ¤t, &names)) error... * * current points to a currently open file. (If no file * is open, current may be NULL.) names points to a list * of names of files. The first name in the list in * the name of the currently open file. Subsequent names * identify files from which input is to be read when * current is exhausted. The name "-" is taken to mean * stdin. igets reads a line, if necessary * closing current and opening the next file in the list. * current and names are updated if this is necessary. * igets returns NULL if there is an error or endfile on * current and there are no more files, or the next file * can't be opened. */ #include #include "common.h" #include "readline.h" char *fgets(); int fclose(); FILE *fopen(); char * igets(s, n, f, fl) char *s; int n; FILE **f; FNL **fl; { FNL *fp; if (NULL == *f) { if (NULL == *fl) return(NULL); if (0 == strcmp("-", (*fl)->f_n)) { *f = stdin; } else { if (NULL == (*f = fopen((*fl)->f_n, "r"))) error1("unable to open %s", (*fl)->f_n); } } while(NULL == fgets(s, n, *f)) { if (EOF == fclose(*f)) error1("unable to close %s", (*fl)->f_n); fp = *fl; *fl = fp->f_nxt; free(fp->f_n); free(fp); *f = NULL; if (NULL == *fl) return(NULL); if (NULL == (*f = fopen((*fl)->f_n, "r"))) error1("unable to open %s", (*fl)->f_n); } return(s); } /* ieof -- check for endfile on input * * FILE *current; * FNL *names; * int atend; * atend = ieof(current, names); * * current and names are as for igets (but note that ieof * expects current and names, not their addresses). ieof * returns TRUE iff the end of file current has been reached * and there are no more files. * * ieof uses the cumbersome method of attempting to read * the next char, because feof doesn't work properly on * the VAX. On other machines, simpler methods should * work. */ int ieof(f, fl) FILE *f; FNL *fl; { register int c; if (NULL == f) { return(NULL == fl); } else { if ( ((NULL == fl) || (NULL == fl->f_nxt)) && (EOF == (c = getc(f))) ) return(TRUE); ungetc(c, f); return(FALSE); } }