diff options
author | Nathan Scott <nathans@sgi.com> | 2005-02-22 02:49:39 +0000 |
---|---|---|
committer | Nathan Scott <nathans@sgi.com> | 2005-02-22 02:49:39 +0000 |
commit | c9017cd8e22a9d70f5ba105fc7d03c3ed24d49b4 (patch) | |
tree | f6f6199262916dc1aaced5a3641a465da896e2a0 /libmisc/next_line.c | |
parent | 3af9ec3b25013fbec25e53693fa9fe37bff739ca (diff) | |
download | attr-c9017cd8e22a9d70f5ba105fc7d03c3ed24d49b4.tar.gz |
Updated next_line fix from AndreasG
Merge of master-melb:xfs-cmds:21594a by kenmcd.
Diffstat (limited to 'libmisc/next_line.c')
-rw-r--r-- | libmisc/next_line.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/libmisc/next_line.c b/libmisc/next_line.c new file mode 100644 index 0000000..4b5c56a --- /dev/null +++ b/libmisc/next_line.c @@ -0,0 +1,37 @@ +#include <stdio.h> +#include <string.h> +#include <limits.h> +#include "misc.h" + +char *next_line(FILE *file) +{ + static char *line; + static size_t line_size; + char *c; + int eol = 0; + + if (!line) { + if (high_water_alloc((void **)&line, &line_size, PATH_MAX)) + return NULL; + } + c = line; + do { + if (!fgets(c, line_size - (c - line), file)) + return NULL; + c = strrchr(c, '\0'); + while (c > line && (*(c-1) == '\n' || *(c-1) == '\r')) { + c--; + *c = '\0'; + eol = 1; + } + if (feof(file)) + break; + if (!eol) { + if (high_water_alloc((void **)&line, &line_size, + 2 * line_size)) + return NULL; + c = strrchr(line, '\0'); + } + } while (!eol); + return line; +} |