diff --git a/htdocs/archive/submit.c b/htdocs/archive/addfile.c similarity index 100% rename from htdocs/archive/submit.c rename to htdocs/archive/addfile.c diff --git a/htdocs/archive/deletefile.c b/htdocs/archive/deletefile.c new file mode 100644 index 000000000..0b2bcb7da --- /dev/null +++ b/htdocs/archive/deletefile.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include + +#include "webarchive.h" + +const char *validate(const char *data) +{ + int i; + if (strncmp(data,"name=",5) != 0) + return "invalid query string: must start with 'name='"; + i = 5; + while (isalnum(data[i])) + i++; + if (i == 5) + return "invalid query string: no name"; + if (i > 35) + return "invalid query string: max name size is 32"; + if (data[i] != '\0') + return "invalid delete command"; + + return NULL; +} + +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"); + } else if (NULL != validate(query_string)) { + printf("Content-type: text/plain\r\n\r\n"); + puts(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"); + } + sortdata(data, MAX_RECORDS); + + char name[32] = {0}; + strcpy(name, getname(query_string)); + int index = -1; + for (int i = 0; i < MAX_RECORDS && data[i]; i++) { + if (strcmp(name, getname(data[i])) == 0) { + index = i; + break; + } + } + + if (index == -1) { + puts("Content-type: text/plain\r\n\r\n"); + puts("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"); + } + } + } + + return EXIT_SUCCESS; +} + diff --git a/htdocs/archive/renamefile.c b/htdocs/archive/renamefile.c new file mode 100644 index 000000000..1fe4cc5f2 --- /dev/null +++ b/htdocs/archive/renamefile.c @@ -0,0 +1,104 @@ +#include +#include +#include +#include + +#include "webarchive.h" + +const char *validate(const char *data) +{ + int i; + + // name1 + if (strncmp(data,"name1=",6) != 0) + return "invalid query string: must start with 'name1='"; + i = 6; + while (isalnum(data[i])) + i++; + if (i == 6) + return "invalid query string: no name1"; + if (i > 36) + return "invalid query string: max name1 size is 30"; + + // name2 + const int i1 = i; + if (strncmp(data+i,"&name2=",7) != 0) + return "invalid query string: no name2"; + i += 7; + if (!isalnum(data[i])) + return "invalid query string: empty name2"; + while (isalnum(data[i])) + i++; + if (i - i1 > 37) + return "invalid query string: max name2 size is 30"; + + if (data[i] != '\0') + return "invalid query string: invalid char in name2"; + + return NULL; +} + +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"); + } else if (NULL != validate(query_string)) { + printf("Content-type: text/plain\r\n\r\n"); + puts(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"); + } + sortdata(data, MAX_RECORDS); + + // Get name1 and name2.. + char buf[strlen(query_string)]; + strcpy(buf, query_string); + const char * const name1 = strstr(buf, "name1=") + 6; + const char * const name2 = strstr(buf, "name2=") + 6; + for (int i = 0; buf[i] != '\0'; i++) { + if (buf[i] == '&') + buf[i] = '\0'; + } + int index = -1; + for (int i = 0; i < MAX_RECORDS && data[i]; i++) { + if (strcmp(name1, getname(data[i])) == 0) { + index = i; + break; + } + } + + if (index == -1) { + puts("Content-type: text/plain\r\n\r\n"); + puts("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"); + return EXIT_SUCCESS; + } + + for (int i = 0; i < MAX_RECORDS && data[i]; i++) { + if (i == index) + fprintf(f, "name=%s%s\n", name2, data[i]+5+strlen(name1)); + else + fprintf(f, "%s\n", data[i]); + } + + fclose(f); + + puts("Content-type: text/plain\r\n\r\n"); + puts("file renamed"); + } + + return EXIT_SUCCESS; +} + diff --git a/htdocs/archive/report.c b/htdocs/archive/report.c index b8bc102cf..6cc8fd54d 100644 --- a/htdocs/archive/report.c +++ b/htdocs/archive/report.c @@ -9,13 +9,39 @@ void listAll(char **data) { puts("Content-type: text/html\r\n\r\n"); - puts("\n"); - puts("\n"); + puts(""); + puts(""); + puts(""); + puts(""); + puts(""); + puts(""); + puts("
"); for (int i = 0; i < MAX_RECORDS && data[i]; i++) { const char *name = getname(data[i]); - printf("\n", name); + printf("", name); + printf("", name); + printf("", name); + printf("", name); + printf("\n"); } - puts("
%sDelete this
%s
\n"); + puts("
"); puts(""); } @@ -48,7 +74,7 @@ int main() sortdata(data,MAX_RECORDS); const char *query_string = getenv("QUERY_STRING"); - if (query_string == NULL) { + if (query_string == NULL || *query_string == '\0') { listAll(data); } else if (strncmp(query_string, "name=", 5) == 0 && getname(query_string) != NULL) { char name[32] = {0}; diff --git a/htdocs/archive/webarchive.h b/htdocs/archive/webarchive.h index 4801d1acb..2c0568dbb 100644 --- a/htdocs/archive/webarchive.h +++ b/htdocs/archive/webarchive.h @@ -1,4 +1,5 @@ #include +#include #define MAX_RECORDS 1000 @@ -12,7 +13,10 @@ int readdata(char * * const data, int sz) int i = 0; while (i < sz && fgets(line,sizeof(line)-2,f)) { if (strncmp(line, "name=", 5) == 0) { - data[i] = malloc(strlen(line)); + int len = strlen(line); + while (line[len-1] == '\n' || line[len-1] == '\r' || line[len-1] == '\t' || line[len-1] == ' ') + line[--len] = '\0'; + data[i] = malloc(len); strcpy(data[i], line); i++; } @@ -46,8 +50,8 @@ void sortdata(char * * const data, int sz) char *p = data[i-1]; data[i-1] = data[i]; data[i] = p; - if (i > 1) - i--; + if (i >= 2) + i -= 2; } } }