OpenCoverage

array.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/bash/src/array.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/*-
2 * array.c - functions to create, destroy, access, and manipulate arrays-
3 * of strings.-
4 *-
5 * Arrays are sparse doubly-linked lists. An element's index is stored-
6 * with it.-
7 *-
8 * Chet Ramey-
9 * chet@ins.cwru.edu-
10 */-
11-
12/* Copyright (C) 1997-2016 Free Software Foundation, Inc.-
13-
14 This file is part of GNU Bash, the Bourne Again SHell.-
15-
16 Bash is free software: you can redistribute it and/or modify-
17 it under the terms of the GNU General Public License as published by-
18 the Free Software Foundation, either version 3 of the License, or-
19 (at your option) any later version.-
20-
21 Bash is distributed in the hope that it will be useful,-
22 but WITHOUT ANY WARRANTY; without even the implied warranty of-
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-
24 GNU General Public License for more details.-
25-
26 You should have received a copy of the GNU General Public License-
27 along with Bash. If not, see <http://www.gnu.org/licenses/>.-
28*/-
29-
30#include "config.h"-
31-
32#if defined (ARRAY_VARS)-
33-
34#if defined (HAVE_UNISTD_H)-
35# ifdef _MINIX-
36# include <sys/types.h>-
37# endif-
38# include <unistd.h>-
39#endif-
40-
41#include <stdio.h>-
42#include "bashansi.h"-
43-
44#include "shell.h"-
45#include "array.h"-
46#include "builtins/common.h"-
47-
48#define ADD_BEFORE(ae, new) \-
49 do { \-
50 ae->prev->next = new; \-
51 new->prev = ae->prev; \-
52 ae->prev = new; \-
53 new->next = ae; \-
54 } while(0)-
55 -
56#define ADD_AFTER(ae, new) \-
57 do { \-
58 ae->next->prev = new; \-
59 new->next = ae->next; \-
60 new->prev = ae; \-
61 ae->next = new; \-
62 } while (0)-
63-
64static char *array_to_string_internal __P((ARRAY_ELEMENT *, ARRAY_ELEMENT *, char *, int));-
65-
66static char *spacesep = " ";-
67-
68#define IS_LASTREF(a) (a->lastref)-
69-
70#define LASTREF_START(a, i) \-
71 (IS_LASTREF(a) && i >= element_index(a->lastref)) ? a->lastref \-
72 : element_forw(a->head)-
73-
74#define LASTREF(a) (a->lastref ? a->lastref : element_forw(a->head))-
75-
76#define INVALIDATE_LASTREF(a) a->lastref = 0-
77#define SET_LASTREF(a, e) a->lastref = (e)-
78#define UNSET_LASTREF(a) a->lastref = 0;-
79-
80ARRAY *-
81array_create()-
82{-
83 ARRAY *r;-
84 ARRAY_ELEMENT *head;-
85-
86 r = (ARRAY *)xmalloc(sizeof(ARRAY));-
87 r->type = array_indexed;-
88 r->max_index = -1;-
89 r->num_elements = 0;-
90 r->lastref = (ARRAY_ELEMENT *)0;-
91 head = array_create_element(-1, (char *)NULL); /* dummy head */-
92 head->prev = head->next = head;-
93 r->head = head;-
94 return(r);
executed 760887 times by 1 test: return(r);
Executed by:
  • Self test
760887
95}-
96-
97void-
98array_flush (a)-
99ARRAY *a;-
100{-
101 register ARRAY_ELEMENT *r, *r1;-
102-
103 if (a == 0)
a == 0Description
TRUEnever evaluated
FALSEevaluated 722402 times by 1 test
Evaluated by:
  • Self test
0-722402
104 return;
never executed: return;
0
105 for (r = element_forw(a->head); r != a->head; ) {
r != a->headDescription
TRUEevaluated 734452 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 722402 times by 1 test
Evaluated by:
  • Self test
722402-734452
106 r1 = element_forw(r);-
107 array_dispose_element(r);-
108 r = r1;-
109 }
executed 734452 times by 1 test: end of block
Executed by:
  • Self test
734452
110 a->head->next = a->head->prev = a->head;-
111 a->max_index = -1;-
112 a->num_elements = 0;-
113 INVALIDATE_LASTREF(a);-
114}
executed 722402 times by 1 test: end of block
Executed by:
  • Self test
722402
115-
116void-
117array_dispose(a)-
118ARRAY *a;-
119{-
120 if (a == 0)
a == 0Description
TRUEnever evaluated
FALSEevaluated 711728 times by 1 test
Evaluated by:
  • Self test
0-711728
121 return;
never executed: return;
0
122 array_flush (a);-
123 array_dispose_element(a->head);-
124 free(a);-
125}
executed 711728 times by 1 test: end of block
Executed by:
  • Self test
711728
126-
127ARRAY *-
128array_copy(a)-
129ARRAY *a;-
130{-
131 ARRAY *a1;-
132 ARRAY_ELEMENT *ae, *new;-
133-
134 if (a == 0)
a == 0Description
TRUEnever evaluated
FALSEevaluated 711168 times by 1 test
Evaluated by:
  • Self test
0-711168
135 return((ARRAY *) NULL);
never executed: return((ARRAY *) ((void *)0) );
0
136 a1 = array_create();-
137 a1->type = a->type;-
138 a1->max_index = a->max_index;-
139 a1->num_elements = a->num_elements;-
140 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
ae != a->headDescription
TRUEevaluated 712634 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 711168 times by 1 test
Evaluated by:
  • Self test
711168-712634
141 new = array_create_element(element_index(ae), element_value(ae));-
142 ADD_BEFORE(a1->head, new);-
143 if (ae == LASTREF(a))
ae == (a->last...>head)->next))Description
TRUEevaluated 711168 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1466 times by 1 test
Evaluated by:
  • Self test
a->lastrefDescription
TRUEevaluated 712634 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-712634
144 SET_LASTREF(a1, new);
executed 711168 times by 1 test: a1->lastref = (new);
Executed by:
  • Self test
711168
145 }
executed 712634 times by 1 test: end of block
Executed by:
  • Self test
712634
146 return(a1);
executed 711168 times by 1 test: return(a1);
Executed by:
  • Self test
