1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  *	Split buffer into fields delimited by tabs and newlines.
32  *	Fill pointer array with pointers to fields.
33  * 	Return the number of fields assigned to the array[].
34  *	The remainder of the array elements point to the end of the buffer.
35  *
36  *      Note:
37  *	The delimiters are changed to null-bytes in the buffer and array of
38  *	pointers is only valid while the buffer is intact.
39  */
40 
41 #include <sys/types.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <thread.h>
45 #include <pthread.h>
46 
47 #ifndef _REENTRANT
48 static char *bsplitchar = "\t\n";	/* characters that separate fields */
49 #endif
50 
51 #ifdef _REENTRANT
52 static char **
_get_bsplitchar(thread_key_t * keyp)53 _get_bsplitchar(thread_key_t *keyp)
54 {
55 	static char *init_bsplitchar = "\t\n";
56 	char **strp;
57 
58 	if (thr_keycreate_once(keyp, free) != 0)
59 		return (NULL);
60 	strp = pthread_getspecific(*keyp);
61 	if (strp == NULL) {
62 		strp = malloc(sizeof (char *));
63 		if (thr_setspecific(*keyp, strp) != 0) {
64 			if (strp)
65 				(void) free(strp);
66 			strp = NULL;
67 		}
68 		if (strp != NULL)
69 			*strp = init_bsplitchar;
70 	}
71 	return (strp);
72 }
73 #endif /* _REENTRANT */
74 
75 size_t
bufsplit(char * buf,size_t dim,char ** array)76 bufsplit(char *buf, size_t dim, char **array)
77 {
78 #ifdef _REENTRANT
79 	static thread_key_t key = THR_ONCE_KEY;
80 	char  **bsplitchar = _get_bsplitchar(&key);
81 #define	bsplitchar (*bsplitchar)
82 #endif /* _REENTRANT */
83 
84 	unsigned numsplit;
85 	int	i;
86 
87 	if (!buf)
88 		return (0);
89 	if (!dim ^ !array)
90 		return (0);
91 	if (buf && !dim && !array) {
92 		bsplitchar = buf;
93 		return (1);
94 	}
95 	numsplit = 0;
96 	while (numsplit < dim) {
97 		array[numsplit] = buf;
98 		numsplit++;
99 		buf = strpbrk(buf, bsplitchar);
100 		if (buf)
101 			*(buf++) = '\0';
102 		else
103 			break;
104 		if (*buf == '\0') {
105 			break;
106 		}
107 	}
108 	buf = strrchr(array[numsplit-1], '\0');
109 	for (i = numsplit; i < dim; i++)
110 		array[i] = buf;
111 	return (numsplit);
112 }
113