From: David Levine Date: Tue, 26 Feb 2013 05:02:05 +0000 (-0600) Subject: Added cpytrim() function. Unlike trimcpy(), it does not modify X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/1c570cbefde061bf4aee1d0ab99bebb9fab40846?ds=sidebyside;hp=--cc Added cpytrim() function. Unlike trimcpy(), it does not modify its argument. --- 1c570cbefde061bf4aee1d0ab99bebb9fab40846 diff --git a/h/prototypes.h b/h/prototypes.h index 2d798e20..ae6a115e 100644 --- a/h/prototypes.h +++ b/h/prototypes.h @@ -51,6 +51,7 @@ char *copy (const char *, char *); char **copyip (char **, char **, int); void cpydata (int, int, char *, char *); void cpydgst (int, int, char *, char *); +char *cpytrim (const char *); int decode_rfc2047 (char *, char *, size_t); void discard (FILE *); int default_done (int); diff --git a/sbr/trimcpy.c b/sbr/trimcpy.c index 3a2fbd76..6bcf3aec 100644 --- a/sbr/trimcpy.c +++ b/sbr/trimcpy.c @@ -10,6 +10,7 @@ */ #include +#include char * @@ -38,3 +39,36 @@ trimcpy (char *cp) /* now return a copy */ return getcpy(cp); } + + +/* + * cpytrim() -- return a copy of the argument with: + * -- stripped leading and trailing whitespace, and + * -- internal whitespace replaced with spaces. + * + * This code is Copyright (c) 2013, by the authors of nmh. See the + * COPYRIGHT file in the root directory of the nmh distribution for + * complete copyright information. + */ +char * +cpytrim (const char *sp) { + char *dp; + char *cp; + + /* skip over leading whitespace */ + while (isspace ((unsigned char) *sp)) ++sp; + + dp = add (sp, NULL); + + /* start at the end and zap trailing whitespace */ + for (cp = dp + strlen (dp) - 1; + cp >= dp && isspace ((unsigned char) *cp); + *cp-- = '\0') continue; + + /* replace remaining whitespace with spaces */ + for (cp = dp; *cp; ++cp) { + if (isspace ((unsigned char) *cp)) *cp = ' '; + } + + return dp; +} diff --git a/uip/mhlistsbr.c b/uip/mhlistsbr.c index a4e831a0..bc29243b 100644 --- a/uip/mhlistsbr.c +++ b/uip/mhlistsbr.c @@ -198,8 +198,7 @@ list_content (CT ct, int toplevel, int realsize, int verbose, int debug) if (ct->c_descr) { char *dp; - dp = trimcpy (cp = add (ct->c_descr, NULL)); - free (cp); + dp = cpytrim (ct->c_descr); printf (LSTFMT2d1, dp); free (dp); } @@ -220,8 +219,7 @@ list_content (CT ct, int toplevel, int realsize, int verbose, int debug) if (ci->ci_comment) { char *dp; - dp = trimcpy (cp = add (ci->ci_comment, NULL)); - free (cp); + dp = cpytrim (ci->ci_comment); snprintf (buffer, sizeof(buffer), "(%s)", dp); free (dp); printf (LSTFMT2d2, buffer);