diff options
-rw-r--r-- | include/proto.h | 1 | ||||
-rw-r--r-- | mbbsd/file.c | 33 | ||||
-rw-r--r-- | mbbsd/friend.c | 32 |
3 files changed, 30 insertions, 36 deletions
diff --git a/include/proto.h b/include/proto.h index 254a77af..92d23207 100644 --- a/include/proto.h +++ b/include/proto.h @@ -271,7 +271,6 @@ void subscribe_newfav(void); /* file */ int file_count_line(const char *file); int file_append_line(const char *file, const char *string); -int file_delete_line(const char *file, const char *string, int case_sensitive); int file_exist_record(const char *file, const char *string); /* friend */ diff --git a/mbbsd/file.c b/mbbsd/file.c index a5937673..343822b7 100644 --- a/mbbsd/file.c +++ b/mbbsd/file.c @@ -45,39 +45,6 @@ int file_append_line(const char *file, const char *string) return 0; } -#ifndef _BBS_UTIL_C_ -/** - * 從檔案 file 中刪除 prefix 為 string 的每一行。(小心 race) - * @param file - * @param string - * @param case_sensitive 字串比對是否 case sensitive - */ -int file_delete_line(const char *file, const char *string, int case_sensitive) -{ - FILE *fp, *nfp = NULL; - char fnew[80]; - char genbuf[STRLEN + 1]; - - sprintf(fnew, "%s.%3.3X", file, (unsigned int)(random() & 0xFFF)); - if ((fp = fopen(file, "r")) && (nfp = fopen(fnew, "w"))) { - int length = strlen(string); - - while (fgets(genbuf, sizeof(genbuf), fp)) - if ((genbuf[0] > ' ')) { - if (((case_sensitive && strncmp(genbuf, string, length)) || - (!case_sensitive && strncasecmp(genbuf, string, length)))) - fputs(genbuf, nfp); - } - Rename(fnew, file); - } - if(fp) - fclose(fp); - if(nfp) - fclose(nfp); - return 0; -} -#endif - /** * 傳回檔案 file 中是否有 string 這個字串。 */ diff --git a/mbbsd/friend.c b/mbbsd/friend.c index 8f62713f..7d7d3720 100644 --- a/mbbsd/friend.c +++ b/mbbsd/friend.c @@ -179,12 +179,40 @@ friend_append(int type, int count) } } +static int +delete_friend_from_file(const char *file, const char *string, int case_sensitive) +{ + FILE *fp, *nfp = NULL; + char fnew[80]; + char genbuf[STRLEN + 1]; + + sprintf(fnew, "%s.%3.3X", file, (unsigned int)(random() & 0xFFF)); + if ((fp = fopen(file, "r")) && (nfp = fopen(fnew, "w"))) { + int length = strlen(string); + + while (fgets(genbuf, sizeof(genbuf), fp)) + if ((genbuf[0] > ' ')) { + char buf[32]; + sscanf(genbuf, " %s", &buf); + if (((case_sensitive && strcmp(buf, string)) || + (!case_sensitive && strcasecmp(buf, string)))) + fputs(genbuf, nfp); + } + Rename(fnew, file); + } + if(fp) + fclose(fp); + if(nfp) + fclose(nfp); + return 0; +} + void friend_delete(const char *uident, int type) { char fn[80]; setfriendfile(fn, type); - file_delete_line(fn, uident, 0); + delete_friend_from_file(fn, uident, 0); } static void @@ -195,7 +223,7 @@ delete_user_friend(const char *uident, const char *thefriend, int type) if (type == FRIEND_ALOHA) { #endif sethomefile(fn, uident, "aloha"); - file_delete_line(fn, thefriend, 0); + delete_friend_from_file(fn, thefriend, 0); #if 0 } else { |