htdocs: web archive. when addfile/deletefile/renamefile are done, show message and OK button.

This commit is contained in:
Daniel Marjamäki 2013-07-07 18:06:52 +02:00
parent c74b74da0d
commit 88e96a42fe
4 changed files with 53 additions and 49 deletions

View File

@ -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.");
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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("<html>");
puts("<head><script>");
puts("function ok() { window.location = \"http://cppcheck.sf.net/cgi-bin/report.cgi\"; }");
puts("</script></head>");
puts("<body>");
puts(msg);
puts("<br><input type=\"button\" value=\"OK\" onclick=\"ok()\"></body></html>");
}