File: strsearch.c

package info (click to toggle)
bibutils 7.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,040 kB
  • sloc: ansic: 112,579; sh: 462; makefile: 42
file content (41 lines) | stat: -rw-r--r-- 972 bytes parent folder | download | duplicates (2)
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
/* strsearch.c
 *
 * Copyright (c) Chris Putnam 1995-2021
 *
 * Source code released under the GPL version 2
 *
 * strsearch() locates a case-independent substring
 *    e.g. a case-independent version of strstr()
 *
 * returns pointer to first occurrence of substring needle in
 * the string haystack when found, NULL if not found
 *
 * '\0' characters terminating strings are not compared
 *
 * strsearch returns haystack when needle is empty as per strstr()
 * conventions
 *
 */
#include <stdio.h>
#include <ctype.h>
#include "strsearch.h"

char *strsearch (const char *haystack, const char *needle)
{
	char *returnptr=NULL;
	unsigned long pos=0;

	if ( !(*needle) ) returnptr = (char *) haystack;

	while (*(haystack+pos) && returnptr==NULL) {
		if ( toupper((unsigned char)*(haystack+pos)) == toupper((unsigned char)*(needle+pos)) )
			pos++;
		else {
			pos = 0;
			haystack++;
		}
		if ( ! (*(needle+pos)) ) returnptr = (char *) haystack;
	}

	return returnptr;
}