From 81c98e60178549e9933961219d370252e91198cb Mon Sep 17 00:00:00 2001 From: victor Date: Mon, 20 Sep 2004 12:57:23 +0000 Subject: fix bug: when deleting an user, the aloha record won't be deleted. add file: file.c, move some useful subroutine out of friend.c git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@2205 63ad8ddf-47c3-0310-b6dd-a9e9d9715204 --- mbbsd/file.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 mbbsd/file.c (limited to 'mbbsd/file.c') diff --git a/mbbsd/file.c b/mbbsd/file.c new file mode 100644 index 00000000..aced35ff --- /dev/null +++ b/mbbsd/file.c @@ -0,0 +1,83 @@ +/* $Id: file.c 2191 2004-09-10 00:49:47Z victor $ */ + +#include "bbs.h" + +int file_count_line(char *file) +{ + FILE *fp; + int count = 0; + char buf[200]; + + if ((fp = fopen(file, "r"))) { + while (fgets(buf, sizeof(buf), fp)) + count++; + fclose(fp); + } + return count; +} + +int file_append_line(char *file, char *string) +{ + FILE *fp; + if ((fp = fopen(file, "a")) == NULL) + return -1; + flock(fileno(fp), LOCK_EX); + fputs(string, fp); + flock(fileno(fp), LOCK_UN); + fclose(fp); + return 0; +} + +int file_delete_line(char *file, char *string) +{ + FILE *fp, *nfp = NULL; + char fnew[80]; + char genbuf[STRLEN + 1]; + + sprintf(fnew, "%s.%3.3X", file, rand() & 0xFFF); + if ((fp = fopen(file, "r")) && (nfp = fopen(fnew, "w"))) { + int length = strlen(string); + + while (fgets(genbuf, sizeof(genbuf), fp)) + if ((genbuf[0] > ' ') && strncmp(genbuf, string, length)) + fputs(genbuf, nfp); + Rename(fnew, file); + } + if(fp) + fclose(fp); + if(nfp) + fclose(nfp); + return 0; +} + +int file_exist_record(char *file, char *string) +{ + FILE *fp; + char buf[STRLEN], *ptr; + + if ((fp = fopen(file, "r")) == NULL) + return 0; + + while (fgets(buf, STRLEN, fp)) { + if ((ptr = strtok(buf, str_space)) && !strcasecmp(ptr, string)) + return 1; + } + fclose(fp); + return 0; +} + +int file_foreach_entry(char *file, int (*func)(char *, int), int info) +{ + char line[80]; + FILE *fp; + + if ((fp = fopen(file, "r")) == NULL) + return -1; + + while (fgets(line, sizeof(line), fp)) { + (*func)(line, info); + } + + fclose(fp); + return 0; +} -- cgit v1.2.3