-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftpd.cpp
More file actions
97 lines (90 loc) · 2.46 KB
/
ftpd.cpp
File metadata and controls
97 lines (90 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*************************************************************************
> File Name: ftpd.cpp
> Author: MidCHeck
> Mail: midcheck@foxmail.com
> Created Time: 2019年07月03日 星期三 01时03分09秒
************************************************************************/
#include <fstream>
#include "FTP_Server.h"
using namespace MidCHeck;
static bool parse_conf(char* buffer, const char* key, char* value){
if(strncmp(buffer, key, strlen(key))){ // 如果不是以key开头
return false;
}
while(!isdigit(*buffer)) ++buffer;
strcpy(value, buffer);
strcat(value, "\0");
return true;
}
static bool parse_conf(char* buffer, char* user, char* passwd, char* home){
while(*buffer == ' ') ++buffer;
char *ptr = strstr(buffer, " ");
if(ptr == nullptr) return false;
strncpy(user, buffer, ptr-buffer);
strcat(user, "\0");
while(*(ptr+1) == ' ') ++ptr;
buffer = ++ptr;
ptr = strstr(buffer, " ");
strncpy(passwd, buffer, ptr-buffer);
strcat(user, "\0");
while(*(ptr+1) == ' ') ++ptr;
strcpy(home, ++ptr);
if(ptr = strstr(home, " ")){
*ptr = '\0';
}else{
strcat(home, "\0");
}
return true;
}
int main(int argc, char **argv){
if(argc < 2){
std::cout << "缺少参数" << std::endl
<< "用法: server 配置文件" << std::endl;
return 1;
}
std::ifstream conf_file(argv[1], std::ios::in);
if(!conf_file.is_open()){
std::cerr << "打开文件失败!" << std::endl;
return 1;
}
Database* db = new Database;
Shardata* sd = Shardata::GetEntity();
char buffer[256], ip[20], port[10];
char name[32], passwd[64], home[128];
bool gflag = false, uflag = false, dflag = false;
while(!conf_file.eof()){
conf_file.getline(buffer, 256);
if(buffer[0] == '#') continue;
if(!strncmp(buffer, "[global]", 8)) gflag = true;
if(!strncmp(buffer, "[data]", 6)) dflag = true;
if(!strncmp(buffer, "[user]", 6)) uflag = true;
if(gflag){
parse_conf(buffer, "ip", ip);
if(parse_conf(buffer, "port", port))
gflag = false;
}
if(dflag){
if(parse_conf(buffer, "ip", sd->data_ip))
dflag = false;
}
if(uflag){
if(parse_conf(buffer, name, passwd, home)){
db->add(name, passwd, home);
}
memset(name, 0, 32);
memset(passwd, 0, 64);
memset(home, 0, 128);
}
memset(buffer, 0, 256);
}
Debug("config ip:%s, port:%d, data ip:%s", ip, atoi(port), sd->data_ip);
conf_file.close();
sd->db = db;
FTP_Server ftp(ip, atoi(port));
#ifndef DEBUG
daemon(0,0); // 设置守护进程
#endif
ftp.start();
delete db;
return 0;
}