OpenCoverage

stringlib.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/bash/src/stringlib.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* stringlib.c - Miscellaneous string functions. */-
2-
3/* Copyright (C) 1996-2009 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-
8 it under the terms of the GNU General Public License as published by-
9 the Free Software Foundation, either version 3 of the License, or-
10 (at your option) any later version.-
11-
12 Bash is distributed in the hope that it will be useful,-
13 but WITHOUT ANY WARRANTY; without even the implied warranty of-
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-
15 GNU General Public License for more details.-
16-
17 You should have received a copy of the GNU General Public License-
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.-
19*/-
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#include "pathexp.h"-
35-
36#include <glob/glob.h>-
37-
38#if defined (EXTENDED_GLOB)-
39# include <glob/strmatch.h>-
40#endif-
41-
42/* **************************************************************** */-
43/* */-
44/* Functions to manage arrays of strings */-
45/* */-
46/* **************************************************************** */-
47-
48/* Find STRING in ALIST, a list of string key/int value pairs. If FLAGS-
49 is 1, STRING is treated as a pattern and matched using strmatch. */-
50int-
51find_string_in_alist (string, alist, flags)-
52 char *string;-
53 STRING_INT_ALIST *alist;-
54 int flags;-
55{-
56 register int i;-
57 int r;-
58-
59 for (i = r = 0; alist[i].word; i++)
alist[i].wordDescription
TRUEevaluated 1332030 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 666015 times by 1 test
Evaluated by:
  • Self test
666015-1332030
60 {-
61#if defined (EXTENDED_GLOB)-
62 if (flags)
flagsDescription
TRUEevaluated 1332030 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-1332030
63 r = strmatch (alist[i].word, string, FNM_EXTMATCH) != FNM_NOMATCH;
executed 1332030 times by 1 test: r = strmatch (alist[i].word, string, (1 << 5)) != 1;
Executed by:
  • Self test
1332030
64 else-
65#endif-
66 r = STREQ (string, alist[i].word);
never executed: r = ((string)[0] == (alist[i].word)[0] && __extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p ( string ) && __builtin_constant_p ( alist[i].word ) && (__s1_len = __builtin_strlen ( string ), __s2_len = __builtin_strlen ( alist[i].word ), (!(...har *) (const char *) ( alist[i].word ))[2] - __s2[2]); if (__s2_len > 2 && __result == 0) __result = (((const unsigned char *) (const char *) ( alist[i].word ))[3] - __s2[3]); } } __result; }))) : __builtin_strcmp ( string , alist[i].word )))); }) == 0);
never executed: __result = (((const unsigned char *) (const char *) ( string ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
never executed: __result = (((const unsigned char *) (const char *) ( alist[i].word ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
(string)[0] ==...st[i].word)[0]Description
TRUEnever evaluated
FALSEnever evaluated
__extension__ ... )))); }) == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
67-
68 if (r)
rDescription
TRUEnever evaluated
FALSEevaluated 1332030 times by 1 test
Evaluated by:
  • Self test
0-1332030
69 return (alist[i].token);
never executed: return (alist[i].token);
0
70 }
executed 1332030 times by 1 test: end of block
Executed by:
  • Self test
1332030
71 return -1;
executed 666015 times by 1 test: return -1;
Executed by:
  • Self test
