76b5636614186528203003a28984525d06fef08a
[xfstests-dev.git] / lib / string_to_tokens.c
1 /*
2  * Copyright (c) 2000 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
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.
8  *
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.
13  *
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
17  */
18 /**********************************************************
19  * 
20  *    OS Testing - Silicon Graphics, Inc.
21  * 
22  *    FUNCTION NAME     : string_to_tokens
23  * 
24  *    FUNCTION TITLE    : Break a string into its tokens
25  * 
26  *    SYNOPSIS:
27  *
28  * int string_to_tokens(arg_string, arg_array, array_size, separator)
29  *    char *arg_string;
30  *    char *arg_array[];
31  *    int array_size;
32  *    char *separator;
33  * 
34  *    AUTHOR            : Richard Logan
35  *
36  *    DATE              : 10/94
37  *
38  *    INITIAL RELEASE   : UNICOS 7.0
39  * 
40  *    DESCRIPTION
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.
48  *
49  *    RETURN VALUE
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.
53  * 
54  *    WARNING
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.
58  *
59  *
60  *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
61 #include <stdio.h>         
62 #include <string.h>        /* for string functions */
63 #include "string_to_tokens.h"
64
65 int
66 string_to_tokens(char *arg_string, char *arg_array[], int array_size, char *separator)
67 {
68    int num_toks = 0;  /* number of tokens found */
69    char *strtok();
70         
71    if ( arg_array == NULL || array_size <= 1 || separator == NULL )
72         return -1;
73
74    /*
75     * Use strtok() to parse 'arg_string', placing pointers to the
76     * individual tokens into the elements of 'arg_array'.
77     */
78    if ( (arg_array[num_toks] = strtok(arg_string, separator)) == NULL ) {
79         return 0;
80    }
81
82    for (num_toks=1;num_toks<array_size; num_toks++) {
83         if ( (arg_array[num_toks] = strtok(NULL, separator)) == NULL )
84             break;
85    }
86
87    if ( num_toks == array_size )
88         arg_array[num_toks] = NULL;
89
90    /*
91     * Return the number of tokens that were found in 'arg_string'.
92     */
93    return(num_toks);
94
95 } /* end of string_to_tokens */