summaryrefslogtreecommitdiff
path: root/src/clib/bsearch.c
blob: 3e55009a6702c426f61e7325579e5a730a888bb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2010, 2012 Oracle and/or its affiliates.  All rights reserved.
 *
 * $Id$
 */

#include "db_config.h"

#include "db_int.h"

/*
 * bsearch --
 *
 * PUBLIC: #ifndef HAVE_BSEARCH
 * PUBLIC: void *bsearch __P((const void *, const void *, size_t,
 * PUBLIC:	size_t, int (*)(const void *, const void *)));
 * PUBLIC: #endif
 */

void *bsearch(key, base, nmemb, size, cmp)
	const void *key;
	const void *base;
	size_t nmemb;
	size_t size;
	int (*cmp) __P((const void *, const void *));
{
	size_t i;

	/* not doing a binary search, but searching linearly */
	for (i=0; i < nmemb; i++) {
		if ((*cmp)(key, (const void *)((char *)base + i * size)) == 0)
			return ((void *)((char *)base + i * size));
	}

	return (NULL);
}