]> asedeno.scripts.mit.edu Git - PuTTY.git/commitdiff
Utility function: 'chomp'.
authorSimon Tatham <anakin@pobox.com>
Tue, 12 May 2015 09:47:33 +0000 (10:47 +0100)
committerSimon Tatham <anakin@pobox.com>
Tue, 12 May 2015 11:14:41 +0000 (12:14 +0100)
Basically like Perl's, only we forgive \r\n line endings.

misc.c
misc.h

diff --git a/misc.c b/misc.c
index f3a0eeddbe3d33a3e81de95a3d84c0d2454024eb..a53aa1274236753505f698b947cbceda819eb328 100644 (file)
--- 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 4c6bd7ff684a52e4d3c3599602b06fccb248ca8e..09c102e94e477e925ec2d5757bf33b6a7dc0f14e 100644 (file)
--- 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);