d13554850c9e0939807bad6c79d11e0400e2f9fb
[mmh] / sbr / trimcpy.c
1
2 /*
3  * trimcpy.c -- strip leading and trailing whitespace,
4  *           -- replace internal whitespace with spaces,
5  *           -- then return a copy.
6  *
7  * $Id$
8  */
9
10 #include <h/mh.h>
11
12
13 char *
14 trimcpy (char *cp)
15 {
16     char *sp;
17
18     /* skip over leading whitespace */
19     while (isspace(*cp))
20         cp++;
21
22     /* start at the end and zap trailing whitespace */
23     for (sp = cp + strlen(cp) - 1; sp >= cp; sp--) {
24         if (isspace(*sp))
25             *sp = '\0';
26         else
27             break;
28     }
29
30     /* replace remaining whitespace with spaces */
31     for (sp = cp; *sp; sp++) {
32         if (isspace(*sp))
33             *sp = ' ';
34     }
35
36     /* now return a copy */
37     return getcpy(cp);
38 }