www.sas.com > Service and Support > Technical Support
 
Technical Support SAS - The power to know(tm)
  TS Home | Intro to Services | News and Info | Contact TS | Site Map | FAQ | Feedback


/*---------------------------------------------------------------------+
|                Copyright (c) 1995, SAS Institute Inc.                |
|                  Unpublished - All Rights Reserved                   |
|                      S A S / C   S A M P L E                         |
|                                                                      |
|         NAME: BSDSTR                                                 |
|     LANGUAGE: C                                                      |
|      PURPOSE: BSD Compatability Routines                             |
|        NOTES: BSDSTR is implemented as a full-function C MAIN        |
|               routine. To implement BSDSTR as sub-functions remove   |
|               the C MAIN function, compile BSDSTR and link with your |
|               application.                                           |
|   MVS -                                                              |
|      COMPILE, LINK, EXECUTE: SUBMIT prefix.SAMPLE.AUX(LC370CLG)      |
|        NOTES: Refer to prefix.MACLIBC(STRINGS) where "prefix" is the |
|               SAS/C production installation prefix.                  |
|   TSO -                                                              |
|      COMPILE: LC370 CLIST                                            |
|         LINK: CLK370 CLIST                                           |
|      EXECUTE: CALL 'your.load.library(BSDSTR)'                       |
|        NOTES: Refer to prefix.MACLIBC(STRINGS) where "prefix" is the |
|               SAS/C production installation prefix.                  |
|   CMS -                                                              |
|      COMPILE: LC370 BSDSTR                                           |
|         LINK: CLINK BSDSTR (GENMOD                                   |
|      EXECUTE: BSDSTR                                                 |
|        NOTES: Refer to LC370 MACLIB (STRINGS) for additional         |
|               information regarding BSDSTR.                          |
|   MISC NOTES: Declarations for these functions are contained in      |
|               <strings.h>. To implement these functions (other than  |
|               ffs ) via SAS/C builtin functions, uncomment the       |
|               #define _BSD_MACROS statement below, or specify        |
|               DEFINE(_BSD_MACROS) at compile time.                   |
|   MISC NOTES: There are several "#undef" statements below. This      |
|               ensures a routine is defined as a function and not a   |
|               macro.                                                 |
+---------------------------------------------------------------------*/

#include <stdlib.h>
#include <stddef.h>

/*---------------------------------------------------------------------+
| NOTE: Uncomment the #define _BSD_MACROS statement below to inline    |
|       the BSDSTR functions.                                          |
+---------------------------------------------------------------------*/
/* #define _BSD_MACROS   */

#include <string.h>
#include <strings.h>

#define BUF_SIZE 20

main()
{

char *msg = "abcd1234abcd";   /* string literal value                 */
int rc;                       /* return code for function calls       */
char *index_rc;               /* return code for function calls       */
/*---------------------------------------------------------------------+
| declare char pointers and malloc storage for each array.             |
+---------------------------------------------------------------------*/
char *array_1, *array_2;
array_1 = (char *) malloc(BUF_SIZE);
array_2 = (char *) malloc(BUF_SIZE);

/*---------------------------------------------------------------------+
| initialize each array, call bzero which does a memset().             |
+---------------------------------------------------------------------*/
bzero(array_1,BUF_SIZE);
bzero(array_2,BUF_SIZE);

/*---------------------------------------------------------------------+
| call bcopy without regard to contents/length of "source".            |
+---------------------------------------------------------------------*/
bcopy(msg,array_1,BUF_SIZE);

/*---------------------------------------------------------------------+
| call bcmp, at this point array_1 and array_2 should NOT be equal.    |
+---------------------------------------------------------------------*/
rc = bcmp(array_1,array_2,BUF_SIZE);
if (rc == 0)
   printf("array_1 equals array_2\n");
else
   printf("array_1 does not equal array_2\n");

/*---------------------------------------------------------------------+
| call bcopy without regard to contents/length of "source".            |
+---------------------------------------------------------------------*/
bcopy(msg,array_2,BUF_SIZE);

/*---------------------------------------------------------------------+
| call bcmp, at this point array_1 and array_2 MAY be equal.           |
+---------------------------------------------------------------------*/
rc = bcmp(array_1,array_2,BUF_SIZE);
if (rc == 0)
   printf("array_1 equals array_2\n");
else
   printf("array_1 does not equal array_2\n");

/*---------------------------------------------------------------------+
| call index and search for a 'b' in array_1, then print what was      |
| found.                                                               |
+---------------------------------------------------------------------*/
index_rc = index(array_1,'b');
if (index_rc == NULL)
   printf("index() did NOT find match\n");
else
   printf("return from index() is: %c\n",*index_rc);

/*---------------------------------------------------------------------+
| call rindex and search for a 'b' in array_1, then print what was     |
| found.                                                               |
+---------------------------------------------------------------------*/
index_rc = rindex(array_1,'b');
if (index_rc == NULL)
   printf("rindex() did NOT find match\n");
else
   printf("return from rindex() is: %c\n",*index_rc);

/*---------------------------------------------------------------------+
| call ffs with a int value of 8, which "should" return bit 4          |
+---------------------------------------------------------------------*/
rc = 8;
rc = ffs(rc);
printf("return from ffs() is: %d\n",rc);

} /* main()                                                           */

/*---------------------------------------------------------------------+
| bcopy  -  Copy "n" bytes from "source" to "target", and allow for    |
|           overlap.                                                   |
+---------------------------------------------------------------------*/
#undef bcopy
void bcopy(const void *source, void *target, int n)
{
   (void) memmove(target, source, (size_t)n);
   return;
}

/*---------------------------------------------------------------------+
| bzero  -  Zero "n" bytes of "target"                                 |
+---------------------------------------------------------------------*/
#undef bzero
void bzero(void *target, int n)
{
   (void) memset(target, '\0', (size_t)n);
   return;
}

/*---------------------------------------------------------------------+
| bcmp   -  Compare "n" bytes of "b1" and "b2"                         |
+---------------------------------------------------------------------*/
#undef bcmp
int bcmp(const void *b1, const void *b2, int n)
{
   return memcmp(b1, b2, (size_t)n);
}

/*---------------------------------------------------------------------+
| index  -  find first instance of a character "c" in a string "s".    |
+---------------------------------------------------------------------*/
#undef index
char *index(const char *s, int c)
{
   return strchr(s, c);
}

/*---------------------------------------------------------------------+
| rindex -  find last instance of a character "c" in a string "s".     |
+---------------------------------------------------------------------*/
#undef rindex
char *rindex(const char *s, int c)
{
   return strrchr(s,c);
}


/*---------------------------------------------------------------------+
| ffs - find first bit in an integer. Bits are numbered 1 (lowest order|
|    in value) through 32. A zero is returned if the integer has no    |
|    bits set (i.e. its value is zero).                                |
+---------------------------------------------------------------------*/
#undef ffs
int ffs(int i)
{
   int n;
   if (i==0) return 0;
   for(n=1; ;i = (unsigned)i>>1u, n++)
      if ((unsigned)i & 0x1) return n;
}

Copyright (c) 2000 SAS Institute Inc. All Rights Reserved.
Terms of Use & Legal Information | Privacy Statement