1 /* split.c
2 Split a string into tokens.
3
4 Copyright (C) 1992 Ian Lance Taylor
5
6 This file is part of the Taylor UUCP uuconf library.
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public License
10 as published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
17
18 You should have received a copy of the GNU Library General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21
22 The author of the program may be contacted at ian@airs.com.
23 */
24
25 #include "uucnfi.h"
26
27 #if USE_RCS_ID
28 const char _uuconf_split_rcsid[] = "$FreeBSD$";
29 #endif
30
31 #include <ctype.h>
32
33 /* Split a string into tokens. The bsep argument is the separator to
34 use. If it is the null byte, white space is used as the separator,
35 and leading white space is discarded. Otherwise, each occurrence
36 of the separator character delimits a field (and thus some fields
37 may be empty). The array and size arguments may be used to reuse
38 the same memory. This function is not tied to uuconf; the only way
39 it can fail is if malloc or realloc fails. */
40
41 int
_uuconf_istrsplit(zline,bsep,ppzsplit,pcsplit)42 _uuconf_istrsplit (zline, bsep, ppzsplit, pcsplit)
43 register char *zline;
44 int bsep;
45 char ***ppzsplit;
46 size_t *pcsplit;
47 {
48 size_t i;
49
50 i = 0;
51
52 while (TRUE)
53 {
54 if (bsep == '\0')
55 {
56 while (isspace (BUCHAR (*zline)))
57 ++zline;
58 if (*zline == '\0')
59 break;
60 }
61
62 if (i >= *pcsplit)
63 {
64 char **pznew;
65 size_t cnew;
66
67 if (*pcsplit == 0)
68 {
69 cnew = 8;
70 pznew = (char **) malloc (cnew * sizeof (char *));
71 }
72 else
73 {
74 cnew = *pcsplit * 2;
75 pznew = (char **) realloc ((pointer) *ppzsplit,
76 cnew * sizeof (char *));
77 }
78 if (pznew == NULL)
79 return -1;
80 *ppzsplit = pznew;
81 *pcsplit = cnew;
82 }
83
84 (*ppzsplit)[i] = zline;
85 ++i;
86
87 if (bsep == '\0')
88 {
89 while (*zline != '\0' && ! isspace (BUCHAR (*zline)))
90 ++zline;
91 }
92 else
93 {
94 while (*zline != '\0' && *zline != bsep)
95 ++zline;
96 }
97
98 if (*zline == '\0')
99 break;
100
101 *zline++ = '\0';
102 }
103
104 return i;
105 }
106