diff --git a/htdocs/archive/deletefile.c b/htdocs/archive/deletefile.c index 057839c3f..72d74f6a5 100644 --- a/htdocs/archive/deletefile.c +++ b/htdocs/archive/deletefile.c @@ -5,73 +5,58 @@ #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) { generatepage("Internal error: invalid request"); - } else if (NULL != validate(query_string)) { - generatepage(validate(query_string)); - } else { - - char *data[MAX_RECORDS] = {0}; - if (!readdata(data, MAX_RECORDS)) { - generatepage("Failed to delete file, try again"); - return EXIT_SUCCESS; - } - 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) { - generatepage("File not found"); - return EXIT_SUCCESS; - } - - 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; } + if (NULL != validate_name_version(query_string)) { + generatepage(validate_name_version(query_string)); + return EXIT_SUCCESS; + } + + char *data[MAX_RECORDS] = {0}; + if (!readdata(data, MAX_RECORDS)) { + generatepage("Failed to delete file, try again"); + return EXIT_SUCCESS; + } + 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) { + generatepage("File not found"); + return EXIT_SUCCESS; + } + + 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/edit.c b/htdocs/archive/edit.c index 247cfc646..ce65af1c6 100644 --- a/htdocs/archive/edit.c +++ b/htdocs/archive/edit.c @@ -57,8 +57,8 @@ int main() puts(""); puts("
"); printf(" \n",name); - printf(" \n",version); - printf("
\n",olddata); + printf("
\n",1+version); + printf("
\n",olddata); puts(" "); puts("
"); puts(""); diff --git a/htdocs/archive/webarchive.h b/htdocs/archive/webarchive.h index 195b55cea..28e0a2664 100644 --- a/htdocs/archive/webarchive.h +++ b/htdocs/archive/webarchive.h @@ -97,7 +97,7 @@ void generatepage(const char msg[]) puts("Content-type: text/html\r\n\r\n"); puts(""); puts(""); puts(""); puts(msg);