43 lines
956 B
C
43 lines
956 B
C
#define __STDC_WANT_LIB_EXT1__ 1
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
static const char *keywords[] = {
|
|
"auto",
|
|
"break",
|
|
"case",
|
|
"char",
|
|
/* . */
|
|
/* . */
|
|
/* . */
|
|
"while"
|
|
};
|
|
static void * context = NULL;
|
|
|
|
#define NUM_KW sizeof(keywords) / sizeof(char *)
|
|
|
|
int kw_compare( const void *p1, const void *p2, void *context )
|
|
{
|
|
const char *p1c = (const char *) p1;
|
|
const char **p2c = (const char **) p2;
|
|
return( strcmp( p1c, *p2c ) );
|
|
}
|
|
|
|
int keyword_lookup( const char *name )
|
|
{
|
|
const char **key;
|
|
key = (char const **) bsearch_s( name, keywords, NUM_KW,
|
|
sizeof( char * ), kw_compare, context );
|
|
if( key == NULL ) return( -1 );
|
|
return key - keywords;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
printf( "%d\n", keyword_lookup( "case" ) );
|
|
printf( "%d\n", keyword_lookup( "crigger" ) );
|
|
printf( "%d\n", keyword_lookup( "auto" ) );
|
|
return 0;
|
|
}
|