711168
147}-
148-
149/*-
150 * Make and return a new array composed of the elements in array A from-
151 * S to E, inclusive.-
152 */-
153ARRAY *-
154array_slice(array, s, e)-
155ARRAY *array;-
156ARRAY_ELEMENT *s, *e;-
157{-
158 ARRAY *a;-
159 ARRAY_ELEMENT *p, *n;-
160 int i;-
161 arrayind_t mi;-
162-
163 a = array_create ();-
164 a->type = array->type;-
165-
166 for (mi = 0, p = s, i = 0; p != e; p = element_forw(p), i++) {
p != eDescription
TRUEevaluated 170 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 75 times by 1 test
Evaluated by:
  • Self test
75-170
167 n = array_create_element (element_index(p), element_value(p));-
168 ADD_BEFORE(a->head, n);-
169 mi = element_index(n);-
170 }
executed 170 times by 1 test: end of block
Executed by:
  • Self test
170
171 a->num_elements = i;-
172 a->max_index = mi;-
173 return a;
executed 75 times by 1 test: return a;
Executed by:
  • Self test
75
174}-
175-
176/*-
177 * Walk the array, calling FUNC once for each element, with the array-
178 * element as the argument.-
179 */-
180void-
181array_walk(a, func, udata)-
182ARRAY *a;-
183sh_ae_map_func_t *func;-
184void *udata;-
185{-
186 register ARRAY_ELEMENT *ae;-
187-
188 if (a == 0 || array_empty(a))
a == 0Description
TRUEnever evaluated
FALSEnever evaluated
((a)->num_elements == 0)Description
TRUEnever evaluated
FALSEnever evaluated
0
189 return;
never executed: return;
0
190 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae))
ae != a->headDescription
TRUEnever evaluated
FALSEnever evaluated
0
191 if ((*func)(ae, udata) < 0)
(*func)(ae, udata) < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
192 return;
never executed: return;
0
193}
never executed: end of block
0
194-
195/*-
196 * Shift the array A N elements to the left. Delete the first N elements-
197 * and subtract N from the indices of the remaining elements. If FLAGS-
198 * does not include AS_DISPOSE, this returns a singly-linked null-terminated-
199 * list of elements so the caller can dispose of the chain. If FLAGS-
200 * includes AS_DISPOSE, this function disposes of the shifted-out elements-
201 * and returns NULL.-
202 */-
203ARRAY_ELEMENT *-
204array_shift(a, n, flags)-
205ARRAY *a;-
206int n, flags;-
207{-
208 register ARRAY_ELEMENT *ae, *ret;-
209 register int i;-
210-
211 if (a == 0 || array_empty(a) || n <= 0)
a == 0Description
TRUEnever evaluated
FALSEevaluated 4917167 times by 1 test
Evaluated by:
  • Self test
((a)->num_elements == 0)Description
TRUEevaluated 15 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 4917152 times by 1 test
Evaluated by:
  • Self test
n <= 0Description
TRUEnever evaluated
FALSEevaluated 4917152 times by 1 test
Evaluated by:
  • Self test
0-4917167
212 return ((ARRAY_ELEMENT *)NULL);
executed 15 times by 1 test: return ((ARRAY_ELEMENT *) ((void *)0) );
Executed by:
  • Self test
15
213-
214 INVALIDATE_LASTREF(a);-
215 for (i = 0, ret = ae = element_forw(a->head); ae != a->head && i < n; ae = element_forw(ae), i++)
ae != a->headDescription
TRUEevaluated 9833104 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1200 times by 1 test
Evaluated by:
  • Self test
i < nDescription
TRUEevaluated 4917152 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 4915952 times by 1 test
Evaluated by:
  • Self test
1200-9833104
216 ;
executed 4917152 times by 1 test: ;
Executed by:
  • Self test
4917152
217 if (ae == a->head) {
ae == a->headDescription
TRUEevaluated 1200 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 4915952 times by 1 test
Evaluated by:
  • Self test
1200-4915952
218 /* Easy case; shifting out all of the elements */-
219 if (flags & AS_DISPOSE) {
flags & 0x01Description
TRUEnever evaluated
FALSEevaluated 1200 times by 1 test
Evaluated by:
  • Self test
0-1200
220 array_flush (a);-
221 return ((ARRAY_ELEMENT *)NULL);
never executed: return ((ARRAY_ELEMENT *) ((void *)0) );
0
222 }-
223 for (ae = ret; element_forw(ae) != a->head; ae = element_forw(ae))
((ae)->next) != a->headDescription
TRUEnever evaluated
FALSEevaluated 1200 times by 1 test
Evaluated by:
  • Self test
0-1200
224 ;
never executed: ;
0
225 element_forw(ae) = (ARRAY_ELEMENT *)NULL;-
226 a->head->next = a->head->prev = a->head;-
227 a->max_index = -1;-
228 a->num_elements = 0;-
229 return ret;
executed 1200 times by 1 test: return ret;
Executed by:
  • Self test
1200
230 }-
231 /*-
232 * ae now points to the list of elements we want to retain.-
233 * ret points to the list we want to either destroy or return.-
234 */-
235 ae->prev->next = (ARRAY_ELEMENT *)NULL; /* null-terminate RET */-
236-
237 a->head->next = ae; /* slice RET out of the array */-
238 ae->prev = a->head;-
239-
240 for ( ; ae != a->head; ae = element_forw(ae))
ae != a->headDescription
TRUEevaluated 5334252 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 4915952 times by 1 test
Evaluated by:
  • Self test
4915952-5334252
241 element_index(ae) -= n; /* renumber retained indices */
executed 5334252 times by 1 test: ((ae)->ind) -= n;
Executed by:
  • Self test
5334252
242-
243 a->num_elements -= n; /* modify bookkeeping information */-
244 a->max_index = element_index(a->head->prev);-
245-
246 if (flags & AS_DISPOSE) {
flags & 0x01Description
TRUEnever evaluated
FALSEevaluated 4915952 times by 1 test
Evaluated by:
  • Self test
0-4915952
247 for (ae = ret; ae; ) {
aeDescription
TRUEnever evaluated
FALSEnever evaluated
0
248 ret = element_forw(ae);-
249 array_dispose_element(ae);-
250 ae = ret;-
251 }
never executed: end of block
0
252 return ((ARRAY_ELEMENT *)NULL);
never executed: return ((ARRAY_ELEMENT *) ((void *)0) );
0
253 }-
254-
255 return ret;
executed 4915952 times by 1 test: return ret;
Executed by:
  • Self test
4915952
256}-
257-
258/*-
259 * Shift array A right N indices. If S is non-null, it becomes the value of-
260 * the new element 0. Returns the number of elements in the array after the-
261 * shift.-
262 */-
263int-
264array_rshift (a, n, s)-
265ARRAY *a;-
266int n;-
267char *s;-
268{-
269 register ARRAY_ELEMENT *ae, *new;-
270-
271 if (a == 0 || (array_empty(a) && s == 0))
a == 0Description
TRUEnever evaluated
FALSEevaluated 4945747 times by 1 test
Evaluated by:
  • Self test
((a)->num_elements == 0)Description
TRUEevaluated 17028 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 4928719 times by 1 test
Evaluated by:
  • Self test
s == 0Description
TRUEnever evaluated
FALSEevaluated 17028 times by 1 test
Evaluated by:
  • Self test
0-4945747
272 return 0;
never executed: return 0;
0
273 else if (n <= 0)
n <= 0Description
TRUEnever evaluated
FALSEevaluated 4945747 times by 1 test
Evaluated by:
  • Self test
0-4945747
274 return (a->num_elements);
never executed: return (a->num_elements);
0
275-
276 ae = element_forw(a->head);-
277 if (s) {
sDescription
TRUEevaluated 4945747 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-4945747
278 new = array_create_element(0, s);-
279 ADD_BEFORE(ae, new);-
280 a->num_elements++;-
281 if (array_num_elements(a) == 1) { /* array was empty */
((a)->num_elements) == 1Description
TRUEevaluated 17028 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 4928719 times by 1 test
Evaluated by:
  • Self test
17028-4928719
282 a->max_index = 0;-
283 return 1;
executed 17028 times by 1 test: return 1;
Executed by:
  • Self test
17028
284 }-
285 }
executed 4928719 times by 1 test: end of block
Executed by:
  • Self test
4928719
286-
287 /*-
288 * Renumber all elements in the array except the one we just added.-
289 */-
290 for ( ; ae != a->head; ae = element_forw(ae))
ae != a->headDescription
TRUEevaluated 5419831 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 4928719 times by 1 test
Evaluated by:
  • Self test
4928719-5419831
291 element_index(ae) += n;
executed 5419831 times by 1 test: ((ae)->ind) += n;
Executed by:
  • Self test
5419831
292-
293 a->max_index = element_index(a->head->prev);-
294-
295 INVALIDATE_LASTREF(a);-
296 return (a->num_elements);
executed 4928719 times by 1 test: return (a->num_elements);
Executed by:
  • Self test
4928719
297}-
298-
299ARRAY_ELEMENT *-
300array_unshift_element(a)-
301ARRAY *a;-
302{-
303 return (array_shift (a, 1, 0));
never executed: return (array_shift (a, 1, 0));
0
304}-
305-
306int-
307array_shift_element(a, v)-
308ARRAY *a;-
309char *v;-
310{-
311 return (array_rshift (a, 1, v));
never executed: return (array_rshift (a, 1, v));
0
312}-
313-
314ARRAY *-
315array_quote(array)-
316ARRAY *array;-
317{-
318 ARRAY_ELEMENT *a;-
319 char *t;-
320-
321 if (array == 0 || array_head(array) == 0 || array_empty(array))
array == 0Description
TRUEnever evaluated
FALSEevaluated 78 times by 1 test
Evaluated by:
  • Self test
((array)->head) == 0Description
TRUEnever evaluated
FALSEevaluated 78 times by 1 test
Evaluated by:
  • Self test
((array)->num_elements == 0)Description
TRUEnever evaluated
FALSEevaluated 78 times by 1 test
Evaluated by:
  • Self test
0-78
322 return (ARRAY *)NULL;
never executed: return (ARRAY *) ((void *)0) ;
0
323 for (a = element_forw(array->head); a != array->head; a = element_forw(a)) {
a != array->headDescription
TRUEevaluated 237 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 78 times by 1 test
Evaluated by:
  • Self test
78-237
324 t = quote_string (a->value);-
325 FREE(a->value);
executed 237 times by 1 test: sh_xfree((a->value), "array.c", 325);
Executed by:
  • Self test
a->valueDescription
TRUEevaluated 237 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-237
326 a->value = t;-
327 }
executed 237 times by 1 test: end of block
Executed by:
  • Self test
237
328 return array;
executed 78 times by 1 test: return array;
Executed by:
  • Self test
78
329}-
330-
331ARRAY *-
332array_quote_escapes(array)-
333ARRAY *array;-
334{-
335 ARRAY_ELEMENT *a;-
336 char *t;-
337-
338 if (array == 0 || array_head(array) == 0 || array_empty(array))
array == 0Description
TRUEnever evaluated
FALSEevaluated 146 times by 1 test
Evaluated by:
  • Self test
((array)->head) == 0Description
TRUEnever evaluated
FALSEevaluated 146 times by 1 test
Evaluated by:
  • Self test
((array)->num_elements == 0)Description
TRUEnever evaluated
FALSEevaluated 146 times by 1 test
Evaluated by:
  • Self test
0-146
339 return (ARRAY *)NULL;
never executed: return (ARRAY *) ((void *)0) ;
0
340 for (a = element_forw(array->head); a != array->head; a = element_forw(a)) {
a != array->headDescription
TRUEevaluated 732 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 146 times by 1 test
Evaluated by:
  • Self test
146-732
341 t = quote_escapes (a->value);-
342 FREE(a->value);
executed 732 times by 1 test: sh_xfree((a->value), "array.c", 342);
Executed by:
  • Self test
a->valueDescription
TRUEevaluated 732 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-732
343 a->value = t;-
344 }
executed 732 times by 1 test: end of block
Executed by:
  • Self test
732
345 return array;
executed 146 times by 1 test: return array;
Executed by:
  • Self test
146
346}-
347-
348ARRAY *-
349array_dequote(array)-
350ARRAY *array;-
351{-
352 ARRAY_ELEMENT *a;-
353 char *t;-
354-
355 if (array == 0 || array_head(array) == 0 || array_empty(array))
array == 0Description
TRUEnever evaluated
FALSEnever evaluated
((array)->head) == 0Description
TRUEnever evaluated
FALSEnever evaluated
((array)->num_elements == 0)Description
TRUEnever evaluated
FALSEnever evaluated
0
356 return (ARRAY *)NULL;
never executed: return (ARRAY *) ((void *)0) ;
0
357 for (a = element_forw(array->head); a != array->head; a = element_forw(a)) {
a != array->headDescription
TRUEnever evaluated
FALSEnever evaluated
0
358 t = dequote_string (a->value);-
359 FREE(a->value);
never executed: sh_xfree((a->value), "array.c", 359);
a->valueDescription
TRUEnever evaluated
FALSEnever evaluated
0
360 a->value = t;-
361 }
never executed: end of block
0
362 return array;
never executed: return array;
0
363}-
364-
365ARRAY *-
366array_dequote_escapes(array)-
367ARRAY *array;-
368{-
369 ARRAY_ELEMENT *a;-
370 char *t;-
371-
372 if (array == 0 || array_head(array) == 0 || array_empty(array))
array == 0Description
TRUEnever evaluated
FALSEnever evaluated
((array)->head) == 0Description
TRUEnever evaluated
FALSEnever evaluated
((array)->num_elements == 0)Description
TRUEnever evaluated
FALSEnever evaluated
0
373 return (ARRAY *)NULL;
never executed: return (ARRAY *) ((void *)0) ;
0
374 for (a = element_forw(array->head); a != array->head; a = element_forw(a)) {
a != array->headDescription
TRUEnever evaluated
FALSEnever evaluated
0
375 t = dequote_escapes (a->value);-
376 FREE(a->value);
never executed: sh_xfree((a->value), "array.c", 376);
a->valueDescription
TRUEnever evaluated
FALSEnever evaluated
0
377 a->value = t;-
378 }
never executed: end of block
0
379 return array;
never executed: return array;
0
380}-
381-
382ARRAY *-
383array_remove_quoted_nulls(array)-
384ARRAY *array;-
385{-
386 ARRAY_ELEMENT *a;-
387-
388 if (array == 0 || array_head(array) == 0 || array_empty(array))
array == 0Description
TRUEnever evaluated
FALSEevaluated 29 times by 1 test
Evaluated by:
  • Self test
((array)->head) == 0Description
TRUEnever evaluated
FALSEevaluated 29 times by 1 test
Evaluated by:
  • Self test
((array)->num_elements == 0)Description
TRUEnever evaluated
FALSEevaluated 29 times by 1 test
Evaluated by:
  • Self test
0-29
389 return (ARRAY *)NULL;
never executed: return (ARRAY *) ((void *)0) ;
0
390 for (a = element_forw(array->head); a != array->head; a = element_forw(a))
a != array->headDescription
TRUEevaluated 200 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 29 times by 1 test
Evaluated by:
  • Self test
29-200
391 a->value = remove_quoted_nulls (a->value);
executed 200 times by 1 test: a->value = remove_quoted_nulls (a->value);
Executed by:
  • Self test
200
392 return array;
executed 29 times by 1 test: return array;
Executed by:
  • Self test
29
393}-
394-
395/*-
396 * Return a string whose elements are the members of array A beginning at-
397 * index START and spanning NELEM members. Null elements are counted.-
398 * Since arrays are sparse, unset array elements are not counted.-
399 */-
400char *-
401array_subrange (a, start, nelem, starsub, quoted)-
402ARRAY *a;-
403arrayind_t start, nelem;-
404int starsub, quoted;-
405{-
406 ARRAY *a2;-
407 ARRAY_ELEMENT *h, *p;-
408 arrayind_t i;-
409 char *ifs, *sifs, *t;-
410 int slen;-
411-
412 p = a ? array_head (a) : 0;
aDescription
TRUEevaluated 75 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-75
413 if (p == 0 || array_empty (a) || start > array_max_index(a))
p == 0Description
TRUEnever evaluated
FALSEevaluated 75 times by 1 test
Evaluated by:
  • Self test
((a)->num_elements == 0)Description
TRUEnever evaluated
FALSEevaluated 75 times by 1 test
Evaluated by:
  • Self test
start > ((a)->max_index)Description
TRUEnever evaluated
FALSEevaluated 75 times by 1 test
Evaluated by:
  • Self test
0-75
414 return ((char *)NULL);
never executed: return ((char *) ((void *)0) );
0
415-
416 /*-
417 * Find element with index START. If START corresponds to an unset-
418 * element (arrays can be sparse), use the first element whose index-
419 * is >= START. If START is < 0, we count START indices back from-
420 * the end of A (not elements, even with sparse arrays -- START is an-
421 * index).-
422 */-
423 for (p = element_forw(p); p != array_head(a) && start > element_index(p); p = element_forw(p))
p != ((a)->head)Description
TRUEevaluated 194 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
start > ((p)->ind)Description
TRUEevaluated 119 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 75 times by 1 test
Evaluated by:
  • Self test
0-194
424 ;
executed 119 times by 1 test: ;
Executed by:
  • Self test
119
425-
426 if (p == a->head)
p == a->headDescription
TRUEnever evaluated
FALSEevaluated 75 times by 1 test
Evaluated by:
  • Self test
0-75
427 return ((char *)NULL);
never executed: return ((char *) ((void *)0) );
0
428-
429 /* Starting at P, take NELEM elements, inclusive. */-
430 for (i = 0, h = p; p != a->head && i < nelem; i++, p = element_forw(p))
p != a->headDescription
TRUEevaluated 187 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 58 times by 1 test
Evaluated by:
  • Self test
i < nelemDescription
TRUEevaluated 170 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 17 times by 1 test
Evaluated by:
  • Self test
17-187
431 ;
executed 170 times by 1 test: ;
Executed by:
  • Self test
170
432-
433 a2 = array_slice(a, h, p);-
434-
435 if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))
quoted & (0x001|0x002)Description
TRUEevaluated 27 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 48 times by 1 test
Evaluated by:
  • Self test
