Reorder ld commands, hopefully for less dependency on $(LD)=$(CC)
[nci.git] / nci-delete-assignment.c
blob88c96f058019a02ba78443cd2a0fe124abfd20bf
1 /*
2 * Copyright (c) 2016, 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 <string.h>
24 #include <unistd.h>
26 #include <curl/curl.h>
27 #include <yajl_parse.h>
29 #include "macros.h"
30 #include "util.h"
32 int 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 size_t len = 0;
40 char *built_uri = 0;
41 char *deleted_id = 0;
42 int opt = 0;
44 setlocale(LC_ALL, "");
46 while ((opt = getopt(argc, argv, "c:a:")) != -1) {
47 switch (opt) {
48 case 'c':
49 course_id = optarg;
50 break;
51 case 'a':
52 assignment_id = optarg;
53 break;
54 default:
55 break;
59 if (!course_id) {
60 ret = EINVAL;
61 fprintf(stderr, "course-id is mandatory\n");
62 goto cleanup;
65 if (!assignment_id) {
66 ret = EINVAL;
67 fprintf(stderr, "assignment-id is mandatory\n");
68 goto cleanup;
71 curl_global_init(CURL_GLOBAL_DEFAULT);
73 if (!(url_base = get_url_base())) {
74 ret = ENOENT;
76 /* Error should have already been printed */
77 goto cleanup;
80 if (!(auth_token = get_auth_token())) {
81 ret = ENOENT;
83 /* Error should have already been printed */
84 goto cleanup;
87 len = snprintf(0, 0, "%s/api/v1/courses/%s/assignments/%s", url_base,
88 course_id, assignment_id);
90 if (!(built_uri = malloc(len + 1))) {
91 ret = errno;
92 perror(L("malloc"));
93 goto cleanup;
96 sprintf(built_uri, "%s/api/v1/courses/%s/assignments/%s", url_base,
97 course_id, assignment_id);
99 if ((ret = send_and_id_scan(built_uri, auth_token, 0, "DELETE",
100 &deleted_id))) {
101 goto cleanup;
104 if (deleted_id) {
105 printf("%s\n", deleted_id);
108 ret = 0;
109 cleanup:
110 free(deleted_id);
111 free(built_uri);
112 free(url_base);
113 free(auth_token);
114 curl_global_cleanup();
116 return ret;