/*
*
* @(#)64        1.4  src/examples/common/os2/brtns.c, examples.src, os2dce21.dss, 960602a.1  11/17/95  09:41:24
*
* COMPONENT_NAME:  examples.src
*
* FUNCTIONS:
*
* ORIGINS: 27
*
* OBJECT CODE ONLY SOURCE MATERIALS
*
* (C) COPYRIGHT International Business Machines Corp. 1992, 1994
*  All Rights Reserved
*  Licensed Materials - Property of IBM
*
*  US Government Users Restricted Rights - Use, duplication or
*  disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*/
 /********************************************************************/
 /*                                                                  */
 /* PROGRAM NAME: btns.c                                             */
 /*                                                                  */
 /* DESCRIPTIVE NAME: bcopy, bcmp, bzero and ffs routines            */
 /*                                                                  */
 /*    MODULE STRUCTURE:                                             */
 /*                                                                  */
 /*       bcopy(from, to, len)                                       */
 /*       bcmp(one_area, another, len)                               */
 /*       bzero(starting, len)                                       */
 /*       ffs(word)                                                  */
 /*                                                                  */
 /*                                                                  */
 /*   IMPLEMENTATION SPECIFICS:  bcpy returns the index of the       */
 /*   first non-matching character.  The AIX subroutines reference   */
 /*   just specifies that this be a non-negative value.  In some     */
 /*   implementations, though, the 'real' bcpy does return the       */
 /*   index, so it's  done it here.                                  */
 /*                                                                  */
 /*                                                                  */
 /********************************************************************/

#include <ctype.h>

/* -------------------------------------------------------------------------- */
/* bcopy()                                                                    */
/* bcmp()                                                                     */
/* bzero()                                                                    */
/* ffs()                                                                      */
/*                                                                            */
/* Syntax:                                                                    */
/*                                                                            */
/*     void bcopy(char *Source, char *Destination, int Length);               */
/*                                                                            */
/*     int  bcmp(char *String1, char *String2, int Length);                   */
/*                                                                            */
/*     void bzero(char *String, int length);                                  */
/*                                                                            */
/*     int ffs(int (Index);                                                   */
/*                                                                            */
/*                                                                            */
/*                                                                            */
/* Description:                                                               */
/*                                                                            */
/*     The bcopy, bcmp and bzero subroutines operate on varible length        */
/*     strings of bytes.  They do not check for null bytes as do the          */
/*     string routines.                                                       */
/*                                                                            */
/*     The bcopy subroutine copies the values of the Length parameter in      */
/*     bytes from the string the the Source parameter to the string in the    */
/*     Destination parameter.                                                 */
/*                                                                            */
/*     The bcmp subroutine compares byte string in the String 1 parameter     */
/*     against byte string of the String2 parameter, returning a zero         */
/*     value if the two strings are identical and a nonzero value             */
/*     otherwise.  Both strings are assumed to be Length bytes long.          */
/*                                                                            */
/*     The bzero subroutine zeroes out the string in the String parameter     */
/*     for the value of the Length parameter in bytes.                        */
/*                                                                            */
/*     The ffs subroutine finds the first bit set in the Index parameter      */
/*     passed to it and returns the index of that bit.  Bits are numbered     */
/*     starting at 1.  A return value of 0 indicates that the value passed    */
/*     is 0.                                                                  */
/*                                                                            */
/* Warning:  The bcopy subroutine takes parameters backwards from the strcpy  */
/* subroutines.                                                               */
/*                                                                            */
/* -------------------------------------------------------------------------- */
/* ------------- */
/*     bcopy     */
/* ------------- */
void bcopy(char *Src, char *Dest, int Len)
{
   char *p;
   int i;

   for (i=0, p=Dest; i < Len; *p++ = Src[i++]);
}

/* ------------- */
/*     bcmp      */
/* ------------- */
int  bcmp(char *Str1, char *Str2, int Len)
{
   char *p;
   int i;

   for (i = 0, p = Str1; i < Len; p++, i++)
      if (*p != Str2[i])
        return(i+1);
   return(0);
}

/* ------------- */
/*     bzero     */
/* ------------- */
void bzero(char *Str, int Len)
{
   int i;

   for (i = 0; i < Len; Str[i++] = '\0');
}

/* ------------- */
/*      ffs      */
/* ------------- */
int ffs(int Index)               /* presuming an 8-bit byte */
{
   int bit, bog, lastbit;

   lastbit = sizeof(int) * 8 + 1;
   for (bit = 1, bog = 0x1; bit < lastbit; bit++)
      {
         if (bog & Index)
           return(bit);
         bog <<= 1;
      }
   return(0);
}

/* ------------- */
/*      end      */
/* ------------- */


int strcasecmp(char *s1, char *s2)
{
  /* assuming valid pointers */
  while (*s1 && *s2 && (tolower(*s1) == tolower(*s2)))
    {
      s1++;
      s2++;
    }

  /* strings are equal iff both pointers reached terminator */
  return (!*s1 && !*s2) ? 0 : 1;
}

