source: MondoRescue/branches/3.0/mindi-busybox/libbb/strrstr.c@ 2899

Last change on this file since 2899 was 2725, checked in by Bruno Cornec, 13 years ago
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
  • Property svn:eol-style set to native
File size: 1.5 KB
Line 
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 2008 Bernhard Reutner-Fischer
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 */
9
10#ifdef __DO_STRRSTR_TEST
11#include <stdlib.h>
12#include <string.h>
13#include <stdio.h>
14#else
15#include "libbb.h"
16#endif
17
18/*
19 * The strrstr() function finds the last occurrence of the substring needle
20 * in the string haystack. The terminating nul characters are not compared.
21 */
22char* FAST_FUNC strrstr(const char *haystack, const char *needle)
23{
24 char *r = NULL;
25
26 if (!needle[0])
27 return (char*)haystack + strlen(haystack);
28 while (1) {
29 char *p = strstr(haystack, needle);
30 if (!p)
31 return r;
32 r = p;
33 haystack = p + 1;
34 }
35}
36
37#ifdef __DO_STRRSTR_TEST
38int main(int argc, char **argv)
39{
40 static const struct {
41 const char *h, *n;
42 int pos;
43 } test_array[] = {
44 /* 0123456789 */
45 { "baaabaaab", "aaa", 5 },
46 { "baaabaaaab", "aaa", 6 },
47 { "baaabaab", "aaa", 1 },
48 { "aaa", "aaa", 0 },
49 { "aaa", "a", 2 },
50 { "aaa", "bbb", -1 },
51 { "a", "aaa", -1 },
52 { "aaa", "", 3 },
53 { "", "aaa", -1 },
54 { "", "", 0 },
55 };
56
57 int i;
58
59 i = 0;
60 while (i < sizeof(test_array) / sizeof(test_array[0])) {
61 const char *r = strrstr(test_array[i].h, test_array[i].n);
62 printf("'%s' vs. '%s': '%s' - ", test_array[i].h, test_array[i].n, r);
63 if (r == NULL)
64 r = test_array[i].h - 1;
65 printf("%s\n", r == test_array[i].h + test_array[i].pos ? "PASSED" : "FAILED");
66 i++;
67 }
68
69 return 0;
70}
71#endif
Note: See TracBrowser for help on using the repository browser.