-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
71 lines (57 loc) · 1.16 KB
/
util.c
File metadata and controls
71 lines (57 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* See LICENSE file for copyright and license details. */
#include "util.h"
int
setplaytoken(struct info *data)
{
char *js;
char url[] = "http://8tracks.com/sets/new";
int errn;
size_t len;
json_t *root, *playtoken, *status;
if (data->playtoken)
return SUCCESS;
js = NULL;
root = NULL;
/* Fetch url */
js = malloc(1);
if (js == NULL)
err(1, NULL);
errn = fetch(&js, url);
if (errn == ERROR)
goto error;
/* Parse json string */
root = json_loads(js, 0, NULL);
if (root == NULL)
goto error;
status = json_object_get(root, "status");
if (strcmp(json_string_value(status), "200 OK") != 0)
goto error;
playtoken = json_object_get(root, "play_token");
if (playtoken == NULL)
goto error;
/* Set playtoken */
len = strlen(json_string_value(playtoken)) + 1;
data->playtoken = malloc(len * sizeof(char));
if (data->playtoken == NULL)
err(1, NULL);
(void)strlcpy(data->playtoken, json_string_value(playtoken), len);
/* Cleanup */
free(js);
json_decref(root);
return SUCCESS;
error:
if (js)
free(js);
if (root)
json_decref(root);
return ERROR;
}
int
mod(int a, int b)
{
int ret;
ret = a % b;
if (ret < 0)
ret += b;
return ret;
}