Reformat everything
[nci.git] / nci-edit-assignment.c
blobc9e3eb7bac21985d0027ef47414fcf4688f0ccb2
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>
23 #include <unistd.h>
25 #include <curl/curl.h>
26 #include <yajl_parse.h>
28 #include "macros.h"
29 #include "util.h"
31 int
32 main(int argc, char **argv)
34 int ret = EINVAL;
35 char *url_base = 0;
36 char *auth_token = 0;
37 char *course_id = 0;
38 char *assignment_id = 0;
39 char *name = 0;
40 char *max_points_arg = 0;
41 double max_points_d = 0;
42 char *max_points = 0;
43 char *due_date = 0;
44 char *group_id = 0;
45 size_t len = 0;
46 char *built_uri = 0;
47 struct curl_httppost *post = 0;
48 int opt = 0;
50 setlocale(LC_ALL, "");
52 while ((opt = getopt(argc, argv, "a:c:n:m:d:g:")) != -1) {
53 switch (opt) {
54 case 'c':
55 course_id = optarg;
56 break;
57 case 'a':
58 assignment_id = optarg;
59 break;
60 case 'n':
61 name = optarg;
62 break;
63 case 'm':
64 max_points_arg = optarg;
65 break;
66 case 'd':
67 due_date = optarg;
68 break;
69 case 'g':
70 group_id = optarg;
71 break;
72 default:
73 break;
77 if (!course_id) {
78 ret = EINVAL;
79 fprintf(stderr, "course-id is mandatory\n");
80 goto cleanup;
83 if (!assignment_id) {
84 ret = EINVAL;
85 fprintf(stderr, "assignment-id is mandatory\n");
86 goto cleanup;
89 if (max_points_arg) {
90 max_points_d = strtod(max_points_arg, 0);
91 len = snprintf(0, 0, "%lf", max_points_d);
93 if (len + 1 < len) {
94 ret = errno = EOVERFLOW;
95 perror(L(""));
96 goto cleanup;
99 if (!(max_points = malloc(len + 1))) {
100 ret = errno;
101 perror(L("malloc"));
102 goto cleanup;
105 sprintf(max_points, "%lf", max_points_d);
108 curl_global_init(CURL_GLOBAL_DEFAULT);
110 if (!(url_base = get_url_base())) {
111 ret = ENOENT;
113 /* Error should have already been printed */
114 goto cleanup;
117 if (!(auth_token = get_auth_token())) {
118 ret = ENOENT;
120 /* Error should have already been printed */
121 goto cleanup;
124 len = snprintf(0, 0, "%s/api/v1/courses/%s/assignments/%s", url_base,
125 course_id, assignment_id);
127 if (len + 1 < len) {
128 ret = errno = EOVERFLOW;
129 perror(L(""));
130 goto cleanup;
133 if (!(built_uri = malloc(len + 1))) {
134 ret = errno;
135 perror(L("malloc"));
136 goto cleanup;
139 sprintf(built_uri, "%s/api/v1/courses/%s/assignments/%s", url_base,
140 course_id, assignment_id);
142 if (!(post = make_assignment_post(name, max_points, due_date,
143 group_id))) {
144 /* XXX: pass back proper error code from make_assignment_post() */
145 goto cleanup;
148 if ((ret = send_and_id_scan(built_uri, auth_token, post, "PUT", 0))) {
149 goto cleanup;
152 ret = 0;
153 cleanup:
154 curl_formfree(post);
155 free(built_uri);
156 free(max_points);
157 free(url_base);
158 free(auth_token);
159 curl_global_cleanup();
161 return ret;