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