From 6179c5cc7cf993d0e1b69d3982584d82911e6d08 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Tue, 12 May 2015 10:47:33 +0100 Subject: [PATCH] Utility function: 'chomp'. Basically like Perl's, only we forgive \r\n line endings. --- misc.c | 18 ++++++++++++++++++ misc.h | 1 + 2 files changed, 19 insertions(+) diff --git a/misc.c b/misc.c index f3a0eedd..a53aa127 100644 --- a/misc.c +++ b/misc.c @@ -473,6 +473,24 @@ char *fgetline(FILE *fp) return ret; } +/* + * Perl-style 'chomp', for a line we just read with fgetline. Unlike + * Perl chomp, however, we're deliberately forgiving of strange + * line-ending conventions. Also we forgive NULL on input, so you can + * just write 'line = chomp(fgetline(fp));' and not bother checking + * for NULL until afterwards. + */ +char *chomp(char *str) +{ + if (str) { + int len = strlen(str); + while (len > 0 && (str[len-1] == '\r' || str[len-1] == '\n')) + len--; + str[len] = '\0'; + } + return str; +} + /* ---------------------------------------------------------------------- * Core base64 encoding and decoding routines. */ diff --git a/misc.h b/misc.h index 4c6bd7ff..09c102e9 100644 --- a/misc.h +++ b/misc.h @@ -42,6 +42,7 @@ void burnstr(char *string); int toint(unsigned); char *fgetline(FILE *fp); +char *chomp(char *str); void base64_encode_atom(unsigned char *data, int n, char *out); int base64_decode_atom(char *atom, unsigned char *out); -- 2.45.2