aboutsummaryrefslogtreecommitdiff
path: root/examples/append.c
blob: fe854922e7ec1f737372ac8b766657d2ba317486 (plain)
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
/* This is the very basic example, showing work with form fields to append
 * specified by user in form addon into some file (i.e. "file_to_append").
 */
#include <magi.h>
#include <stdio.h>


void append(magi_request *r)
{
    char *data = magi_request_param(r, "addon");
    if (data) {  /* If field "addon" was in request: */
        FILE *file = fopen("file_to_append", "a");
        fputs(data, file);  /* Append addon into the file. */
        fclose(file);
        printf("<p>Appended!</p>");  /* And display success message. */
    }
}

void response(magi_request *r)
{
    magi_response_default();  /* Pass default headers and send body: */
    printf("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' "
           "'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>"
           "<html xmlns='http://www.w3.org/1999/xhtml'>"
           "<head><title>Append to File</title></head>"
           "<body>");
    append(r);
    printf("<form action='/cgi-bin/append' method='post'><fieldset>"
           "<input type='text' name='addon' value='Whatever you want to add.' />"
           "<input type='submit' value='Append' />"
           "</fieldset></form>"
           "</body></html>");
}

int main()
{
    magi_request request;
    magi_request_init(&request);  /* Setting defaults. */
    if (magi_parse(&request)) {   /* If parsing was done successful */
        response(&request);       /* we need to response the request. */
    } else {                      /* And display error overwise: */
        magi_error_response(request.error);
    }
    magi_request_free(&request);  /* Don't forget to free everything after. */
    return 0;
}