--- lbbs/src/common.c 2025/04/30 09:18:19 1.11 +++ lbbs/src/common.c 2025/05/13 03:07:54 1.19 @@ -1,16 +1,15 @@ /*************************************************************************** common.c - description ------------------- - begin : Mon Oct 18 2004 - copyright : (C) 2004 by Leaflet - email : leaflet@leafok.com + Copyright : (C) 2004-2025 by Leaflet + Email : leaflet@leafok.com ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * + * the Free Software Foundation; either version 3 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ @@ -21,15 +20,10 @@ #include #include #include -#include // Version information char app_version[256] = "LBBS-devel version 1.0"; -// Global declaration for enviroment -char app_home_dir[256]; -char app_temp_dir[256]; - // Global declaration for sockets int socket_server; int socket_client; @@ -38,16 +32,11 @@ char hostaddr_client[50]; int port_server; int port_client; -// Global declaration for database -char DB_host[256]; -char DB_username[50]; -char DB_password[50]; -char DB_database[50]; -char DB_timezone[50]; - // Global declaration for system -int SYS_exit; -int SYS_child_process_count; +volatile int SYS_server_exit = 0; +volatile int SYS_child_process_count = 0; +volatile int SYS_child_exit = 0; +volatile int SYS_menu_reload = 0; // Common function const char *str_space(char *string, int length) @@ -61,70 +50,97 @@ const char *str_space(char *string, int return string; } -const char *get_time_str(char *string, size_t length) +const char *get_time_str(char *s, size_t len) { - char week[10], buffer[256]; - time_t curtime; + time_t curtime = time(NULL); struct tm *loctime; - - curtime = time(NULL); loctime = localtime(&curtime); - strftime(buffer, 256, "%Y年%m月%d日%H:%M:%S ", loctime); + size_t j = strftime(s, len, "%Y年%m月%d日%H:%M:%S ", loctime); + + if (j == 0) + { + return NULL; + } switch (loctime->tm_wday) { case 0: - strcpy(week, "星期天"); + strncat(s, "星期天", len - j); break; case 1: - strcpy(week, "星期一"); + strncat(s, "星期一", len - j); break; case 2: - strcpy(week, "星期二"); + strncat(s, "星期二", len - j); break; case 3: - strcpy(week, "星期三"); + strncat(s, "星期三", len - j); break; case 4: - strcpy(week, "星期四"); + strncat(s, "星期四", len - j); break; case 5: - strcpy(week, "星期五"); + strncat(s, "星期五", len - j); break; case 6: - strcpy(week, "星期六"); + strncat(s, "星期六", len - j); break; } - strcat(buffer, week); - strncpy(string, buffer, length); + return s; +} - return string; +void sig_hup_handler(int i) +{ + SYS_menu_reload = 1; } -void reload_bbs_menu(int i) +void sig_term_handler(int i) { - if (reload_menu(&bbs_menu) < 0) - log_error("Reload menu failed\n"); - else - log_std("Reload menu successfully\n"); + log_std("Debug: SIGTERM captured\n"); + SYS_server_exit = 1; } -void system_exit(int i) +void sig_chld_handler(int i) { - SYS_exit = 1; + SYS_child_exit = 1; } -void child_exit(int i) +const char * ip_mask(char * s, int level, char mask) { - int pid; + char * p = s; - pid = wait(0); + if (level <= 0) + { + return s; + } + if (level > 4) + { + level = 4; + } - if (pid > 0) + for (int i = 0; i < 4 - level; i++) { - SYS_child_process_count--; - log_std("Child process (%d) exited\n", pid); + p = strchr(p, '.'); + if (p == NULL) + { + return s; + } + p++; } + + for (int i = 0; i < level; i++) + { + *p = mask; + p++; + if (i < level - 1) + { + *p = '.'; + p++; + } + } + *p = '\0'; + + return s; }