btrfs: test fstrim after doing a device replace
[xfstests-dev.git] / lib / string_to_tokens.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 /**********************************************************
7  * 
8  *    OS Testing - Silicon Graphics, Inc.
9  * 
10  *    FUNCTION NAME     : string_to_tokens
11  * 
12  *    FUNCTION TITLE    : Break a string into its tokens
13  * 
14  *    SYNOPSIS:
15  *
16  * int string_to_tokens(arg_string, arg_array, array_size, separator)
17  *    char *arg_string;
18  *    char *arg_array[];
19  *    int array_size;
20  *    char *separator;
21  * 
22  *    AUTHOR            : Richard Logan
23  *
24  *    DATE              : 10/94
25  *
26  *    INITIAL RELEASE   : UNICOS 7.0
27  * 
28  *    DESCRIPTION
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.
36  *
37  *    RETURN VALUE
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.
41  * 
42  *    WARNING
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.
46  *
47  *
48  *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
49 #include <stdio.h>         
50 #include <string.h>        /* for string functions */
51 #include "string_to_tokens.h"
52
53 int
54 string_to_tokens(char *arg_string, char *arg_array[], int array_size, char *separator)
55 {
56    int num_toks = 0;  /* number of tokens found */
57    char *strtok();
58         
59    if ( arg_array == NULL || array_size <= 1 || separator == NULL )
60         return -1;
61
62    /*
63     * Use strtok() to parse 'arg_string', placing pointers to the
64     * individual tokens into the elements of 'arg_array'.
65     */
66    if ( (arg_array[num_toks] = strtok(arg_string, separator)) == NULL ) {
67         return 0;
68    }
69
70    for (num_toks=1;num_toks<array_size; num_toks++) {
71         if ( (arg_array[num_toks] = strtok(NULL, separator)) == NULL )
72             break;
73    }
74
75    if ( num_toks == array_size )
76         arg_array[num_toks] = NULL;
77
78    /*
79     * Return the number of tokens that were found in 'arg_string'.
80     */
81    return(num_toks);
82
83 } /* end of string_to_tokens */