diff options
Diffstat (limited to 'mbbsd/stuff.c')
-rw-r--r-- | mbbsd/stuff.c | 448 |
1 files changed, 3 insertions, 445 deletions
diff --git a/mbbsd/stuff.c b/mbbsd/stuff.c index 371583f4..abe65680 100644 --- a/mbbsd/stuff.c +++ b/mbbsd/stuff.c @@ -1,6 +1,5 @@ /* $Id$ */ #include "bbs.h" -#include "fnv_hash.h" /* ----------------------------------------------------- */ /* set file path for boards/user home */ @@ -9,7 +8,6 @@ static const char * const str_home_file = "home/%c/%s/%s"; static const char * const str_board_file = "boards/%c/%s/%s"; static const char * const str_board_n_file = "boards/%c/%s/%s.%d"; -static char cdate_buffer[32]; static const char * const str_dotdir = FN_DIR; @@ -127,81 +125,6 @@ subject(char *title) return title; } -/* ----------------------------------------------------- */ -/* 字串轉換檢查函數 */ -/* ----------------------------------------------------- */ -/** - * 將字串 s 轉為小寫存回 t - * @param t allocated char array - * @param s - */ -void -str_lower(char *t, const char *s) -{ - register unsigned char ch; - - do { - ch = *s++; - *t++ = char_lower(ch); - } while (ch); -} - -/** - * 移除字串 buf 後端多餘的空白。 - * @param buf - */ -void -trim(char *buf) -{ /* remove trailing space */ - char *p = buf; - - while (*p) - p++; - while (--p >= buf) { - if (*p == ' ') - *p = '\0'; - else - break; - } -} - -/** - * 移除 src 的 '\n' 並改成 '\0' - * @param src - */ -void chomp(char *src) -{ - while(*src){ - if (*src == '\n') - *src = 0; - else - src++; - } -} - -/* ----------------------------------------------------- */ -/* 字串檢查函數:英文、數字、檔名、E-mail address */ -/* ----------------------------------------------------- */ - -int -invalid_pname(const char *str) -{ - const char *p1, *p2, *p3; - - p1 = str; - while (*p1) { - if (!(p2 = strchr(p1, '/'))) - p2 = str + strlen(str); - if (p1 + 1 > p2 || p1 + strspn(p1, ".") == p2) /* 不允許用 / 開頭, 或是 // 之間只有 . */ - return 1; - for (p3 = p1; p3 < p2; p3++) - if (not_alnum(*p3) && !strchr("@[]-._", *p3)) /* 只允許 alnum 或這些符號 */ - return 1; - p1 = p2 + (*p2 ? 1 : 0); - } - return 0; -} - int is_validuserid(const char *id) { int len, i; @@ -212,10 +135,10 @@ int is_validuserid(const char *id) if (len < 2 || len>IDLEN) return 0; - if (not_alpha(id[0])) + if (!isalpha(id[0])) return 0; for (i = 1; i < len; i++) - if (not_alnum(id[i])) + if (!isalnum(id[i])) return 0; return 1; } @@ -279,197 +202,12 @@ userid_is_BM(const char *userid, const char *list) return 0; } -/* ----------------------------------------------------- */ -/* 檔案檢查函數:檔案、目錄、屬於 */ -/* ----------------------------------------------------- */ - -/** - * 傳回 fname 的檔案大小 - * @param fname - */ -off_t -dashs(const char *fname) -{ - struct stat st; - - if (!stat(fname, &st)) - return st.st_size; - else - return -1; -} - -/** - * 傳回 fname 的 mtime - * @param fname - */ -time4_t -dasht(const char *fname) -{ - struct stat st; - - if (!stat(fname, &st)) - return st.st_mtime; - else - return -1; -} - -/** - * 傳回 fname 是否為 symbolic link - * @param fname - */ -int -dashl(const char *fname) -{ - struct stat st; - - return (lstat(fname, &st) == 0 && S_ISLNK(st.st_mode)); -} - -/** - * 傳回 fname 是否為一般的檔案 - * @param fname - */ -int -dashf(const char *fname) -{ - struct stat st; - - return (stat(fname, &st) == 0 && S_ISREG(st.st_mode)); -} - -/** - * 傳回 fname 是否為目錄 - * @param fname - */ -int -dashd(const char *fname) -{ - struct stat st; - - return (stat(fname, &st) == 0 && S_ISDIR(st.st_mode)); -} - -#define BUFFER_SIZE 8192 -static int copy_file_to_file(const char *src, const char *dst) -{ - char buf[BUFFER_SIZE]; - int fdr, fdw, len; - - if ((fdr = open(src, O_RDONLY)) < 0) - return -1; - - if ((fdw = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) { - close(fdr); - return -1; - } - - while (1) { - len = read(fdr, buf, sizeof(buf)); - if (len <= 0) - break; - write(fdw, buf, len); - if (len < BUFFER_SIZE) - break; - } - - close(fdr); - close(fdw); - return 0; -} -#undef BUFFER_SIZE - -static int copy_file_to_dir(const char *src, const char *dst) -{ - char buf[PATHLEN]; - char *slash; - if ((slash = rindex(src, '/')) == NULL) - snprintf(buf, PATHLEN, "%s/%s", dst, src); - else - snprintf(buf, PATHLEN, "%s/%s", dst, slash); - return copy_file_to_file(src, buf); -} - -static int copy_dir_to_dir(const char *src, const char *dst) -{ - DIR *dir; - struct dirent *entry; - struct stat st; - char buf[PATHLEN], buf2[PATHLEN]; - - if (stat(dst, &st) < 0) - if (mkdir(dst, 0700) < 0) - return -1; - - if ((dir = opendir(src)) == NULL) - return -1; - - while ((entry = readdir(dir)) != NULL) { - if (strcmp(entry->d_name, ".") == 0 || - strcmp(entry->d_name, "..") == 0) - continue; - snprintf(buf, PATHLEN, "%s/%s", src, entry->d_name); - snprintf(buf2, PATHLEN, "%s/%s", dst, entry->d_name); - if (stat(buf, &st) < 0) - continue; - if (S_ISDIR(st.st_mode)) - mkdir(buf2, 0700); - copy_file(buf, buf2); - } - - closedir(dir); - return 0; -} - -/** - * copy src to dst (recursively) - * @param src and dst are file or dir - * @return -1 if failed - */ -int copy_file(const char *src, const char *dst) -{ - struct stat st; - - if (stat(dst, &st) == 0 && S_ISDIR(st.st_mode)) { - if (stat(src, &st) < 0) - return -1; - - if (S_ISDIR(st.st_mode)) - return copy_dir_to_dir(src, dst); - else if (S_ISREG(st.st_mode)) - return copy_file_to_dir(src, dst); - return -1; - } - else if (stat(src, &st) == 0 && S_ISDIR(st.st_mode)) - return copy_dir_to_dir(src, dst); - return copy_file_to_file(src, dst); -} - int belong(const char *filelist, const char *key) { return file_exist_record(filelist, key); } -unsigned int -ipstr2int(const char *ip) -{ - unsigned int i, val = 0; - char buf[32]; - char *nil, *p; - - strlcpy(buf, ip, sizeof(buf)); - p = buf; - for (i = 0; i < 4; i++) { - nil = strchr(p, '.'); - if (nil != NULL) - *nil = 0; - val *= 256; - val += atoi(p); - if (nil != NULL) - p = nil + 1; - } - return val; -} #ifndef _BBS_UTIL_C_ /* getdata_buf */ time4_t @@ -502,35 +240,6 @@ gettime(int line, time4_t dt, const char*head) } #endif -char * -Cdate(const time4_t *clock) -{ - time_t temp = (time_t)*clock; - struct tm *mytm = localtime(&temp); - - strftime(cdate_buffer, sizeof(cdate_buffer), "%m/%d/%Y %T %a", mytm); - return cdate_buffer; -} - -char * -Cdatelite(const time4_t *clock) -{ - time_t temp = (time_t)*clock; - struct tm *mytm = localtime(&temp); - - strftime(cdate_buffer, sizeof(cdate_buffer), "%m/%d/%Y %T", mytm); - return cdate_buffer; -} - -char * -Cdatedate(const time4_t * clock) -{ - time_t temp = (time_t)*clock; - struct tm *mytm = localtime(&temp); - - strftime(cdate_buffer, sizeof(cdate_buffer), "%m/%d/%Y", mytm); - return cdate_buffer; -} #ifndef _BBS_UTIL_C_ /* 這一區都是有關於畫面處理的, 故 _BBS_UTIL_C_ 不須要 */ @@ -858,36 +567,7 @@ log_user(const char *fmt, ...) va_end(ap); sethomefile(filename, cuser.userid, "USERLOG"); - return log_file(filename, LOG_CREAT | LOG_VF, - "%s: %s %s", cuser.userid, msg, Cdate(&now)); -} - -int -log_file(const char *fn, int flag, const char *fmt,...) -{ - int fd; - char msg[256]; - const char *realmsg; - if( !(flag & LOG_VF) ){ - realmsg = fmt; - } - else{ - va_list ap; - va_start(ap, fmt); - vsnprintf(msg , 128, fmt, ap); - va_end(ap); - realmsg = msg; - } - - if( (fd = open(fn, O_APPEND | O_WRONLY | ((flag & LOG_CREAT)? O_CREAT : 0), - ((flag & LOG_CREAT) ? 0664 : 0))) < 0 ) - return -1; - if( write(fd, realmsg, strlen(realmsg)) < 0 ){ - close(fd); - return -1; - } - close(fd); - return 0; + return log_filef(filename, LOG_CREAT, "%s: %s %s", cuser.userid, msg, Cdate(&now)); } void @@ -954,11 +634,6 @@ void FREE(void *ptr) } #endif -unsigned -StringHash(const char *s) -{ - return fnv1a_32_strcase(s, FNV1_32_INIT); -} unsigned DBCS_StringHash(const char *s) @@ -1007,120 +682,3 @@ uintbsearch(const unsigned int key, const unsigned int *base0, const int nmemb) return (NULL); } -int qsort_intcompar(const void *a, const void *b) -{ - return *(int *)a - *(int *)b; -} - -#ifdef TIMET64 -char * -ctime4(const time4_t *clock) -{ - time_t temp = (time_t)*clock; - - return ctime(&temp); -} - -struct tm *localtime4(const time4_t *t) -{ - if( t == NULL ) - return localtime(NULL); - else { - time_t temp = (time_t)*t; - return localtime(&temp); - } -} - -time4_t time4(time4_t *ptr) -{ - if( ptr == NULL ) - return time(NULL); - else - return *ptr = (time4_t)time(NULL); -} -#endif - -#ifdef OUTTACACHE -int tobind(const char * host, int port) -{ - int sockfd, val = 1; - struct sockaddr_in servaddr; - - if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) { - perror("socket()"); - exit(1); - } - setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, - (char *)&val, sizeof(val)); - bzero(&servaddr, sizeof(servaddr)); - servaddr.sin_family = AF_INET; - if (host == NULL || host[0] == 0) - servaddr.sin_addr.s_addr = htonl(INADDR_ANY); - else if (inet_aton(host, &servaddr.sin_addr) == 0) { - perror("inet_aton()"); - exit(1); - } - servaddr.sin_port = htons(port); - if( bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 ) { - perror("bind()"); - exit(1); - } - if( listen(sockfd, 5) < 0 ) { - perror("listen()"); - exit(1); - } - - return sockfd; -} - -int toconnect(const char *host, int port) -{ - int sock; - struct sockaddr_in serv_name; - if( (sock = socket(AF_INET, SOCK_STREAM, 0)) < 0 ){ - perror("socket"); - return -1; - } - - serv_name.sin_family = AF_INET; - serv_name.sin_addr.s_addr = inet_addr(host); - serv_name.sin_port = htons(port); - if( connect(sock, (struct sockaddr*)&serv_name, sizeof(serv_name)) < 0 ){ - close(sock); - return -1; - } - return sock; -} - -/** - * same as read(2), but read until exactly size len - */ -int toread(int fd, void *buf, int len) -{ - int l; - for( l = 0 ; len > 0 ; ) - if( (l = read(fd, buf, len)) <= 0 ) - return -1; - else{ - buf += l; - len -= l; - } - return l; -} - -/** - * same as write(2), but write until exactly size len - */ -int towrite(int fd, const void *buf, int len) -{ - int l; - for( l = 0 ; len > 0 ; ) - if( (l = write(fd, buf, len)) <= 0 ) - return -1; - else{ - buf += l; - len -= l; - } - return l; -} -#endif |