fix segfault on total grade calculation with no assignments
[nci.git] / nci-edit-assignment.c
blob29837f48cf221c6c06f25d754f8d3509934f34e5
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 main(int argc, char **argv)
33 int ret = EINVAL;
34 char *url_base = 0;
35 char *auth_token = 0;
36 char *course_id = 0;
37 char *assignment_id = 0;
38 char *name = 0;
39 char *max_points_arg = 0;
40 double max_points_d = 0;
41 char *max_points = 0;
42 char *due_date = 0;
43 char *group_id = 0;
44 size_t len = 0;
45 char *built_uri = 0;
46 struct curl_httppost *post = 0;
47 int opt = 0;
49 setlocale(LC_ALL, "");
51 while ((opt = getopt(argc, argv, "a:c:n:m:d:g:")) != -1) {
52 switch (opt) {
53 case 'c':
54 course_id = optarg;
55 break;
56 case 'a':
57 assignment_id = optarg;
58 break;
59 case 'n':
60 name = optarg;
61 break;
62 case 'm':
63 max_points_arg = optarg;
64 break;
65 case 'd':
66 due_date = optarg;
67 break;
68 case 'g':
69 group_id = optarg;
70 break;
71 default:
72 break;
76 if (!course_id) {
77 ret = EINVAL;
78 fprintf(stderr, "course-id is mandatory\n");
79 goto cleanup;
82 if (!assignment_id) {
83 ret = EINVAL;
84 fprintf(stderr, "assignment-id is mandatory\n");
85 goto cleanup;
88 if (max_points_arg) {
89 max_points_d = strtod(max_points_arg, 0);
90 len = snprintf(0, 0, "%lf", max_points_d);
92 if (len + 1 < len) {
93 ret = errno = EOVERFLOW;
94 perror(L(""));
95 goto cleanup;
98 if (!(max_points = malloc(len + 1))) {
99 ret = errno;
100 perror(L("malloc"));
101 goto cleanup;
104 sprintf(max_points, "%lf", max_points_d);
107 curl_global_init(CURL_GLOBAL_DEFAULT);
109 if (!(url_base = get_url_base())) {
110 ret = ENOENT;
112 /* Error should have already been printed */
113 goto cleanup;
116 if (!(auth_token = get_auth_token())) {
117 ret = ENOENT;
119 /* Error should have already been printed */
120 goto cleanup;
123 len = snprintf(0, 0, "%s/api/v1/courses/%s/assignments/%s", url_base,
124 course_id, assignment_id);
126 if (len + 1 < len) {
127 ret = errno = EOVERFLOW;
128 perror(L(""));
129 goto cleanup;
132 if (!(built_uri = malloc(len + 1))) {
133 ret = errno;
134 perror(L("malloc"));
135 goto cleanup;
138 sprintf(built_uri, "%s/api/v1/courses/%s/assignments/%s", url_base,
139 course_id, assignment_id);
141 if (!(post = make_assignment_post(name, max_points, due_date,
142 group_id))) {
143 /* XXX: pass back proper error code from make_assignment_post() */
144 goto cleanup;
147 if ((ret = send_and_id_scan(built_uri, auth_token, post, "PUT", 0))) {
148 goto cleanup;
151 ret = 0;
152 cleanup:
153 curl_formfree(post);
154 free(built_uri);
155 free(max_points);
156 free(url_base);
157 free(auth_token);
158 curl_global_cleanup();
160 return ret;