1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000 Silicon Graphics, Inc.
6 /**********************************************************
8 * OS Testing - Silicon Graphics, Inc.
10 * FUNCTION NAME : string_to_tokens
12 * FUNCTION TITLE : Break a string into its tokens
16 * int string_to_tokens(arg_string, arg_array, array_size, separator)
22 * AUTHOR : Richard Logan
26 * INITIAL RELEASE : UNICOS 7.0
29 * This function parses the string 'arg_string', placing pointers to
30 * the 'separator' separated tokens into the elements of 'arg_array'.
31 * The array is terminated with a null pointer.
32 * 'arg_array' must contains at least 'array_size' elements.
33 * Only the first 'array_size' minus one tokens will be placed into
34 * 'arg_array'. If there are more than 'array_size'-1 tokens, the rest are
35 * ignored by this routine.
38 * This function returns the number of 'separator' separated tokens that
39 * were found in 'arg_string'.
40 * If 'arg_array' or 'separator' is NULL or 'array_size' is less than 2, -1 is returned.
43 * This function uses strtok() to parse 'arg_string', and thus
44 * physically alters 'arg_string' by placing null characters where the
45 * separators originally were.
48 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
50 #include <string.h> /* for string functions */
51 #include "string_to_tokens.h"
54 string_to_tokens(char *arg_string, char *arg_array[], int array_size, char *separator)
56 int num_toks = 0; /* number of tokens found */
59 if ( arg_array == NULL || array_size <= 1 || separator == NULL )
63 * Use strtok() to parse 'arg_string', placing pointers to the
64 * individual tokens into the elements of 'arg_array'.
66 if ( (arg_array[num_toks] = strtok(arg_string, separator)) == NULL ) {
70 for (num_toks=1;num_toks<array_size; num_toks++) {
71 if ( (arg_array[num_toks] = strtok(NULL, separator)) == NULL )
75 if ( num_toks == array_size )
76 arg_array[num_toks] = NULL;
79 * Return the number of tokens that were found in 'arg_string'.
83 } /* end of string_to_tokens */