#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#include "extern.h"

chgjul()
   {   int i,j,crflg=0,fnumb,fnoz;
       char nbord[9],fno[5];
       long ptr;

       i = atoi(cmdnum);
       
       if ( strlen(cmdopt) || i > 999 )  { paraerr(); return(0); }
       if ( ! i ) { paraneed(); return(0); }
       if ( ! strcmp(nowjle,mainjle) )
           { strcpy(ssent,"\n\n! MAIN からは移動ができません。\n\n");
             rsout();
             return(0);
           }
       if ( ! strcmp(nowjle,mailjle) )
           { strcpy(ssent,"\n\n! MAIL からは移動ができません。\n\n");
             rsout();
             return(0);
           }
       if ( ! strncmp(nowjle,"PDS",3) )
           { strcpy(ssent,"\n\n! PDS は移動ができません。\n\n");
             rsout();
             return(0);
           }
       if ( ! strncmp(nowjle,"DAT",3) )
           { strcpy(ssent,"\n\n! データライブラリは移動ができません。\n\n");
             rsout();
             return(0);
           }
       fnumb = *(rtbl + i);
       if ( fnumb == NULL ) { paraerr(); return(0); }
       
       makfname("MSG",fnumb,pfna);
       fpw = fopen(pfna,"r");
       if ( fpw == NULL ) return(0);
       if ( fgets(fbuff,255,fpw ) == NULL ) return(0);
       fclose(fpw);
       strcpy(ssent,fbuff);
       strcat(ssent,"\n移動先ボードは？ ( ｷｬﾝｾﾙ ﾊ cr ) =>");
       rsout();
       rsin(0);
       if ( *(rsent) == '\n' ) return(0);
       
       strncpy(nbord,rsent,8);
       *(nbord + 8) = NULL;
       
       for ( j = 0 ; j < 8 ; j++ )
          {  if ( islower(*(nbord + j)) ) { *(nbord + j) -= 0x20; }
             if (*(nbord + j) == '\n' )   { crflg = 1; }
             if ( crflg ) { *(nbord + j) = ' '; }
          }
       *(nbord + 8) = NULL;
       
       if ( ! strcmp(nbord,mailjle) || ! strcmp(nbord,mainjle)
         || ! strcmp(nbord,"PDS     ") 
         || ! strcmp(nbord,"DATALIB ") )
           { strcpy(ssent,"\n\n! 移動できません。\n\n");
             rsout();
             return(0);
           }
       for ( j = 0 ; j <= JUNLE_MAX ; j++ )
           { if ( jtbl[j][0] == NULL )
                { strcpy(ssent,"\n\n! ボード名にエラーがあります。\n\n");
                  rsout();
                  return(0);
                }
             if ( ! strcmp(nbord,jtbl[j])) break;
           }
       
       fpw = fopen(HDR_FIL,"r+");
       if ( fpw == NULL ) return(0);
       while(1)
          {   if ( fgets(fbuff,5,fpw) == NULL ) { fclose(fpw);
                                                  return(0); }

              substr(fbuff,0,4,fno);
              fnoz = atoi(fno);
              if ( ! isdigit(*(fbuff)) || fnoz != fnumb ) 
                 { fgets(fbuff,255,fpw); continue; }
	      ptr = ftell(fpw) + 1;
	      fseek(fpw,ptr,0);
              fputs(nbord,fpw);
              break;
          }
       fclose(fpw);
       
       fpw = fopen(pfna,"r+");
       if ( fpw == NULL ) return(0);
       fputs(nbord,fpw );
       fclose(fpw);
  
       return(0);
       }
