/[LeafOK_CVS]/lbbs/src/common.c
ViewVC logotype

Diff of /lbbs/src/common.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.13 by sysadm, Sun May 4 14:54:55 2025 UTC Revision 1.17 by sysadm, Fri May 9 11:21:11 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                                                    common.c  -  description                                                    common.c  -  description
3                                                           -------------------                                                           -------------------
4          begin                : Mon Oct 18 2004          Copyright            : (C) 2004-2025 by Leaflet
5          copyright            : (C) 2004 by Leaflet          Email                : leaflet@leafok.com
         email                : leaflet@leafok.com  
6   ***************************************************************************/   ***************************************************************************/
7    
8  /***************************************************************************  /***************************************************************************
9   *                                                                         *   *                                                                         *
10   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
11   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
12   *   the Free Software Foundation; either version 2 of the License, or     *   *   the Free Software Foundation; either version 3 of the License, or     *
13   *   (at your option) any later version.                                   *   *   (at your option) any later version.                                   *
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
# Line 21  Line 20 
20  #include <string.h>  #include <string.h>
21  #include <time.h>  #include <time.h>
22  #include <sys/types.h>  #include <sys/types.h>
 #include <sys/wait.h>  
23    
24  // Version information  // Version information
25  char app_version[256] = "LBBS-devel version 1.0";  char app_version[256] = "LBBS-devel version 1.0";
# Line 35  int port_server; Line 33  int port_server;
33  int port_client;  int port_client;
34    
35  // Global declaration for system  // Global declaration for system
36  int SYS_exit;  volatile int SYS_server_exit = 0;
37  int SYS_child_process_count;  volatile int SYS_child_process_count = 0;
38    volatile int SYS_child_exit_count = 0;
39    volatile int SYS_menu_reload = 0;
40    
41  // Common function  // Common function
42  const char *str_space(char *string, int length)  const char *str_space(char *string, int length)
# Line 50  const char *str_space(char *string, int Line 50  const char *str_space(char *string, int
50          return string;          return string;
51  }  }
52    
53  const char *get_time_str(char *string, size_t length)  const char *get_time_str(char *s, size_t len)
54  {  {
55          char week[20];          time_t curtime = time(NULL);
         char buffer[LINE_BUFFER_LEN];  
         time_t curtime;  
56          struct tm *loctime;          struct tm *loctime;
   
         curtime = time(NULL);  
57          loctime = localtime(&curtime);          loctime = localtime(&curtime);
58    
59          strftime(buffer, sizeof(buffer), "%Y年%m月%d日%H:%M:%S ", loctime);          size_t j = strftime(s, len, "%Y年%m月%d日%H:%M:%S ", loctime);
60    
61            if (j == 0)
62            {
63                    return NULL;
64            }
65    
66          switch (loctime->tm_wday)          switch (loctime->tm_wday)
67          {          {
68          case 0:          case 0:
69                  strncpy(week, "星期天", sizeof(week));                  strncat(s, "星期天", len - j);
70                  break;                  break;
71          case 1:          case 1:
72                  strncpy(week, "星期一", sizeof(week));                  strncat(s, "星期一", len - j);
73                  break;                  break;
74          case 2:          case 2:
75                  strncpy(week, "星期二", sizeof(week));                  strncat(s, "星期二", len - j);
76                  break;                  break;
77          case 3:          case 3:
78                  strncpy(week, "星期三", sizeof(week));                  strncat(s, "星期三", len - j);
79                  break;                  break;
80          case 4:          case 4:
81                  strncpy(week, "星期四", sizeof(week));                  strncat(s, "星期四", len - j);
82                  break;                  break;
83          case 5:          case 5:
84                  strncpy(week, "星期五", sizeof(week));                  strncat(s, "星期五", len - j);
85                  break;                  break;
86          case 6:          case 6:
87                  strncpy(week, "星期六", sizeof(week));                  strncat(s, "星期六", len - j);
88                  break;                  break;
89          }          }
         strncat(buffer, week, sizeof(buffer) - 1 - strnlen(buffer, sizeof(buffer)));  
90    
91          strncpy(string, buffer, length);          return s;
92    }
93    
94          return string;  void sig_hup_handler(int i)
95    {
96            SYS_menu_reload = 1;
97  }  }
98    
99  void reload_bbs_menu(int i)  void sig_term_handler(int i)
100  {  {
101          if (reload_menu(&bbs_menu) < 0)          SYS_server_exit = 1;
                 log_error("Reload menu failed\n");  
         else  
                 log_std("Reload menu successfully\n");  
102  }  }
103    
104  void system_exit(int i)  void sig_chld_handler(int i)
105  {  {
106          SYS_exit = 1;          SYS_child_exit_count++;
107  }  }
108    
109  void child_exit(int i)  const char * ip_mask(char * s, int level, char mask)
110  {  {
111          int pid;          char * p = s;
112    
113          pid = wait(0);          if (level <= 0)
114            {
115                    return s;
116            }
117            if (level > 4)
118            {
119                    level = 4;
120            }
121    
122          if (pid > 0)          for (int i = 0; i < 4 - level; i++)
123          {          {
124                  SYS_child_process_count--;                  p = strchr(p, '.');
125                  log_std("Child process (%d) exited\n", pid);                  if (p == NULL)
126                    {
127                            return s;
128                    }
129                    p++;
130          }          }
131    
132            for (int i = 0; i < level; i++)
133            {
134                    *p = mask;
135                    p++;
136                    if (i < level - 1)
137                    {
138                            *p = '.';
139                            p++;
140                    }
141            }
142            *p = '\0';
143    
144            return s;
145  }  }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1