bump copyright year
[nci.git] / nci-list-courses.c
blob3c3f0ba88af8bc99e7ffc9139205b19715cd731a
1 /*
2 * Copyright (c) 2016-2018 S. Gilles <sgilles@math.umd.edu>
4 * Permission to use, copy, modify, and/or distribute this software
5 * for any purpose with or without fee is hereby granted, provided
6 * that the above copyright notice and this permission notice appear
7 * in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
13 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
14 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <errno.h>
19 #include <locale.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
24 #include <curl/curl.h>
25 #include <yajl_parse.h>
27 #include "macros.h"
28 #include "util.h"
30 static int list_courses(const char *url_base, const char *auth_token)
32 size_t j = 0;
33 int ret = EINVAL;
34 size_t len = 0;
35 char *built_uri = 0;
36 const char *fn_courses[] = { "id", "name", 0 };
37 map courses = { 0 };
38 char **courses_ids = 0;
39 char **c = 0;
40 size_t courses_num = 0;
42 len = snprintf(0, 0, "%s/api/v1/courses?per_page=9999", url_base);
44 if (len + 1 < len) {
45 ret = errno = EOVERFLOW;
46 perror(L(""));
47 goto cleanup;
50 if (!(built_uri = malloc(len + 1))) {
51 ret = errno;
52 perror(L("malloc"));
53 goto cleanup;
56 sprintf(built_uri, "%s/api/v1/courses?per_page=9999", url_base);
58 if ((ret = key_value_extract(built_uri, auth_token, fn_courses, 0,
59 &courses))) {
60 goto cleanup;
63 map_get_keys(&courses, &courses_ids, &courses_num);
65 for (j = 0; j < courses_num; ++j) {
66 c = map_get(&courses, courses_ids[j]);
68 if (c[0]) {
69 printf("%s %s\n", courses_ids[j], c[0]);
73 cleanup:
74 free(courses_ids);
75 map_clean(&courses);
76 free(built_uri);
78 return ret;
81 int main(void)
83 int ret = EINVAL;
84 char *url_base = 0;
85 char *auth_token = 0;
87 setlocale(LC_ALL, "");
88 curl_global_init(CURL_GLOBAL_DEFAULT);
90 if (!(url_base = get_url_base())) {
91 ret = ENOENT;
93 /* Error should have already been printed */
94 goto cleanup;
97 if (!(auth_token = get_auth_token())) {
98 ret = ENOENT;
100 /* Error should have already been printed */
101 goto cleanup;
104 if ((ret = list_courses(url_base, auth_token))) {
105 goto cleanup;
108 ret = 0;
109 cleanup:
110 free(url_base);
111 free(auth_token);
112 curl_global_cleanup();
114 return ret;