Add NLST by Liu Yang.
[xylftp.git] / server / test / read_configure.c
bloba32f34a7f270836643c5ffc7c1a3e8f446373906
1 /*
2 * Copyright (c) 2007,西安邮电学院Linux兴趣小组
3 * All rights reserved.
5 * 文件名称:read_configure.c
6 * 摘要:读取配置文件内容,测试使用。
8 * 当前版本:0.1
9 * 作者:林峰
10 * 完成日期:2007年5月15日
12 * 修改者:林峰
13 * 修改日期:2007年6月1日
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include "xylftp.h"
22 #define CONFIG_NUM 13 /*item number of the configue file*/
23 struct run_env run_env;
25 int _read_line(int fd,char *buf);
26 int _analyze_para(char *buf,int *array);
27 int _check_configure(int *array);
28 #ifdef DEBUG
29 void print(void);
31 void print(void)
33 printf("run_env.anonymous_enable = %d\n",run_env.anonymous_enable);
34 printf("run_env.ftp_port = %d\n",run_env.ftp_port);
35 printf("run_env.local_umask = %d\n",run_env.local_umask);
36 printf("run_env.log_file_enable = %d\n",run_env.log_file_enable);
37 printf("run_env.log_file = %s\n",run_env.log_file);
38 printf("run_env.idle_session_timeout = %d\n",run_env.idle_session_timeout);
39 printf("run_env.data_connection_timeout = %d \n",run_env.data_connection_timeout);
40 printf("run_env.ftpd_banner = %s\n",run_env.ftpd_banner);
41 printf("run_env.max_clients = %d\n",run_env.max_clients);
42 printf("run_env.max_links = %d\n",run_env.max_links);
43 printf("run_env.passive_port = %d,%d\n",run_env.passive_port_min,run_env.passive_port_max);
44 printf("run_env.ftp_dir = %s\n",run_env.ftp_dir);
45 printf("run_env.user_pass_file = %s\n",run_env.user_pass_file);
47 #endif
49 int _analyze_para(char *buf,int *array)
51 char *tmp;
52 char *option = NULL,*value = NULL; /*used to store the option strings and value strings*/
53 char *saveptr1,*saveptr2; /*used for the function strtok_r*/
54 if ((tmp = strtok_r(buf,"=",&saveptr1)) == NULL) {
55 fprintf(stderr,"error in configure file\n");
56 return -1;
57 } /*get the option string*/
58 if ((option = malloc(sizeof(char) * (strlen(tmp) + 1))) == NULL) {
59 fprintf(stderr,"Not enough memory!\n");
60 goto ret;
62 strcpy(option,buf); /*store option*/
63 if((tmp = strtok_r(NULL,"=",&saveptr1)) == NULL) {
64 fprintf(stderr,"error in configure file\n");
65 return -1;
67 if ((value = malloc(sizeof(char) * ((strlen(tmp) + 1)))) == NULL) {
68 fprintf(stderr,"Not enough memory!\n");
69 goto ret;
71 strcpy(value,tmp); /*store value*/
73 /*codes below are used to value the parameters of the struct run_env*/
74 if (!strcmp(option,"Anonymous_enable")) {
75 if (!strcmp(value,"YES")) {
76 run_env.anonymous_enable = TRUE;
77 } else {
78 run_env.anonymous_enable = FALSE;
80 array[0] = 1;
81 } else if (!strcmp(option,"FTP_port")) {
82 run_env.ftp_port = (unsigned int)(atoi(value));
83 array[1] = 1;
84 } else if (!strcmp(option,"Local_umask")) {
85 run_env.local_umask = (unsigned int)(atoi(value));
86 array[2] = 1;
87 } else if (!strcmp(option,"Log_file_enable")) {
88 if (!strcmp(value,"YES")) {
89 run_env.log_file_enable = 1;
90 } else {
91 run_env.log_file_enable = 0;
93 array[3] = 1;
94 } else if (!strcmp(option,"Log_file")) {
95 if ((run_env.log_file = malloc((strlen(value) + 1) * sizeof (char))) == NULL) {
96 fprintf(stderr,"Not enough memory!\n");
97 goto ret;
99 strcpy(run_env.log_file,value);
100 array[4] = 1;
101 } else if (!strcmp(option,"Idle_session_timeout")) {
102 run_env.idle_session_timeout = (unsigned int)(atoi(value));
103 array[5] = 1;
104 } else if (!strcmp(option,"Data_connection_timeout")) {
105 run_env.data_connection_timeout = (unsigned int)(atoi(value));
106 array[6] = 1;
107 } else if (!strcmp(option,"Ftpd_banner")) {
108 if ((run_env.ftpd_banner = malloc((strlen(value) + 1) * sizeof (char))) == NULL) {
109 fprintf(stderr,"Not enough memory!\n");
110 return -1;
112 strcpy(run_env.ftpd_banner,value);
113 array[7] = 1;
114 } else if (!strcmp(option,"Max_clients")) {
115 run_env.max_clients = (unsigned int)(atoi(value));
116 array[8] = 1;
117 } else if (!strcmp(option,"Max_links")) {
118 run_env.max_links = (unsigned int)(atoi(value));
119 array[9] = 1;
120 } else if (!strcmp(option,"Passive_port")) {
121 if ((tmp = strtok_r(value,",",&saveptr2)) == NULL) {
122 fprintf(stderr,"error in configure file\n");
123 return -1;
125 run_env.passive_port_min = (unsigned int)(atoi(tmp)); /*get the min_port and store it*/
126 if ((tmp = strtok_r(NULL,",",&saveptr2)) == NULL) {
127 fprintf(stderr,"error in configure file\n");
128 return -1;
129 } /*get the max_port and store it*/
130 run_env.passive_port_max = (unsigned int)(atoi(tmp));
131 array[10] = 1;
132 } else if (!strcmp(option,"FTP_dir")) {
133 if ((strlen(value) + 1) > PATH_NAME_LEN) {
134 fprintf(stderr, "Path name is too long!\n");
135 return -1;
137 strcpy(run_env.ftp_dir,value);
138 array[11] = 1;
139 } else if (!strcmp(option,"User_pass_file")) {
140 if ((run_env.user_pass_file = malloc((strlen(value) + 1) * sizeof (char))) == NULL) {
141 fprintf(stderr,"Not enough memory!\n");
142 goto ret;
144 strcpy(run_env.user_pass_file,value);
145 array[12] = 1;
146 } else {
147 fprintf(stderr,"Parameter can't be analyzed!\n");
148 goto ret;
150 /*codes above are used to value the parameters of the struct run_env*/
152 return 0;
153 ret:
154 free(run_env.user_pass_file); /*free the memory*/
155 free(run_env.log_file); /*free the memory*/
156 free(value); /*free the memory*/
157 free(option); /*free the memory*/
158 return -1;
162 int read_configure()
164 int fd;
165 char buffer[_BUFFER_LENGTH];
166 int i;
167 int config_array[CONFIG_NUM];
169 for (i = 0;i < CONFIG_NUM;i++) {
170 config_array[i] = 0;
173 i = 0;
174 if ((fd = open(CONFIG_FILE,O_RDONLY)) == -1) { /*open the configure file error*/
175 fprintf(stderr,"can't open the configure file,please use the tool xylftppass to solve this problem!\n");
176 return -1;
177 } else {
178 i = _read_line(fd,buffer); /*get an valuable line from the configure file*/
179 #ifdef DEBUG
180 while (i != EOF ) {
182 if (i == 1) {
183 printf("%s\n",buffer);
184 fflush(stdout);
185 _analyze_para(buffer,config_array); /*analyze the line got from the file*/
187 i = _read_line(fd,buffer);
189 print();
190 #endif
191 close(fd);
194 if (_check_configure(config_array) == -1) {
195 /*write_log("configure file error!",0);*/
196 fprintf(stderr,"configure file error!");
197 return -1;
199 return 0;
202 int _read_line(int fd,char *buf)
204 char ch;
205 int i = 0,j;
207 if ((j = read(fd,&ch,1)) == 0) { /*end of the file*/
208 return EOF;
213 while (ch != '\n' && j != 0 ){ /*if the character we got is not an end of a line or the end of the file*/
214 if (ch == '#' ) { /*if it is a comment line,skip it over*/
215 while (ch != '\n' ) {
216 if ((j = read(fd,&ch,1)) == 0) {
217 break;
220 j = read(fd,&ch,1);
221 } else if (ch == ' ') { /*if it is a space,skip to the next character*/
222 if (i != 0) {
223 buf[i++] = ch;
225 j = read(fd,&ch,1);
226 } else if (j != 0){ /*a valuable character,store it*/
227 buf[i++] = ch;
228 j = read(fd,&ch,1);
232 if ( j == 0 ) { /*end of the line,also the end of the file,return EOF*/
233 buf[i] = '\0';
234 return EOF;
235 } else if (i != 0) { /*end of an valuable line*/
236 buf[i] = '\0';
237 return 1;
239 return 0;
242 int _check_configure(int *array)
244 int i = 0;
245 if(!array[0]) {
246 fprintf(stderr,"no Anonymous_enable item or error\n");
248 if (!array[1]) {
249 fprintf(stderr,"no FTP_port item or error\n");
251 if (!array[2]) {
252 fprintf(stderr,"no Local_umask item or error\n");
254 if (!array[3]) {
255 fprintf(stderr,"no Log_file_enable item or error\n");
257 if (!array[4]) {
258 fprintf(stderr,"no Log_file item or error\n");
260 if (!array[5]) {
261 fprintf(stderr,"no Idle_session_timeout item or error\n");
263 if (!array[6]) {
264 fprintf(stderr,"no Data_connection_timeout item or error\n");
266 if (!array[7]) {
267 fprintf(stderr,"no Ftpd_banner item or error\n");
269 if (!array[8]) {
270 fprintf(stderr,"no Max_clients item or error\n");
272 if (!array[9]) {
273 fprintf(stderr,"no Max_links item or error\n");
275 if (!array[10]) {
276 fprintf(stderr,"no Passive_port item or error\n");
278 if (!array[11]) {
279 fprintf(stderr,"no FTP_dir item or error\n");
281 if (!array[12]) {
282 fprintf(stderr,"no User_pass_file item or error\n");
285 for (i = 0;i < CONFIG_NUM;i++) {
286 if (array[i] == 0) {
287 return -1;
291 return 0;
294 int main(void)
296 read_configure();
297 return 0;