2 * Copyright (c) 2000 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 /**********************************************************
20 * OS Testing - Silicon Graphics, Inc.
22 * FUNCTION NAME : string_to_tokens
24 * FUNCTION TITLE : Break a string into its tokens
28 * int string_to_tokens(arg_string, arg_array, array_size, separator)
34 * AUTHOR : Richard Logan
38 * INITIAL RELEASE : UNICOS 7.0
41 * This function parses the string 'arg_string', placing pointers to
42 * the 'separator' separated tokens into the elements of 'arg_array'.
43 * The array is terminated with a null pointer.
44 * 'arg_array' must contains at least 'array_size' elements.
45 * Only the first 'array_size' minus one tokens will be placed into
46 * 'arg_array'. If there are more than 'array_size'-1 tokens, the rest are
47 * ignored by this routine.
50 * This function returns the number of 'separator' separated tokens that
51 * were found in 'arg_string'.
52 * If 'arg_array' or 'separator' is NULL or 'array_size' is less than 2, -1 is returned.
55 * This function uses strtok() to parse 'arg_string', and thus
56 * physically alters 'arg_string' by placing null characters where the
57 * separators originally were.
60 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
62 #include <string.h> /* for string functions */
63 #include "string_to_tokens.h"
66 string_to_tokens(char *arg_string, char *arg_array[], int array_size, char *separator)
68 int num_toks = 0; /* number of tokens found */
71 if ( arg_array == NULL || array_size <= 1 || separator == NULL )
75 * Use strtok() to parse 'arg_string', placing pointers to the
76 * individual tokens into the elements of 'arg_array'.
78 if ( (arg_array[num_toks] = strtok(arg_string, separator)) == NULL ) {
82 for (num_toks=1;num_toks<array_size; num_toks++) {
83 if ( (arg_array[num_toks] = strtok(NULL, separator)) == NULL )
87 if ( num_toks == array_size )
88 arg_array[num_toks] = NULL;
91 * Return the number of tokens that were found in 'arg_string'.
95 } /* end of string_to_tokens */