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

Diff of /lbbs/src/init.c

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

Revision 1.20 by sysadm, Tue May 13 11:32:01 2025 UTC Revision 1.31 by sysadm, Tue Oct 21 06:24:51 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #include "init.h"  
 #include "database.h"  
17  #include "bbs.h"  #include "bbs.h"
18  #include "common.h"  #include "common.h"
19  #include "log.h"  #include "database.h"
20    #include "init.h"
21  #include "io.h"  #include "io.h"
22  #include <string.h>  #include "log.h"
 #include <stdlib.h>  
23  #include <signal.h>  #include <signal.h>
24    #include <stdlib.h>
25    #include <string.h>
26    #include <unistd.h>
27  #include <sys/param.h>  #include <sys/param.h>
 #include <sys/types.h>  
28  #include <sys/stat.h>  #include <sys/stat.h>
29  #include <unistd.h>  #include <sys/types.h>
30    
31    #define CONF_DELIM_WITH_SPACE " \t\r\n"
32    
33  int init_daemon(void)  int init_daemon(void)
34  {  {
# Line 65  int init_daemon(void) Line 67  int init_daemon(void)
67    
68  int load_conf(const char *conf_file)  int load_conf(const char *conf_file)
69  {  {
70          char temp[256];          char buffer[LINE_BUFFER_LEN];
71            char *saveptr = NULL;
72          // Load configuration          char *p, *q, *r;
73          char c_name[256];          char *y, *m, *d;
74            int v;
75          FILE *fin;          FILE *fin;
76    
77            // Load configuration
78          if ((fin = fopen(conf_file, "r")) == NULL)          if ((fin = fopen(conf_file, "r")) == NULL)
79          {          {
80                  log_error("Open %s failed", conf_file);                  log_error("Open %s failed", conf_file);
81                  return -1;                  return -1;
82          }          }
83    
84          while (fscanf(fin, "%s", c_name) != EOF)          while (fgets(buffer, sizeof(buffer), fin) != NULL)
85          {          {
86                  if (c_name[0] == '#')                  p = strtok_r(buffer, CONF_DELIM_WITH_SPACE, &saveptr);
87                    if (p == NULL) // Blank line
88                    {
89                            continue;
90                    }
91    
92                    if (*p == '#' || *p == '\r' || *p == '\n') // Comment or blank line
93                    {
94                            continue;
95                    }
96    
97                    q = strtok_r(NULL, CONF_DELIM_WITH_SPACE, &saveptr);
98                    if (q == NULL) // Empty value
99                  {                  {
100                          fgets(temp, 256, fin);                          log_error("Skip empty value of config item: %s\n", p);
101                          continue;                          continue;
102                  }                  }
103                  fscanf(fin, "%*c");  
104                  if (strcmp(c_name, "bbs_id") == 0)                  // Check syntax
105                    r = strtok_r(NULL, CONF_DELIM_WITH_SPACE, &saveptr);
106                    if (r != NULL && r[0] != '#')
107                  {                  {
108                          fscanf(fin, "%s", BBS_id);                          log_error("Skip config line with extra value %s = %s %s\n", p, q, r);
109                            continue;
110                  }                  }
111                  if (strcmp(c_name, "bbs_name") == 0)  
112                    if (strcasecmp(p, "bbs_id") == 0)
113                  {                  {
114                          fscanf(fin, "%s", BBS_name);                          strncpy(BBS_id, q, sizeof(BBS_id) - 1);
115                            BBS_id[sizeof(BBS_id) - 1] = '\0';
116                  }                  }
117                  if (strcmp(c_name, "bbs_server") == 0)                  else if (strcasecmp(p, "bbs_name") == 0)
118                  {                  {
119                          fscanf(fin, "%s", BBS_server);                          strncpy(BBS_name, q, sizeof(BBS_name) - 1);
120                            BBS_name[sizeof(BBS_name) - 1] = '\0';
121                  }                  }
122                  if (strcmp(c_name, "bbs_address") == 0)                  else if (strcasecmp(p, "bbs_server") == 0)
123                  {                  {
124                          fscanf(fin, "%s", BBS_address);                          strncpy(BBS_server, q, sizeof(BBS_server) - 1);
125                            BBS_server[sizeof(BBS_server) - 1] = '\0';
126                  }                  }
127                  if (strcmp(c_name, "bbs_port") == 0)                  else if (strcasecmp(p, "bbs_address") == 0)
128                  {                  {
129                          fscanf(fin, "%hu", &BBS_port);                          strncpy(BBS_address, q, sizeof(BBS_address) - 1);
130                            BBS_address[sizeof(BBS_address) - 1] = '\0';
131                  }                  }
132                  if (strcmp(c_name, "bbs_max_client") == 0)                  else if (strcasecmp(p, "bbs_port") == 0)
133                  {                  {
134                          fscanf(fin, "%d", &BBS_max_client);                          BBS_port[0] = (in_port_t)atoi(q);
135                    }
136                    else if (strcasecmp(p, "bbs_ssh_port") == 0)
137                    {
138                            BBS_port[1] = (in_port_t)atoi(q);
139                    }
140                    else if (strcasecmp(p, "bbs_max_client") == 0)
141                    {
142                            BBS_max_client = atoi(q);
143                            if (BBS_max_client <= 0 || BBS_max_client > MAX_CLIENT_LIMIT)
144                            {
145                                    log_error("Ignore config bbs_max_client with incorrect value %d\n", BBS_max_client);
146                                    BBS_max_client = MAX_CLIENT_LIMIT;
147                            }
148                    }
149                    else if (strcasecmp(p, "bbs_max_client_per_ip") == 0)
150                    {
151                            BBS_max_client_per_ip = atoi(q);
152                            if (BBS_max_client_per_ip <= 0 || BBS_max_client_per_ip > MAX_CLIENT_PER_IP_LIMIT)
153                            {
154                                    log_error("Ignore config bbs_max_client with incorrect value %d\n", BBS_max_client_per_ip);
155                                    BBS_max_client_per_ip = MAX_CLIENT_PER_IP_LIMIT;
156                            }
157                    }
158                    else if (strcasecmp(p, "bbs_start_dt") == 0)
159                    {
160                            y = strtok_r(q, "-", &saveptr);
161                            m = strtok_r(NULL, "-", &saveptr);
162                            d = strtok_r(NULL, "-", &saveptr);
163    
164                            if (y == NULL || m == NULL || d == NULL)
165                            {
166                                    log_error("Ignore config bbs_start_dt with incorrect value\n");
167                                    continue;
168                            }
169                            snprintf(BBS_start_dt, sizeof(BBS_start_dt), "%4så¹´%2s月%2sæ—¥", y, m, d);
170                  }                  }
171                  if (strcmp(c_name, "bbs_max_user") == 0)                  else if (strcasecmp(p, "bbs_sys_id") == 0)
172                  {                  {
173                          fscanf(fin, "%d", &BBS_max_user);                          v = atoi(q);
174                            if (v <= 0)
175                            {
176                                    log_error("Ignore config bbs_sys_id with incorrect value %d\n", v);
177                                    continue;
178                            }
179                            BBS_sys_id = v;
180                  }                  }
181                  if (strcmp(c_name, "bbs_start_dt") == 0)                  else if (strcasecmp(p, "db_host") == 0)
182                  {                  {
183                          int y = 0, m = 0, d = 0;                          strncpy(DB_host, q, sizeof(DB_host) - 1);
184                          fscanf(fin, "%d-%d-%d", &y, &m, &d);                          DB_host[sizeof(DB_host) - 1] = '\0';
                         snprintf(BBS_start_dt, sizeof(BBS_start_dt), "%4dÄê%2dÔÂ%2dÈÕ", y, m, d);  
185                  }                  }
186                  if (strcmp(c_name, "db_host") == 0)                  else if (strcasecmp(p, "db_username") == 0)
187                  {                  {
188                          fscanf(fin, "%s", DB_host);                          strncpy(DB_username, q, sizeof(DB_username) - 1);
189                            DB_username[sizeof(DB_username) - 1] = '\0';
190                  }                  }
191                  if (strcmp(c_name, "db_username") == 0)                  else if (strcasecmp(p, "db_password") == 0)
192                  {                  {
193                          fscanf(fin, "%s", DB_username);                          strncpy(DB_password, q, sizeof(DB_password) - 1);
194                            DB_password[sizeof(DB_password) - 1] = '\0';
195                  }                  }
196                  if (strcmp(c_name, "db_password") == 0)                  else if (strcasecmp(p, "db_database") == 0)
197                  {                  {
198                          fscanf(fin, "%s", DB_password);                          strncpy(DB_database, q, sizeof(DB_database) - 1);
199                            DB_database[sizeof(DB_database) - 1] = '\0';
200                  }                  }
201                  if (strcmp(c_name, "db_database") == 0)                  else if (strcasecmp(p, "db_timezone") == 0)
202                  {                  {
203                          fscanf(fin, "%s", DB_database);                          strncpy(DB_timezone, q, sizeof(DB_timezone) - 1);
204                            DB_timezone[sizeof(DB_timezone) - 1] = '\0';
205                  }                  }
206                  if (strcmp(c_name, "db_timezone") == 0)                  else
207                  {                  {
208                          fscanf(fin, "%s", DB_timezone);                          log_error("Unknown config %s = %s\n", p, q);
209                  }                  }
210          }          }
211    


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

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