#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#include "extern.h"

/* Log ｺﾏﾝﾄﾞ */
exelog()
        {   
        char fcall[9], fdate[9], ftime[6],  fno[5];
          /* ｶｷｺﾐ ｺｰﾙ    ﾋｽﾞｹ      ｼﾞｺｸ */       
        char fday[14];   /* fday + ftime   ﾋｶｸ ﾊﾝﾀﾞﾝ ﾖｳ */
        char wdate[9];   /* ﾊﾟﾗﾒｰﾀ ﾆ ﾖﾙ ﾎｾｲ ｼﾀ find ﾋｽﾞｹ */
        char rdate[9];   /* 5ﾆﾁ ﾏｴ ﾉ ﾋｽﾞｹ  ﾏﾀﾊ  ﾖﾐﾀﾞｽ ｶｲｼ ﾋｽﾞｹ */
        char rtime[6];   /* ﾖﾐﾀﾞｼ ｶｲｼ ｼﾞｺｸ */
        char rday[14];   /* rday + rtime   ﾋｶｸ ﾊﾝﾀﾞﾝ ﾖｳ */

        int i,j; /* ﾜｰｸ ｼﾋｮｳ */
        short numd;     /* ｺﾏﾝﾄﾞ ｵﾌﾟｼｮﾝ ｽｳﾁ */
                
        numd = atoi(cmdnum);
                    
                 nwdate();                              /* ｹﾞﾝｻﾞｲ ﾋｽﾞｹ ﾊｱｸ */
                 for ( i = 0 ; i < 5 ; i++)             /* 5ﾆﾁ ﾏｴ ｻﾝｼｭﾂ */
                 {  td[2]--;
                    if (td[2] == 0) { td[1]--; td[2] = 30;}
                    if (td[1] == 0) { td[0]--; td[1] = 12;}
				 }
            sprintf(rdate,"%02d/%02d/%02d",td[0],td[1],td[2]);
                                                   /* ﾖﾐﾀﾞｼ ｶｲｼ ｦ 5ﾆﾁ ﾏｴ ﾆ */
        sprintf(rtime,"00:00");
 
        if (strcmp(lastdate,rdate) > 0) { strcpy(rdate,lastdate);
                                          strcpy(rtime,lasttime);
                                           }
                        /* ﾖﾐﾀﾞｼ ｶｲｼ ﾋｽﾞｹ ｦ ﾎｾｲ (ｻｲｼｭｳ ﾖﾐﾀﾞｼ ﾆ ﾎｾｲ) */
        strcpy(rday,rdate); strcat(rday,rtime);    /* ﾋｶｸ ﾊﾝﾀﾞﾝ ﾖｳ ﾍﾝｼｭｳ */
        
        fno[0] = rday[6]; fno[1] = rday[7]; fno[2] = NULL;
        i = atoi(fno);

        if (numd > 0)                 /* ﾖﾐﾀﾞｼ ﾋｽﾞｹ ｻﾝｼｭﾂ */
           {    nwdate();             /* ｹﾞﾝｻﾞｲ ｼﾞｺｸ ﾋｽﾞｹ ﾊｱｸ */
                
                if (numd < 99)                  /* 2ｹﾀ ｲﾅｲ ﾅﾗ ﾂｷ ｦ ﾎｾｲ */
                   {    if (numd > 31) {paraerr(); return(0); }
                        if (td[2] < numd) { td[1]--; }
                        if (td[1] == 0)   { td[1] = 12; td[0]--; }
                        td[2] = numd;
                       }
                else
                   {    i = numd / 100;         /* 3ｹﾀ ｲｼﾞｮｳ ﾅﾗ ﾂｷ､ﾈﾝ ﾎｾｲ */
                        numd = numd - (i * 100);
                        if (numd > 31 || i > 12) {paraerr(); return(0); }
                        if (td[1] < i )  td[0]--;
                        td[1] = i; td[2] = numd;
                       }
         sprintf(wdate,"%02d/%02d/%02d",td[0],td[1],td[2]);
              }                         /* ﾖﾐﾀﾞｼ ﾋｽﾞｹ ｶｸﾉｳ */

        if ( strcmp(hiscall,sysop) )  j = 1;  else j = 0;

      for (i = j ; i < 2 ; i++ )
      { if ( i == 0 ) {fpw = fopen(FORWARD_LOG,"r");
                       strcpy(ssent,"\n  === ﾌｧｲﾙ交換 ===\n"); }
        if ( i == 1 ) {fpw = fopen(LOG_FIL,"r");
                       if ( ! strcmp(hiscall,sysop))
                       strcpy(ssent,"\n  === 電話回線 ===\n");}
       
        if ( i ) { strcat(ssent,"  日付   開始   終了 ID\n"); }
           else  { strcat(ssent,"  日付 　開始　 終了 ID        送  受\n"); }
        if ( ! exp_flg ) rsout();
        *(ssent) = NULL;
        while(fpw)
          {  if ( fgets(fbuff,255,fpw) == NULL ) break;
             substr(fbuff,0,8,fdate);
             substr(fbuff,9,5,ftime);
             substr(fbuff,21,8,fcall);
             strcpy(fday,fdate); strcat(fday,ftime);
             if(numd == 0 && strcmp(fday,rday) < 0 ) continue;
             if(numd == -9998 && strcmp(fcall,hiscall) != 0 ) continue;
             if(numd >  0 && strcmp(fdate,wdate)) continue;
             strcat(ssent,fbuff);
			 if ( cutdsp(rsout()) ) return(0);
           }        
         if ( strlen(ssent) )  rsout();
         fclose(fpw);
     }
      return(0);
    }
