summaryrefslogtreecommitdiff
path: root/libbb/strrstr.c
blob: 5a2685920f4a8c8026c171ce634a56dd745a5bb7 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* vi: set sw=4 ts=4: */
/*
 * Utility routines.
 *
 * Copyright (C) 2008 Bernhard Fischer
 *
 * Licensed under GPLv2 or later, see file License in this tarball for details.
 */

#include "libbb.h"

/*
 * The strrstr() function finds the last occurrence of the substring needle
 * in the string haystack. The terminating nul characters are not compared.
 */
char* strrstr(const char *haystack, const char *needle)
{
	char *r = NULL;

	if (!needle[0])
			return r;
	while (1) {
			char *p = strstr(haystack, needle);
			if (!p)
					return r;
			r = p;
			haystack = p + 1;
	}
}

#ifdef __DO_STRRSTR_TEST
/* Test */
int main(int argc, char **argv)
{
	int ret = 0;
	int n;
	char *tmp;

	ret |= !(n = ((tmp = strrstr("baaabaaab", "aaa")) != NULL && strcmp(tmp, "aaab") == 0));
	printf("'baaabaaab'  vs. 'aaa'       : %s\n", n ? "PASSED" : "FAILED");

	ret |= !(n = ((tmp = strrstr("baaabaaaab", "aaa")) != NULL && strcmp(tmp, "aaab") == 0));
	printf("'baaabaaaab' vs. 'aaa'       : %s\n", n ? "PASSED" : "FAILED");

	ret |= !(n = ((tmp = strrstr("baaabaab", "aaa")) != NULL && strcmp(tmp, "aaabaab") == 0));
	printf("'baaabaab'   vs. 'aaa'       : %s\n", n ? "PASSED" : "FAILED");

	ret |= !(n = (strrstr("aaa", "aaa") != NULL));
	printf("'aaa'        vs. 'aaa'       : %s\n", n ? "PASSED" : "FAILED");

	ret |= !(n = (strrstr("aaa", "a") != NULL));
	printf("'aaa'        vs. 'a'         : %s\n", n ? "PASSED" : "FAILED");

	ret |= !(n = (strrstr("aaa", "bbb") == NULL));
	printf("'aaa'        vs. 'bbb'       : %s\n", n ? "PASSED" : "FAILED");

	ret |= !(n = (strrstr("a", "aaa") == NULL));
	printf("'a'          vs. 'aaa'       : %s\n", n ? "PASSED" : "FAILED");

	ret |= !(n = ((tmp = strrstr("aaa", "")) != NULL && strcmp(tmp, "aaa") == 0));
	printf("'aaa'        vs. ''          : %s\n", n ? "FAILED" : "PASSED");

	ret |= !(n = (strrstr("", "aaa") == NULL));
	printf("''           vs. 'aaa'       : %s\n", n ? "PASSED" : "FAILED");

	ret |= !(n = ((tmp = strrstr("", "")) != NULL && strcmp(tmp, "") == 0));
	printf("''           vs. ''          : %s\n", n ? "PASSED" : "FAILED");

	/*ret |= !(n = (strrstr(NULL, NULL) == NULL));
	printf("'NULL'       vs. 'NULL'      : %s\n", n ? "PASSED" : "FAILED");
	ret |= !(n = (strrstr("", NULL) == NULL));
	printf("''           vs. 'NULL'      : %s\n", n ? "PASSED" : "FAILED");
	ret |= !(n = (strrstr(NULL, "") == NULL));
	printf("'NULL'       vs. ''          : %s\n", n ? "PASSED" : "FAILED");
	ret |= !(n = (strrstr("aaa", NULL) == NULL));
	printf("'aaa'        vs. 'NULL'      : %s\n", n ? "PASSED" : "FAILED");
	ret |= !(n = (strrstr(NULL, "aaa") == NULL));
	printf("'NULL'       vs. 'aaa'       : %s\n", n ? "PASSED" : "FAILED");*/

	return ret;
}
#endif