tvote.c: add preliminary implementation to create polls - vote - simple cgi voting system for web and gopher
 (HTM) git clone git://src.adamsgaard.dk/vote
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 4cb97e229cbb0d54d85e3960e3d7c4daa53a429e
 (DIR) parent f5b5aff4672c9a58d24ef7a2d06629870e411a6e
 (HTM) Author: Anders Damsgaard <anders@adamsgaard.dk>
       Date:   Sun, 27 Sep 2020 10:42:16 +0200
       
       vote.c: add preliminary implementation to create polls
       
       Diffstat:
         M vote.c                              |      72 ++++++++++++++++++++++++++++---
       
       1 file changed, 66 insertions(+), 6 deletions(-)
       ---
 (DIR) diff --git a/vote.c b/vote.c
       t@@ -97,9 +97,67 @@ print_poll_file(FILE *fp, const char *poll_name)
        }
        
        void
       +create_poll_file(const char *name, const char *question, const char *options)
       +{
       +        FILE *fp;
       +        char fname[PATH_MAX];
       +        char buf[PATH_MAX];
       +        struct stat sb;
       +
       +        strlcpy(buf, name, sizeof(buf));
       +        escapechars(buf);
       +        if (snprintf(fname, sizeof(fname), "%s/%s", POLLS_DIR, buf) < 0) {
       +                http_status(500);
       +                err(1, "create_poll_file: snprintf fname %s/%s", POLLS_DIR, buf);
       +        }
       +
       +        if (stat(fname, &sb) == 0) {
       +                printf("<p>Poll '%s' already exists</p>", name);
       +        } else {
       +
       +                if (!(fp = fopen(fname, "w"))) {
       +                        http_status(404);
       +                        exit(1);
       +                } else {
       +                        fputs(question, fp);
       +                        fputs("\n0\t", fp);
       +                        /*while (*question != '\0') {
       +                                switch(*question) {
       +                                case '<':
       +                                case '>':
       +                                        fputc(' ', fp);
       +                                        break;
       +                                default:
       +                                        fputc(*question, fp);
       +                                }
       +                                (void)*question++;
       +                        }*/
       +                        while (*options != '\0') {
       +                                switch(*options) {
       +                                case '<':
       +                                case '>':
       +                                case '\t':
       +                                        fputc(' ', fp);
       +                                        break;
       +                                case '\n':
       +                                        fprintf(fp, "\n0\t");
       +                                        break;
       +                                default:
       +                                        fputc(*options, fp);
       +                                        break;
       +                                }
       +                                (void)*options++;
       +                        }
       +                        fputc('\n', fp);
       +                        fclose(fp);
       +                }
       +        }
       +}
       +
       +void
        show_poll(const char *poll_name)
        {
       -        FILE *fd;
       +        FILE *fp;
                char fname[PATH_MAX];
                char buf[PATH_MAX];
        
       t@@ -107,15 +165,15 @@ show_poll(const char *poll_name)
                escapechars(buf);
                if (snprintf(fname, sizeof(fname), "%s/%s", POLLS_DIR, buf) < 0) {
                        http_status(500);
       -                err(1, "snprintf fname %s/%s", POLLS_DIR, buf);
       +                err(1, "show_poll: snprintf fname %s/%s", POLLS_DIR, buf);
                }
        
       -        if (!(fd = fopen(fname, "r"))) {
       +        if (!(fp = fopen(fname, "r"))) {
                        http_status(404);
                        exit(1);
                } else {
       -                print_poll_file(fd, poll_name);
       -                fclose(fd);
       +                print_poll_file(fp, poll_name);
       +                fclose(fp);
                }
        }
        
       t@@ -145,7 +203,7 @@ main()
                        err(1, "unveil");
                }
        
       -        if (pledge("stdio cpath rpath", NULL) == -1) {
       +        if (pledge("stdio cpath rpath wpath", NULL) == -1) {
                        http_status(500);
                        err(1, "pledge");
                }
       t@@ -163,6 +221,8 @@ main()
                if (*poll)
                        show_poll(poll);
        
       +        create_poll_file("test poll2", "What is your favorite color?", "Red\nGreen\nBlue");
       +
                print_html_foot();
        
                return 0;