1 | |
2 | |
3 | |
4 | |
5 | #include "system.h" |
6 | |
7 | #include <stdlib.h> |
8 | #include <rpm/argv.h> |
9 | #include <rpm/rpmstring.h> |
10 | |
11 | #include "debug.h" |
12 | |
13 | void argvPrint(const char * msg, ARGV_const_t argv, FILE * fp) |
14 | { |
15 | ARGV_const_t av; |
16 | |
17 | if (fp == NULL((void*)0)) fp = stderrstderr; |
18 | |
19 | if (msg) |
20 | fprintf(fp, "===================================== %s\n", msg); |
21 | |
22 | if (argv) |
23 | for (av = argv; *av; av++) |
24 | fprintf(fp, "%s\n", *av); |
25 | |
26 | } |
27 | |
28 | ARGV_t argvNew(void) |
29 | { |
30 | ARGV_t argv = xcalloc(1, sizeof(*argv))rcalloc((1), (sizeof(*argv))); |
31 | return argv; |
32 | } |
33 | |
34 | ARGI_t argiFree(ARGI_t argi) |
35 | { |
36 | if (argi) { |
37 | argi->nvals = 0; |
38 | free(argi->vals); |
39 | free(argi); |
40 | } |
41 | return NULL((void*)0); |
42 | } |
43 | |
44 | ARGV_t argvFree(ARGV_t argv) |
45 | { |
46 | if (argv) { |
47 | for (ARGV_t av = argv; *av; av++) |
48 | free(*av); |
49 | free(argv); |
50 | } |
51 | return NULL((void*)0); |
52 | } |
53 | |
54 | int argiCount(ARGI_const_t argi) |
55 | { |
56 | int nvals = 0; |
57 | if (argi) |
58 | nvals = argi->nvals; |
59 | return nvals; |
60 | } |
61 | |
62 | ARGint_t argiData(ARGI_const_t argi) |
63 | { |
64 | ARGint_t vals = NULL((void*)0); |
65 | if (argi && argi->nvals > 0) |
66 | vals = argi->vals; |
67 | return vals; |
68 | } |
69 | |
70 | int argvCount(ARGV_const_t argv) |
71 | { |
72 | int argc = 0; |
73 | if (argv) |
74 | while (argv[argc] != NULL((void*)0)) |
75 | argc++; |
76 | return argc; |
77 | } |
78 | |
79 | ARGV_t argvData(ARGV_t argv) |
80 | { |
81 | return argv; |
82 | } |
83 | |
84 | int argvCmp(const void * a, const void * b) |
85 | { |
86 | const char *astr = *(ARGV_t)a; |
87 | const char *bstr = *(ARGV_t)b; |
88 | return strcmp(astr, bstr); |
89 | } |
90 | |
91 | int argvSort(ARGV_t argv, int (*compar)(const void *, const void *)) |
92 | { |
93 | if (compar == NULL((void*)0)) |
94 | compar = argvCmp; |
95 | qsort(argv, argvCount(argv), sizeof(*argv), compar); |
96 | return 0; |
97 | } |
98 | |
99 | ARGV_t argvSearch(ARGV_const_t argv, const char *val, |
100 | int (*compar)(const void *, const void *)) |
101 | { |
102 | if (argv == NULL((void*)0)) |
103 | return NULL((void*)0); |
104 | if (compar == NULL((void*)0)) |
105 | compar = argvCmp; |
106 | return bsearch(&val, argv, argvCount(argv), sizeof(*argv), compar); |
107 | } |
108 | |
109 | int argiAdd(ARGI_t * argip, int ix, int val) |
110 | { |
111 | ARGI_t argi; |
112 | |
113 | if (argip == NULL((void*)0)) |
114 | return -1; |
115 | if (*argip == NULL((void*)0)) |
116 | *argip = xcalloc(1, sizeof(**argip))rcalloc((1), (sizeof(**argip))); |
117 | argi = *argip; |
118 | if (ix < 0) |
119 | ix = argi->nvals; |
120 | if (ix >= argi->nvals) { |
121 | argi->vals = xrealloc(argi->vals, (ix + 1) * sizeof(*argi->vals))rrealloc((argi->vals), ((ix + 1) * sizeof(*argi->vals)) ); |
122 | memset(argi->vals + argi->nvals, 0, |
123 | (ix - argi->nvals) * sizeof(*argi->vals)); |
124 | argi->nvals = ix + 1; |
125 | } |
126 | argi->vals[ix] = val; |
127 | return 0; |
128 | } |
129 | |
130 | int argvAdd(ARGV_t * argvp, const char *val) |
131 | { |
132 | ARGV_t argv; |
133 | int argc; |
134 | |
135 | if (argvp == NULL((void*)0)) |
136 | return -1; |
137 | argc = argvCount(*argvp); |
138 | *argvp = xrealloc(*argvp, (argc + 1 + 1) * sizeof(**argvp))rrealloc((*argvp), ((argc + 1 + 1) * sizeof(**argvp))); |
139 | argv = *argvp; |
140 | argv[argc++] = xstrdup(val)rstrdup((val)); |
141 | argv[argc ] = NULL((void*)0); |
142 | return 0; |
143 | } |
144 | |
145 | int argvAddNum(ARGV_t *argvp, int val) |
146 | { |
147 | char *valstr = NULL((void*)0); |
148 | int rc; |
149 | rasprintf(&valstr, "%d", val); |
150 | rc = argvAdd(argvp, valstr); |
151 | free(valstr); |
152 | return rc; |
153 | } |
154 | |
155 | int argvAppend(ARGV_t * argvp, ARGV_const_t av) |
156 | { |
157 | ARGV_t argv = *argvp; |
158 | int argc = argvCount(argv); |
159 | int ac = argvCount(av); |
160 | int i; |
161 | |
162 | argv = xrealloc(argv, (argc + ac + 1) * sizeof(*argv))rrealloc((argv), ((argc + ac + 1) * sizeof(*argv))); |
163 | for (i = 0; i < ac; i++) |
164 | argv[argc + i] = xstrdup(av[i])rstrdup((av[i])); |
165 | argv[argc + ac] = NULL((void*)0); |
166 | *argvp = argv; |
167 | return 0; |
168 | } |
169 | |
170 | ARGV_t argvSplitString(const char * str, const char * seps, argvFlags flags) |
171 | { |
172 | char *dest = NULL((void*)0); |
173 | ARGV_t argv; |
174 | int argc = 1; |
175 | const char * s; |
176 | char * t; |
177 | int c; |
178 | |
179 | if (str == NULL((void*)0) || seps == NULL((void*)0)) |
180 | return NULL((void*)0); |
181 | |
182 | dest = xmalloc(strlen(str) + 1)rmalloc((strlen(str) + 1)); |
183 | for (argc = 1, s = str, t = dest; (c = *s); s++, t++) { |
184 | if (strchr(seps, c)) { |
185 | argc++; |
186 | c = '\0'; |
187 | } |
188 | *t = c; |
189 | } |
190 | *t = '\0'; |
191 | |
192 | argv = xmalloc( (argc + 1) * sizeof(*argv))rmalloc(((argc + 1) * sizeof(*argv))); |
193 | |
194 | for (c = 0, s = dest; s < t; s+= strlen(s) + 1) { |
195 | if (*s == '\0' && (flags & ARGV_SKIPEMPTY)) |
196 | continue; |
197 | argv[c] = xstrdup(s)rstrdup((s)); |
198 | c++; |
199 | } |
200 | argv[c] = NULL((void*)0); |
201 | free(dest); |
202 | return argv; |
203 | } |
204 | |
205 | |
206 | int argvSplit(ARGV_t * argvp, const char * str, const char * seps) |
207 | { |
208 | if (argvp) { |
209 | *argvp = argvSplitString(str, seps, ARGV_SKIPEMPTY); |
210 | } |
211 | return 0; |
212 | } |
213 | |
214 | char *argvJoin(ARGV_const_t argv, const char *sep) |
215 | { |
216 | int argc = 0; |
217 | size_t argvlen = 0; |
218 | char *dest = NULL((void*)0); |
219 | |
220 | if (argv) { |
| 1 | Assuming 'argv' is non-null | |
|
| |
221 | ARGV_const_t arg; |
222 | for (arg = argv; *arg; arg++) |
| 3 | | Loop condition is true. Entering loop body | |
|
| 4 | | Loop condition is false. Execution continues on line 224 | |
|
223 | argvlen += strlen(*arg); |
224 | argc = arg - argv; |
225 | } |
226 | |
227 | if (argc > 0) { |
| |
| |
228 | size_t seplen = (sep != NULL((void*)0)) ? strlen(sep) : 0; |
| 7 | | Assuming 'sep' is equal to NULL | |
|
| |
229 | char *p; |
230 | |
231 | dest = xmalloc(argvlen + (seplen * (argc - 1)) + 1)rmalloc((argvlen + (seplen * (argc - 1)) + 1)); |
232 | |
233 | p = stpcpy(dest, argv[0]); |
234 | for (int i = 1; i < argc; i++) { |
| 9 | | Assuming 'i' is < 'argc' | |
|
| 10 | | Loop condition is true. Entering loop body | |
|
235 | if (seplen) |
| |
236 | p = stpcpy(p, sep); |
237 | p = stpcpy(p, argv[i]); |
| 12 | | Null pointer passed as an argument to a 'nonnull' parameter |
|
238 | } |
239 | *p = '\0'; |
240 | } |
241 | |
242 | return dest; |
243 | } |
244 | |