Debian Codegraph @

src/playlist.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2007, 2009 Moritz Grimm <[email protected]>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
20
21 #include <sys/stat.h>
22 #ifdef HAVE_SYS_RANDOM_H
23 # include <sys/random.h>
24 #endif
25
26 #include <errno.h>
27 #include <limits.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
32 #include <unistd.h>
33
34 #include "log.h"
35 #include "playlist.h"
36 #include "xalloc.h"
37
38 /* Usually defined in <sys/stat.h>. */
39 #ifndef S_IEXEC
40 # define S_IEXEC S_IXUSR
41 #endif /* !S_IEXEC */
42
43 struct playlist {
44 char *filename;
45 char **list;
46 size_t size;
47 size_t num;
48 size_t index;
49 int program;
50 char *prog_track;
51 };
52
53 static struct playlist * _playlist_create(const char *);
54 static int _playlist_add(struct playlist *, const char *);
55 static unsigned int _playlist_random(void);
56 static const char * _playlist_run_program(struct playlist *);
57
58 static struct playlist *
59 _playlist_create(const char *filename)
60 {
61 struct playlist *pl;
62
63 pl = xcalloc(1UL, sizeof(*pl));
64 pl->filename = xstrdup(filename);
65
66 return (pl);
67 }
68
69 static int
70 _playlist_add(struct playlist *pl, const char *entry)
71 {
72 size_t num;
73
74 num = pl->num + 1;
75
76 if (pl->size == 0) {
77 pl->list = xcalloc(2UL, sizeof(char *));
78 pl->size = 2 * sizeof(char *);
79 }
80
81 if (pl->size / sizeof(char *) <= num) {
82 size_t i;
83
84 pl->list = xreallocarray(pl->list, 2UL, pl->size);
85 pl->size = 2 * pl->size;
86
87 for (i = num; i < pl->size / sizeof(char *); i++)
88 pl->list[i] = NULL;
89 }
90
91 pl->list[num - 1] = xstrdup(entry);
92 pl->num = num;
93
94 return (1);
95 }
96
97 static unsigned int
98 _playlist_random(void)
99 {
100 unsigned int ret = 0;
101
102 #ifdef HAVE_ARC4RANDOM
103 ret = arc4random();
104 #elif HAVE_GETRANDOM
105 if (sizeof(ret) != getrandom(&ret, sizeof(ret), 0)) {
106 log_alert("getrandom: %s", strerror(errno));
107 exit(1);
108 }
109 #else
110 # warning "using deterministic randomness for shuffling playlists"
111 ret = (unsigned int)random();
112 #endif
113
114 return (ret);
115 }
116
117 static const char *
118 _playlist_run_program(struct playlist *pl)
119 {
120 FILE *filep;
121 char buf[PATH_MAX + 1];
122
123 fflush(NULL);
124 errno = 0;
125 log_debug("running command: %s", pl->filename);
126 if ((filep = popen(pl->filename, "r")) == NULL) {
127 /* popen() does not set errno reliably ... */
128 if (errno)
129 log_error("execution error: %s: %s", pl->filename,
130 strerror(errno));
131 else
132 log_error("execution error: %s", pl->filename);
133 return (NULL);
134 }
135
136 if (NULL == fgets(buf, (int)sizeof(buf), filep)) {
137 if (ferror(filep))
138 log_error("%s: output read error: %s", pl->filename,
139 strerror(ferror(filep)));
140 else
141 log_error("%s: output read error: EOF", pl->filename);
142 pclose(filep);
143 return (NULL);
144 }
145 pclose(filep);
146
147 if (strlen(buf) == sizeof(buf) - 1) {
148 log_error("%s: output too long", pl->filename);
149 return (NULL);
150 }
151
152 buf[strcspn(buf, "\n")] = '\0';
153 buf[strcspn(buf, "\r")] = '\0';
154 if (buf[0] == '\0')
155 /* Empty line (end of playlist.) */
156 return (NULL);
157
158 xfree(pl->prog_track);
159 pl->prog_track = xstrdup(buf);
160
161 return ((const char *)pl->prog_track);
162 }
163
164 int
165 playlist_init(void)
166 {
167 srandom((unsigned int)time(NULL));
168
169 return (0);
170 }
171
172 void playlist_exit(void) {}
173
174 struct playlist *
175 playlist_read(const char *filename)
176 {
177 struct playlist *pl;
178 unsigned long line;
179 FILE *filep;
180 char buf[PATH_MAX];
181
182 if (filename != NULL) {
183 pl = _playlist_create(filename);
184
185 if ((filep = fopen(filename, "r")) == NULL) {
186 log_error("%s: %s", filename, strerror(errno));
187 playlist_free(&pl);
188 return (NULL);
189 }
190 } else {
191 pl = _playlist_create("stdin");
192
193 if ((filep = fdopen(STDIN_FILENO, "r")) == NULL) {
194 log_error("stdin: %s", strerror(errno));
195 playlist_free(&pl);
196 return (NULL);
197 }
198 }
199
200 line = 0;
201 while (fgets(buf, (int)sizeof(buf), filep) != NULL) {
202 line++;
203
204 if (strlen(buf) == sizeof(buf) - 1) {
205 char skip_buf[2];
206
207 log_error("%s[%lu]: file or path name too long",
208 pl->filename, line);
209 /* Discard any excess chars in that line. */
210 while (fgets(skip_buf, (int)sizeof(skip_buf), filep) != NULL) {
211 if (skip_buf[0] == '\n')
212 break;
213 }
214 continue;
215 }
216
217 /*
218 * fgets() would happily fill a buffer with
219 * { '\0', ..., '\n', '\0' }. Also skip lines that begin with
220 * a '#', which is considered a comment.
221 */
222 if (buf[0] == '\0' || buf[0] == '#')
223 continue;
224
225 /* Trim any trailing newlines and carriage returns. */
226 buf[strcspn(buf, "\n")] = '\0';
227 buf[strcspn(buf, "\r")] = '\0';
228
229 /* Skip lines that are empty after the trimming. */
230 if (buf[0] == '\0')
231 continue;
232
233 /* We got one. */
234 if (!_playlist_add(pl, buf)) {
235 fclose(filep);
236 playlist_free(&pl);
237 return (NULL);
238 }
239 }
240 if (ferror(filep)) {
241 log_error("playlist_read: %s: %s", pl->filename,
242 strerror(errno));
243 fclose(filep);
244 playlist_free(&pl);
245 return (NULL);
246 }
247
248 fclose(filep);
249 return (pl);
250 }
251
252 struct playlist *
253 playlist_program(const char *filename)
254 {
255 struct playlist *pl;
256 struct stat st;
257
258 if (stat(filename, &st) == -1) {
259 log_error("%s: %s", filename, strerror(errno));
260 return (NULL);
261 }
262 if (st.st_mode & S_IWOTH) {
263 log_error("%s: world writeable", filename);
264 return (NULL);
265 }
266 if (!(st.st_mode & (S_IEXEC | S_IXGRP | S_IXOTH))) {
267 log_error("%s: not an executable program", filename);
268 return (NULL);
269 }
270
271 pl = _playlist_create(filename);
272 pl->program = 1;
273
274 return (pl);
275 }
276
277 void
278 playlist_free(struct playlist **pl_p)
279 {
280 struct playlist *pl;
281
282 if (pl_p == NULL || (pl = *pl_p) == NULL)
283 return;
284
285 if (pl->filename != NULL) {
286 xfree(pl->filename);
287 pl->filename = NULL;
288 }
289
290 if (pl->list != NULL) {
291 if (pl->size > 0) {
292 size_t i;
293
294 for (i = 0; i < pl->size / sizeof(char *); i++) {
295 if (pl->list[i] != NULL) {
296 xfree(pl->list[i]);
297 pl->list[i] = NULL;
298 } else
299 break;
300 }
301 }
302
303 xfree(pl->list);
304 }
305
306 if (pl->prog_track != NULL) {
307 xfree(pl->prog_track);
308 pl->prog_track = NULL;
309 }
310
311 xfree(*pl_p);
312 *pl_p = NULL;
313 }
314
315 const char *
316 playlist_get_next(struct playlist *pl)
317 {
318 if (pl->program)
319 return (_playlist_run_program(pl));
320
321 if (pl->num == 0)
322 return (NULL);
323
324 return ((const char *)pl->list[pl->index++]);
325 }
326
327 void
328 playlist_skip_next(struct playlist *pl)
329 {
330 if (pl->program || pl->num == 0)
331 return;
332
333 if (pl->list[pl->index] != NULL)
334 pl->index++;
335 }
336
337 unsigned long
338 playlist_get_num_items(struct playlist *pl)
339 {
340 if (pl->program)
341 return (0);
342
343 return ((unsigned long)pl->num);
344 }
345
346 unsigned long
347 playlist_get_position(struct playlist *pl)
348 {
349 if (pl->program)
350 return (0);
351
352 return ((unsigned long)pl->index);
353 }
354
355 int
356 playlist_goto_entry(struct playlist *pl, const char *entry)
357 {
358 unsigned long i;
359
360 if (pl->program)
361 return (0);
362
363 for (i = 0; i < pl->num; i++) {
364 if (strcmp(pl->list[i], entry) == 0) {
365 pl->index = (size_t)i;
366 return (1);
367 }
368 }
369
370 return (0);
371 }
372
373 void
374 playlist_rewind(struct playlist *pl)
375 {
376 if (pl->program)
377 return;
378
379 pl->index = 0;
380 }
381
382 int
383 playlist_reread(struct playlist **plist)
384 {
385 struct playlist *new_pl, *pl;
386
387 pl = *plist;
388
389 if (pl->program)
390 return (0);
391
392 if ((new_pl = playlist_read(pl->filename)) == NULL)
393 return (0);
394
395 playlist_free(&pl);
396 *plist = new_pl;
397
398 return (1);
399 }
400
401 /*
402 * Yet another implementation of the "Knuth Shuffle":
403 */
404 void
405 playlist_shuffle(struct playlist *pl)
406 {
407 size_t d, i;
408 char *temp;
409
410 if (pl->program || pl->num < 2)
411 return;
412
413 for (i = 0; i < pl->num; i++) {
414 size_t range;
415
416 range = pl->num - i;
417
418 /*
419 * Only accept a random number if it is smaller than the
420 * largest multiple of our range. This reduces PRNG bias.
421 */
422 do {
423 d = (unsigned long)_playlist_random();
424 } while (d > RAND_MAX - (RAND_MAX % range));
425
426 /*
427 * The range starts at the item we want to shuffle, excluding
428 * already shuffled items.
429 */
430 d = i + (d % range);
431
432 temp = pl->list[d];
433 pl->list[d] = pl->list[i];
434 pl->list[i] = temp;
435 }
436 }