[dm-devel] [PATCH 2/5] libmultipath: Simplify read_line()

Bart Van Assche bvanassche at acm.org
Fri Jul 18 12:53:11 UTC 2014


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 at acm.org>
---
 libmultipath/parser.c | 22 ++++++----------------
 1 file changed, 6 insertions(+), 16 deletions(-)

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;
 
-- 
1.8.4.5




More information about the dm-devel mailing list