Use a saner variant for computing the number of groups in cpuctl tests
[ltp-debian.git] / pan / splitstr.c
blob6c4cea8a1678bc362a459f4b07047d2d5d24b25c
1 /*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
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.
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.
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.
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., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
26 * http://www.sgi.com
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
33 /* $Id: splitstr.c,v 1.2 2000/09/21 20:42:31 nstraz Exp $ */
35 * Synopsis
37 * const char **splitstr(const char *str, const char *separator, int *argcount)
39 * Description
40 * This function splits a string (str) into components that are separated by
41 * one or more of the characters in the (separator) string. An array of
42 * strings is returned, along with argcount being set to the number of strings
43 * found. Argcount can be NULL. There will always be a NULL element in the
44 * array after the last valid element. If an error occurs, NULL will be
45 * returned and argcount will be set to zero.
47 * To rid yourself of the memory allocated for splitstr(), pass the return
48 * value from splitstr() unmodified to splitstr_free():
50 * void splitstr_free( const char ** return_from_splitstr );
53 #include <stdio.h>
54 #include <malloc.h>
55 #include <string.h> /* for string functions */
56 #ifdef UNIT_TEST
57 #include <assert.h>
58 #endif /* UNIT_TEST */
59 #include "splitstr.h"
61 const char **
62 splitstr(const char *str, const char *separator, int *argcount)
64 char *arg_string =NULL,
65 **arg_array =NULL,
66 *cur_tok =NULL;
68 int num_toks =0,
69 max_toks =20,
73 * In most recoverable errors, if argcount is not NULL,
74 * set argcount to 0. Then return NULL.
76 if ( str == NULL )
78 if ( argcount != NULL )
79 *argcount = 0;
80 return(NULL);
84 * set aside temporary space to work on the string.
86 arg_string = strdup( str );
88 if ( arg_string == NULL )
90 if ( argcount != NULL )
91 *argcount = 0;
92 return(NULL);
96 * set aside an initial char ** array for string array.
98 arg_array = (char **)malloc( sizeof(char *) * max_toks );
100 if ( arg_array == NULL )
102 if ( argcount != NULL )
103 *argcount = 0;
104 return(NULL);
107 if(separator==NULL)
108 separator = " \t";
111 * Use strtok() to parse 'arg_string', placing pointers to the
112 * individual tokens into the elements of 'arg_array'. Expand
113 * 'arg_array' if necessary.
115 cur_tok = strtok(arg_string, separator);
116 while ( cur_tok != NULL )
118 arg_array[num_toks++] = cur_tok;
119 cur_tok = strtok(NULL, separator);
120 if ( num_toks == max_toks )
122 max_toks += 20;
123 arg_array = (char **)realloc((void *)arg_array, sizeof(char *)*max_toks );
126 arg_array[num_toks] = NULL;
129 * If there are any spaces left in our array, make them NULL
131 for(i=num_toks+1;i<max_toks;i++)
132 arg_array[i] = NULL;
134 /* This seems nice, but since memory is allocated on a page basis, this
135 * isn't really helpful:
136 * arg_array = (char **)realloc((void *)arg_array, sizeof(char *)*num_toks+1 );*/
138 if ( argcount != NULL )
139 *argcount = num_toks;
142 * Return the argument array.
144 return((const char **)arg_array);
148 * splitster_free( const char ** )
150 * This takes the return value from splitster() and free()s memory
151 * allocated by splitster. Assuming: ret=splitster(...), this
152 * requires that ret and *ret returned from splitster() have not
153 * been modified.
155 void splitstr_free( const char **p_return )
157 if ( *p_return != NULL )
158 free( (char *)*p_return );
159 if ( p_return != NULL )
160 free( (char **)p_return );
163 #ifdef UNIT_TEST
165 int main()
167 int i,y,test_size=1000,size_ret;
168 char test_str[32768];
169 char buf[16];
170 char *test_str_array[test_size];
171 const char **ret;
173 for(i=0;i<test_size;i++)
175 snprintf(buf,16,"arg%d",i);
176 test_str_array[i] = strdup(buf);
179 for(i=0;i<test_size;i++)
181 test_str[0]='\0';
182 for(y=0;y<i;y++)
184 snprintf(buf,16,"arg%d ",y);
185 strncat(test_str,buf,16);
187 ret = splitstr(test_str,NULL,&size_ret);
188 assert(size_ret == i);
189 for(y=0;y<i;y++)
190 assert( strcmp(ret[y],test_str_array[y])==0 );
192 splitstr_free(ret);
194 return 0;
197 #endif