aboutsummaryrefslogtreecommitdiff
path: root/libiberty/memchr.c
blob: cce300394375b0e28ae1ad40b3f034fdbc1a0b47 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
FUNCTION
	<<memchr>>---find character in memory

INDEX
	memchr

ANSI_SYNOPSIS
	#include <string.h>
	void *memchr(const void *<[src]>, int <[c]>, size_t <[length]>);

TRAD_SYNOPSIS
	#include <string.h>
	void *memchr(<[src]>, <[c]>, <[length]>)
	void *<[src]>;
	void *<[c]>;
	size_t <[length]>;

DESCRIPTION
	This function searches memory starting at <<*<[src]>>> for the
	character <[c]>.  The search only ends with the first
	occurrence of <[c]>, or after <[length]> characters; in
	particular, <<NULL>> does not terminate the search.

RETURNS
	If the character <[c]> is found within <[length]> characters
	of <<*<[src]>>>, a pointer to the character is returned. If
	<[c]> is not found, then <<NULL>> is returned. 	

PORTABILITY
<<memchr>>  requires no supporting OS subroutines.

QUICKREF
	memchr ansi pure

*/

#include <ansidecl.h>
#ifdef __STDC__
#include <stddef.h>
#else
#define size_t unsigned long
#endif

PTR
memchr (src_void, c, length)
     register const PTR src_void;
     int c;
     size_t length;
{
  const unsigned char *src = (const unsigned char *)src_void;
  
  while (--length >= 0)
  {
    if (*src == c)
     return (PTR)src;
    src++;
  }
  return NULL;
}