From 4152c4f6b51f576136f901f13605874d951f5ca1 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 25 Dec 2008 14:09:21 +0100 Subject: conf: use buffered I/O for reading the config file Don't read byte by byte until a newline is found. Use fgets() instead. --- src/conf.c | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/conf.c b/src/conf.c index 979fd91c4..27b202428 100644 --- a/src/conf.c +++ b/src/conf.c @@ -502,35 +502,25 @@ parse_line(char *line) static int read_rc_file(char *filename) { - int fd; - int quit = 0; + FILE *file; + char line[MAX_LINE_LENGTH]; if (filename == NULL) return -1; - if ((fd = open(filename,O_RDONLY)) < 0) { + file = fopen(filename, "r"); + if (file == NULL) { perror(filename); return -1; } - while (!quit) { + while (fgets(line, sizeof(line), file) != NULL) { int i; int len; - char line[MAX_LINE_LENGTH]; - - i = 0; - /* read a line ending with '\n' */ - do { - len = read(fd, &line[i], 1); - if (len == 1) - i++; - else - quit = 1; - } while (!quit && i < MAX_LINE_LENGTH && line[i-1] != '\n'); + i = strlen(line); /* remove trailing whitespace */ - line[i] = '\0'; i--; while (i >= 0 && g_ascii_isspace(line[i])) { line[i] = '\0'; @@ -551,6 +541,7 @@ read_rc_file(char *filename) } } + fclose(file); return 0; } -- cgit v1.2.3