From 09c44e87fbec3546531be753da2cb4936aa4bbd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Tue, 30 May 2023 21:27:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96unlock?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmoe.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/cmoe.c b/cmoe.c index a424152..e092e86 100644 --- a/cmoe.c +++ b/cmoe.c @@ -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 flease(fp) { \ + fflush(fp); \ + flock(fileno(fp), LOCK_UN); \ + fclose(fp); fp = NULL; \ +} + #define QS (argv[2]) // Usage: cmoe method query_string int main(int argc, char **argv) { @@ -210,8 +216,7 @@ int main(int argc, char **argv) { return -3; } return_count(fp, name, theme); - flock(fileno(fp), LOCK_UN); - fclose(fp); fp = NULL; + flease(fp); return 0; } reg = get_arg(reg + 4); @@ -232,13 +237,11 @@ int main(int argc, char **argv) { return -5; } if(name_exist(fp, name)) { - flock(fileno(fp), LOCK_UN); - fclose(fp); fp = NULL; + flease(fp); http_error(HTTP400, "Name Exist."); return 7; } - flock(fileno(fp), LOCK_UN); - fclose(fp); fp = NULL; + flease(fp); fp = fopen(datfile, "ab+"); if (!fp) { http_error(HTTP500, "Open File Error."); @@ -249,8 +252,7 @@ int main(int argc, char **argv) { return -7; } add_user(name, 0, fp); - flock(fileno(fp), LOCK_UN); - fclose(fp); fp = NULL; + flease(fp); char* msg = "
Success.\r\n"; if(headers(strlen(msg), "text/html")) { write(1, "\0\0\0\0", 4); @@ -260,8 +262,5 @@ int main(int argc, char **argv) { } static void __attribute__((destructor)) defer_close_fp() { - if(fp) { - flock(fileno(fp), LOCK_UN); - fclose(fp); - } + if(fp) flease(fp); }