readline.c
1023 Bytes
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
41
42
43
44
45
#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;
}