#include "lfgets.h" #include #include #include /* dynamic (expanding buffer) fgets */ /* OrangeTide 19971201 */ /* PUBLIC DOMAIN - Jon Mayo - December 1, 1997 */ /* if limit is -1, then don't do limiting * limit must be greater than LFGETS_INITSIZE * if nlstrip then cut the nl off before returning * * return: * NULL on reading EOF or alloc error * dynamically created buffer on success * cropped buffer on reading beyond the limit * */ char *lfgets(FILE *stream, int nlstrip, int limit) { char *Ret = NULL; int curr = 0; int currsize = LFGETS_INITSIZE; if(limit0 && curr>=limit) { return Ret; /* limit exceeded, just return it */ } currsize*=2; /* double the size for the next realloc() */ if(limit>0 && currsize>limit) currsize=limit; } /* ((limit >= currsize) || (limit < 0)) ) */ return curr ? Ret : (free(Ret),NULL); }