Fix buggy chomp() -- Chop off only trailing newlines, all of them.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2015-06-17 23:45:39 +02:00
1 parent 1b4dd15614
commit 82ca189a52
1 file changed
+27 -15
+27 -15
View File
@@ -1,6 +1,6 @@
/* Perl inspired chomp() implementation. /* Perl inspired chomp() implementation.
* *
* Copyright (c) 2014 Joachim Nilsson <troglobit@gmail.com> * Copyright (c) 2014-2015 Joachim Nilsson <troglobit@gmail.com>
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@@ -19,34 +19,46 @@
#include <string.h> #include <string.h>
/** /**
* 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 * @str: String to chomp
* *
* This function is like Perl chomp, but it's set to chop of all
* trailing newlines.
*
* Returns: * Returns:
* If @str is a valid pointer this function returns @str, otherwise * If @str is a valid pointer this function returns @str, otherwise
* @errno is set to %EINVAL and this function returns %NULL. * @errno is set to %EINVAL and this function returns %NULL.
*/ */
char *chomp (char *str) char *chomp(char *str)
{ {
char *p; char *p;
if (!str) if (!str || strlen(str) < 1) {
{ errno = EINVAL;
errno = EINVAL; return NULL;
return NULL; }
}
p = strrchr (str, '\n'); p = str + strlen(str) - 1;
if (p) while (*p == '\n')
*p = 0; *p-- = 0;
return str; return str;
} }
#ifdef UNITTEST
#include <stdio.h>
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: * Local Variables:
* compile-command: "gcc -DUNITTEST -o chompy chomp.c && ./chompy"
* version-control: t * version-control: t
* indent-tabs-mode: nil * indent-tabs-mode: t
* c-file-style: "ellemtel" * c-file-style: "linux"
* End: * End:
*/ */