1 /*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 */
32 /**********************************************************
33 *
34 * OS Testing - Silicon Graphics, Inc.
35 *
36 * FUNCTION NAME : string_to_tokens
37 *
38 * FUNCTION TITLE : Break a string into its tokens
39 *
40 * SYNOPSIS:
41 *
42 * int string_to_tokens(arg_string, arg_array, array_size, separator)
43 * char *arg_string;
44 * char *arg_array[];
45 * int array_size;
46 * char *separator;
47 *
48 * AUTHOR : Richard Logan
49 *
50 * DATE : 10/94
51 *
52 * INITIAL RELEASE : UNICOS 7.0
53 *
54 * DESCRIPTION
55 * This function parses the string 'arg_string', placing pointers to
56 * the 'separator' separated tokens into the elements of 'arg_array'.
57 * The array is terminated with a null pointer.
58 * 'arg_array' must contains at least 'array_size' elements.
59 * Only the first 'array_size' minus one tokens will be placed into
60 * 'arg_array'. If there are more than 'array_size'-1 tokens, the rest are
61 * ignored by this routine.
62 *
63 * RETURN VALUE
64 * This function returns the number of 'separator' separated tokens that
65 * were found in 'arg_string'.
66 * If 'arg_array' or 'separator' is NULL or 'array_size' is less than 2, -1 is returned.
67 *
68 * WARNING
69 * This function uses strtok() to parse 'arg_string', and thus
70 * physically alters 'arg_string' by placing null characters where the
71 * separators originally were.
72 *
73 *
74 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
75 #include <stdio.h>
76 #include <string.h> /* for string functions */
77 #include "string_to_tokens.h"
78
79 int
string_to_tokens(char * arg_string,char * arg_array[],int array_size,char * separator)80 string_to_tokens(char *arg_string, char *arg_array[], int array_size,
81 char *separator)
82 {
83 int num_toks = 0; /* number of tokens found */
84 char *strtok();
85
86 if (arg_array == NULL || array_size <= 1 || separator == NULL)
87 return -1;
88
89 /*
90 * Use strtok() to parse 'arg_string', placing pointers to the
91 * individual tokens into the elements of 'arg_array'.
92 */
93 if ((arg_array[num_toks] = strtok(arg_string, separator)) == NULL) {
94 return 0;
95 }
96
97 for (num_toks = 1; num_toks < array_size; num_toks++) {
98 if ((arg_array[num_toks] = strtok(NULL, separator)) == NULL)
99 break;
100 }
101
102 if (num_toks == array_size)
103 arg_array[num_toks] = NULL;
104
105 /*
106 * Return the number of tokens that were found in 'arg_string'.
107 */
108 return (num_toks);
109
110 } /* end of string_to_tokens */
111