27-48
436 array_quote(a2);
executed 27 times by 1 test: array_quote(a2);
Executed by:
  • Self test
27
437 else-
438 array_quote_escapes(a2);
executed 48 times by 1 test: array_quote_escapes(a2);
Executed by:
  • Self test
48
439-
440 if (starsub && (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT))) {
starsubDescription
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 70 times by 1 test
Evaluated by:
  • Self test
(quoted & (0x001|0x002))Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 2 times by 1 test
Evaluated by:
  • Self test
2-70
441 /* ${array[*]} */-
442 array_remove_quoted_nulls (a2);-
443 sifs = ifs_firstchar ((int *)NULL);-
444 t = array_to_string (a2, sifs, 0);-
445 free (sifs);-
446 } else if (quoted & (Q_DOUBLE_QUOTES|Q_HERE_DOCUMENT)) {
executed 3 times by 1 test: end of block
Executed by:
  • Self test
quoted & (0x001|0x002)Description
TRUEevaluated 24 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 48 times by 1 test
Evaluated by:
  • Self test
3-48
447 /* ${array[@]} */-
448 sifs = ifs_firstchar (&slen);-
449 ifs = getifs ();-
450 if (ifs == 0 || *ifs == 0) {
ifs == 0Description
TRUEnever evaluated
FALSEevaluated 24 times by 1 test
Evaluated by:
  • Self test
*ifs == 0Description
TRUEevaluated 4 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 20 times by 1 test
Evaluated by:
  • Self test
0-24
451 if (slen < 2)
slen < 2Description
TRUEevaluated 4 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-4
452 sifs = xrealloc(sifs, 2);
executed 4 times by 1 test: sifs = sh_xrealloc((sifs), (2), "array.c", 452);
Executed by:
  • Self test
4
453 sifs[0] = ' ';-
454 sifs[1] = '\0';-
455 }
executed 4 times by 1 test: end of block
Executed by:
  • Self test
4
456 t = array_to_string (a2, sifs, 0);-
457 free (sifs);-
458 } else
executed 24 times by 1 test: end of block
Executed by:
  • Self test
24
459 t = array_to_string (a2, " ", 0);
executed 48 times by 1 test: t = array_to_string (a2, " ", 0);
Executed by:
  • Self test
48
460 array_dispose(a2);-
461-
462 return t;
executed 75 times by 1 test: return t;
Executed by:
  • Self test
