6690 set_nfsv4_ephemeral_mount_to() tries to read AUTOMOUNT_TIMEOUT from defunct...
[unleashed.git] / usr / src / test / libc-tests / tests / regex / split.c
blob664b8375afe40bc2770ac2bc6f501a8eaecab090
1 /*
2 * Copyright (c) 1993 The NetBSD Foundation, Inc.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
15 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
27 #include <regex.h>
28 #include <stdio.h>
29 #include <string.h>
31 #include "test_regex.h"
34 * split - divide a string into fields, like awk split()
36 * returns number of fields, including overflow
38 * fields[] list is not NULL-terminated
39 * nfields number of entries available in fields[]
40 * sep "" white, "c" single char, "ab" [ab]+
42 int
43 split(char *string, char *fields[], int nfields, const char *sep)
45 char *p = string;
46 char c; /* latest character */
47 char sepc = *sep;
48 char sepc2;
49 int fn;
50 char **fp = fields;
51 const char *sepp;
52 int trimtrail;
54 /* white space */
55 if (sepc == '\0') {
56 while ((c = *p++) == ' ' || c == '\t')
57 continue;
58 p--;
59 trimtrail = 1;
60 sep = " \t"; /* note, code below knows this is 2 long */
61 sepc = ' ';
62 } else
63 trimtrail = 0;
64 sepc2 = sep[1]; /* now we can safely pick this up */
66 /* catch empties */
67 if (*p == '\0')
68 return (0);
70 /* single separator */
71 if (sepc2 == '\0') {
72 fn = nfields;
73 for (;;) {
74 *fp++ = p;
75 fn--;
76 if (fn == 0)
77 break;
78 while ((c = *p++) != sepc)
79 if (c == '\0')
80 return (nfields - fn);
81 *(p-1) = '\0';
83 /* we have overflowed the fields vector -- just count them */
84 fn = nfields;
85 for (;;) {
86 while ((c = *p++) != sepc)
87 if (c == '\0')
88 return (fn);
89 fn++;
91 /* not reached */
94 /* two separators */
95 if (sep[2] == '\0') {
96 fn = nfields;
97 for (;;) {
98 *fp++ = p;
99 fn--;
100 while ((c = *p++) != sepc && c != sepc2)
101 if (c == '\0') {
102 if (trimtrail && **(fp-1) == '\0')
103 fn++;
104 return (nfields - fn);
106 if (fn == 0)
107 break;
108 *(p-1) = '\0';
109 while ((c = *p++) == sepc || c == sepc2)
110 continue;
111 p--;
113 /* we have overflowed the fields vector -- just count them */
114 fn = nfields;
115 while (c != '\0') {
116 while ((c = *p++) == sepc || c == sepc2)
117 continue;
118 p--;
119 fn++;
120 while ((c = *p++) != '\0' && c != sepc && c != sepc2)
121 continue;
123 /* might have to trim trailing white space */
124 if (trimtrail) {
125 p--;
126 while ((c = *--p) == sepc || c == sepc2)
127 continue;
128 p++;
129 if (*p != '\0') {
130 if (fn == nfields+1)
131 *p = '\0';
132 fn--;
135 return (fn);
138 /* n separators */
139 fn = 0;
140 for (;;) {
141 if (fn < nfields)
142 *fp++ = p;
143 fn++;
144 for (;;) {
145 c = *p++;
146 if (c == '\0')
147 return (fn);
148 sepp = sep;
149 while ((sepc = *sepp++) != '\0' && sepc != c)
150 continue;
151 if (sepc != '\0') /* it was a separator */
152 break;
154 if (fn < nfields)
155 *(p-1) = '\0';
156 for (;;) {
157 c = *p++;
158 sepp = sep;
159 while ((sepc = *sepp++) != '\0' && sepc != c)
160 continue;
161 if (sepc == '\0') /* it wasn't a separator */
162 break;
164 p--;
167 /* not reached */