2 ** brkstring.c -- (destructively) split a string into
3 ** -- an array of substrings
5 ** This code is Copyright (c) 2002, by the authors of nmh. See the
6 ** COPYRIGHT file in the root directory of the nmh distribution for
7 ** complete copyright information.
14 /* allocate this number of pointers at a time */
17 static char **broken = NULL; /* array of substring start addresses */
18 static int len = 0; /* current size of "broken" */
22 ** returns pointer to static memory
25 brkstring(char *str, char *brksep, char *brkterm)
30 /* allocate initial space for pointers on first call */
33 broken = (char **)mh_xmalloc((size_t)(len * sizeof(*broken)));
37 ** scan string, replacing separators with zeroes
38 ** and enter start addresses in "broken".
42 /* enlarge pointer array, if necessary */
45 broken = mh_xrealloc(broken,
46 (size_t)(len * sizeof(*broken)));
49 /* handle separators */
50 while ((c=*s) && brksep && strchr(brksep, c)) {
55 ** we are either at the end of the string, or the
56 ** terminator found has been found, so finish up.
58 if (!c || (brkterm && strchr(brkterm, c))) {
64 /* set next start addr and walk over word */
67 if (brksep && strchr(brksep, c)) {
70 if (brkterm && strchr(brkterm, c)) {
75 adios(EX_SOFTWARE, "brkstring()", "reached unreachable point");