#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "extern.h"

/* Kill ｺﾏﾝﾄﾞ */
exedlt()
        {   
        char fno[5], ftitle[31], fjunle[9], fcall[9], fdate[9], ftime[6];
          /* ﾌｧｲﾙ No. ﾀｲﾄﾙ        ｼﾞｬﾝﾙ   ｶｷｺﾐ ｺｰﾙ    ﾋｽﾞｹ      ｼﾞｺｸ  */   
        char fmail[2];
          /*   mail ﾌﾗｸﾞ */       

        int  i;
        int  numd;     /* ｺﾏﾝﾄﾞ ｵﾌﾟｼｮﾝ ｽｳﾁ */
        long pnf;

		if ( mail && *(cmdopt) == 'A' )  { mldlt(); return(0); }
							 /* MAIL KILL All コマンド */

        i = atoi(cmdnum);    /* ｺﾏﾝﾄﾞ ｵﾌﾟｼｮﾝ ｽｳﾁ ﾍﾝｶﾝ */
                    
        if (strlen(cmdopt) || i > 999)
                   { paraerr(); return(0);  }   /* ｺﾏﾝﾄﾞ ｵﾌﾟｼｮﾝ ｽｳﾁ ﾁｪｯｸ */
        
        if (! i) { paraneed(); return(0); }

        if (rtbl[i] == NULL || rtbl[i] == -1)
           {   sprintf(ssent,": ファイルＮｏ．にエラーがあります\n\n");
               rsout();
               return(0);       }
        
        numd = rtbl[i];                    /* ｼﾞﾂ ﾌｧｲﾙ No. ﾊｱｸ */
        printf("*** DLT FILE = %d\n",numd);

        if ( mail ) { fpw = fopen(MAIL_HDR,"r+"); }               
             else   { fpw = fopen(HDR_FIL,"r+");  }    /* ﾍｯﾀﾞ ﾌｧｲﾙ open */ 
        if (fpw == NULL) { return(0);}
        rewind(fpw);

   while(1)
    {   pnf = ftell(fpw);
        if ( fgets(fbuff,255,fpw) == NULL) { fclose(fpw);
                                             return(0);}         /* ﾖﾐｺﾐ */
        
        if ( ! isdigit(*(fbuff)) ) continue;    /* ｻｸｼﾞｮ ｽﾐ ﾌｧｲﾙ ﾊ ﾖﾐﾄﾊﾞｼ */
        
        substr(fbuff,0,4,fno);
        if (numd != atoi(fno))   continue;     /* ﾌｧｲﾙ No. ｲｯﾁ ｼﾅｹﾚﾊﾞ ﾖﾐﾄﾊﾞｼ */
        substr(fbuff,4,1,fmail);
        substr(fbuff,5,8,fjunle);
        substr(fbuff,15,30,ftitle);
        substr(fbuff,47,8,fcall);
        substr(fbuff,55,8,fdate);
        substr(fbuff,63,5,ftime);
        break;
    } 
    if ( ! mail )
      { if ( strcmp(sigop,hiscall) != 0 && strcmp(sysop,hiscall) != 0 )
         { if (strcmp(fcall,hiscall) != 0 )
           {   sprintf(ssent,": %s が書きこんだファイルです。削除できません。\n\n",fcall);
               rsout();
               fclose(fpw);
               return(0);   }
         }
        if (strcmp(nowjle,mainjle) == 0)
           {sprintf(ssent,": %s %s   %s %s\n",fjunle,ftitle,fdate,ftime);}
         else    
           {sprintf(ssent,": %s   %s  %s\n",ftitle,fdate,ftime);}
        rsout();                        /* ﾌｧｲﾙ ﾍｯﾀﾞ ｵｸﾘﾀﾞｼ */
          
                    /* sigop , ｶｷｺﾐ ｷｮｸ ｲｶﾞｲ ﾉ ｻｸｼﾞｮ ｷﾝｼ */
      }
    else
         { if ( strcmp(fjunle,hiscall) && *(fmail) == 'R' )
             {  sprintf(ssent,": %s が保存中のメールです。削除できません。\n",fjunle);
                rsout();
                fclose(fpw);
                return(0);
             }
           if ( ! strcmp(fjunle,hiscall) && *(fmail) == ' ' && strcmp(fcall,hiscall) )
             {  sprintf(ssent,": まだ読まれていないメールです。削除できません。\n");
                rsout();
                fclose(fpw);
                return(0);
             }
           sprintf(ssent,"To=%s  From=%s  %s %s %s\n",fjunle,fcall,ftitle,fdate,ftime);
           rsout();
         }
                      
        sprintf(ynsent,": 削除 "); 
        if (y_or_n())
           { sprintf(ssent,": 削除はキャンセルされました\n\n");
             rsout();
             fclose(fpw);
             return(0);
             }     

        i = atoi(cmdnum);
        rtbl[i] = -1;                           /* ﾖﾐﾀﾞｼ ﾃｰﾌﾞﾙ ｺｳｼﾝ */

        fseek(fpw,pnf,0);              /* ﾚｺｰﾄﾞ ﾉ ｾﾝﾄｳ ﾆ ﾓﾄﾞﾙ */
        
        if ( mail && ! strcmp(fjunle,hiscall) && strcmp(fcall,hiscall) )
           { fseek(fpw,4L,1);
             fputs("X",fpw);
             fclose(fpw);
             sprintf(ssent,"***** ファイルＮｏ． %2d を削除しました *****\n\n",i);
             rsout();
             return(0);
           }                          /* ﾒｰﾙ ｵｸﾘｻｷ ﾉ ｷｮｸ ﾆ ﾖﾙ ｻｸｼﾞｮ */

        fputs("D",fpw);               /* 'D' ｦ ｲﾚﾃ ﾚｺｰﾄﾞ ｺｳｼﾝ */

        fclose(fpw);

      /* ｶﾝﾘ ﾌｧｲﾙ ｺｳｼﾝ */
      
      fpw = fopen(KANRI_FIL,"r+");
                
      pnf = numd;
      fseek(fpw,pnf,0);
      
      fbuff[0] = '0'; fbuff[1] = NULL;
      fputs(fbuff,fpw);
      
      fclose(fpw);        

   /* MSG & PDS & DATALIB ﾌｧｲﾙ ｻｸｼﾞｮ */
      makfname("MSG",numd,fname);
      remove(fname);
      
      if ( ! strncmp(fjunle,"PDS",3) )
         { 
	   makfname("PDS",numd,fname);
           remove(fname);
         }
      if ( ! strncmp(fjunle,"DAT",3) )
         { 
	   makfname("DAT",numd,fname);
           remove(fname);
         }
                  
      sprintf(ssent,"***** ファイルＮｏ． %2d を削除しました *****\n\n",i);
      rsout();
                
      return(0);
      } 