75
463}-
464-
465char *-
466array_patsub (a, pat, rep, mflags)-
467ARRAY *a;-
468char *pat, *rep;-
469int mflags;-
470{-
471 ARRAY *a2;-
472 ARRAY_ELEMENT *e;-
473 char *t, *sifs, *ifs;-
474 int slen;-
475-
476 if (a == 0 || array_head(a) == 0 || array_empty(a))
a == 0Description
TRUEnever evaluated
FALSEevaluated 138 times by 1 test
Evaluated by:
  • Self test
((a)->head) == 0Description
TRUEnever evaluated
FALSEevaluated 138 times by 1 test
Evaluated by:
  • Self test
((a)->num_elements == 0)Description
TRUEnever evaluated
FALSEevaluated 138 times by 1 test
Evaluated by:
  • Self test
0-138
477 return ((char *)NULL);
never executed: return ((char *) ((void *)0) );
0
478-
479 a2 = array_copy(a);-
480 for (e = element_forw(a2->head); e != a2->head; e = element_forw(e)) {
e != a2->headDescription
TRUEevaluated 770 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 138 times by 1 test
Evaluated by:
  • Self test
138-770
481 t = pat_subst(element_value(e), pat, rep, mflags);-
482 FREE(element_value(e));
executed 770 times by 1 test: sh_xfree((((e)->value)), "array.c", 482);
Executed by:
  • Self test
((e)->value)Description
TRUEevaluated 770 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-770
483 e->value = t;-
484 }
executed 770 times by 1 test: end of block
Executed by:
  • Self test
770
485-
486 if (mflags & MATCH_QUOTED)
mflags & 0x020Description
TRUEevaluated 46 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 92 times by 1 test
Evaluated by:
  • Self test
46-92
487 array_quote(a2);
executed 46 times by 1 test: array_quote(a2);
Executed by:
  • Self test
46
488 else-
489 array_quote_escapes(a2);
executed 92 times by 1 test: array_quote_escapes(a2);
Executed by:
  • Self test
92
490-
491 if (mflags & MATCH_STARSUB) {
mflags & 0x080Description
TRUEevaluated 24 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 114 times by 1 test
Evaluated by:
  • Self test
24-114
492 array_remove_quoted_nulls (a2);-
493 if ((mflags & MATCH_QUOTED) == 0 && ifs_is_null)
(mflags & 0x020) == 0Description
TRUEevaluated 21 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 3 times by 1 test
Evaluated by:
  • Self test
ifs_is_nullDescription
TRUEnever evaluated
FALSEevaluated 21 times by 1 test
Evaluated by:
  • Self test
0-21
494 sifs = spacesep;
never executed: sifs = spacesep;
0
495 else-
496 sifs = ifs_firstchar((int *)NULL);
executed 24 times by 1 test: sifs = ifs_firstchar((int *) ((void *)0) );
Executed by:
  • Self test
24
497 t = array_to_string (a2, sifs, 0);-
498 if (sifs != spacesep)
sifs != spacesepDescription
TRUEevaluated 24 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-24
499 free(sifs);
executed 24 times by 1 test: sh_xfree((sifs), "array.c", 499);
Executed by:
  • Self test
24
500 } else if (mflags & MATCH_QUOTED) {
executed 24 times by 1 test: end of block
Executed by:
  • Self test
mflags & 0x020Description
TRUEevaluated 43 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 71 times by 1 test
Evaluated by:
  • Self test
24-71
501 /* ${array[@]} */-
502 sifs = ifs_firstchar (&slen);-
503 ifs = getifs ();-
504 if (ifs == 0 || *ifs == 0) {
ifs == 0Description
TRUEnever evaluated
FALSEevaluated 43 times by 1 test
Evaluated by:
  • Self test
*ifs == 0Description
TRUEnever evaluated
FALSEevaluated 43 times by 1 test
Evaluated by:
  • Self test
0-43
505 if (slen < 2)
slen < 2Description
TRUEnever evaluated
FALSEnever evaluated
0
506 sifs = xrealloc (sifs, 2);
never executed: sifs = sh_xrealloc((sifs), (2), "array.c", 506);
0
507 sifs[0] = ' ';-
508 sifs[1] = '\0';-
509 }
never executed: end of block
0
510 t = array_to_string (a2, sifs, 0);-
511 free(sifs);-
512 } else
executed 43 times by 1 test: end of block
Executed by:
  • Self test
43
513 t = array_to_string (a2, " ", 0);
executed 71 times by 1 test: t = array_to_string (a2, " ", 0);
Executed by:
  • Self test
71
514 array_dispose (a2);-
515-
516 return t;
executed 138 times by 1 test: return t;
Executed by:
  • Self test
138
517}-
518-
519char *-
520array_modcase (a, pat, modop, mflags)-
521ARRAY *a;-
522char *pat;-
523int modop;-
524int mflags;-
525{-
526 ARRAY *a2;-
527 ARRAY_ELEMENT *e;-
528 char *t, *sifs, *ifs;-
529 int slen;-
530-
531 if (a == 0 || array_head(a) == 0 || array_empty(a))
a == 0Description
TRUEnever evaluated
FALSEevaluated 11 times by 1 test
Evaluated by:
  • Self test
((a)->head) == 0Description
TRUEnever evaluated
FALSEevaluated 11 times by 1 test
Evaluated by:
  • Self test
((a)->num_elements == 0)Description
TRUEnever evaluated
FALSEevaluated 11 times by 1 test
Evaluated by:
  • Self test
0-11
532 return ((char *)NULL);
never executed: return ((char *) ((void *)0) );
0
533-
534 a2 = array_copy(a);-
535 for (e = element_forw(a2->head); e != a2->head; e = element_forw(e)) {
e != a2->headDescription
TRUEevaluated 29 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 11 times by 1 test
Evaluated by:
  • Self test
11-29
536 t = sh_modcase(element_value(e), pat, modop);-
537 FREE(element_value(e));
executed 29 times by 1 test: sh_xfree((((e)->value)), "array.c", 537);
Executed by:
  • Self test
((e)->value)Description
TRUEevaluated 29 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-29
538 e->value = t;-
539 }
executed 29 times by 1 test: end of block
Executed by:
  • Self test
29
540-
541 if (mflags & MATCH_QUOTED)
mflags & 0x020Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 6 times by 1 test
Evaluated by:
  • Self test
5-6
542 array_quote(a2);
executed 5 times by 1 test: array_quote(a2);
Executed by:
  • Self test
5
543 else-
544 array_quote_escapes(a2);
executed 6 times by 1 test: array_quote_escapes(a2);
Executed by:
  • Self test
6
545-
546 if (mflags & MATCH_STARSUB) {
mflags & 0x080Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 9 times by 1 test
Evaluated by:
  • Self test
2-9
547 array_remove_quoted_nulls (a2);-
548 if ((mflags & MATCH_QUOTED) == 0 && ifs_is_null)
(mflags & 0x020) == 0Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • Self test
ifs_is_nullDescription
TRUEnever evaluated
FALSEnever evaluated
0-2
549 sifs = spacesep;
never executed: sifs = spacesep;
0
550 else-
551 sifs = ifs_firstchar((int *)NULL);
executed 2 times by 1 test: sifs = ifs_firstchar((int *) ((void *)0) );
Executed by:
  • Self test
2
552 t = array_to_string (a2, sifs, 0);-
553 if (sifs != spacesep)
sifs != spacesepDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-2
554 free(sifs);
executed 2 times by 1 test: sh_xfree((sifs), "array.c", 554);
Executed by:
  • Self test
2
555 } else if (mflags & MATCH_QUOTED) {
executed 2 times by 1 test: end of block
Executed by:
  • Self test
mflags & 0x020Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 6 times by 1 test
Evaluated by:
  • Self test
2-6
556 /* ${array[@]} */-
557 sifs = ifs_firstchar (&slen);-
558 ifs = getifs ();-
559 if (ifs == 0 || *ifs == 0) {
ifs == 0Description
TRUEnever evaluated
FALSEevaluated 3 times by 1 test
Evaluated by:
  • Self test
*ifs == 0Description
TRUEnever evaluated
FALSEevaluated 3 times by 1 test
Evaluated by:
  • Self test
0-3
560 if (slen < 2)
slen < 2Description
TRUEnever evaluated
FALSEnever evaluated
0
561 sifs = xrealloc (sifs, 2);
never executed: sifs = sh_xrealloc((sifs), (2), "array.c", 561);
0
562 sifs[0] = ' ';-
563 sifs[1] = '\0';-
564 }
never executed: end of block
0
565 t = array_to_string (a2, sifs, 0);-
566 free(sifs);-
567 } else
executed 3 times by 1 test: end of block
Executed by:
  • Self test
3
568 t = array_to_string (a2, " ", 0);
executed 6 times by 1 test: t = array_to_string (a2, " ", 0);
Executed by:
  • Self test
6
569 array_dispose (a2);-
570-
571 return t;
executed 11 times by 1 test: return t;
Executed by:
  • Self test
11
572}-
573/*-
574 * Allocate and return a new array element with index INDEX and value-
575 * VALUE.-
576 */-
577ARRAY_ELEMENT *-
578array_create_element(indx, value)-
579arrayind_t indx;-
580char *value;-
581{-
582 ARRAY_ELEMENT *r;-
583-
584 r = (ARRAY_ELEMENT *)xmalloc(sizeof(ARRAY_ELEMENT));-
585 r->ind = indx;-
586 r->value = value ? savestring(value) : (char *)NULL;
valueDescription
TRUEevaluated 5732043 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 760887 times by 1 test
Evaluated by:
  • Self test
760887-5732043
587 r->next = r->prev = (ARRAY_ELEMENT *) NULL;-
588 return(r);
executed 6492930 times by 1 test: return(r);
Executed by:
  • Self test
6492930
589}-
590-
591#ifdef INCLUDE_UNUSED-
592ARRAY_ELEMENT *-
593array_copy_element(ae)-
594ARRAY_ELEMENT *ae;-
595{-
596 return(ae ? array_create_element(element_index(ae), element_value(ae))-
597 : (ARRAY_ELEMENT *) NULL);-
598}-
599#endif-
600-
601void-
602array_dispose_element(ae)-
603ARRAY_ELEMENT *ae;-
604{-
605 if (ae) {
aeDescription
TRUEevaluated 6373478 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 15 times by 1 test
Evaluated by:
  • Self test
15-6373478
606 FREE(ae->value);
executed 5651639 times by 1 test: sh_xfree((ae->value), "array.c", 606);
Executed by:
  • Self test
ae->valueDescription
TRUEevaluated 5651639 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 721839 times by 1 test
Evaluated by:
  • Self test
721839-5651639
607 free(ae);-
608 }
executed 6373478 times by 1 test: end of block
Executed by:
  • Self test
6373478
609}
executed 6373493 times by 1 test: end of block
Executed by:
  • Self test
