From 88e96a42fe503015fa5673d995884b834142b033 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 7 Jul 2013 18:06:52 +0200 Subject: [PATCH] htdocs: web archive. when addfile/deletefile/renamefile are done, show message and OK button. --- htdocs/archive/addfile.c | 24 ++++++++++--------- htdocs/archive/deletefile.c | 46 ++++++++++++++++--------------------- htdocs/archive/renamefile.c | 19 ++++++--------- htdocs/archive/webarchive.h | 13 +++++++++++ 4 files changed, 53 insertions(+), 49 deletions(-) diff --git a/htdocs/archive/addfile.c b/htdocs/archive/addfile.c index 33d0f5787..e1b6f8af8 100644 --- a/htdocs/archive/addfile.c +++ b/htdocs/archive/addfile.c @@ -51,33 +51,35 @@ int main() { const char *query_string = getenv("QUERY_STRING"); if (query_string == NULL) { - printf("Content-type: text/plain\r\n\r\n"); - printf("empty/invalid data\n"); + generatepage("Internal error: empty/invalid data"); } else if (strlen(query_string) > 1024) { - printf("Content-type: text/plain\r\n\r\n"); - printf("data size limit exceeded (1024)\n"); + generatepage("Internal error: data size limit exceeded (1024)"); } else if (NULL != validate(query_string)) { - printf("Content-type: text/plain\r\n\r\n"); - printf("%s\n", validate(query_string)); + generatepage(validate(query_string)); } else { char data[4096] = {0}; unencode(query_string, data); - printf("Content-type: text/plain\r\n\r\n"); - if (NULL != validate(data)) { - printf("%s\n", validate(data)); + generatepage(validate(data)); } else { char *olddata[MAX_RECORDS] = {0}; olddata[0] = data; - readdata(&olddata[1], MAX_RECORDS-1); + if (!readdata(&olddata[1], MAX_RECORDS-1)) { + generatepage("Failed to add file (access denied). Try again."); + return EXIT_SUCCESS; + } sortdata(olddata, MAX_RECORDS); FILE *f = fopen("data.txt", "wt"); + if (f == NULL) { + generatepage("Failed to add file (access denied). Try again."); + return EXIT_SUCCESS; + } for (int i = 0; i < MAX_RECORDS && olddata[i]; i++) fprintf(f, "%s\n", olddata[i]); fclose(f); - printf("saved\n"); + generatepage("saved."); } } diff --git a/htdocs/archive/deletefile.c b/htdocs/archive/deletefile.c index 0b2bcb7da..057839c3f 100644 --- a/htdocs/archive/deletefile.c +++ b/htdocs/archive/deletefile.c @@ -27,17 +27,15 @@ int main() { const char *query_string = getenv("QUERY_STRING"); if (query_string == NULL) { - printf("Content-type: text/plain\r\n\r\n"); - puts("empty/invalid data"); + generatepage("Internal error: invalid request"); } else if (NULL != validate(query_string)) { - printf("Content-type: text/plain\r\n\r\n"); - puts(validate(query_string)); + generatepage(validate(query_string)); } else { char *data[MAX_RECORDS] = {0}; if (!readdata(data, MAX_RECORDS)) { - printf("Content-type: text/plain\r\n\r\n"); - puts("failed to delete file, try again"); + generatepage("Failed to delete file, try again"); + return EXIT_SUCCESS; } sortdata(data, MAX_RECORDS); @@ -52,30 +50,26 @@ int main() } if (index == -1) { - puts("Content-type: text/plain\r\n\r\n"); - puts("file not found"); + generatepage("File not found"); return EXIT_SUCCESS; } - if (index >= 0) { - int deleted = 0; - FILE *f = fopen("data.txt", "wt"); - if (f != NULL) { - for (int i = 0; i < MAX_RECORDS && data[i]; i++) { - if (i != index) - fprintf(f, "%s\n", data[i]); - else - deleted = 1; - } - fclose(f); - - puts("Content-type: text/plain\r\n\r\n"); - puts(deleted ? "file deleted" : "failed to delete file"); - } else { - puts("Content-type: text/plain\r\n\r\n"); - puts("failed to delete file, try again"); - } + FILE *f = fopen("data.txt", "wt"); + if (f == NULL) { + generatepage("Failed to delete file (access denied)"); + return EXIT_SUCCESS; } + + int deleted = 0; + for (int i = 0; i < MAX_RECORDS && data[i]; i++) { + if (i != index) + fprintf(f, "%s\n", data[i]); + else + deleted = 1; + } + fclose(f); + + generatepage(deleted ? "File deleted" : "Failed to delete file"); } return EXIT_SUCCESS; diff --git a/htdocs/archive/renamefile.c b/htdocs/archive/renamefile.c index 1fe4cc5f2..6d3434448 100644 --- a/htdocs/archive/renamefile.c +++ b/htdocs/archive/renamefile.c @@ -42,17 +42,15 @@ int main() { const char *query_string = getenv("QUERY_STRING"); if (query_string == NULL) { - printf("Content-type: text/plain\r\n\r\n"); - puts("empty/invalid data"); + generatepage("Internal error: empty/invalid data"); } else if (NULL != validate(query_string)) { - printf("Content-type: text/plain\r\n\r\n"); - puts(validate(query_string)); + generatepage(validate(query_string)); } else { char *data[MAX_RECORDS] = {0}; if (!readdata(data, MAX_RECORDS)) { - printf("Content-type: text/plain\r\n\r\n"); - puts("access failed, try again"); + generatepage("access failed, try again"); + return EXIT_SUCCESS; } sortdata(data, MAX_RECORDS); @@ -74,15 +72,13 @@ int main() } if (index == -1) { - puts("Content-type: text/plain\r\n\r\n"); - puts("file not found"); + generatepage("file not found"); return EXIT_SUCCESS; } FILE *f = fopen("data.txt", "wt"); if (f == NULL) { - puts("Content-type: text/plain\r\n\r\n"); - puts("failed to rename file (access denied), try again"); + generatepage("failed to rename file (access denied), try again"); return EXIT_SUCCESS; } @@ -95,8 +91,7 @@ int main() fclose(f); - puts("Content-type: text/plain\r\n\r\n"); - puts("file renamed"); + generatepage("file renamed"); } return EXIT_SUCCESS; diff --git a/htdocs/archive/webarchive.h b/htdocs/archive/webarchive.h index 2c0568dbb..bdd7a9299 100644 --- a/htdocs/archive/webarchive.h +++ b/htdocs/archive/webarchive.h @@ -55,3 +55,16 @@ void sortdata(char * * const data, int sz) } } } + +void generatepage(const char msg[]) +{ + puts("Content-type: text/html\r\n\r\n"); + puts(""); + puts(""); + puts(""); + puts(msg); + puts("
"); +} +