3 * brkstring.c -- (destructively) split a string into
4 * -- an array of substrings
8 * This code is Copyright (c) 2002, by the authors of nmh. See the
9 * COPYRIGHT file in the root directory of the nmh distribution for
10 * complete copyright information.
15 /* allocate this number of pointers at a time */
18 static char **broken = NULL; /* array of substring start addresses */
19 static int len = 0; /* current size of "broken" */
24 static int brkany (char, char *);
28 brkstring (char *str, char *brksep, char *brkterm)
33 /* allocate initial space for pointers on first call */
36 if (!(broken = (char **) malloc ((size_t) (len * sizeof(*broken)))))
37 adios (NULL, "unable to malloc array in brkstring");
41 * scan string, replacing separators with zeroes
42 * and enter start addresses in "broken".
48 /* enlarge pointer array, if necessary */
51 if (!(broken = realloc (broken, (size_t) (len * sizeof(*broken)))))
52 adios (NULL, "unable to realloc array in brkstring");
55 while (brkany (c = *s, brksep))
59 * we are either at the end of the string, or the
60 * terminator found has been found, so finish up.
62 if (!c || brkany (c, brkterm)) {
68 /* set next start addr */
71 while ((c = *++s) && !brkany (c, brksep) && !brkany (c, brkterm))
75 return broken; /* NOT REACHED */
80 * If the character is in the string,
81 * return 1, else return 0.
85 brkany (char c, char *str)
90 for (s = str; *s; s++)