6373493
610-
611/*-
612 * Add a new element with index I and value V to array A (a[i] = v).-
613 */-
614int-
615array_insert(a, i, v)-
616ARRAY *a;-
617arrayind_t i;-
618char *v;-
619{-
620 register ARRAY_ELEMENT *new, *ae, *start;-
621 arrayind_t startind;-
622 int direction;-
623-
624 if (a == 0)
a == 0Description
TRUEnever evaluated
FALSEevaluated 73492 times by 1 test
Evaluated by:
  • Self test
0-73492
625 return(-1);
never executed: return(-1);
0
626 new = array_create_element(i, v);-
627 if (i > array_max_index(a)) {
i > ((a)->max_index)Description
TRUEevaluated 63345 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 10147 times by 1 test
Evaluated by:
  • Self test
10147-63345
628 /*-
629 * Hook onto the end. This also works for an empty array.-
630 * Fast path for the common case of allocating arrays-
631 * sequentially.-
632 */-
633 ADD_BEFORE(a->head, new);-
634 a->max_index = i;-
635 a->num_elements++;-
636 SET_LASTREF(a, new);-
637 return(0);
executed 63345 times by 1 test: return(0);
Executed by:
  • Self test
63345
638 } else if (i < array_first_index(a)) {
i < ((a)->head->next->ind)Description
TRUEevaluated 8 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 10139 times by 1 test
Evaluated by:
  • Self test
8-10139
639 /* Hook at the beginning */-
640 ADD_AFTER(a->head, new);-
641 a->num_elements++;-
642 SET_LASTREF(a, new);-
643 return(0);
executed 8 times by 1 test: return(0);
Executed by:
  • Self test
8
644 }-
645#if OPTIMIZE_SEQUENTIAL_ARRAY_ASSIGNMENT-
646 /*-
647 * Otherwise we search for the spot to insert it. The lastref-
648 * handle optimizes the case of sequential or almost-sequential-
649 * assignments that are not at the end of the array.-
650 */-
651 start = LASTREF(a);
a->lastrefDescription
TRUEevaluated 10139 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-10139
652 /* Use same strategy as array_reference to avoid paying large penalty-
653 for semi-random assignment pattern. */-
654 startind = element_index(start);-
655 if (i < startind/2) {
i < startind/2Description
TRUEevaluated 18 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 10121 times by 1 test
Evaluated by:
  • Self test
18-10121
656 start = element_forw(a->head);-
657 startind = element_index(start);-
658 direction = 1;-
659 } else if (i >= startind) {
executed 18 times by 1 test: end of block
Executed by:
  • Self test
i >= startindDescription
TRUEevaluated 10089 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 32 times by 1 test
Evaluated by:
  • Self test
18-10089
660 direction = 1;-
661 } else {
executed 10089 times by 1 test: end of block
Executed by:
  • Self test
10089
662 direction = -1;-
663 }
executed 32 times by 1 test: end of block
Executed by:
  • Self test
32
664#else-
665 start = element_forw(ae->head);-
666 startind = element_index(start);-
667 direction = 1;-
668#endif-
669 for (ae = start; ae != a->head; ) {
ae != a->headDescription
TRUEevaluated 10256 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-10256
670 if (element_index(ae) == i) {
((ae)->ind) == iDescription
TRUEevaluated 10111 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 145 times by 1 test
Evaluated by:
  • Self test
145-10111
671 /*-
672 * Replacing an existing element.-
673 */-
674 free(element_value(ae));-
675 /* Just swap in the new value */-
676 ae->value = new->value;-
677 new->value = 0;-
678 array_dispose_element(new);-
679 SET_LASTREF(a, ae);-
680 return(0);
executed 10111 times by 1 test: return(0);
Executed by:
  • Self test
10111
681 } else if (direction == 1 && element_index(ae) > i) {
direction == 1Description
TRUEevaluated 88 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 57 times by 1 test
Evaluated by:
  • Self test
((ae)->ind) > iDescription
TRUEevaluated 6 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 82 times by 1 test
Evaluated by:
  • Self test
6-88
682 ADD_BEFORE(ae, new);-
683 a->num_elements++;-
684 SET_LASTREF(a, new);-
685 return(0);
executed 6 times by 1 test: return(0);
Executed by:
  • Self test
6
686 } else if (direction == -1 && element_index(ae) < i) {
direction == -1Description
TRUEevaluated 57 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 82 times by 1 test
Evaluated by:
  • Self test
((ae)->ind) < iDescription
TRUEevaluated 22 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 35 times by 1 test
Evaluated by:
  • Self test
22-82
687 ADD_AFTER(ae, new);-
688 a->num_elements++;-
689 SET_LASTREF(a, new);-
690 return(0);
executed 22 times by 1 test: return(0);
Executed by:
  • Self test
22
691 }-
692 ae = direction == 1 ? element_forw(ae) : element_back(ae);
direction == 1Description
TRUEevaluated 82 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 35 times by 1 test
Evaluated by:
  • Self test
35-82
693 }
executed 117 times by 1 test: end of block
Executed by:
  • Self test
117
694 array_dispose_element(new);-
695 INVALIDATE_LASTREF(a);-
696 return (-1); /* problem */
never executed: return (-1);
0
697}-
698-
699/*-
700 * Delete the element with index I from array A and return it so the-
701 * caller can dispose of it.-
702 */-
703ARRAY_ELEMENT *-
704array_remove(a, i)-
705ARRAY *a;-
706arrayind_t i;-
707{-
708 register ARRAY_ELEMENT *ae, *start;-
709 arrayind_t startind;-
710 int direction;-
711-
712 if (a == 0 || array_empty(a))
a == 0Description
TRUEnever evaluated
FALSEevaluated 73 times by 1 test
Evaluated by:
  • Self test
((a)->num_elements == 0)Description
TRUEnever evaluated
FALSEevaluated 73 times by 1 test
Evaluated by:
  • Self test
0-73
713 return((ARRAY_ELEMENT *) NULL);
never executed: return((ARRAY_ELEMENT *) ((void *)0) );
0
714 if (i > array_max_index(a) || i < array_first_index(a))
i > ((a)->max_index)Description
TRUEevaluated 38 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 35 times by 1 test
Evaluated by:
  • Self test
i < ((a)->head->next->ind)Description
TRUEnever evaluated
FALSEevaluated 35 times by 1 test
Evaluated by:
  • Self test
0-38
715 return((ARRAY_ELEMENT *)NULL); /* Keep roving pointer into array to optimize sequential access */
executed 38 times by 1 test: return((ARRAY_ELEMENT *) ((void *)0) );
Executed by:
  • Self test
38
716 start = LASTREF(a);
a->lastrefDescription
TRUEevaluated 35 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-35
717 /* Use same strategy as array_reference to avoid paying large penalty-
718 for semi-random assignment pattern. */-
719 startind = element_index(start);-
720 if (i < startind/2) {
i < startind/2Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • Self test
FALSEevaluated 34 times by 1 test
Evaluated by:
  • Self test
1-34
721 start = element_forw(a->head);-
722 startind = element_index(start);-
723 direction = 1;-
724 } else if (i >= startind) {
executed 1 time by 1 test: end of block
Executed by:
  • Self test
i >= startindDescription
TRUEevaluated 31 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 3 times by 1 test
Evaluated by:
  • Self test
1-31
725 direction = 1;-
726 } else {
executed 31 times by 1 test: end of block
Executed by:
  • Self test
31
727 direction = -1;-
728 }
executed 3 times by 1 test: end of block
Executed by:
  • Self test
3
729 for (ae = start; ae != a->head; ) {
ae != a->headDescription
TRUEevaluated 50 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-50
730 if (element_index(ae) == i) {
((ae)->ind) == iDescription
TRUEevaluated 35 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 15 times by 1 test
Evaluated by:
  • Self test
15-35
731 ae->next->prev = ae->prev;-
732 ae->prev->next = ae->next;-
733 a->num_elements--;-
734 if (i == array_max_index(a))
i == ((a)->max_index)Description
TRUEevaluated 17 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 18 times by 1 test
Evaluated by:
  • Self test
17-18
735 a->max_index = element_index(ae->prev);
executed 17 times by 1 test: a->max_index = ((ae->prev)->ind);
Executed by:
  • Self test
17
736#if 0-
737 INVALIDATE_LASTREF(a);-
738#else-
739 if (ae->next != a->head)
ae->next != a->headDescription
TRUEevaluated 18 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 17 times by 1 test
Evaluated by:
  • Self test
17-18
740 SET_LASTREF(a, ae->next);
executed 18 times by 1 test: a->lastref = (ae->next);
Executed by:
  • Self test
18
741 else if (ae->prev != a->head)
ae->prev != a->headDescription
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 12 times by 1 test
Evaluated by:
  • Self test
5-12
742 SET_LASTREF(a, ae->prev);
executed 5 times by 1 test: a->lastref = (ae->prev);
Executed by:
  • Self test
5
743 else-
744 INVALIDATE_LASTREF(a);
executed 12 times by 1 test: a->lastref = 0;
Executed by:
  • Self test
12
745#endif-
746 return(ae);
executed 35 times by 1 test: return(ae);
Executed by:
  • Self test
35
747 }-
748 ae = (direction == 1) ? element_forw(ae) : element_back(ae);
(direction == 1)Description
TRUEevaluated 12 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 3 times by 1 test
Evaluated by:
  • Self test
3-12
749 if (direction == 1 && element_index(ae) > i)
direction == 1Description
TRUEevaluated 12 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 3 times by 1 test
Evaluated by:
  • Self test
((ae)->ind) > iDescription
TRUEnever evaluated
FALSEevaluated 12 times by 1 test
Evaluated by:
  • Self test
0-12
750 break;
never executed: break;
0
751 else if (direction == -1 && element_index(ae) < i)
direction == -1Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 12 times by 1 test
Evaluated by:
  • Self test
((ae)->ind) < iDescription
TRUEnever evaluated
FALSEevaluated 3 times by 1 test
Evaluated by:
  • Self test
0-12
752 break;
never executed: break;
0
753 }
executed 15 times by 1 test: end of block
Executed by:
  • Self test
15
754 return((ARRAY_ELEMENT *) NULL);
never executed: return((ARRAY_ELEMENT *) ((void *)0) );
0
755}-
756-
757/*-
758 * Return the value of a[i].-
759 */-
760char *-
761array_reference(a, i)-
762ARRAY *a;-
763arrayind_t i;-
764{-
765 register ARRAY_ELEMENT *ae, *start;-
766 arrayind_t startind;-
767 int direction;-
768-
769 if (a == 0 || array_empty(a))
a == 0Description
TRUEnever evaluated
FALSEevaluated 28632 times by 1 test
Evaluated by:
  • Self test
((a)->num_elements == 0)Description
TRUEevaluated 77 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 28555 times by 1 test
Evaluated by:
  • Self test
0-28632
770 return((char *) NULL);
executed 77 times by 1 test: return((char *) ((void *)0) );
Executed by:
  • Self test
77
771 if (i > array_max_index(a) || i < array_first_index(a))
i > ((a)->max_index)Description
TRUEevaluated 34 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 28521 times by 1 test
Evaluated by:
  • Self test
i < ((a)->head->next->ind)Description
TRUEevaluated 34 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 28487 times by 1 test
Evaluated by:
  • Self test
34-28521
772 return((char *)NULL); /* Keep roving pointer into array to optimize sequential access */
executed 68 times by 1 test: return((char *) ((void *)0) );
Executed by:
  • Self test
68
773 start = LASTREF(a); /* lastref pointer */
a->lastrefDescription
TRUEevaluated 16086 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 12401 times by 1 test
Evaluated by:
  • Self test
12401-16086
774 startind = element_index(start);-
775 if (i < startind/2) { /* XXX - guess */
i < startind/2Description
TRUEevaluated 1092 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 27395 times by 1 test
Evaluated by:
  • Self test
1092-27395
776 start = element_forw(a->head);-
777 startind = element_index(start);-
778 direction = 1;-
779 } else if (i >= startind) {
executed 1092 times by 1 test: end of block
Executed by:
  • Self test
i >= startindDescription
TRUEevaluated 23509 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 3886 times by 1 test
Evaluated by:
  • Self test
1092-23509
780 direction = 1;-
781 } else {
executed 23509 times by 1 test: end of block
Executed by:
  • Self test
23509
782 direction = -1;-
783 }
executed 3886 times by 1 test: end of block
Executed by:
  • Self test
3886
784 for (ae = start; ae != a->head; ) {
ae != a->headDescription
TRUEevaluated 381002 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-381002
785 if (element_index(ae) == i) {
((ae)->ind) == iDescription
TRUEevaluated 28097 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 352905 times by 1 test
Evaluated by:
  • Self test
28097-352905
786 SET_LASTREF(a, ae);-
787 return(element_value(ae));
executed 28097 times by 1 test: return(((ae)->value));
Executed by:
  • Self test
28097
788 }-
789 ae = (direction == 1) ? element_forw(ae) : element_back(ae);
(direction == 1)Description
TRUEevaluated 343088 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 9817 times by 1 test
Evaluated by:
  • Self test
9817-343088
790 /* Take advantage of index ordering to short-circuit */-
791 /* If we don't find it, set the lastref pointer to the element-
792 that's `closest', assuming that the unsuccessful reference-
793 will quickly be followed by an assignment. No worse than-
794 not changing it from the previous value or resetting it. */-
795 if (direction == 1 && element_index(ae) > i) {
direction == 1Description
TRUEevaluated 343088 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 9817 times by 1 test
Evaluated by:
  • Self test
((ae)->ind) > iDescription
TRUEevaluated 386 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 342702 times by 1 test
Evaluated by:
  • Self test
386-343088
796 start = ae; /* use for SET_LASTREF below */-
797 break;
executed 386 times by 1 test: break;
Executed by:
  • Self test
386
798 } else if (direction == -1 && element_index(ae) < i) {
direction == -1Description
TRUEevaluated 9817 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 342702 times by 1 test
Evaluated by:
  • Self test
((ae)->ind) < iDescription
TRUEevaluated 4 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 9813 times by 1 test
Evaluated by:
  • Self test
4-342702
799 start = ae; /* use for SET_LASTREF below */-
800 break;
executed 4 times by 1 test: break;
Executed by:
  • Self test
4
801 }-
802 }
executed 352515 times by 1 test: end of block
Executed by:
  • Self test
352515
803#if 0-
804 UNSET_LASTREF(a);-
805#else-
806 SET_LASTREF(a, start);-
807#endif-
808 return((char *) NULL);
executed 390 times by 1 test: return((char *) ((void *)0) );
Executed by:
  • Self test
390
809}-
810-
811/* Convenience routines for the shell to translate to and from the form used-
812 by the rest of the code. */-
813-
814WORD_LIST *-
815array_to_word_list(a)-
816ARRAY *a;-
817{-
818 WORD_LIST *list;-
819 ARRAY_ELEMENT *ae;-
820-
821 if (a == 0 || array_empty(a))
a == 0Description
TRUEnever evaluated
FALSEevaluated 793 times by 1 test
Evaluated by:
  • Self test
((a)->num_elements == 0)Description
TRUEevaluated 26 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 767 times by 1 test
Evaluated by:
  • Self test
0-793
822 return((WORD_LIST *)NULL);
executed 26 times by 1 test: return((WORD_LIST *) ((void *)0) );
Executed by:
  • Self test
26
823 list = (WORD_LIST *)NULL;-
824 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae))
ae != a->headDescription
TRUEevaluated 3328 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 767 times by 1 test
Evaluated by:
  • Self test
