1
0
mirror of https://github.com/fumiama/CMoe-Counter.git synced 2024-11-30 16:24:28 +01:00

优化unlock

This commit is contained in:
源文雨 2023-05-30 21:27:51 +08:00
parent c823a70d43
commit 09c44e87fb

23
cmoe.c
View File

@ -169,6 +169,12 @@ static int name_exist(FILE* fp, char* name) {
#define create_or_open(fp, filename) ((fp = fopen(filename, "rb+"))?(fp):(fp = fopen(filename, "wb+"))) #define create_or_open(fp, filename) ((fp = fopen(filename, "rb+"))?(fp):(fp = fopen(filename, "wb+")))
#define flease(fp) { \
fflush(fp); \
flock(fileno(fp), LOCK_UN); \
fclose(fp); fp = NULL; \
}
#define QS (argv[2]) #define QS (argv[2])
// Usage: cmoe method query_string // Usage: cmoe method query_string
int main(int argc, char **argv) { int main(int argc, char **argv) {
@ -210,8 +216,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); flease(fp);
fclose(fp); fp = NULL;
return 0; return 0;
} }
reg = get_arg(reg + 4); reg = get_arg(reg + 4);
@ -232,13 +237,11 @@ 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); flease(fp);
fclose(fp); fp = NULL;
http_error(HTTP400, "Name Exist."); http_error(HTTP400, "Name Exist.");
return 7; return 7;
} }
flock(fileno(fp), LOCK_UN); flease(fp);
fclose(fp); fp = NULL;
fp = fopen(datfile, "ab+"); fp = fopen(datfile, "ab+");
if (!fp) { if (!fp) {
http_error(HTTP500, "Open File Error."); http_error(HTTP500, "Open File Error.");
@ -249,8 +252,7 @@ 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); flease(fp);
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);
@ -260,8 +262,5 @@ int main(int argc, char **argv) {
} }
static void __attribute__((destructor)) defer_close_fp() { static void __attribute__((destructor)) defer_close_fp() {
if(fp) { if(fp) flease(fp);
flock(fileno(fp), LOCK_UN);
fclose(fp);
}
} }