/* Some replacement routines for the amiga */
#include <stdio.h>
#include <time.h>
/* for Execute call at the end */
#include <libraries/dos.h>
#include "mb.h"
extern char l_date[7], l_time[5],tmpstr[];
#define true 1
#define false 0
/*
 *  Wait a bit.
 */
/* Actually this should not be done like this */
/* it ought to use the timer device and wait for
   it to tick
*/
await(sec)
int sec;
{
   Delay((long)(sec*50));
}
/*
 * Get the audio device for the beep and force some commands at
 * the TNC
 */

ioinit()
{
   initbeep();
   inittnc("tnc.on");
}

dosinit()
{
}
/* This is supposed to turn off interrupts but since it is the last
   thing called by done() in mb.c I use it to do any cleanup for the
   amiga
*/
iooff()
{
   inittnc("tnc.off");
   donebeep();
}
/* What does this do? */
breakport()
{
/*
  inreg.x.dx = port->idn;
  inreg.h.ah = 7;
  int86( 0x14, &inreg, &outreg);
*/
}
/* Checking for dcd ... force a return of TRUE? */
isdcd()
{
   return (true);

/*
   switch (port->dev)
   {
   case p_tnc:
   default: return (true);
   }
*/

}
/*
 *  Allocate the current port to the pipe port.
 *  Put pipe port for local end of pipe into DL.
 *  Put pipe port we want to talk to into DH.
 *  Equivalent to "connect".
 */
/* ON the amiga anything to do with pipes  will be a no-no  */
allocport()
{
}

freeport()
{
}

/*
 *  Block the local pipe port.
 *  Equivalent to "conok off".
 */

blockport()
{
}

/*
 *  Deallocate the local pipe port.
 *  Equivalent to "disconnect".
 */

dealport()
{
}

/*
 *  Check connect status of the local pipe port.
 */

stateport()
{
}

/*
 *  Fill the globals "l_date", "l_time", and "dt" with
 *  the current date and time by reading the system clock.
 *  l_time is HHMM, l_date is YYMMDD.
 *  dt has the format of a directory item date/time.
 */
int cursecs;
curtim()
{
   struct tm *ct;
   long ltime;

   time(&ltime);
   ct = localtime(&ltime);
   cursecs = ct->tm_sec;
   sprintf(l_time, "%02d%02d", ct->tm_hour, ct->tm_min);
   sprintf(l_date, "%02d%02d%02d", ct->tm_year, ct->tm_mon + 1, ct->tm_mday);
   /* month returned by localtime is 0-11 */
}
long gettime()
{
   long cur[3],now;
   DateStamp(&cur[0]);
   now = cur[0]*24*60*60;
   now += cur[1]*60;
   now += cur[2]/50;
   return(now);
}
/*
 *  Check a timer.
 */

chktmr(l)
long l;
{
   extern long gettime();
   long cur[3];

   if (gettime() > l) return (false);
   return (true);
}

/*
 *  Set a timer.
 */

settmr(l, i)
long *l;
int i;
{
   extern  long gettime();
   *l = gettime() + (long)i;
}

send_title()
{
   outstr("[CBBS-V6.1.AMIGA-H$]\n");
}
/* dummy routines for the new stuff introduced into mbibm in 5.1 */