767-3328
825 list = make_word_list (make_bare_word(element_value(ae)), list);
executed 3328 times by 1 test: list = make_word_list (make_bare_word(((ae)->value)), list);
Executed by:
  • Self test
3328
826 return (REVERSE_LIST(list, WORD_LIST *));
executed 767 times by 1 test: return (((list && list->next) ? (WORD_LIST *)list_reverse ((GENERIC_LIST *)list) : (WORD_LIST *)(list)));
Executed by:
  • Self test
767
827}-
828-
829ARRAY *-
830array_from_word_list (list)-
831WORD_LIST *list;-
832{-
833 ARRAY *a;-
834-
835 if (list == 0)
list == 0Description
TRUEnever evaluated
FALSEevaluated 27 times by 1 test
Evaluated by:
  • Self test
0-27
836 return((ARRAY *)NULL);
never executed: return((ARRAY *) ((void *)0) );
0
837 a = array_create();-
838 return (array_assign_list (a, list));
executed 27 times by 1 test: return (array_assign_list (a, list));
Executed by:
  • Self test
27
839}-
840-
841WORD_LIST *-
842array_keys_to_word_list(a)-
843ARRAY *a;-
844{-
845 WORD_LIST *list;-
846 ARRAY_ELEMENT *ae;-
847 char *t;-
848-
849 if (a == 0 || array_empty(a))
a == 0Description
TRUEnever evaluated
FALSEevaluated 8 times by 1 test
Evaluated by:
  • Self test
((a)->num_elements == 0)Description
TRUEnever evaluated
FALSEevaluated 8 times by 1 test
Evaluated by:
  • Self test
0-8
850 return((WORD_LIST *)NULL);
never executed: return((WORD_LIST *) ((void *)0) );
0
851 list = (WORD_LIST *)NULL;-
852 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
ae != a->headDescription
TRUEevaluated 34 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 8 times by 1 test
Evaluated by:
  • Self test
8-34
853 t = itos(element_index(ae));-
854 list = make_word_list (make_bare_word(t), list);-
855 free(t);-
856 }
executed 34 times by 1 test: end of block
Executed by:
  • Self test
34
857 return (REVERSE_LIST(list, WORD_LIST *));
executed 8 times by 1 test: return (((list && list->next) ? (WORD_LIST *)list_reverse ((GENERIC_LIST *)list) : (WORD_LIST *)(list)));
Executed by:
  • Self test
