blob: f313ee61a598310fea65dd4fefb5f7b671e54cf3 [file] [log] [blame]
whrb973f2b2000-05-05 19:34:50 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
vapier45a8ba02009-07-20 10:59:32 +00003 *
whrb973f2b2000-05-05 19:34:50 +00004 * 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.
vapier45a8ba02009-07-20 10:59:32 +00007 *
whrb973f2b2000-05-05 19:34:50 +00008 * 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.
vapier45a8ba02009-07-20 10:59:32 +000011 *
whrb973f2b2000-05-05 19:34:50 +000012 * 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.
vapier45a8ba02009-07-20 10:59:32 +000018 *
whrb973f2b2000-05-05 19:34:50 +000019 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080020 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
vapier45a8ba02009-07-20 10:59:32 +000022 *
whrb973f2b2000-05-05 19:34:50 +000023 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
vapier45a8ba02009-07-20 10:59:32 +000025 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
whrb973f2b2000-05-05 19:34:50 +000030 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 */
32/**********************************************************
vapier45a8ba02009-07-20 10:59:32 +000033 *
alaffincc2e5552000-07-27 17:13:18 +000034 * OS Testing - Silicon Graphics, Inc.
vapier45a8ba02009-07-20 10:59:32 +000035 *
whrb973f2b2000-05-05 19:34:50 +000036 * FUNCTION NAME : string_to_tokens
vapier45a8ba02009-07-20 10:59:32 +000037 *
whrb973f2b2000-05-05 19:34:50 +000038 * FUNCTION TITLE : Break a string into its tokens
vapier45a8ba02009-07-20 10:59:32 +000039 *
whrb973f2b2000-05-05 19:34:50 +000040 * 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;
vapier45a8ba02009-07-20 10:59:32 +000047 *
whrb973f2b2000-05-05 19:34:50 +000048 * AUTHOR : Richard Logan
49 *
50 * DATE : 10/94
51 *
52 * INITIAL RELEASE : UNICOS 7.0
vapier45a8ba02009-07-20 10:59:32 +000053 *
whrb973f2b2000-05-05 19:34:50 +000054 * 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.
vapier45a8ba02009-07-20 10:59:32 +000067 *
whrb973f2b2000-05-05 19:34:50 +000068 * 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 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
vapier45a8ba02009-07-20 10:59:32 +000075#include <stdio.h>
Wanlong Gao354ebb42012-12-07 10:10:04 +080076#include <string.h> /* for string functions */
whrb973f2b2000-05-05 19:34:50 +000077#include "string_to_tokens.h"
78
79int
Wanlong Gao354ebb42012-12-07 10:10:04 +080080string_to_tokens(char *arg_string, char *arg_array[], int array_size,
81 char *separator)
whrb973f2b2000-05-05 19:34:50 +000082{
Wanlong Gao354ebb42012-12-07 10:10:04 +080083 int num_toks = 0; /* number of tokens found */
84 char *strtok();
vapier45a8ba02009-07-20 10:59:32 +000085
Wanlong Gao354ebb42012-12-07 10:10:04 +080086 if (arg_array == NULL || array_size <= 1 || separator == NULL)
87 return -1;
whrb973f2b2000-05-05 19:34:50 +000088
Wanlong Gao354ebb42012-12-07 10:10:04 +080089 /*
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 }
whrb973f2b2000-05-05 19:34:50 +000096
Wanlong Gao354ebb42012-12-07 10:10:04 +080097 for (num_toks = 1; num_toks < array_size; num_toks++) {
98 if ((arg_array[num_toks] = strtok(NULL, separator)) == NULL)
99 break;
100 }
whrb973f2b2000-05-05 19:34:50 +0000101
Wanlong Gao354ebb42012-12-07 10:10:04 +0800102 if (num_toks == array_size)
103 arg_array[num_toks] = NULL;
whrb973f2b2000-05-05 19:34:50 +0000104
Wanlong Gao354ebb42012-12-07 10:10:04 +0800105 /*
106 * Return the number of tokens that were found in 'arg_string'.
107 */
108 return (num_toks);
whrb973f2b2000-05-05 19:34:50 +0000109
Wanlong Gao354ebb42012-12-07 10:10:04 +0800110} /* end of string_to_tokens */