

#include<stdio.h>
#include<unistd.h>
#include<netinet/in.h>
#include<string.h>
#include<stdlib.h>
#include<pthread.h>


#include"zither.h"
#include<almemsys/almemsys.h>


extern global_data_t gd;


int conf_set( char *conf_filename )
{
FILE * FP;
int inA=1;
int stat = 0;
// char * tmp;

char * chB;
char * chC;

char * chA = (char *)calloc(IP_LEN, sizeof(char));
// char * chB = (char *)calloc(IP_LEN, sizeof(char));
// char * chC = (char *)calloc(IP_LEN, sizeof(char));


if((FP = fopen(conf_filename, "r"))!=NULL){
    while(1){
      chA = safe_fpcat(FP, chA, &stat, BUF_MAX);
      if(stat == -1){
         fprintf(stderr,"conf_set(): Terminal of file.\n"); 
         break;
         }
      else {
         int po = mem_tcount(chA, ':');
         int sz = strlen(chA);
         int gg = char_count(chA, ':');
         if( NULL != strchr(chA,':') && 3 <= po && po < sz && 5 <= sz && 8 >= gg &&
           1 <= gg && 1 != mem_count(chA, '#')){ 

            chB = (char *)calloc(strlen(chA), sizeof(char));
            chC = (char *)calloc(strlen(chA), sizeof(char));


            chB = count_memcpy2(chB, chA, 0, (mem_tcount(chA, ':') - 1));
            chC = safe_memcpy(chC, (strchr(chA, ':') + 1), BUF_MAX);

// v6 test 用コメント
// printf("chA=%s chB=%s chC=%s\n", chA, chB, chC);


	    if(BUF_LEN > strlen(chC) && IP_LEN > strlen(chB)){
               rm_crlf(chC, strlen(chC));
               inA = path_check(chB);
               switch(inA){
                  case 1 : gd.fp.errlog=safe_memcpy(gd.fp.errlog, chC, LOGPATH_LEN);                        break;
                  case 2 : gd.fp.file_conf=safe_memcpy(gd.fp.file_conf, chC, LOGPATH_LEN);                  break;
                  case 3 : gd.ms.ip_addr = safe_memcpy(gd.ms.ip_addr, chC, IP_LEN);                         break;
                  case 4 : gd.ms.chport = safe_memcpy(gd.ms.chport, chC, IP_LEN);
                           gd.ms.port = strtol(chC, (char **)NULL, 10);                                     break;
                  case 5 : gd.ms.max_count = strtol(chC, (char **)NULL, 10);                                break;
                  case 6 : gd.md.ch_MailFrom = safe_memcpy(gd.md.ch_MailFrom, chC, IP_LEN);                 break;
                  case 7 : gd.md.ch_MailRcptTo = safe_memcpy(gd.md.ch_MailRcptTo, chC, IP_LEN);             break;
                  case 8 : gd.md.ch_MailServ = safe_memcpy(gd.md.ch_MailServ, chC, IP_LEN);                 break;
                  case 9 : gd.bs.life_int= strtol(chC, (char **)NULL, 10);                                  break;
                  case 10 : gd.bs.life_los= strtol(chC, (char **)NULL, 10);                                 break;
                  case 11 : gd.md.ch_MailCc = safe_memcpy(gd.md.ch_MailCc, chC, IP_LEN);                    break;
                  case 12 : gd.fp.lock_path=safe_memcpy(gd.fp.lock_path, chC, LOGPATH_LEN);                 break;
                  case 13 : gd.exid.uid = strtol(chC, (char **)NULL, 10);                                   break;
                  case 14 : gd.md.in_mailonoff = strtol(chC, (char **)NULL, 10);                            break;
                  case 15 : gd.fp.clock_path=safe_memcpy(gd.fp.clock_path, chC, LOGPATH_LEN);               break;
                  case 16 : gd.fp.connectlog=safe_memcpy(gd.fp.connectlog, chC, LOGPATH_LEN);               break;
                  case 17 : gd.pg.postgres_data=safe_memcpy(gd.pg.postgres_data, chC, LOGPATH_LEN);         break;
                  case 18 : gd.pg.pg_webserver=safe_memcpy(gd.pg.pg_webserver, chC, LOGPATH_LEN);           break;
                  case 20 : gd.mysql.mysql_server=safe_memcpy(gd.mysql.mysql_server, chC, LOGPATH_LEN);     break;
                  case 21 : gd.mysql.mysql_username=safe_memcpy(gd.mysql.mysql_username, chC, LOGPATH_LEN); break;
                  case 22 : gd.mysql.mysql_password=safe_memcpy(gd.mysql.mysql_password, chC, LOGPATH_LEN); break;
                  case 23 : gd.mysql.mysql_db=safe_memcpy(gd.mysql.mysql_db, chC, LOGPATH_LEN);             break;
                  case 24 : gd.db_select=safe_memcpy(gd.db_select, chC, LOGPATH_LEN);                       break;
                  case 25 : gd.on_bcc=safe_memcpy(gd.on_bcc, chC, LOGPATH_LEN);                             break;
                  case 26 : gd.cron_onoff = strtol(chC, (char **)NULL, 10);                                 break;
                  case 27 :
                            #ifdef SIGNATURE_MODE
                            gd.sig.sig_filename = safe_memcpy(gd.sig.sig_filename, chC, LOGPATH_LEN);
                            #endif
                            break;
                  case 28 :
                            #ifdef SIGNATURE_MODE
                            gd.sig.sig_interval = strtol(chC, (char **)NULL, 10);
                            #endif
                            break;
                  case 29 : gd.fi.midump_filename = safe_memcpy(gd.fi.midump_filename, chC, LOGPATH_LEN);     break;
                  case 30 : gd.fi.midump_filename_2 = safe_memcpy(gd.fi.midump_filename_2, chC, LOGPATH_LEN); break;
                  case 31 : gd.td.midcount= strtol(chC, (char **)NULL, 10);                                   break; //MID_MAX
                  case 32 : gd.td.midint= strtol(chC, (char **)NULL, 10);                                     break; //MID_INT

                  default : break;
                  }
	       }

            free(chB);
            free(chC);
            }
         }
      } 
   fclose( FP );
   }
else {
   fprintf(stderr,"-- CONF FILE NOT OPEN %s\n",conf_filename);
   fprintf(stderr,"-- zither shutdown.\n");
   exit(1);
   }

free(chA);
// free(chB);
// free(chC);

return 0;
}