8
858}-
859-
860ARRAY *-
861array_assign_list (array, list)-
862ARRAY *array;-
863WORD_LIST *list;-
864{-
865 register WORD_LIST *l;-
866 register arrayind_t i;-
867-
868 for (l = list, i = 0; l; l = l->next, i++)
lDescription
TRUEevaluated 87 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 27 times by 1 test
Evaluated by:
  • Self test
27-87
869 array_insert(array, i, l->word->word);
executed 87 times by 1 test: array_insert(array, i, l->word->word);
Executed by:
  • Self test
87
870 return array;
executed 27 times by 1 test: return array;
Executed by:
  • Self test
27
871}-
872-
873char **-
874array_to_argv (a)-
875ARRAY *a;-
876{-
877 char **ret, *t;-
878 int i;-
879 ARRAY_ELEMENT *ae;-
880-
881 if (a == 0 || array_empty(a))
a == 0Description
TRUEnever evaluated
FALSEnever evaluated
((a)->num_elements == 0)Description
TRUEnever evaluated
FALSEnever evaluated
0
882 return ((char **)NULL);
never executed: return ((char **) ((void *)0) );
0
883 ret = strvec_create (array_num_elements (a) + 1);-
884 i = 0;-
885 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
ae != a->headDescription
TRUEnever evaluated
FALSEnever evaluated
0
886 t = element_value (ae);-
887 ret[i++] = t ? savestring (t) : (char *)NULL;
tDescription
TRUEnever evaluated
FALSEnever evaluated
0
888 }
never executed: end of block
0
889 ret[i] = (char *)NULL;-
890 return (ret);
never executed: return (ret);
0
891}-
892 -
893/*-
894 * Return a string that is the concatenation of the elements in A from START-
895 * to END, separated by SEP.-
896 */-
897static char *-
898array_to_string_internal (start, end, sep, quoted)-
899ARRAY_ELEMENT *start, *end;-
900char *sep;-
901int quoted;-
902{-
903 char *result, *t;-
904 ARRAY_ELEMENT *ae;-
905 int slen, rsize, rlen, reg;-
906-
907 if (start == end) /* XXX - should not happen */
start == endDescription
TRUEnever evaluated
FALSEevaluated 224 times by 1 test
Evaluated by:
  • Self test
0-224
908 return ((char *)NULL);
never executed: return ((char *) ((void *)0) );
0
909-
910 slen = strlen(sep);-
911 result = NULL;-
912 for (rsize = rlen = 0, ae = start; ae != end; ae = element_forw(ae)) {
ae != endDescription
TRUEevaluated 969 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 224 times by 1 test
Evaluated by:
  • Self test
224-969
913 if (rsize == 0)
rsize == 0Description
TRUEevaluated 224 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 745 times by 1 test
Evaluated by:
  • Self test
224-745
914 result = (char *)xmalloc (rsize = 64);
executed 224 times by 1 test: result = (char *)sh_xmalloc((rsize = 64), "array.c", 914);
Executed by:
  • Self test
224
915 if (element_value(ae)) {
((ae)->value)Description
TRUEevaluated 969 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-969
916 t = quoted ? quote_string(element_value(ae)) : element_value(ae);
quotedDescription
TRUEnever evaluated
FALSEevaluated 969 times by 1 test
Evaluated by:
  • Self test
0-969
917 reg = strlen(t);-
918 RESIZE_MALLOCED_BUFFER (result, rlen, (reg + slen + 2),
executed 9 times by 1 test: rsize += (rsize);
Executed by:
  • Self test
executed 9 times by 1 test: end of block
Executed by:
  • Self test
(rlen) + ((reg...+ 2)) >= rsizeDescription
TRUEevaluated 9 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 960 times by 1 test
Evaluated by:
  • Self test
(rlen) + ((reg...+ 2)) >= rsizeDescription
TRUEevaluated 9 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 9 times by 1 test
Evaluated by:
  • Self test
9-960
919 rsize, rsize);-
920 strcpy(result + rlen, t);-
921 rlen += reg;-
922 if (quoted)
quotedDescription
TRUEnever evaluated
FALSEevaluated 969 times by 1 test
Evaluated by:
  • Self test
0-969
923 free(t);
never executed: sh_xfree((t), "array.c", 923);
0
924 /*-
925 * Add a separator only after non-null elements.-
926 */-
927 if (element_forw(ae) != end) {
((ae)->next) != endDescription
TRUEevaluated 745 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 224 times by 1 test
Evaluated by:
  • Self test
224-745
928 strcpy(result + rlen, sep);-
929 rlen += slen;-
930 }
executed 745 times by 1 test: end of block
Executed by:
  • Self test
745
931 }
executed 969 times by 1 test: end of block
Executed by:
  • Self test
969
932 }
executed 969 times by 1 test: end of block
Executed by:
  • Self test
969
933 if (result)
resultDescription
TRUEevaluated 224 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-224
934 result[rlen] = '\0'; /* XXX */
executed 224 times by 1 test: result[rlen] = '\0';
Executed by:
  • Self test
224
935 return(result);
executed 224 times by 1 test: return(result);
Executed by:
  • Self test
224
936}-
937-
938char *-
939array_to_assign (a, quoted)-
940ARRAY *a;-
941int quoted;-
942{-
943 char *result, *valstr, *is;-
944 char indstr[INT_STRLEN_BOUND(intmax_t) + 1];-
945 ARRAY_ELEMENT *ae;-
946 int rsize, rlen, elen;-
947-
948 if (a == 0 || array_empty (a))
a == 0Description
TRUEnever evaluated
FALSEevaluated 277 times by 1 test
Evaluated by:
  • Self test
((a)->num_elements == 0)Description
TRUEevaluated 51 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 226 times by 1 test
Evaluated by:
  • Self test
0-277
949 return((char *)NULL);
executed 51 times by 1 test: return((char *) ((void *)0) );
Executed by:
  • Self test
51
950-
951 result = (char *)xmalloc (rsize = 128);-
952 result[0] = '(';-
953 rlen = 1;-
954-
955 for (ae = element_forw(a->head); ae != a->head; ae = element_forw(ae)) {
ae != a->headDescription
TRUEevaluated 523 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 226 times by 1 test
Evaluated by:
  • Self test
226-523
956 is = inttostr (element_index(ae), indstr, sizeof(indstr));-
957 valstr = element_value (ae) ?
((ae)->value)Description
TRUEevaluated 523 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-523
958 (ansic_shouldquote (element_value (ae)) ?
ansic_shouldqu...((ae)->value))Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 520 times by 1 test
Evaluated by:
  • Self test
3-520
959 ansic_quote (element_value(ae), 0, (int *)0) :-
960 sh_double_quote (element_value (ae)))-
961 : (char *)NULL;-
962 elen = STRLEN (is) + 8 + STRLEN (valstr);
(is)[1]Description
TRUEevaluated 9 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 514 times by 1 test
Evaluated by:
  • Self test
(is)[2]Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 6 times by 1 test
Evaluated by:
  • Self test
(valstr)[1]Description
TRUEevaluated 523 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
(valstr)[2]Description
TRUEevaluated 505 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 18 times by 1 test
Evaluated by:
  • Self test
(is)Description
TRUEevaluated 523 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
(is)[0]Description
TRUEevaluated 523 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
(valstr)Description
TRUEevaluated 523 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
(valstr)[0]Description
TRUEevaluated 523 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-523
963 RESIZE_MALLOCED_BUFFER (result, rlen, (elen + 1), rsize, rsize);
never executed: rsize += (rsize);
never executed: end of block
(rlen) + ((elen + 1)) >= rsizeDescription
TRUEnever evaluated
FALSEevaluated 523 times by 1 test
Evaluated by:
  • Self test
(rlen) + ((elen + 1)) >= rsizeDescription
TRUEnever evaluated
FALSEnever evaluated
0-523
964-
965 result[rlen++] = '[';-
966 strcpy (result + rlen, is);-
967 rlen += STRLEN (is);
(is)[1]Description
TRUEevaluated 9 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 514 times by 1 test
Evaluated by:
  • Self test
(is)[2]Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 6 times by 1 test
Evaluated by:
  • Self test
(is)Description
TRUEevaluated 523 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
(is)[0]Description
TRUEevaluated 523 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-523
968 result[rlen++] = ']';-
969 result[rlen++] = '=';-
970 if (valstr) {
valstrDescription
TRUEevaluated 523 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-523
971 strcpy (result + rlen, valstr);-
972 rlen += STRLEN (valstr);
(valstr)[1]Description
TRUEevaluated 523 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
(valstr)[2]Description
TRUEevaluated 505 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 18 times by 1 test
Evaluated by:
  • Self test
(valstr)Description
TRUEevaluated 523 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
(valstr)[0]Description
TRUEevaluated 523 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-523
973 }
executed 523 times by 1 test: end of block
Executed by:
  • Self test
523
974-
975 if (element_forw(ae) != a->head)
((ae)->next) != a->headDescription
TRUEevaluated 297 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 226 times by 1 test
Evaluated by:
  • Self test
226-297
976 result[rlen++] = ' ';
executed 297 times by 1 test: result[rlen++] = ' ';
Executed by:
  • Self test
297
977-
978 FREE (valstr);
executed 523 times by 1 test: sh_xfree((valstr), "array.c", 978);
Executed by:
  • Self test
valstrDescription
TRUEevaluated 523 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-523
979 }
executed 523 times by 1 test: end of block
Executed by:
  • Self test
523
980 RESIZE_MALLOCED_BUFFER (result, rlen, 1, rsize, 8);
never executed: rsize += (8);
never executed: end of block
(rlen) + (1) >= rsizeDescription
TRUEnever evaluated
FALSEevaluated 226 times by 1 test
Evaluated by:
  • Self test