666015
72}-
73-
74/* Find TOKEN in ALIST, a list of string/int value pairs. Return the-
75 corresponding string. Allocates memory for the returned-
76 string. FLAGS is currently ignored, but reserved. */-
77char *-
78find_token_in_alist (token, alist, flags)-
79 int token;-
80 STRING_INT_ALIST *alist;-
81 int flags;-
82{-
83 register int i;-
84-
85 for (i = 0; alist[i].word; i++)
alist[i].wordDescription
TRUEevaluated 1689 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 40 times by 1 test
Evaluated by:
  • Self test
40-1689
86 {-
87 if (alist[i].token == token)
alist[i].token == tokenDescription
TRUEevaluated 30 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1659 times by 1 test
Evaluated by:
  • Self test
30-1659
88 return (savestring (alist[i].word));
executed 30 times by 1 test: return ((char *)strcpy (sh_xmalloc((1 + strlen (alist[i].word)), "stringlib.c", 88), (alist[i].word)));
Executed by:
  • Self test
30
89 }
executed 1659 times by 1 test: end of block
Executed by:
  • Self test
1659
90 return ((char *)NULL);
executed 40 times by 1 test: return ((char *) ((void *)0) );
Executed by:
  • Self test
40
91}-
92-
93int-
94find_index_in_alist (string, alist, flags)-
95 char *string;-
96 STRING_INT_ALIST *alist;-
97 int flags;-
98{-
99 register int i;-
100 int r;-
101-
102 for (i = r = 0; alist[i].word; i++)
alist[i].wordDescription
TRUEnever evaluated
FALSEnever evaluated
0
103 {-
104#if defined (EXTENDED_GLOB)-
105 if (flags)
flagsDescription
TRUEnever evaluated
FALSEnever evaluated
0
106 r = strmatch (alist[i].word, string, FNM_EXTMATCH) != FNM_NOMATCH;
never executed: r = strmatch (alist[i].word, string, (1 << 5)) != 1;
0
107 else-
108#endif-
109 r = STREQ (string, alist[i].word);
never executed: r = ((string)[0] == (alist[i].word)[0] && __extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p ( string ) && __builtin_constant_p ( alist[i].word ) && (__s1_len = __builtin_strlen ( string ), __s2_len = __builtin_strlen ( alist[i].word ), (!(...har *) (const char *) ( alist[i].word ))[2] - __s2[2]); if (__s2_len > 2 && __result == 0) __result = (((const unsigned char *) (const char *) ( alist[i].word ))[3] - __s2[3]); } } __result; }))) : __builtin_strcmp ( string , alist[i].word )))); }) == 0);
never executed: __result = (((const unsigned char *) (const char *) ( string ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
never executed: __result = (((const unsigned char *) (const char *) ( alist[i].word ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
(string)[0] ==...st[i].word)[0]Description
TRUEnever evaluated
FALSEnever evaluated
__extension__ ... )))); }) == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
110-
111 if (r)
rDescription
TRUEnever evaluated
FALSEnever evaluated
0
112 return (i);
never executed: return (i);
0
113 }
never executed: end of block
0
114-
115 return -1;
never executed: return -1;
0
116}-
117-
118/* **************************************************************** */-
119/* */-
120/* String Management Functions */-
121/* */-
122/* **************************************************************** */-
123-
124/* Cons a new string from STRING starting at START and ending at END,-
125 not including END. */-
126char *-
127substring (string, start, end)-
128 const char *string;-
129 int start, end;-
130{-
131 register int len;-
132 register char *result;-
133-
134 len = end - start;-
135 result = (char *)xmalloc (len + 1);-
136 memcpy (result, string + start, len);-
137 result[len] = '\0';-
138 return (result);
executed 121777313 times by 1 test: return (result);
Executed by:
  • Self test
121777313
139}-
140-
141/* Replace occurrences of PAT with REP in STRING. If GLOBAL is non-zero,-
142 replace all occurrences, otherwise replace only the first.-
143 This returns a new string; the caller should free it. */-
144char *-
145strsub (string, pat, rep, global)-
146 char *string, *pat, *rep;-
147 int global;-
148{-
149 int patlen, replen, templen, tempsize, repl, i;-
150 char *temp, *r;-
151-
152 patlen = strlen (pat);-
153 replen = strlen (rep);-
154 for (temp = (char *)NULL, i = templen = tempsize = 0, repl = 1; string[i]; )
string[i]Description
TRUEevaluated 39 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 3 times by 1 test
Evaluated by:
  • Self test
3-39
155 {-
156 if (repl && STREQN (string + i, pat, patlen))
never executed: __result = (((const unsigned char *) (const char *) ( string + i ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
never executed: __result = (((const unsigned char *) (const char *) ( pat ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
(patlen == 0)Description
TRUEnever evaluated
FALSEevaluated 39 times by 1 test
Evaluated by:
  • Self test
replDescription
TRUEevaluated 39 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
((patlen == 0)...len ))) == 0))Description
TRUEevaluated 9 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 30 times by 1 test
Evaluated by:
  • Self test
(string + i)[0] == (pat)[0]Description
TRUEevaluated 9 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 30 times by 1 test
Evaluated by:
  • Self test
(__extension__...atlen ))) == 0Description
TRUEevaluated 9 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
__builtin_cons...t_p ( patlen )Description
TRUEnever evaluated
FALSEevaluated 9 times by 1 test
Evaluated by:
  • Self test
__builtin_cons...( string + i )Description
TRUEnever evaluated
FALSEnever evaluated
strlen ( strin...t) ( patlen ))Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_constant_p ( pat )Description
TRUEnever evaluated
FALSEnever evaluated
strlen ( pat )...t) ( patlen ))Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
0-39
157 {-
158 if (replen)
replenDescription
TRUEevaluated 9 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-9
159 RESIZE_MALLOCED_BUFFER (temp, templen, replen, tempsize, (replen * 2));
never executed: tempsize += ((replen * 2));
never executed: end of block
executed 9 times by 1 test: end of block
Executed by:
  • Self test
(templen) + (r...n) >= tempsizeDescription
TRUEnever evaluated
FALSEevaluated 9 times by 1 test
Evaluated by:
  • Self test
(templen) + (r...n) >= tempsizeDescription
TRUEnever evaluated
FALSEnever evaluated
0-9
160-
161 for (r = rep; *r; ) /* can rep == "" */
*rDescription
TRUEevaluated 9 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 9 times by 1 test
Evaluated by:
  • Self test
9
162 temp[templen++] = *r++;
executed 9 times by 1 test: temp[templen++] = *r++;
Executed by:
  • Self test
9
163-
164 i += patlen ? patlen : 1; /* avoid infinite recursion */
patlenDescription
TRUEevaluated 9 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-9
165 repl = global != 0;-
166 }
executed 9 times by 1 test: end of block
Executed by:
  • Self test
