[pacman-dev] [PATCH] Convert strtrim/strlen paired calls to only strtrim

Dan McGee dan at archlinux.org
Fri Dec 23 15:48:10 EST 2011


This utilizes the new return value so we don't have to find the length
of the string again.

Signed-off-by: Dan McGee <dan at archlinux.org>
---
 src/pacman/conf.c |    3 +--
 src/pacman/util.c |   18 ++++++++++--------
 2 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/src/pacman/conf.c b/src/pacman/conf.c
index 117aecd4..7ba2791 100644
--- a/src/pacman/conf.c
+++ b/src/pacman/conf.c
@@ -726,8 +726,7 @@ static int _parseconfig(const char *file, struct section_t *section,
 			*ptr = '\0';
 		}
 
-		strtrim(line);
-		line_len = strlen(line);
+		line_len = strtrim(line);
 
 		if(line_len == 0) {
 			continue;
diff --git a/src/pacman/util.c b/src/pacman/util.c
index 4160c44..467bedf 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -1228,14 +1228,14 @@ static int multiselect_parse(char *array, int count, char *response)
 	for(str = response; ; str = NULL) {
 		int include = 1;
 		int start, end;
+		size_t len;
 		char *ends = NULL;
 		char *starts = strtok_r(str, " ", &saveptr);
 
 		if(starts == NULL) {
 			break;
 		}
-		strtrim(starts);
-		int len = strlen(starts);
+		len = strtrim(starts);
 		if(len == 0)
 			continue;
 
@@ -1314,6 +1314,7 @@ int multiselect_question(char *array, int count)
 
 		if(fgets(response, response_len, stdin)) {
 			const size_t response_incr = 64;
+			size_t len;
 			/* handle buffer not being large enough to read full line case */
 			while(*lastchar == '\0' && lastchar[-1] != '\n') {
 				response_len += response_incr;
@@ -1330,8 +1331,9 @@ int multiselect_question(char *array, int count)
 					return -1;
 				}
 			}
-			strtrim(response);
-			if(strlen(response) > 0) {
+
+			len = strtrim(response);
+			if(len > 0) {
 				if(multiselect_parse(array, count, response) == -1) {
 					/* only loop if user gave an invalid answer */
 					continue;
@@ -1374,8 +1376,8 @@ int select_question(int count)
 		flush_term_input();
 
 		if(fgets(response, sizeof(response), stdin)) {
-			strtrim(response);
-			if(strlen(response) > 0) {
+			size_t len = strtrim(response);
+			if(len > 0) {
 				int n;
 				if(parseindex(response, &n, 1, count) != 0)
 					continue;
@@ -1423,8 +1425,8 @@ static int question(short preset, char *fmt, va_list args)
 	flush_term_input();
 
 	if(fgets(response, sizeof(response), stdin)) {
-		strtrim(response);
-		if(strlen(response) == 0) {
+		size_t len = strtrim(response);
+		if(len == 0) {
 			return preset;
 		}
 
-- 
1.7.8.1



More information about the pacman-dev mailing list