diff options
Diffstat (limited to 'test/bsort.c')
-rw-r--r-- | test/bsort.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/test/bsort.c b/test/bsort.c new file mode 100644 index 0000000..89d7ddb --- /dev/null +++ b/test/bsort.c @@ -0,0 +1,52 @@ +/* bsort() requires <stdlib.h> */ + +/* example taken pretty much directly from SuS */ + +/* + * Items: bsort( + * Requires: <stdlib.h> + * Standardized-By: SuS + * Not-Detected-by: gcc-4.4.3 + Linux + */ + +#ifndef __DEHEADER__ +#include <stdio.h> +#endif +#include <stdlib.h> +#include <string.h> + +#define TABSIZE 1000 + +struct node { /* these are stored in the table */ + char *string; + int length; +}; +struct node table[TABSIZE]; /* table to be searched */ + +int +node_compare(const void *node1, const void *node2) +{ + return strcoll(((const struct node *)node1)->string, + ((const struct node *)node2)->string); +} + +main(int argc, char **argv) +{ + struct node *node_ptr, node; + char str_space[20]; /* space to read string into */ + + node.string = str_space; + while (scanf("%s", node.string) != EOF) { + node_ptr = (struct node *)bsearch((void *)(&node), + (void *)table, TABSIZE, + sizeof(struct node), node_compare); + if (node_ptr != NULL) { + (void)printf("string = %20s, length = %d\n", + node_ptr->string, node_ptr->length); + } else { + (void)printf("not found: %s\n", node.string); + } + } +} + + |