From 5a6fe0574c77342f29370219fb9010b72d6d3a00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Courr=C3=A8ges-Anglas?= Date: Sat, 13 Apr 2013 04:36:23 +0200 Subject: read_rc_file: use getline(3) instead of emulating it --- src/main.c | 49 +++++++++++-------------------------------------- 1 file changed, 11 insertions(+), 38 deletions(-) diff --git a/src/main.c b/src/main.c index ea72f67..de2a793 100644 --- a/src/main.c +++ b/src/main.c @@ -365,56 +365,29 @@ set_close_on_exec (FILE *fd) void read_rc_file (FILE *file) { - size_t n = 256; - char *partial; char *line; - size_t linesize = n; + size_t linesize = 256; - partial = (char*)xmalloc(n); - line = (char*)xmalloc(linesize); + line = xmalloc (linesize); - *line = '\0'; - while (fgets (partial, n, file) != NULL) + while (getline (&line, &linesize, file) != -1) { - if ((strlen (line) + strlen (partial)) >= linesize) - { - linesize *= 2; - line = (char*) xrealloc (line, linesize); - } + line[strcspn (line, "\n")] = '\0'; - strcat (line, partial); + PRINT_DEBUG (("rcfile line: %s\n", line)); - if (feof(file) || (*(line + strlen(line) - 1) == '\n')) + if (*line != '\0' && *line != '#') { - /* FIXME: this is a hack, command() should properly parse - the command and args (ie strip whitespace, etc) - - We should not care if there is a newline (or vertical - tabs or linefeeds for that matter) at the end of the - command (or anywhere between tokens). */ - if (*(line + strlen(line) - 1) == '\n') - *(line + strlen(line) - 1) = '\0'; + cmdret *result; + result = command (0, line); - PRINT_DEBUG (("rcfile line: %s\n", line)); - - /* do it */ - if (*line != '#') - { - cmdret *result; - result = command (0, line); - - /* Gobble the result. */ - if (result) - cmdret_free (result); - } - - *line = '\0'; + /* Gobble the result. */ + if (result) + cmdret_free (result); } } - free (line); - free (partial); } static void -- cgit v1.2.3