/* Assorted pieces needed for VMS
   Taken from all over the place,
   credits to the original authors hopefully intact!
*/

# if __VMS_VER < 70000000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <types.h>
char *strdup(char *str)
{
        int len;
        char *temp;

        if (str == NULL) return(NULL);
        len = strlen(str);

        temp = (char *) malloc(sizeof(char) * len + 1);

        strcpy(temp, str);
        return(temp);
}
/*----------------------------------------------------------------------------*/
/* The original source for strcasecmp comes from the create.c file in the     */
/* xpm-lib distribution, Copyright 1989-95 GROUPE BULL, and was developed by  */
/* Arnaud Le Hors (lehors@sophia.inria.fr). Please look at                    */
/* ftp://koala.inria.fr/pub/xpm for a full statement of rights.               */
/*                                                                            */
/* strncasecmp was made up by me and if you have problems with it please      */
/* don't bother Arnaud.                                                       */
/* 950718 Martin P.J. Zinser (M.Zinser@gsi.de or eurmpz@eur.sas.com           */
/*----------------------------------------------------------------------------*/

#include <ctype.h>

int strcasecmp(register char *s1, register char *s2)
{
  register int c1, c2;
  
  while (*s1 && *s2){
      c1 = tolower(*s1);
      c2 = tolower(*s2);
      if (c1 != c2)
          return (c1-c2);
      s1++;
      s2++;
     }
  return (int) (*s1 - *s2);
} 


int strncasecmp(register char *s1, register char *s2, register int n)
{
  register int c1, c2, i;
  i = 1;
 
  
  while (*s1 && *s2 && i <= n){
      c1 = tolower(*s1);
      c2 = tolower(*s2);
      if (c1 != c2)
          return (c1-c2);
      s1++;
      s2++;
      i++;
     }
  if ( i = n )
      return 0;
  else
      return (int) (*s1 - *s2);
} 

/* purloined from source code for Xanim, from Mark Podlipec
   not clear if this was the original of this code

   mangled to kill the timezone pieces */
/*      
 *      Provide the UNIX gettimeofday() function for VMS C.
 *      The timezone is currently unsupported.
 */

#include <time.h>

int sys$gettim();
int lib$subx();
int lib$ediv();

int gettimeofday( struct timeval *tp, int * tzp)
{
   long curr_time[2];   /* Eight byte VAX time variable */
   long jan_01_1970[2] = { 0x4BEB4000,0x7C9567} ;
   long diff[2];
   long result;
   long vax_sec_conv = 10000000;
 
   result = sys$gettim( &curr_time );
 
   result = lib$subx( &curr_time, &jan_01_1970, &diff);
 
   if ( tp != 0) {
       result = lib$ediv( &vax_sec_conv, &diff,
                          &(tp->tv_sec), &(tp->tv_usec) );
       tp->tv_usec = tp->tv_usec / 10;  /* convert 1.e-7 to 1.e-6 */
   }
   return ( 0);
}

#endif /* __VMS_VER < 70000000 */
