1 /* stringvec.c - function for managing arrays of strings. */
3 /* Copyright (C) 2000 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23 #include <bashtypes.h>
25 #if defined (HAVE_UNISTD_H)
31 #include <chartypes.h>
36 /* Find NAME in ARRAY. Return the index of NAME, or -1 if not present.
37 ARRAY should be NULL terminated. */
39 find_name_in_array (name, array)
44 for (i = 0; array[i]; i++)
45 if (STREQ (name, array[i]))
52 /* Allocate an array of strings with room for N members. */
57 return ((char **)xmalloc ((n) * sizeof (char *)));
60 /* Return the length of ARRAY, a NULL terminated array of char *. */
67 for (i = 0; array[i]; i++);
71 /* Free the contents of ARRAY, a NULL terminated array of char *. */
73 free_array_members (array)
81 for (i = 0; array[i]; i++)
92 free_array_members (array);
96 /* Allocate and return a new copy of ARRAY and its contents. */
105 len = array_len (array);
107 ret = (char **)xmalloc ((len + 1) * sizeof (char *));
108 for (i = 0; array[i]; i++)
109 ret[i] = savestring (array[i]);
110 ret[i] = (char *)NULL;
115 /* Comparison routine for use with qsort() on arrays of strings. Uses
116 strcoll(3) if available, otherwise it uses strcmp(3). */
118 qsort_string_compare (s1, s2)
119 register char **s1, **s2;
121 #if defined (HAVE_STRCOLL)
122 return (strcoll (*s1, *s2));
123 #else /* !HAVE_STRCOLL */
126 if ((result = **s1 - **s2) == 0)
127 result = strcmp (*s1, *s2);
130 #endif /* !HAVE_STRCOLL */
133 /* Sort ARRAY, a null terminated array of pointers to strings. */
135 sort_char_array (array)
138 qsort (array, array_len (array), sizeof (char *), (QSFUNC *)qsort_string_compare);