#include <string.h>

#include "extern.h"

/* login */
        
login()
        {
         int i,j;
         char pno[3],exp_ch[2];
		 char w_junle[9];
		 long ptr;
         
         /* OPENNING ﾒｯｾｰｼﾞ */
         nwdate();
         *(ssent) = NULL;
         sprintf(fbuff,"\nWelcome  %s-BBS Ver1.2 %s %s\n",rbbscall,nowdate,nowtime);
         strcat(ssent,fbuff);
         sprintf(fbuff,"\n  Sysop:%s %s\n\n",sysop,sysopname);
         strcat(ssent,fbuff);
         rsout();

		strcpy(fname,OPN_FIL);
		sendfile();
                     /* オープニングメッセージをファイルより直送信 90.11.30 */

         /* ﾛｸﾞ ﾌｱｲﾙ ｹﾝｻｸ */
         fpw = fopen(LOG_FIL,"a");
         
         fprintf(fpw,"%s %s %s %s %1d\n",nowdate,nowtime,nowtime,hiscall,devdrv);
    	 freopen(LOG_FIL,"r",fpw);
         fseek(fpw,-18L,2);
         logptr = ftell(fpw);
         fclose(fpw);
           /* log ｶｷｺﾐ */
         
         sprintf(lastdate,"00/00/00"); sprintf(lasttime,"00:00"); userptr = 0;
         exp_flg = 0;
                                        /* first access ｷﾖｸ ﾊ ｾﾞﾝｶｲ zero */
         fpw = fopen(USER_FIL,"r");
        
         while(1)                       /* ﾕｰｻﾞ･ ﾌｧｲﾙ ｹﾝｻｸ */
            {  if (fgets(fbuff,255,fpw) == NULL)  { userptr = -1; break; }
               if ( ! strncmp(hiscall,fbuff,8))        
                  {   substr(fbuff,18,8,lastdate);
					  substr(fbuff,27,5,lasttime);
					  substr(fbuff,39,1,exp_ch); 
					  if( *(exp_ch) == '*' ) exp_flg = 1;
                      break;     }
        
               userptr = ftell(fpw);   /* ﾌｧｲﾙ ﾎﾟｲﾝﾀ ｽｽﾒ */
            }          
        
         fclose(fpw);
        
		strcpy(fname,ST_FIL);
		sendfile();
                         /* スタートメッセージをファイルより直送信 90.11.30 */
          
         /* USED USER ﾅﾗ ｾﾞﾝｶｲ ﾛｸﾞｲﾝ ﾋｮｳｼﾞ */

         if ( userptr != -1)
		  {if ( ! exp_flg )
			{
              sprintf(fbuff,"前回のログ・イン %s %s\n\n",lastdate,lasttime);
              strcat(ssent,fbuff);
            }
			else
			{ sprintf(fbuff,"LAST=%s %s",lastdate,lasttime);
			  strcat(ssent,fbuff);
			}
		  }  
         /* ﾒｰﾙ ｹﾝｻｸ */
         if ( ! exp_flg )
          { if ( mlchk(0) )
            { sprintf(fbuff,"メールが届いています ..'MAIL,R' で読めます\n\n");
              strcat(ssent,fbuff); }
		  }
		 else
		  { if ( mlchk(0) )
            { strcat(ssent," ﾒｰﾙ到着\n"); }
			else
			{ strcat(ssent,"\n"); }
		  } 
         
         if (strlen(ssent))  rsout();

         /* who file ｺｳｼﾝ */
         fpw = fopen(whofile,"r+");
         if (fpw != NULL)
            {  while(1)
               {  if (fgets(fbuff,255,fpw) == NULL )   break;
                  substr(fbuff,0,1,pno);
                  if (  devdrv != atoi(pno) )	    continue;
		  ptr = ftell(fpw) - strlen(fbuff) + 1;
                  fseek(fpw,ptr,0);
                  fprintf(fpw,"%s %s",hiscall,nowtime);
                  break;
                }
             }
         fclose(fpw);
         /* ｶｲｼ ﾋｽﾞｹ･ｼﾞｺｸ ｶｸﾉｳ */
         sprintf(statdate,"%s",nowdate); sprintf(stattime,"%s",nowtime);
         cnttime++;

         strcpy(sigop,sysop); strcpy(nowjle,mainjle);
         strcpy(jlepmt,"MAIN ->");
         for ( i = 0; i <= 1000 ; i++) *(rtbl + i) = 0;
         
         *(pfna) = NULL; *(pastcmd) = NULL; *(nodtitle) = NULL;
         resv    = seek_flg = 0;

         /* ﾖﾐﾀﾞｼ ｷﾝｼ ﾃｰﾌﾞﾙ ｾｯﾃｲ */
		 for ( i = 0 ; i <= JUNLE_MAX ; i++ ) { stbl[i][0] = NULL; }
         fpw = fopen(SELECT_FIL,"r");
        
        if ( fpw != NULL )
		 {
			while(1)                       /* ﾖﾐﾀﾞｼ ｷﾝｼ ﾌｧｲﾙ ｹﾝｻｸ */
            {  if (fgets(fbuff,255,fpw) == NULL)  { break; }
               if ( ! strncmp(hiscall,fbuff,8))        
                  {   j = 0;
					  for ( i = 8 ; i < strlen(fbuff) ; i = i + 8)
						{ substr(fbuff,i,8,stbl[j]); j++; }
					  break;
				  }
			}
        
            fclose(fpw);
		  } 
		chgjyo("CON");
         
	return(0);
       }        

