Imported from ../bash-2.05a.tar.gz.
[platform/upstream/bash.git] / lib / sh / stringvec.c
1 /* stringvec.c - function for managing arrays of strings. */
2
3 /* Copyright (C) 2000 Free Software Foundation, Inc.
4
5    This file is part of GNU Bash, the Bourne Again SHell.
6
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
10    version.
11
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
15    for more details.
16
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. */
20
21 #include <config.h>
22
23 #include <bashtypes.h>
24
25 #if defined (HAVE_UNISTD_H)
26 #  include <unistd.h>
27 #endif
28
29 #include <bashansi.h>
30 #include <stdio.h>
31 #include <chartypes.h>
32
33 #include "shell.h"
34
35 #ifdef INCLUDE_UNUSED
36 /* Find NAME in ARRAY.  Return the index of NAME, or -1 if not present.
37    ARRAY should be NULL terminated. */
38 int
39 find_name_in_array (name, array)
40      char *name, **array;
41 {
42   int i;
43
44   for (i = 0; array[i]; i++)
45     if (STREQ (name, array[i]))
46       return (i);
47
48   return (-1);
49 }
50 #endif
51
52 /* Allocate an array of strings with room for N members. */
53 char **
54 alloc_array (n)
55      int n;
56 {
57   return ((char **)xmalloc ((n) * sizeof (char *)));
58 }
59
60 /* Return the length of ARRAY, a NULL terminated array of char *. */
61 int
62 array_len (array)
63      char **array;
64 {
65   register int i;
66
67   for (i = 0; array[i]; i++);
68   return (i);
69 }
70
71 /* Free the contents of ARRAY, a NULL terminated array of char *. */
72 void
73 free_array_members (array)
74      char **array;
75 {
76   register int i;
77
78   if (array == 0)
79     return;
80
81   for (i = 0; array[i]; i++)
82     free (array[i]);
83 }
84
85 void
86 free_array (array)
87      char **array;
88 {
89   if (array == 0)
90     return;
91
92   free_array_members (array);
93   free (array);
94 }
95
96 /* Allocate and return a new copy of ARRAY and its contents. */
97 char **
98 copy_array (array)
99      char **array;
100 {
101   register int i;
102   int len;
103   char **ret;
104
105   len = array_len (array);
106
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;
111
112   return (ret);
113 }
114
115 /* Comparison routine for use with qsort() on arrays of strings.  Uses
116    strcoll(3) if available, otherwise it uses strcmp(3). */
117 int
118 qsort_string_compare (s1, s2)
119      register char **s1, **s2;
120 {
121 #if defined (HAVE_STRCOLL)
122    return (strcoll (*s1, *s2));
123 #else /* !HAVE_STRCOLL */
124   int result;
125
126   if ((result = **s1 - **s2) == 0)
127     result = strcmp (*s1, *s2);
128
129   return (result);
130 #endif /* !HAVE_STRCOLL */
131 }
132
133 /* Sort ARRAY, a null terminated array of pointers to strings. */
134 void
135 sort_char_array (array)
136      char **array;
137 {
138   qsort (array, array_len (array), sizeof (char *), (QSFUNC *)qsort_string_compare);
139 }