Debian Codegraph @

src/util.c

raw symbols SCIP
1 /*
2 * ezstream - source client for Icecast with external en-/decoder support
3 * Copyright (C) 2003, 2004, 2005, 2006 Ed Zaleski <[email protected]>
4 * Copyright (C) 2007, 2009, 2017, 2020 Moritz Grimm <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16 #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:412:9`!
17 # include "config.h"cxx . . $ `<file>/src/config.h`/
18 #endif
19
20 #include "compat.h"cxx . . $ `<file>/compat/compat.h`/
21
22 #include <sys/types.h>
23 #include <sys/file.h>
24
25 #include <ctype.h>
26 #include <errno.h>
27 #include <langinfo.h>
28 #include <limits.h>
29 #include <locale.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
33
34 #ifdef HAVE_ICONVcxx . . $ `src/config.h:17:9`!
35 # include <iconv.h>
36 #endif
37
38 #include "log.h"cxx . . $ `<file>/src/log.h`/
39 #include "util.h"cxx . . $ `<file>/src/util.h`/
40 #include "xalloc.h"cxx . . $ `<file>/src/xalloc.h`/
41
42 #ifndef BUFSIZcxx . . $ `<external>/232824719582829767/stdio.h:104:9`!
43 # define BUFSIZ 1024
44 #endif
45
46 static char *pidfile_pathcxx . . $ pidfile_path.;
47 static FILEcxx . . $ FILE# *pidfile_filecxx . . $ pidfile_file.;
48 static pid_tcxx . . $ pid_t# pidfile_pidcxx . . $ pidfile_pid.;
49 static unsigned int pidfile_numlockscxx . . $ pidfile_numlocks.;
50
51 static char * _util_iconvertcxx . . $ _util_iconvert(11d6a3b64d462fd0).(const char *, const char *, const char *);
52 static void _util_cleanup_pidfilecxx . . $ _util_cleanup_pidfile(49f6e7a06ebc5aa8).(void);
53
54 static char *
55 _util_iconvertcxx . . $ _util_iconvert(11d6a3b64d462fd0).(const char *in_str, const char *from, const char *to)
56 {
57 #ifdef HAVE_ICONVcxx . . $ `src/config.h:17:9`!
58 iconv_tcxx . . $ iconv_t# cd;
59 ICONV_CONSTcxx . . $ `src/config.h:74:9`! char *input, *ip;
60 size_t input_len;
61 char *output;
62 size_t output_size;
63 char buf[BUFSIZcxx . . $ `<external>/232824719582829767/stdio.h:104:9`!], *bp;
64 size_t bufavail;
65 size_t out_pos;
66 char *tocode;
67
68 if (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! == in_str)
69 return (xstrdupcxx . . $ `src/xalloc.h:25:9`!(""));
70
71 tocode = xstrdupcxx . . $ `src/xalloc.h:25:9`!(to);
72 if ((cd = iconv_opencxx . . $ iconv_open(12e6862c879384a7).(tocode, from)) == (iconv_tcxx . . $ iconv_t#)-1 &&
73 (cd = iconv_opencxx . . $ iconv_open(12e6862c879384a7).("", from)) == (iconv_tcxx . . $ iconv_t#)-1 &&
74 (cd = iconv_opencxx . . $ iconv_open(12e6862c879384a7).(tocode, "")) == (iconv_tcxx . . $ iconv_t#)-1) {
75 xfreecxx . . $ `src/xalloc.h:26:9`!(tocode);
76 log_syserrcxx . . $ log_syserr(7edabc5ef8d007a9).(ERRORcxx . . $ ERROR., errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!, "iconv_open");
77 return (xstrdupcxx . . $ `src/xalloc.h:25:9`!(in_str));
78 }
79
80 ip = input = (ICONV_CONSTcxx . . $ `src/config.h:74:9`! char *)in_str;
81 input_len = strlencxx . . $ strlen(751346f8406fd082).(input);
82 output_size = 1;
83 output = xcalloccxx . . $ `src/xalloc.h:23:9`!(output_size, sizeof(char));
84 out_pos = 0;
85 output[out_pos] = '\0';
86 while (input_len > 0) {
87 char *op;
88 size_t count;
89
90 buf[0] = '\0';
91 bp = buf;
92 bufavail = sizeof(buf) - 1;
93
94 if (iconvcxx . . $ iconv(df3c92a1a902995d).(cd, &ip, &input_len, &bp, &bufavail) == (size_t)-1 &&
95 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != E2BIGcxx . . $ `<external>/8987895939074593727/errno-base.h:11:9`!) {
96 *bp++ = '?';
97 ip++;
98 input_len--;
99 bufavail--;
100 }
101 *bp = '\0';
102
103 count = sizeof(buf) - bufavail - 1;
104
105 output_size += count;
106 op = output = xreallocarraycxx . . $ `src/xalloc.h:24:9`!(output, output_size, sizeof(char));
107 op += out_pos;
108 memcpycxx . . $ memcpy(cc817cb175f282d).(op, buf, count);
109 out_pos += count;
110 op += count;
111 *op = '\0';
112 }
113
114 if (iconv_closecxx . . $ iconv_close(dedf1e9b1c614a07).(cd) == -1) {
115 log_syserrcxx . . $ log_syserr(7edabc5ef8d007a9).(ERRORcxx . . $ ERROR., errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!, "iconv_close");
116 xfreecxx . . $ `src/xalloc.h:26:9`!(output);
117 xfreecxx . . $ `src/xalloc.h:26:9`!(tocode);
118 return (xstrdupcxx . . $ `src/xalloc.h:25:9`!(in_str));
119 }
120
121 xfreecxx . . $ `src/xalloc.h:26:9`!(tocode);
122 return (output);
123 #else
124 (void)from;
125 (void)to;
126
127 if (NULL == in_str)
128 return (xstrdup(""));
129
130 return (xstrdup(in_str));
131 #endif /* HAVE_ICONV */
132 }
133
134 static void
135 _util_cleanup_pidfilecxx . . $ _util_cleanup_pidfile(49f6e7a06ebc5aa8).(void)
136 {
137 if (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! != pidfile_pathcxx . . $ pidfile_path. && getpidcxx . . $ getpid(b126dc7c1de90089).() == pidfile_pidcxx . . $ pidfile_pid.) {
138 (void)unlinkcxx . . $ unlink(5c4306d8553b3696).(pidfile_pathcxx . . $ pidfile_path.);
139 (void)fclosecxx . . $ fclose(89bc1efcdac0ca22).(pidfile_filecxx . . $ pidfile_file.);
140 }
141 }
142
143 const char *
144 util_get_prognamecxx . . $ util_get_progname(fb58d2e09a2a1ce6).(const char *argv0)
145 {
146 #ifdef HAVE___PROGNAMEcxx . . $ `src/config.h:71:9`!
147 extern char *__prognamecxx . . $ __progname.;
148
149 (void)argv0;
150
151 return (__prognamecxx . . $ __progname.);
152 #else
153 if (argv0 == NULL) {
154 return (UTIL_DEFAULT_PROGNAME);
155 } else {
156 const char *p = strrchr(argv0, '/');
157
158 if (p == NULL)
159 p = argv0;
160 else
161 p++;
162
163 return (p);
164 }
165 #endif /* HAVE___PROGNAME */
166 }
167
168 int
169 util_write_pid_filecxx . . $ util_write_pid_file(5c4306d8553b3696).(const char *path)
170 {
171 int save_errno = 0;
172 pid_tcxx . . $ pid_t# pid;
173
174 if (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! == path)
175 return (0);
176
177 xfreecxx . . $ `src/xalloc.h:26:9`!(pidfile_path);
178 pidfile_pathcxx . . $ pidfile_path. = xstrdupcxx . . $ `src/xalloc.h:25:9`!(path);
179
180 if (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! != pidfile_filecxx . . $ pidfile_file.)
181 fclosecxx . . $ fclose(89bc1efcdac0ca22).(pidfile_filecxx . . $ pidfile_file.);
182 if (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! == (pidfile_filecxx . . $ pidfile_file. = fopencxx . . $ fopen(82ec0787cd37e0a3).(pidfile_pathcxx . . $ pidfile_path., "w"))) {
183 xfreecxx . . $ `src/xalloc.h:26:9`!(pidfile_path);
184 pidfile_pathcxx . . $ pidfile_path. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
185 return (-1);
186 }
187
188 pid = getpidcxx . . $ getpid(b126dc7c1de90089).();
189 if (0 >= fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(pidfile_filecxx . . $ pidfile_file., "%ld\n", (long)pid) ||
190 0 > fflushcxx . . $ fflush(89bc1efcdac0ca22).(pidfile_filecxx . . $ pidfile_file.) ||
191 0 > flockcxx . . $ flock(9b79fb6aee4c0440).(filenocxx . . $ fileno(89bc1efcdac0ca22).(pidfile_filecxx . . $ pidfile_file.), LOCK_EXcxx . . $ `<external>/8566841665215861632/file.h:41:9`! | LOCK_NBcxx . . $ `<external>/8566841665215861632/file.h:45:9`!))
192 goto error;
193
194 if (0 == pidfile_numlockscxx . . $ pidfile_numlocks.) {
195 pidfile_pidcxx . . $ pidfile_pid. = pid;
196 if (0 != atexitcxx . . $ atexit(8919c3b35e68c42d).(_util_cleanup_pidfilecxx . . $ _util_cleanup_pidfile(49f6e7a06ebc5aa8).))
197 goto error;
198 pidfile_numlockscxx . . $ pidfile_numlocks.++;
199 }
200
201 return (0);
202
203 error:
204 save_errno = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
205 (void)unlinkcxx . . $ unlink(5c4306d8553b3696).(pidfile_pathcxx . . $ pidfile_path.);
206 xfreecxx . . $ `src/xalloc.h:26:9`!(pidfile_path);
207 pidfile_pathcxx . . $ pidfile_path. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
208 (void)fclosecxx . . $ fclose(89bc1efcdac0ca22).(pidfile_filecxx . . $ pidfile_file.);
209 pidfile_filecxx . . $ pidfile_file. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
210 pidfile_pidcxx . . $ pidfile_pid. = 0;
211 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = save_errno;
212
213 return (-1);
214 }
215
216 int
217 util_strrcmpcxx . . $ util_strrcmp(5ae29d726ae57f88).(const char *s, const char *sub)
218 {
219 size_t slen = strlencxx . . $ strlen(751346f8406fd082).(s);
220 size_t sublen = strlencxx . . $ strlen(751346f8406fd082).(sub);
221
222 if (sublen > slen)
223 return (1);
224
225 return (!!memcmpcxx . . $ memcmp(36f949851efa3055).(s + slen - sublen, sub, sublen));
226 }
227
228 int
229 util_strrcasecmpcxx . . $ util_strrcasecmp(5ae29d726ae57f88).(const char *s, const char *sub)
230 {
231 char *s_cpy = xstrdupcxx . . $ `src/xalloc.h:25:9`!(s);
232 char *sub_cpy = xstrdupcxx . . $ `src/xalloc.h:25:9`!(sub);
233 char *p;
234 int ret;
235
236 for (p = s_cpy; *p != '\0'; p++)
237 *p = (char)tolowercxx . . $ `<external>/17904084882347303253/ctype.h:220:11`!((int)*p);
238
239 for (p = sub_cpy; *p != '\0'; p++)
240 *p = (char)tolowercxx . . $ `<external>/17904084882347303253/ctype.h:220:11`!((int)*p);
241
242 ret = util_strrcmpcxx . . $ util_strrcmp(5ae29d726ae57f88).(s_cpy, sub_cpy);
243
244 xfreecxx . . $ `src/xalloc.h:26:9`!(s_cpy);
245 xfreecxx . . $ `src/xalloc.h:26:9`!(sub_cpy);
246
247 return (ret);
248 }
249
250 char *
251 util_char2utf8cxx . . $ util_char2utf8(a8b8e32347182212).(const char *in_str)
252 {
253 char *codeset;
254
255 setlocalecxx . . $ setlocale(10ed28bce9115888).(LC_CTYPEcxx . . $ `<external>/3736975272097309885/locale.h:35:9`!, "");
256 codeset = nl_langinfocxx . . $ nl_langinfo(ff224f080444ee45).((nl_itemcxx . . $ nl_item#)CODESETcxx . . $ `<external>/4324151732927229490/langinfo.h:357:9`!);
257 setlocalecxx . . $ setlocale(10ed28bce9115888).(LC_CTYPEcxx . . $ `<external>/3736975272097309885/locale.h:35:9`!, "C");
258
259 return (_util_iconvertcxx . . $ _util_iconvert(11d6a3b64d462fd0).(in_str, codeset, "UTF-8"));
260 }
261
262 char *
263 util_utf82charcxx . . $ util_utf82char(a8b8e32347182212).(const char *in_str)
264 {
265 char *codeset;
266
267 setlocalecxx . . $ setlocale(10ed28bce9115888).(LC_CTYPEcxx . . $ `<external>/3736975272097309885/locale.h:35:9`!, "");
268 codeset = nl_langinfocxx . . $ nl_langinfo(ff224f080444ee45).((nl_itemcxx . . $ nl_item#)CODESETcxx . . $ `<external>/4324151732927229490/langinfo.h:357:9`!);
269 setlocalecxx . . $ setlocale(10ed28bce9115888).(LC_CTYPEcxx . . $ `<external>/3736975272097309885/locale.h:35:9`!, "C");
270
271 return (_util_iconvertcxx . . $ _util_iconvert(11d6a3b64d462fd0).(in_str, "UTF-8", codeset));
272 }
273
274 char *
275 util_expand_wordscxx . . $ util_expand_words(27058f725fae1c57).(const char *in, struct util_dictcxx . . $ util_dict# dicts[])
276 {
277 size_t i;
278 char *out;
279 size_t out_size = strlencxx . . $ strlen(751346f8406fd082).(in) + 1;
280
281 /* empty input string? */
282 if (1 == out_size)
283 return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
284
285 out = xstrdupcxx . . $ `src/xalloc.h:25:9`!(in);
286 i = out_size - 1;
287 while (i--) {
288 struct util_dictcxx . . $ util_dict# *d = dicts;
289
290 while (d && d->fromcxx . . $ util_dict#from.) {
291 if (0 == strncmpcxx . . $ strncmp(fd278af2f295fa2e).(&out[i], d->fromcxx . . $ util_dict#from., strlencxx . . $ strlen(751346f8406fd082).(d->fromcxx . . $ util_dict#from.))) {
292 char *buf, *tmp;
293 size_t buf_len;
294
295 buf_len = strlencxx . . $ strlen(751346f8406fd082).(&out[i]) + strlencxx . . $ strlen(751346f8406fd082).(d->tocxx . . $ util_dict#to.)
296 - strlencxx . . $ strlen(751346f8406fd082).(d->fromcxx . . $ util_dict#from.);
297 buf = xcalloccxx . . $ `src/xalloc.h:23:9`!(buf_len + 1, sizeof(*buf));
298 snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, buf_len + 1, "%s%s",
299 d->tocxx . . $ util_dict#to., &out[i + strlencxx . . $ strlen(751346f8406fd082).(d->fromcxx . . $ util_dict#from.)]);
300
301 out_size += buf_len;
302 tmp = xcalloccxx . . $ `src/xalloc.h:23:9`!(out_size, sizeof(*tmp));
303 snprintfcxx . . $ snprintf(ea502d73e74c754c).(tmp, i + 1, "%s", out);
304 snprintfcxx . . $ snprintf(ea502d73e74c754c).(tmp + i, out_size - i, "%s", buf);
305 freecxx . . $ free(5d22bdacc48458e8).(buf);
306 freecxx . . $ free(5d22bdacc48458e8).(out);
307 out = tmp;
308
309 break;
310 }
311 d++;
312 }
313 }
314
315 return (out);
316 }
317
318 #define SHELLQUOTE_OUTLEN_MAXcxx . . $ `src/util.c:318:9`! 8191UL
319
320 char *
321 util_shellquotecxx . . $ util_shellquote(eabdfdef77619b3d).(const char *in, size_t outlen_max)
322 {
323 char *out, *out_p;
324 size_t out_len;
325 const char *in_p;
326
327 if (!outlen_max || outlen_max > SHELLQUOTE_OUTLEN_MAXcxx . . $ `src/util.c:318:9`!)
328 outlen_max = SHELLQUOTE_OUTLEN_MAXcxx . . $ `src/util.c:318:9`!;
329 out_len = outlen_max;
330
331 out = xcalloccxx . . $ `src/xalloc.h:23:9`!(out_len + 1, sizeof(char));
332
333 out_p = out;
334 in_p = in;
335
336 *out_p++ = '\'';
337 out_len--;
338 while (*in_p && 1 < out_len) {
339 int stop = 0;
340
341 switch (*in_p) {
342 case '\'':
343 if (4 < out_len) {
344 *out_p++ = '\'';
345 *out_p++ = '\\';
346 *out_p++ = '\'';
347 out_len -= 3;
348 } else
349 stop = 1;
350 break;
351 default:
352 break;
353 }
354 if (stop)
355 break;
356 *out_p++ = *in_p++;
357 out_len--;
358 }
359 *out_p++ = '\'';
360
361 return (out);
362 }