64dd84879ed313fe7aee77fa9390a6a203283135
[mmh] / sbr / trimcpy.c
1 /*
2 ** trimcpy.c -- strip leading and trailing whitespace,
3 **           -- replace internal whitespace with spaces,
4 **           -- then return a copy.
5 **
6 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
7 ** COPYRIGHT file in the root directory of the nmh distribution for
8 ** complete copyright information.
9 */
10
11 #include <h/mh.h>
12
13
14 char *
15 trimcpy(unsigned char *cp)
16 {
17         unsigned char *sp;
18
19         /* skip over leading whitespace */
20         while (isspace(*cp))
21                 cp++;
22
23         /* start at the end and zap trailing whitespace */
24         for (sp = cp + strlen(cp) - 1; sp >= cp; sp--) {
25                 if (isspace(*sp))
26                         *sp = '\0';
27                 else
28                         break;
29         }
30
31         /* replace remaining whitespace with spaces */
32         for (sp = cp; *sp; sp++) {
33                 if (isspace(*sp))
34                         *sp = ' ';
35         }
36
37         /* now return a copy */
38         return getcpy(cp);
39 }