| 1 |
/*************************************************************************** |
/*************************************************************************** |
| 2 |
bbsd.c - description |
main.c - description |
| 3 |
------------------- |
------------------- |
| 4 |
begin : Mon Oct 11 2004 |
begin : Mon Oct 11 2004 |
| 5 |
copyright : (C) 2004 by Leaflet |
copyright : (C) 2004 by Leaflet |
| 15 |
* * |
* * |
| 16 |
***************************************************************************/ |
***************************************************************************/ |
| 17 |
|
|
| 18 |
|
#include "bbs.h" |
| 19 |
#include "common.h" |
#include "common.h" |
| 20 |
|
#include "io.h" |
| 21 |
|
#include <string.h> |
| 22 |
|
|
| 23 |
int |
void |
| 24 |
main (int argc, char *argv[]) |
app_help(void) |
| 25 |
{ |
{ |
| 26 |
FILE *fp; |
prints ( |
| 27 |
time_t t; |
"Usage: bbsd [-fhv] [...]\n\n" |
| 28 |
|
"-f\t--foreground\t\tForce program run in foreground\n" |
| 29 |
|
"-h\t--help\t\t\tDisplay this help message\n" |
| 30 |
|
"-v\t--version\t\tDisplay version information\n" |
| 31 |
|
"\t--display-log\t\tDisplay standard log information\n" |
| 32 |
|
"\t--display-error-log\tDisplay error log information\n" |
| 33 |
|
"\n If meet any bug, please report to <leaflet@leafok.com>\n\n" |
| 34 |
|
); |
| 35 |
|
} |
| 36 |
|
|
| 37 |
init_daemon (); |
void |
| 38 |
|
arg_error(void) |
| 39 |
|
{ |
| 40 |
|
prints ("Invalid arguments\n"); |
| 41 |
|
app_help(); |
| 42 |
|
} |
| 43 |
|
|
| 44 |
while (1) |
int |
| 45 |
|
main (int argc, char *argv[]) |
| 46 |
|
{ |
| 47 |
|
char log_dir[256], file_log_std[256], file_log_error[256], file_config[256]; |
| 48 |
|
int i,j; |
| 49 |
|
int daemon = 1, std_log_redir = 0, error_log_redir = 0; |
| 50 |
|
|
| 51 |
|
//Parse args |
| 52 |
|
for (i=1; i<argc; i++) |
| 53 |
|
{ |
| 54 |
|
switch (argv[i][0]) |
| 55 |
{ |
{ |
| 56 |
sleep (60); |
case '-': |
| 57 |
if ((fp = fopen ("bbsd.log", "a")) >= 0) |
if (argv[i][1] != '-') |
| 58 |
{ |
{ |
| 59 |
t = time (0); |
for (j=1; j<strlen(argv[i]); j++) |
| 60 |
fprintf (fp, "I'm here at %s\n", asctime (localtime (&t))); |
{ |
| 61 |
fclose (fp); |
switch (argv[i][j]) |
| 62 |
} |
{ |
| 63 |
|
case 'f': |
| 64 |
|
daemon = 0; |
| 65 |
|
break; |
| 66 |
|
case 'h': |
| 67 |
|
app_help(); |
| 68 |
|
exit(0); |
| 69 |
|
case 'v': |
| 70 |
|
puts (app_version); |
| 71 |
|
exit(0); |
| 72 |
|
default: |
| 73 |
|
arg_error(); |
| 74 |
|
exit(1); |
| 75 |
|
} |
| 76 |
|
} |
| 77 |
|
} |
| 78 |
|
else |
| 79 |
|
{ |
| 80 |
|
if (strcmp(argv[i]+2, "foreground") == 0) |
| 81 |
|
{ |
| 82 |
|
daemon = 0; |
| 83 |
|
break; |
| 84 |
|
} |
| 85 |
|
if (strcmp(argv[i]+2, "help") == 0) |
| 86 |
|
{ |
| 87 |
|
app_help(); |
| 88 |
|
exit(0); |
| 89 |
|
} |
| 90 |
|
if (strcmp(argv[i]+2, "version") == 0) |
| 91 |
|
{ |
| 92 |
|
puts (app_version); |
| 93 |
|
exit(0); |
| 94 |
|
} |
| 95 |
|
if (strcmp(argv[i]+2, "display-log") == 0) |
| 96 |
|
{ |
| 97 |
|
std_log_redir = 1; |
| 98 |
|
} |
| 99 |
|
if (strcmp(argv[i]+2, "display-error-log") == 0) |
| 100 |
|
{ |
| 101 |
|
error_log_redir = 1; |
| 102 |
|
} |
| 103 |
|
} |
| 104 |
|
break; |
| 105 |
} |
} |
| 106 |
|
} |
| 107 |
|
|
| 108 |
|
//Initialize daemon |
| 109 |
|
if (daemon) |
| 110 |
|
init_daemon (); |
| 111 |
|
|
| 112 |
|
//Initialize log |
| 113 |
|
strncpy(app_home_dir, argv[0], rindex(argv[0],'/')-argv[0]+1); |
| 114 |
|
strcat(app_home_dir, "../"); |
| 115 |
|
strcpy(log_dir, app_home_dir); |
| 116 |
|
strcat(log_dir, "log/"); |
| 117 |
|
strcpy(file_log_std, log_dir); |
| 118 |
|
strcpy(file_log_error, log_dir); |
| 119 |
|
strcat(file_log_std, "bbsd.log"); |
| 120 |
|
strcat(file_log_error, "error.log"); |
| 121 |
|
mkdir(log_dir,0700); |
| 122 |
|
if (log_begin(file_log_std,file_log_error)<0) |
| 123 |
|
exit(-1); |
| 124 |
|
if ((!daemon) && std_log_redir) |
| 125 |
|
log_std_redirect(2); |
| 126 |
|
if ((!daemon) && error_log_redir) |
| 127 |
|
log_err_redirect(3); |
| 128 |
|
|
| 129 |
|
//Load configuration |
| 130 |
|
strcpy(file_config, app_home_dir); |
| 131 |
|
strcat(file_config, "conf/bbsd.conf"); |
| 132 |
|
if (load_conf(file_config)<0) |
| 133 |
|
exit(-2); |
| 134 |
|
|
| 135 |
|
//Initialize socket server |
| 136 |
|
net_server(BBS_address, BBS_port); |
| 137 |
|
|
| 138 |
return 0; |
return 0; |
| 139 |
} |
} |