
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/socket.h>
#include<netinet/in_systm.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
#include<errno.h>

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

extern global_data_t gd;


int lockfile_remove(char * neg_filename, char * lock_path, char * conf_path)
{
static int ret;
FILE * fp;
int fp_stat = 0;
int count0 = 0;
int count1 = 0;
char chA;
char data[2][IP_LEN + 2];
char * alllock_filename = (char *)calloc(BUF_DEFLEN, sizeof(char));

ret = 0;
if((fp = fopen(neg_filename, "r+"))!=NULL){
   while(fp_stat == 0){
      chA = fgetc(fp);
      switch(chA){
         case ':' : data[count0][count1] = (char)0x00;
                    if(count0 < 1){
                       count1 = 0;
                       count0++;
                       }
                    break;
         case ';' : data[count0][count1] = (char)0x00;
                    if(count1 != 0){
                       if(0 != strlen(data[count0])){
                          alllock_filename = filename_create(alllock_filename, lock_path, "_lock", data[0]);
                          lock_remove(alllock_filename);
                          alllock_filename = filename_create(alllock_filename, conf_path, ".list_lock", data[0]);
                          lock_remove(alllock_filename);
                          alllock_filename = filename_create(alllock_filename, conf_path, ".stat_lock", data[0]);
                          lock_remove(alllock_filename);
                          alllock_filename = filename_create(alllock_filename, conf_path, ".conf_lock", data[0]);
                          lock_remove(alllock_filename);
                          alllock_filename = filename_create(alllock_filename, conf_path, ".command_lock", data[0]);
                          lock_remove(alllock_filename);
                          if(gd.cron_onoff == 1){
                             alllock_filename = filename_create(alllock_filename, conf_path, ".cronfile_lock", data[0]);
                             lock_remove(alllock_filename);
                             alllock_filename = filename_create(alllock_filename, conf_path, ".cronlist_lock", data[0]);
                             lock_remove(alllock_filename);
                             }
                          }
                       count0 = 0; count1 = 0;
                       }
                    break;
         case '\n' : data[count0][count1] = (char)0x00;
                    if(count1 != 0){
                       if(0 != strlen(data[count0])){
                          alllock_filename = filename_create(alllock_filename, lock_path, "_lock", data[0]);
                          lock_remove(alllock_filename);
                          alllock_filename = filename_create(alllock_filename, conf_path, ".list_lock", data[0]);
                          lock_remove(alllock_filename);
                          alllock_filename = filename_create(alllock_filename, conf_path, ".stat_lock", data[0]);
                          lock_remove(alllock_filename);
                          alllock_filename = filename_create(alllock_filename, conf_path, ".conf_lock", data[0]);
                          lock_remove(alllock_filename);
                          alllock_filename = filename_create(alllock_filename, conf_path, ".command_lock", data[0]);
                          lock_remove(alllock_filename);
                          if(gd.cron_onoff == 1){
                             alllock_filename = filename_create(alllock_filename, conf_path, ".cronfile_lock", data[0]);
                             lock_remove(alllock_filename);
                             alllock_filename = filename_create(alllock_filename, conf_path, ".cronlist_lock", data[0]);
                             lock_remove(alllock_filename);
                             }
                          }
                       count0 = 0; count1 = 0;
                       }
                    break;
         case EOF : fp_stat = 1;
                    data[count0][count1] = (char)0x00;
                    break;
         default  : data[count0][count1] = chA;
                    if(count1 < IP_LEN){
                       count1++;
                       }
                    break;
         }
      }
   fclose(fp);
   free(alllock_filename);
   }
else {
   fprintf(stderr,"lockfile_remove(): There is no nego list file. %s\n", neg_filename);
   ret = 0;
   }

return(ret);
} 