(rlen) + (1) >= rsizeDescription
TRUEnever evaluated
FALSEnever evaluated
0-226
981 result[rlen++] = ')';-
982 result[rlen] = '\0';-
983 if (quoted) {
quotedDescription
TRUEnever evaluated
FALSEevaluated 226 times by 1 test
Evaluated by:
  • Self test
0-226
984 /* This is not as efficient as it could be... */-
985 valstr = sh_single_quote (result);-
986 free (result);-
987 result = valstr;-
988 }
never executed: end of block
0
989 return(result);
executed 226 times by 1 test: return(result);
Executed by:
  • Self test
226
990}-
991-
992char *-
993array_to_string (a, sep, quoted)-
994ARRAY *a;-
995char *sep;-
996int quoted;-
997{-
998 if (a == 0)
a == 0Description
TRUEnever evaluated
FALSEevaluated 224 times by 1 test
Evaluated by:
  • Self test
0-224
999 return((char *)NULL);
never executed: return((char *) ((void *)0) );
0
1000 if (array_empty(a))
((a)->num_elements == 0)Description
TRUEnever evaluated
FALSEevaluated 224 times by 1 test
Evaluated by:
  • Self test
0-224
1001 return(savestring(""));
never executed: return((char *)strcpy (sh_xmalloc((1 + strlen ("")), "array.c", 1001), ("")));
0
1002 return (array_to_string_internal (element_forw(a->head), a->head, sep, quoted));
executed 224 times by 1 test: return (array_to_string_internal (((a->head)->next), a->head, sep, quoted));
Executed by:
  • Self test
224
1003}-
1004-
1005#if defined (INCLUDE_UNUSED) || defined (TEST_ARRAY)-
1006/*-
1007 * Return an array consisting of elements in S, separated by SEP-
1008 */-
1009ARRAY *-
1010array_from_string(s, sep)-
1011char *s, *sep;-
1012{-
1013 ARRAY *a;-
1014 WORD_LIST *w;-
1015-
1016 if (s == 0)-
1017 return((ARRAY *)NULL);-
1018 w = list_string (s, sep, 0);-
1019 if (w == 0)-
1020 return((ARRAY *)NULL);-
1021 a = array_from_word_list (w);-
1022 return (a);-
1023}-
1024#endif-
1025-
1026#if defined (TEST_ARRAY)-
1027/*-
1028 * To make a running version, compile -DTEST_ARRAY and link with:-
1029 * xmalloc.o syntax.o lib/malloc/libmalloc.a lib/sh/libsh.a-
1030 */-
1031int interrupt_immediately = 0;-
1032-
1033int-
1034signal_is_trapped(s)-
1035int s;-
1036{-
1037 return 0;-
1038}-
1039-
1040void-
1041fatal_error(const char *s, ...)-
1042{-
1043 fprintf(stderr, "array_test: fatal memory error\n");-
1044 abort();-
1045}-
1046-
1047void-
1048programming_error(const char *s, ...)-
1049{-
1050 fprintf(stderr, "array_test: fatal programming error\n");-
1051 abort();-
1052}-
1053-
1054WORD_DESC *-
1055make_bare_word (s)-
1056const char *s;-
1057{-
1058 WORD_DESC *w;-
1059-
1060 w = (WORD_DESC *)xmalloc(sizeof(WORD_DESC));-
1061 w->word = s ? savestring(s) : savestring ("");-
1062 w->flags = 0;-
1063 return w;-
1064}-
1065-
1066WORD_LIST *-
1067make_word_list(x, l)-
1068WORD_DESC *x;-
1069WORD_LIST *l;-
1070{-
1071 WORD_LIST *w;-
1072-
1073 w = (WORD_LIST *)xmalloc(sizeof(WORD_LIST));-
1074 w->word = x;-
1075 w->next = l;-
1076 return w;-
1077}-
1078-
1079WORD_LIST *-
1080list_string(s, t, i)-
1081char *s, *t;-
1082int i;-
1083{-
1084 char *r, *a;-
1085 WORD_LIST *wl;-
1086-
1087 if (s == 0)-
1088 return (WORD_LIST *)NULL;-
1089 r = savestring(s);-
1090 wl = (WORD_LIST *)NULL;-
1091 a = strtok(r, t);-
1092 while (a) {-
1093 wl = make_word_list (make_bare_word(a), wl);-
1094 a = strtok((char *)NULL, t);-
1095 }-
1096 return (REVERSE_LIST (wl, WORD_LIST *));-
1097}-
1098-
1099GENERIC_LIST *-
1100list_reverse (list)-
1101GENERIC_LIST *list;-
1102{-
1103 register GENERIC_LIST *next, *prev;-
1104-
1105 for (prev = 0; list; ) {-
1106 next = list->next;-
1107 list->next = prev;-
1108 prev = list;-
1109 list = next;-
1110 }-
1111 return prev;-
1112}-
1113-
1114char *-
1115pat_subst(s, t, u, i)-
1116char *s, *t, *u;-
1117int i;-
1118{-
1119 return ((char *)NULL);-
1120}-
1121-
1122char *-
1123quote_string(s)-
1124char *s;-
1125{-
1126 return savestring(s);-
1127}-
1128-
1129print_element(ae)-
1130ARRAY_ELEMENT *ae;-
1131{-
1132 char lbuf[INT_STRLEN_BOUND (intmax_t) + 1];-
1133-
1134 printf("array[%s] = %s\n",-
1135 inttostr (element_index(ae), lbuf, sizeof (lbuf)),-
1136 element_value(ae));-
1137}-
1138-
1139print_array(a)-
1140ARRAY *a;-
1141{-
1142 printf("\n");-
1143 array_walk(a, print_element, (void *)NULL);-
1144}-
1145-
1146main()-
1147{-
1148 ARRAY *a, *new_a, *copy_of_a;-
1149 ARRAY_ELEMENT *ae, *aew;-
1150 char *s;-
1151-
1152 a = array_create();-
1153 array_insert(a, 1, "one");-
1154 array_insert(a, 7, "seven");-
1155 array_insert(a, 4, "four");-
1156 array_insert(a, 1029, "one thousand twenty-nine");-
1157 array_insert(a, 12, "twelve");-
1158 array_insert(a, 42, "forty-two");-
1159 print_array(a);-
1160 s = array_to_string (a, " ", 0);-
1161 printf("s = %s\n", s);-
1162 copy_of_a = array_from_string(s, " ");-
1163 printf("copy_of_a:");-
1164 print_array(copy_of_a);-
1165 array_dispose(copy_of_a);-
1166 printf("\n");-
1167 free(s);-
1168 ae = array_remove(a, 4);-
1169 array_dispose_element(ae);-
1170 ae = array_remove(a, 1029);-
1171 array_dispose_element(ae);-
1172 array_insert(a, 16, "sixteen");-
1173 print_array(a);-
1174 s = array_to_string (a, " ", 0);-
1175 printf("s = %s\n", s);-
1176 copy_of_a = array_from_string(s, " ");-
1177 printf("copy_of_a:");-
1178 print_array(copy_of_a);-
1179 array_dispose(copy_of_a);-
1180 printf("\n");-
1181 free(s);-
1182 array_insert(a, 2, "two");-
1183 array_insert(a, 1029, "new one thousand twenty-nine");-
1184 array_insert(a, 0, "zero");-
1185 array_insert(a, 134, "");-
1186 print_array(a);-
1187 s = array_to_string (a, ":", 0);-
1188 printf("s = %s\n", s);-
1189 copy_of_a = array_from_string(s, ":");-
1190 printf("copy_of_a:");-
1191 print_array(copy_of_a);-
1192 array_dispose(copy_of_a);-
1193 printf("\n");-
1194 free(s);-
1195 new_a = array_copy(a);-
1196 print_array(new_a);-
1197 s = array_to_string (new_a, ":", 0);-
1198 printf("s = %s\n", s);-
1199 copy_of_a = array_from_string(s, ":");-
1200 free(s);-
1201 printf("copy_of_a:");-
1202 print_array(copy_of_a);-
1203 array_shift(copy_of_a, 2, AS_DISPOSE);-
1204 printf("copy_of_a shifted by two:");-
1205 print_array(copy_of_a);-
1206 ae = array_shift(copy_of_a, 2, 0);-
1207 printf("copy_of_a shifted by two:");-
1208 print_array(copy_of_a);-
1209 for ( ; ae; ) {-
1210 aew = element_forw(ae);-
1211 array_dispose_element(ae);-
1212 ae = aew;-
1213 }-
1214 array_rshift(copy_of_a, 1, (char *)0);-
1215 printf("copy_of_a rshift by 1:");-
1216 print_array(copy_of_a);-
1217 array_rshift(copy_of_a, 2, "new element zero");-
1218 printf("copy_of_a rshift again by 2 with new element zero:");-
1219 print_array(copy_of_a);-
1220 s = array_to_assign(copy_of_a, 0);-
1221 printf("copy_of_a=%s\n", s);-
1222 free(s);-
1223 ae = array_shift(copy_of_a, array_num_elements(copy_of_a), 0);-
1224 for ( ; ae; ) {-
1225 aew = element_forw(ae);-
1226 array_dispose_element(ae);-
1227 ae = aew;-
1228 }-
1229 array_dispose(copy_of_a);-
1230 printf("\n");-
1231 array_dispose(a);-
1232 array_dispose(new_a);-
1233}-
1234-
1235#endif /* TEST_ARRAY */-
1236#endif /* ARRAY_VARS */-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2