Commit 7962bf1b 7962bf1bd7451aaf568cda35aaa18d5c2f4dd1c2 by Sean 'Shaleh' Perry

readline routine added

1 parent 195ed11f
Sean 'Shaleh' Perry <shaleh@debian.org> Thu, 7 Oct 1999 18:31:57 -0700
* included my read_a_line() in examples/
Sean 'Shaleh' Perry <shaleh@debian.org> Wed, 6 Oct 1999 13:55:42 -0700
* Cleanup some compilation issues
......
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef COUNT
#define COUNT 128 /* safe, middle of the road value */
#endif
/**
* reads a single line from *stream
* max line length is max size of size_t
*
* PRE: stream is an open FILE stream
* POST: returns an allocated string containing the line read, or errno is set
*
* note, user must free the returned string
**/
char *read_a_line(FILE *stream) {
char *line = NULL;
size_t buffsize = 0; /* counter of alloc'ed space */
if( feof(stream) ) { /* sent a bum stream */
errno = EINVAL;
return NULL;
}
do {
if ((line = realloc(line, buffsize + (sizeof(char) * COUNT))) == NULL) {
errno = ENOMEM;
return NULL;
}
if( fgets(line + (buffsize ? buffsize - 1 : 0),COUNT,stream) == NULL ) {
if( buffsize == 0 ) {
errno = EINVAL;
return NULL; /* eof w/ nothing read */
}
break; /* read a line w/o a newline char */
}
buffsize += COUNT;
} while( (strchr(line, '\n')) == NULL );
return line;
}
#ifndef _mailutils_read_a_line__
#define _mailutils_read_a_line__
char* read_a_line(FILE *stream);
#endif