]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - gnu/libexec/uucp/libuuconf/split.c
This commit was generated by cvs2svn to compensate for changes in r52894,
[FreeBSD/FreeBSD.git] / gnu / libexec / uucp / libuuconf / split.c
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 or
23    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
24    */
25
26 #include "uucnfi.h"
27
28 #if USE_RCS_ID
29 const char _uuconf_split_rcsid[] = "$FreeBSD$";
30 #endif
31
32 #include <ctype.h>
33 \f
34 /* Split a string into tokens.  The bsep argument is the separator to
35    use.  If it is the null byte, white space is used as the separator,
36    and leading white space is discarded.  Otherwise, each occurrence
37    of the separator character delimits a field (and thus some fields
38    may be empty).  The array and size arguments may be used to reuse
39    the same memory.  This function is not tied to uuconf; the only way
40    it can fail is if malloc or realloc fails.  */
41
42 int
43 _uuconf_istrsplit (zline, bsep, ppzsplit, pcsplit)
44      register char *zline;
45      int bsep;
46      char ***ppzsplit;
47      size_t *pcsplit;
48 {
49   size_t i;
50
51   i = 0;
52
53   while (TRUE)
54     {
55       if (bsep == '\0')
56         {
57           while (isspace (BUCHAR (*zline)))
58             ++zline;
59           if (*zline == '\0')
60             break;
61         }
62
63       if (i >= *pcsplit)
64         {
65           char **pznew;
66           size_t cnew;
67
68           if (*pcsplit == 0)
69             {
70               cnew = 8;
71               pznew = (char **) malloc (cnew * sizeof (char *));
72             }
73           else
74             {
75               cnew = *pcsplit * 2;
76               pznew = (char **) realloc ((pointer) *ppzsplit,
77                                          cnew * sizeof (char *));
78             }
79           if (pznew == NULL)
80             return -1;
81           *ppzsplit = pznew;
82           *pcsplit = cnew;
83         }
84
85       (*ppzsplit)[i] = zline;
86       ++i;
87
88       if (bsep == '\0')
89         {
90           while (*zline != '\0' && ! isspace (BUCHAR (*zline)))
91             ++zline;
92         }
93       else
94         {
95           while (*zline != '\0' && *zline != bsep)
96             ++zline;
97         }
98
99       if (*zline == '\0')
100         break;
101
102       *zline++ = '\0';
103     }
104
105   return i;
106 }