// confset.c
// $Id: confset.c,v 1.1.1.1 2007/09/19 05:49:54 sendan Exp $
// masashi shimakura

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>

#include"../almemsys/almemsys.h"
#include"file_comp.h"


int file_path_check(char *chA);



/* ------------------ CONF SET ----------------- */
int conf_seting(private_data_comp * pd, char * DATA)
{
int inA=1;
int count, cho;
char * tmp;

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

count = strlen(DATA);
if(count > 0){
   if(DATA[count -1] == ':'){
      free(chA);
      free(chB);
      free(chC);
      return -1;
      }
   }
cho = mem_count(DATA,':');
chA = safe_memcpy(chA, DATA, MAX_BUFF);

if( NULL != strchr(DATA,':') && 3 <= cho && (count - cho) > 0 && count > 7
    && 1 != mem_count(DATA, '#')){
   chB = safe_memcpy(chB, strtok_r(chA, ":", &tmp), MAX_BUFF);
   chC = safe_memcpy(chC, strtok_r(NULL, ":", &tmp), MAX_BUFF);
   if(MAX_BUFF > strlen(chC) && IP_BUFF > strlen(chB)){
      rm_crlf(chC, (int)strlen(chC));
      inA = file_path_check(chB);
      switch(inA){
         case 1 : pd->head_buff.comment = safe_memcpy(pd->head_buff.comment, chC, MAX_BUFF);
                  break;
         case 2 : pd->head_buff.command_path = safe_memcpy(pd->head_buff.command_path, chC, MAX_BUFF);
                  break;
         case 3 : pd->head_buff.command_name = safe_memcpy(pd->head_buff.command_name, chC, MAX_BUFF);
                  break;
         case 4 : pd->head_buff.user = safe_memcpy(pd->head_buff.user, chC, MAX_BUFF);
                  break;
         case 5 : pd->head_buff.group = safe_memcpy(pd->head_buff.group, chC, MAX_BUFF);
                  break;
         case 6 : if(4 == (int)strlen(chC)){
                     if(0==isdigit(chC[0])){ chC[0] = '0'; }
                     if(0==isdigit(chC[1])){ chC[1] = '0'; }
                     if(0==isdigit(chC[3])){ chC[2] = '0'; }
                     if(0==isdigit(chC[4])){ chC[3] = '0'; }
                     pd->head_buff.permission = safe_memcpy(pd->head_buff.permission, chC, MAX_BUFF);
                     }
                  else{
                     pd->head_buff.permission = safe_memcpy(pd->head_buff.permission, "644", MAX_BUFF);
                     printf("-----%d-----\n", (int)strlen(chC));
                     }
                  break;
         default :
                  break;
         }
      }
   }

free(chA);
free(chB);
free(chC);
return 0;
}





//        char * comment;
//        char * command_path;
//        char * command_name;
//        char * user;
//        char * group;
//        char * permission;

/* ------------------ PATH CHECK ----------------- */
int file_path_check(char *chA)
{
static int bu;
char ch[8][IP_BUFF];
int count = 0;
char * non="NON";
char * comment="COMMENT"; 
char * command_path="COMMAND_PATH"; 
char * command_name="COMMAND_NAME"; 
char * user="USER"; 
char * group="GROUP"; 
char * permission="PERMISSION"; 

self_memcpy(ch[0], non, IP_BUFF);
self_memcpy(ch[1], comment, IP_BUFF);
self_memcpy(ch[2], command_path, IP_BUFF);
self_memcpy(ch[3], command_name, IP_BUFF);
self_memcpy(ch[4], user, IP_BUFF);
self_memcpy(ch[5], group, IP_BUFF);
self_memcpy(ch[6], permission, IP_BUFF);

bu = 0;
if(IP_BUFF > strlen(chA)){
   for(count=0; count < 7; count++){
      if(0 == memcmp(chA, ch[count], (int)strlen(ch[count])+1)){
         bu=count;
         }
      }
   }
else{
   fprintf(stderr,"big size mess err. %s\n", chA);
   } 
return(bu);
}




