]> diplodocus.org Git - nmh/blob - sbr/trimcpy.c
Fix stupid accidental dependence on a bash quirk in previous
[nmh] / 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 * This code is Copyright (c) 2002, by the authors of nmh. See the
10 * COPYRIGHT file in the root directory of the nmh distribution for
11 * complete copyright information.
12 */
13
14 #include <h/mh.h>
15
16
17 char *
18 trimcpy (char *cp)
19 {
20 char *sp;
21
22 /* skip over leading whitespace */
23 while (isspace(*cp))
24 cp++;
25
26 /* start at the end and zap trailing whitespace */
27 for (sp = cp + strlen(cp) - 1; sp >= cp; sp--) {
28 if (isspace(*sp))
29 *sp = '\0';
30 else
31 break;
32 }
33
34 /* replace remaining whitespace with spaces */
35 for (sp = cp; *sp; sp++) {
36 if (isspace(*sp))
37 *sp = ' ';
38 }
39
40 /* now return a copy */
41 return getcpy(cp);
42 }