svcadm(8)을 검색하려면 섹션에서 8 을 선택하고, 맨 페이지 이름에 svcadm을 입력하고 검색을 누른다.
bsearch(3c)
Standard C Library Functions bsearch(3C)
NAME
bsearch - binary search a sorted table
bsearch_s - binary search a sorted table with additional safety checks
SYNOPSIS
#include <stdlib.h>
void *bsearch(const void *key, const void *base, size_t nel, size_t size,
int (*compar)(const void *, const void *));
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdlib.h>
void *bsearch_s(const void *key, const void *base, rsize_t nel,
rsize_t size, int (*compar)(const void *k, const void *y,
void *context), void *context);
DESCRIPTION
The bsearch() function is a binary search routine generalized from
Knuth (6.2.1) Algorithm B. It returns a pointer into a table (an array)
indicating where a datum may be found or a null pointer if the datum
cannot be found. The table must be previously sorted in increasing
order according to a comparison function pointed to by compar.
The key argument points to a datum instance to be sought in the table.
The base argument points to the element at the base of the table. The
nel argument is the number of elements in the table. The size argument
is the number of bytes in each element.
The comparison function pointed to by compar is called with two argu‐
ments that point to the key object and to an array element, in that
order. The function must return an integer less than, equal to, or
greater than 0 if the key object is considered, respectively, to be
less than, equal to, or greater than the array element.
The bsearch_s() function is part of the bounds checking interfaces
specified in the C11 standard, Annex K. It is similar to the bsearch()
function, except that it adds a third argument to the comparison func‐
tion which is the context argument passed to the bsearch_s() function.
The sole use of context by bsearch_s() is to pass it to the comparison
function. See runtime_constraint_handler(3C) and INCITS/ISO/IEC
9899:2011.
RETURN VALUES
The bsearch() and bsearch_s() functions return a pointer to a matching
member of the array, or a null pointer if no match is found. Addition‐
ally, the bsearch_s() function returns a null pointer if there is a
runtime-constraint violation. If two or more members compare equal,
which member is returned is unspecified.
ERRORS
ERANGE size argument is not a valid value
EINVAL Null pointer is passed
USAGE
The pointers to the key and the element at the base of the table should
be of type pointer-to-element.
The comparison function need not compare every byte, so arbitrary data
may be contained in the elements in addition to the values being com‐
pared.
If the number of elements in the table is less than the size reserved
for the table, nel should be the lower number.
The bsearch() function safely allows concurrent access by multiple
threads to disjoint data, such as overlapping subtrees or tables.
EXAMPLES
Example 1 Examples for searching a table containing pointers to nodes.
The example below searches a table containing pointers to nodes con‐
sisting of a string and its length. The table is ordered alphabetically
on the string in the node pointed to by each entry.
This program reads in strings and either finds the corresponding node
and prints out the string and its length, or prints an error message.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node { /* these are stored in the table */
char *string;
int length;
};
static struct node table[] = { /* table to be searched */
{ "asparagus", 10 },
{ "beans", 6 },
{ "tomato", 7 },
{ "watermelon", 11 },
};
main()
{
struct node *node_ptr, node;
/* routine to compare 2 nodes */
static int node_compare(const void *, const void *);
char str_space[20]; /* space to read string into */
node.string = str_space;
while (scanf("%20s", node.string) != EOF) {
node_ptr = bsearch( &node,
table, sizeof(table)/sizeof(struct node),
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: %20s\n", node.string);
}
}
return(0);
}
/* routine to compare two nodes based on an */
/* alphabetical ordering of the string field */
static int
node_compare(const void *node1, const void *node2) {
return (strcmp(
((const struct node *)node1)−>string,
((const struct node *)node2)−>string));
}
ATTRIBUTES
See attributes(7) for descriptions of the following attributes:
tab() box; cw(2.75i) |cw(2.75i) lw(2.75i) |lw(2.75i) ATTRIBUTE TYPEAT‐
TRIBUTE VALUE _ Interface StabilityCommitted _ MT-LevelSee below _
StandardSee standards(7).
The bsearch() function can be used safely in multithreaded applica‐
tions.
The bsearch_s() function cannot be used safely in a multithreaded
application due to the runtime constraint handler. For more informa‐
tion, see the runtime_constraint_handler(3C) man page.
SEE ALSO
hsearch(3C), lsearch(3C), qsort(3C), qsort_s(3C), tsearch(3C),
attributes(7), standards(7), runtime_constraint_handler(3C)
Oracle Solaris 11.4 11 Jun 2018 bsearch(3C)