switchw()
{
}
begin_lock()
{
}
end_lock()
{
}
setbusy()
{
}
clrbusy()
{
}
char port_flag,cmnd_flag;
putp_flag(c)
char c;
{
   port_flag = c;
}
getp_flag()
{
   return(port_flag);
}
putc_flag(c)
char c;
{
   cmnd_flag = c;
}
getc_flag()
{
   return(cmnd_flag);
}
#ifdef NEWPASS
int numpass,pwdint[MAXPASS];
pass()
{
   char tmp[MAXPASS];

   register char *p;
   register int i,j;
   int sum;
   /* don't do the thing at all if there's no integers to use
      or there was an error in reading the list in mbinit
   */
   if(numpass == 0)return false;
   sum = 0;
   /* clear tmp array */
   /* The tmp array is used to ensure that four distinct integers are
      produced.
   */
   for(p = &tmp[0];p<&tmp[MAXPASS];p++)*p = 0;
   /* get four distinct random integers to generate the password */
   for(i = 0;i < 4;i++) {
      do {
         j = ((rand() & 0x7fff) >> 4) % numpass;
         /* make sure rand produces four distinct numbers otherwise
            it weakens the password scheme
         */
         if(tmp[j])continue;
         tmp[j] = 1;
         sum += pwdint[j];
         sprintf(tmpstr, " %2d", j+1);
         outstr(tmpstr);
         break;
      }while(1);
   }
   outstr("\n");
   getdat();
   if(atoi(port->line) == sum)return true;
   return false;
}
/*
   New and more secure password function called from mbinit to set up
   the password array.
*/
initpwd()
{
   register int i,j;
   register char *p;
   char *rdstr();
   p = rdstr();
   for(i=0;i<MAXPASS;i++) {
      if((pwdint[i] = atoi(p)) <= 0) {
         return(0);
      }
      while(*p && (*p >= '0') && (*p <= '9'))p++;
      if((*p == 0) || (*p == '\n') || (*p == '\r')){
         if(i+1 < MINPASS)return;
         return(i+1);
      }
      while(*p == ' ')p++;
      /* a backslash at the end of a line indicates more integers
         on the next line.
      */
      if(*p == '\\') {
         p = rdstr();
         continue;
      }
   }
   return(i+1);
}
#endif
donefunc()
{
   done(0);
}
titles()
{
   window_title(mfhs->next_msg-1,mfhs->count);
}
funlink(file)
char *file;
{
   return(DeleteFile(file));
}
replace_bbs(up,bp)
char *up,*bp;
{
   int user_num;
   USER ubuf;
   /* the extra tests here try to avoid the expense of the disk I/O
      required if we must look in the user file
   */
   if(!iscall(up))return;
   if((user_num = finduser(up)) == 0)return;
   read_rec(ufl,user_num,&ubuf);
   /* Is there a call in the home_bbs ? */
   if(ubuf.home_bbs[0] == ' ')return;
   strncpy(bp,&ubuf.home_bbs[0],ln_call);
}
system(cmd)
char *cmd;
{
   /* Using Open("*",...) to get a handle on the window does not work.
      It gives a handle to the CLI that started up CBBS ... NOT the
      window that CBBS is in ... so the output goes to the wrong window
      anyway
   */
   long Execute(),Open();
   if(Execute(cmd,0L,0L)) {
      return(0);
   }
   return(1);
}
/* copy chunk of memory .. only used by the sort routine in mbutil */
memcpy(to,from,length)
char *to,*from;
int length;
{
   register char *p,*q;
   register short l;
   p = to;
   q = from;
   for(l=0;l<length;l++) {
      *p++ = *q++;
   }
}

/* convert any lower case in str to upper case */
strupr(str)
char *str;
{
   register char *s;
   s = str;
   while(*s) {
      if((*s >= 'a') && (*s <= 'z'))*s &= ~040;
      s++;
   }
}
/* compare the first len chars in s1 and s2 */
strnicmp(s1,s2,len)
char *s1,*s2;
short len;
{
   register char *p,*q;
   register short l;
   p = s1;
   q = s2;
   for(l=0;l<len;l++) {
      if(*p || *q)return(0);
      if(*p != *q)return(0);
      p++;
      q++;
   }
   return(1);
}





/* copy the string to a temporary area and return pointer to it */
char *strdup(str)
char *str;
{
   register char *p,*q,*r;
   /* get length of string */
   p = str;
   while(*p)p++;
   p++;
   r = q = malloc(p - str);
   p = str;
   while(*p)*q++ = *p++;
   *q++ = 0;
   return(r);
}
