diff mbox

[2/5] libmultipath: Simplify read_line()

Message ID 53C918B7.3010704@acm.org (mailing list archive)
State Not Applicable, archived
Delegated to: Mike Snitzer
Headers show

Commit Message

Bart Van Assche July 18, 2014, 12:53 p.m. UTC
Let read_line() zero-terminate the buffer it returns such that its
callers do not have to clear that buffer before calling read_line().

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 libmultipath/parser.c | 22 ++++++----------------
 1 file changed, 6 insertions(+), 16 deletions(-)
diff mbox

Patch

diff --git a/libmultipath/parser.c b/libmultipath/parser.c
index 0d4c870..b7bdfcc 100644
--- a/libmultipath/parser.c
+++ b/libmultipath/parser.c
@@ -282,18 +282,12 @@  out:
 int
 read_line(char *buf, int size)
 {
-	int ch;
-	int count = 0;
-
-	while ((ch = fgetc(stream)) != EOF && (int) ch != '\n'
-	       && (int) ch != '\r') {
-		if (count < size)
-			buf[count] = (int) ch;
-		else
-			break;
-		count++;
-	}
-	return (ch == EOF) ? 0 : 1;
+	char *p;
+
+	if (fgets(buf, size, stream) == NULL)
+		return 0;
+	strtok_r(buf, "\n\r", &p);
+	return 1;
 }
 
 vector
@@ -341,7 +335,6 @@  read_value_block(void)
 			}
 			free_strvec(vec);
 		}
-		memset(buf, 0, MAXBUF);
 	}
 	FREE(buf);
 	return elements;
@@ -379,7 +372,6 @@  alloc_value_block(vector strvec, void (*alloc_func) (vector))
 
 			free_strvec(vec);
 		}
-		memset(buf, 0, MAXBUF);
 	}
 	FREE(buf);
 	return 0;
@@ -489,8 +481,6 @@  process_stream(vector keywords)
 	while (read_line(buf, MAXBUF)) {
 		line_nr++;
 		strvec = alloc_strvec(buf);
-		memset(buf,0, MAXBUF);
-
 		if (!strvec)
 			continue;