1 /* @(#)breakline.c	1.12 18/06/03 Copyright 1985, 1995-2018 J. Schilling */
2 /*
3  *	break a line pointed to by *buf into fields
4  *	returns the number of tokens, the line was broken into (>= 1)
5  *
6  *	delim is the delimiter to break at
7  *	array[0 .. found-1] point to strings from broken line
8  *	array[found ... len] point to '\0'
9  *	len is the size of the array
10  *
11  *	Copyright (c) 1985, 1995-2018 J. Schilling
12  */
13 /*
14  * The contents of this file are subject to the terms of the
15  * Common Development and Distribution License, Version 1.0 only
16  * (the "License").  You may not use this file except in compliance
17  * with the License.
18  *
19  * See the file CDDL.Schily.txt in this distribution for details.
20  * A copy of the CDDL is also available via the Internet at
21  * http://www.opensource.org/licenses/cddl1.txt
22  *
23  * When distributing Covered Code, include this CDDL HEADER in each
24  * file and include the License file CDDL.Schily.txt from this distribution.
25  */
26 
27 #include <schily/mconfig.h>
28 #include <schily/standard.h>
29 #include <schily/schily.h>
30 
31 #ifdef	PROTOTYPES
32 EXPORT int
breakline(char * buf,register char delim,register char * array[],register int len)33 breakline(char *buf,
34 		register char delim,
35 		register char *array[],
36 		register int len)
37 #else
38 EXPORT int
39 breakline(buf, delim, array, len)
40 		char	*buf;
41 	register char	delim;
42 	register char	*array[];
43 	register int	len;
44 #endif
45 {
46 	register char	*bp = buf;
47 	register char	*dp;
48 	register int	i;
49 	register int	found;
50 
51 	for (i = 0, found = 1; i < len; i++) {
52 		for (dp = bp; *dp != '\0' && *dp != delim; dp++)
53 			/* LINTED */
54 			;
55 
56 		array[i] = bp;
57 		if (*dp == delim) {
58 			*dp++ = '\0';
59 			found++;
60 		}
61 		bp = dp;
62 	}
63 	if (found > len)
64 		found = len;
65 	return (found);
66 }
67