Annotation of embedaddon/pimd/libite/chomp.c, revision 1.1.1.1

1.1       misho       1: /* Perl inspired chomp() implementation.
                      2:  *
                      3:  * Copyright (c) 2014-2015  Joachim Nilsson <troglobit@gmail.com>
                      4:  *
                      5:  * Permission to use, copy, modify, and/or distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: 
                     18: #include <errno.h>
                     19: #include <string.h>
                     20: 
                     21: /**
                     22:  * chomp - Perl like chomp function, chop off last char(s) if newline.
                     23:  * @str: String to chomp
                     24:  *
                     25:  * This function is like Perl chomp, but it's set to chop of all
                     26:  * trailing newlines.
                     27:  *
                     28:  * Returns:
                     29:  * If @str is a valid pointer this function returns @str, otherwise
                     30:  * @errno is set to %EINVAL and this function returns %NULL.
                     31:  */
                     32: char *chomp(char *str)
                     33: {
                     34:        char *p;
                     35: 
                     36:        if (!str || strlen(str) < 1) {
                     37:                errno = EINVAL;
                     38:                return NULL;
                     39:        }
                     40: 
                     41:        p = str + strlen(str) - 1;
                     42:         while (*p == '\n')
                     43:                *p-- = 0;
                     44: 
                     45:        return str;
                     46: }
                     47: 
                     48: #ifdef UNITTEST
                     49: #include <stdio.h>
                     50: int main(void)
                     51: {
                     52:        int i; char t[][16] = { "hej\ndej", "Slime\n\n\\n", "Tripple\n\n\n", "" };
                     53:        for (i = 0; t[i][0]; i++) printf("[%02d]: '%s'\n", i, chomp(t[i])); return 0;
                     54: }
                     55: #endif
                     56: 
                     57: /**
                     58:  * Local Variables:
                     59:  *  compile-command: "make V=1 -f chomp.mk"
                     60:  *  version-control: t
                     61:  *  indent-tabs-mode: t
                     62:  *  c-file-style: "linux"
                     63:  * End:
                     64:  */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>