[sheepdog] [PATCH] remove extra newline character from fgets() result

MORITA Kazutaka morita.kazutaka at gmail.com
Fri Aug 16 21:49:13 CEST 2013


From: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>

This adds chomp() to remove an extra "\n".

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 include/util.h |    1 +
 lib/logger.c   |   10 +++++-----
 lib/util.c     |   10 ++++++++++
 3 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/include/util.h b/include/util.h
index ff1f0c7..9e6b959 100644
--- a/include/util.h
+++ b/include/util.h
@@ -104,6 +104,7 @@ int xftruncate(int fd, off_t length);
 int eventfd_xread(int efd);
 void eventfd_xwrite(int efd, int value);
 void pstrcpy(char *buf, int buf_size, const char *str);
+char *chomp(char *str);
 int rmdir_r(char *dir_path);
 int purge_directory(char *dir_path);
 bool is_numeric(const char *p);
diff --git a/lib/logger.c b/lib/logger.c
index 7c9e540..1f92b08 100644
--- a/lib/logger.c
+++ b/lib/logger.c
@@ -731,12 +731,12 @@ int __sd_dump_variable(const char *var)
 	sd_emerg("dump %s", var);
 	while (fgets(info, sizeof(info), f) != NULL) {
 		if (info[0] == '$') {
-			sd_emerg("%s", info);
+			sd_emerg("%s", chomp(info));
 			break;
 		}
 	}
 	while (fgets(info, sizeof(info), f) != NULL)
-		sd_emerg("%s", info);
+		sd_emerg("%s", chomp(info));
 
 	pclose(f);
 	return 0;
@@ -787,7 +787,7 @@ static int dump_stack_frames(void)
 				}
 				stack_no = no;
 				found = true;
-				sd_emerg("%s", info);
+				sd_emerg("%s", chomp(info));
 				break;
 			}
 		}
@@ -801,7 +801,7 @@ static int dump_stack_frames(void)
 		}
 
 		while (fgets(info, sizeof(info), f) != NULL)
-			sd_emerg("%s", info);
+			sd_emerg("%s", chomp(info));
 
 		pclose(f);
 	}
@@ -840,7 +840,7 @@ void sd_backtrace(void)
 			goto fallback_close;
 
 		if (info[0] != '?' && info[0] != '\0')
-			sd_emerg("%s", info);
+			sd_emerg("%s", chomp(info));
 		else
 			goto fallback_close;
 
diff --git a/lib/util.c b/lib/util.c
index 9781fd8..5454f0a 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -333,6 +333,16 @@ void pstrcpy(char *buf, int buf_size, const char *str)
 	*q = '\0';
 }
 
+/* remove a newline character from the end of a string */
+char *chomp(char *str)
+{
+	char *p = strchr(str, '\n');
+	if (p != NULL)
+		*p = '\0';
+
+	return str;
+}
+
 /* Purge directory recursively */
 int purge_directory(char *dir_path)
 {
-- 
1.7.9.5




More information about the sheepdog mailing list