9
167 else-
168 {-
169 RESIZE_MALLOCED_BUFFER (temp, templen, 1, tempsize, 16);
executed 3 times by 1 test: tempsize += (16);
Executed by:
  • Self test
executed 3 times by 1 test: end of block
Executed by:
  • Self test
(templen) + (1) >= tempsizeDescription
TRUEevaluated 3 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 27 times by 1 test
Evaluated by:
  • Self test
(templen) + (1) >= tempsizeDescription
TRUEevaluated 3 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 3 times by 1 test
Evaluated by:
  • Self test
3-27
170 temp[templen++] = string[i++];-
171 }
executed 30 times by 1 test: end of block
Executed by:
  • Self test
30
172 }-
173 if (temp)
tempDescription
TRUEevaluated 3 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-3
174 temp[templen] = 0;
executed 3 times by 1 test: temp[templen] = 0;
Executed by:
  • Self test
3
175 else-
176 temp = savestring (string);
never executed: temp = (char *)strcpy (sh_xmalloc((1 + strlen (string)), "stringlib.c", 176), (string));
0
177 return (temp);
executed 3 times by 1 test: return (temp);
Executed by:
  • Self test
3
178}-
179-
180/* Replace all instances of C in STRING with TEXT. TEXT may be empty or-
181 NULL. If DO_GLOB is non-zero, we quote the replacement text for-
182 globbing. Backslash may be used to quote C. */-
183char *-
184strcreplace (string, c, text, do_glob)-
185 char *string;-
186 int c;-
187 const char *text;-
188 int do_glob;-
189{-
190 char *ret, *p, *r, *t;-
191 int len, rlen, ind, tlen;-
192-
193 len = STRLEN (text);
(text)[1]Description
TRUEevaluated 136 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
(text)[2]Description
TRUEevaluated 136 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
(text)Description
TRUEevaluated 136 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
(text)[0]Description
TRUEevaluated 136 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-136
194 rlen = len + strlen (string) + 2;-
195 ret = (char *)xmalloc (rlen);-
196-
197 for (p = string, r = ret; p && *p; )
pDescription
TRUEevaluated 272 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
*pDescription
TRUEevaluated 136 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 136 times by 1 test
Evaluated by:
  • Self test
0-272
198 {-
199 if (*p == c)
*p == cDescription
TRUEevaluated 136 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-136
200 {-
201 if (len)
lenDescription
TRUEevaluated 136 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-136
202 {-
203 ind = r - ret;-
204 if (do_glob && (glob_pattern_p (text) || strchr (text, '\\')))
do_globDescription
TRUEevaluated 136 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
glob_pattern_p (text)Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • Self test
FALSEevaluated 135 times by 1 test
Evaluated by:
  • Self test
(__extension__...ext , '\\' )))Description
TRUEevaluated 4 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 131 times by 1 test
Evaluated by:
  • Self test
__builtin_constant_p ( '\\' )Description
TRUEevaluated 135 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
!__builtin_constant_p ( text )Description
TRUEevaluated 135 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
( '\\' ) == '\0'Description
TRUEnever evaluated
FALSEevaluated 135 times by 1 test
Evaluated by:
  • Self test
0-136
205 {-
206 t = quote_globbing_chars (text);-
207 tlen = strlen (t);-
208 RESIZE_MALLOCED_BUFFER (ret, ind, tlen, rlen, rlen);
never executed: rlen += (rlen);
never executed: end of block
(ind) + (tlen) >= rlenDescription
TRUEnever evaluated
FALSEevaluated 5 times by 1 test
Evaluated by:
  • Self test
(ind) + (tlen) >= rlenDescription
TRUEnever evaluated
FALSEnever evaluated
0-5
209 r = ret + ind; /* in case reallocated */-
210 strcpy (r, t);-
211 r += tlen;-
212 free (t);-
213 }
executed 5 times by 1 test: end of block
Executed by:
  • Self test
5
214 else-
215 {-
216 RESIZE_MALLOCED_BUFFER (ret, ind, len, rlen, rlen);
never executed: rlen += (rlen);
never executed: end of block
(ind) + (len) >= rlenDescription
TRUEnever evaluated
FALSEevaluated 131 times by 1 test
Evaluated by:
  • Self test
(ind) + (len) >= rlenDescription
TRUEnever evaluated
FALSEnever evaluated
0-131
217 r = ret + ind; /* in case reallocated */-
218 strcpy (r, text);-
219 r += len;-
220 }
executed 131 times by 1 test: end of block
Executed by:
  • Self test
131
221 }-
222 p++;-
223 continue;
executed 136 times by 1 test: continue;
Executed by:
  • Self test
136
224 }-
225-
226 if (*p == '\\' && p[1] == c)
*p == '\\'Description
TRUEnever evaluated
FALSEnever evaluated
p[1] == cDescription
TRUEnever evaluated
FALSEnever evaluated
0
227 p++;
never executed: p++;
0
228-
229 ind = r - ret;-
230 RESIZE_MALLOCED_BUFFER (ret, ind, 2, rlen, rlen);
never executed: rlen += (rlen);
never executed: end of block
(ind) + (2) >= rlenDescription
TRUEnever evaluated
FALSEnever evaluated
(ind) + (2) >= rlenDescription
TRUEnever evaluated
FALSEnever evaluated
0
231 r = ret + ind; /* in case reallocated */-
232 *r++ = *p++;-
233 }
never executed: end of block
0
234 *r = '\0';-
235-
236 return ret;
executed 136 times by 1 test: return ret;
Executed by:
  • Self test
