1
0
mirror of https://github.com/fumiama/CMoe-Counter.git synced 2024-11-14 09:47:38 +01:00

add funlock

This commit is contained in:
源文雨 2023-03-08 16:38:48 +08:00
parent 946cbbac67
commit 5b380eec53
2 changed files with 11 additions and 4 deletions

13
cmoe.c
View File

@ -139,7 +139,7 @@ static void return_count(FILE* fp, char* name, char* theme) {
h = H_SMALL; h = H_SMALL;
head = (char*)svg_small; head = (char*)svg_small;
} }
if(headers(get_content_len(isbig, len_type, cntstr), image/svg+xml)) { if(headers(get_content_len(isbig, len_type, cntstr), "image/svg+xml")) {
write(1, "\0\0\0\0", 4); write(1, "\0\0\0\0", 4);
return; return;
} }
@ -210,6 +210,7 @@ int main(int argc, char **argv) {
return -3; return -3;
} }
return_count(fp, name, theme); return_count(fp, name, theme);
flock(fileno(fp), LOCK_UN);
fclose(fp); fp = NULL; fclose(fp); fp = NULL;
return 0; return 0;
} }
@ -231,10 +232,12 @@ int main(int argc, char **argv) {
return -5; return -5;
} }
if(name_exist(fp, name)) { if(name_exist(fp, name)) {
flock(fileno(fp), LOCK_UN);
fclose(fp); fp = NULL; fclose(fp); fp = NULL;
http_error(HTTP400, "Name Exist."); http_error(HTTP400, "Name Exist.");
return 7; return 7;
} }
flock(fileno(fp), LOCK_UN);
fclose(fp); fp = NULL; fclose(fp); fp = NULL;
fp = fopen(datfile, "ab+"); fp = fopen(datfile, "ab+");
if (!fp) { if (!fp) {
@ -246,9 +249,10 @@ int main(int argc, char **argv) {
return -7; return -7;
} }
add_user(name, 0, fp); add_user(name, 0, fp);
flock(fileno(fp), LOCK_UN);
fclose(fp); fp = NULL; fclose(fp); fp = NULL;
char* msg = "<P>Success.\r\n"; char* msg = "<P>Success.\r\n";
if(headers(strlen(msg), text/html)) { if(headers(strlen(msg), "text/html")) {
write(1, "\0\0\0\0", 4); write(1, "\0\0\0\0", 4);
return 8; return 8;
} }
@ -256,5 +260,8 @@ int main(int argc, char **argv) {
} }
static void __attribute__((destructor)) defer_close_fp() { static void __attribute__((destructor)) defer_close_fp() {
if(fp) fclose(fp); if(fp) {
flock(fileno(fp), LOCK_UN);
fclose(fp);
}
} }

2
cmoe.h
View File

@ -21,7 +21,7 @@ const uint8_t typel[] = {59, 111, 69};
enum errcode_enum_t {HTTP400, HTTP404, HTTP500}; enum errcode_enum_t {HTTP400, HTTP404, HTTP500};
typedef enum errcode_enum_t errcode_enum_t; typedef enum errcode_enum_t errcode_enum_t;
#define CONTENT_TYPE(x) "Content-Type: " #x "\r\n" #define CONTENT_TYPE(x) "Content-Type: " x "\r\n"
#define CONTENT_LEN "Content-Length: %d\r\n" #define CONTENT_LEN "Content-Length: %d\r\n"
#define CACHE_CTRL "Cache-Control: max-age=0, no-cache, no-store, must-revalidate\r\n" #define CACHE_CTRL "Cache-Control: max-age=0, no-cache, no-store, must-revalidate\r\n"