V10/man/adm/man3/getfields.3

.TH GETFIELDS 3
.CT 2 data_man
.SH NAME
getfields, getmfields, setfields \(mi break a string into fields
.SH SYNOPSIS
.nf
.B int getfields(str, ptrs, nptrs)
.B char *str, **ptrs;
.PP
.B int getmfields(str, ptrs, nptrs)
.B char *str, **ptrs;
.PP
.B void setfields(fielddelim)
.B char *fielddelim;
.fi
.SH DESCRIPTION
.I Getfields
breaks the null-terminated string
.I str
into at most
.I nptrs
null-terminated fields and places pointers to the start of these fields in the array
.IR ptrs .
It returns the number of fields
and terminates the list of pointers with a zero pointer.
It overwrites some of the bytes in
.IR str .
If there are
.I nptr
or more fields, the list will not end with zero
and the last `field' will extend to the end of the
input string and may contain delimiters.
.PP
A field is defined as a maximal sequence of characters not in a set
of field delimiters.
Adjacent fields are separated by exactly one delimiter.
No field follows a delimiter at the end of string.
Thus a string of just two delimiter characters
contains two empty fields,
and a nonempty string with no delimiters contains
one field.
.PP
.I Getmfields
is the same as
.I getfields
except that fields are separated by maximal strings of
field delimiters rather than just one.
.PP
.I Setfields
makes the field delimiters (space and tab by default)
be the characters of the string
.I fielddelim.
.SH EXAMPLES
Print the words in a string, where words are non-whitespace
strings.
There is no bound on the number of words.
.EX
printwords(string)
char *string;
{
	char *ptrs[2];
	int n;
	setfields(" \et\en");		
	for(n=2; n>=2; string=ptrs[1]) {
		n = getmfields(string, ptrs, 2);		
		if(n == 0)
			break;
		if(ptrs[0][0] != 0)	/* skip initial blanks */
			printf("%s\en", ptrs[0]);		
	}
}
.EE
.SH SEE ALSO
.IR string (3)