/* MAIL Kill All ｺﾏﾝﾄﾞ */
mldlt()
        {   
        char fno[5], fcall[9];
        char fmail[2];
          /*   mail ﾌﾗｸﾞ */       

        int  i,dltcnt = 0;
        int  numd;
        long pnf;

		strcpy(ssent,"\n相手が読込済みのメールを削除します\n");
		rsout();
        fpw = fopen(MAIL_HDR,"r+");
        if (fpw == NULL) { return(0);}
        rewind(fpw);

   while(1)
    {   pnf = ftell(fpw);
        if ( fgets(fbuff,255,fpw) == NULL) { break; }         /* ﾖﾐｺﾐ */
        
        if ( ! isdigit(*(fbuff)) ) continue;    /* ｻｸｼﾞｮ ｽﾐ ﾌｧｲﾙ ﾊ ﾖﾐﾄﾊﾞｼ */
        
        substr(fbuff,0,4,fno);
        numd = atoi(fno);
        substr(fbuff,4,1,fmail);
        substr(fbuff,47,8,fcall);
		if ( strcmp(fcall,hiscall) )  continue;
		if ( *(fmail) != 'X' ) continue;

        fseek(fpw,pnf,0);              /* ﾚｺｰﾄﾞ ﾉ ｾﾝﾄｳ ﾆ ﾓﾄﾞﾙ */
        fbuff[0] = 'D';
        fputs(fbuff,fpw);               /* 'D' ｦ ｲﾚﾃ ﾚｺｰﾄﾞ ｺｳｼﾝ */
		pnf = ftell(fpw);
		fseek(fpw,pnf,0);

      /* ｶﾝﾘ ﾌｧｲﾙ ｺｳｼﾝ */
      
      fp2 = fopen(KANRI_FIL,"r+");
                
      pnf = numd;
      fseek(fp2,pnf,0);
      
      fbuff[0] = '0'; fbuff[1] = NULL;
      fputs(fbuff,fp2);
      
      fclose(fp2);        

      makfname("MSG",numd,fname);
      remove(fname);
      dltcnt++;
	}
	 fclose(fpw);
	 if ( dltcnt ) {
     				sprintf(ssent,"\n%d通のメールを削除しました\n",dltcnt);
				   }
		else
				  { strcpy(ssent,"\n削除したメールはありません\n");
				  }
	 rsout();           
     return(0);
   } 

