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