From 82ca189a52688029bc12fa7f3df31a40ef1ffb01 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Wed, 17 Jun 2015 23:45:39 +0200 Subject: [PATCH] Fix buggy chomp() -- Chop off only trailing newlines, all of them. Signed-off-by: Joachim Nilsson --- chomp.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/chomp.c b/chomp.c index 90695af..9f47511 100644 --- a/chomp.c +++ b/chomp.c @@ -1,6 +1,6 @@ /* Perl inspired chomp() implementation. * - * Copyright (c) 2014 Joachim Nilsson + * Copyright (c) 2014-2015 Joachim Nilsson * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -19,34 +19,46 @@ #include /** - * chomp - Perl like chomp function, chop off last char if newline. + * chomp - Perl like chomp function, chop off last char(s) if newline. * @str: String to chomp * + * This function is like Perl chomp, but it's set to chop of all + * trailing newlines. + * * Returns: * If @str is a valid pointer this function returns @str, otherwise * @errno is set to %EINVAL and this function returns %NULL. */ -char *chomp (char *str) +char *chomp(char *str) { - char *p; + char *p; - if (!str) - { - errno = EINVAL; - return NULL; - } + if (!str || strlen(str) < 1) { + errno = EINVAL; + return NULL; + } - p = strrchr (str, '\n'); - if (p) - *p = 0; + p = str + strlen(str) - 1; + while (*p == '\n') + *p-- = 0; - return str; + return str; } +#ifdef UNITTEST +#include +int main(void) +{ + int i; char t[][16] = { "hej\ndej", "Slime\n\n\\n", "Tripple\n\n\n", "" }; + for (i = 0; t[i][0]; i++) printf("'%s'\n", chomp(t[i])); return 0; +} +#endif + /** * Local Variables: + * compile-command: "gcc -DUNITTEST -o chompy chomp.c && ./chompy" * version-control: t - * indent-tabs-mode: nil - * c-file-style: "ellemtel" + * indent-tabs-mode: t + * c-file-style: "linux" * End: */