136
237}-
238-
239#ifdef INCLUDE_UNUSED-
240/* Remove all leading whitespace from STRING. This includes-
241 newlines. STRING should be terminated with a zero. */-
242void-
243strip_leading (string)-
244 char *string;-
245{-
246 char *start = string;-
247-
248 while (*string && (whitespace (*string) || *string == '\n'))-
249 string++;-
250-
251 if (string != start)-
252 {-
253 int len = strlen (string);-
254 FASTCOPY (string, start, len);-
255 start[len] = '\0';-
256 }-
257}-
258#endif-
259-
260/* Remove all trailing whitespace from STRING. This includes-
261 newlines. If NEWLINES_ONLY is non-zero, only trailing newlines-
262 are removed. STRING should be terminated with a zero. */-
263void-
264strip_trailing (string, len, newlines_only)-
265 char *string;-
266 int len;-
267 int newlines_only;-
268{-
269 while (len >= 0)
len >= 0Description
TRUEevaluated 6505045 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 67 times by 1 test
Evaluated by:
  • Self test
67-6505045
270 {-
271 if ((newlines_only && string[len] == '\n') ||
newlines_onlyDescription
TRUEevaluated 6505045 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
string[len] == '\n'Description
TRUEevaluated 3252361 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 3252684 times by 1 test
Evaluated by:
  • Self test
0-6505045
272 (!newlines_only && whitespace (string[len])))
!newlines_onlyDescription
TRUEnever evaluated
FALSEevaluated 3252684 times by 1 test
Evaluated by:
  • Self test
((string[len]) == ' ')Description
TRUEnever evaluated
FALSEnever evaluated
((string[len]) == '\t')Description
TRUEnever evaluated
FALSEnever evaluated
0-3252684
273 len--;
executed 3252361 times by 1 test: len--;
Executed by:
  • Self test
3252361
274 else-
275 break;
executed 3252684 times by 1 test: break;
Executed by:
  • Self test
3252684
276 }-
277 string[len + 1] = '\0';-
278}
executed 3252751 times by 1 test: end of block
Executed by:
  • Self test
3252751
279-
280/* A wrapper for bcopy that can be prototyped in general.h */-
281void-
282xbcopy (s, d, n)-
283 char *s, *d;-
284 int n;-
285{-
286 FASTCOPY (s, d, n);-
287}
executed 745028 times by 1 test: end of block
Executed by:
  • Self test
745028
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2