blob: a803dd983f2fa82eb8a1490adbdc98117816f013 [file] [log] [blame]
Bernhard Reutner-Fischer17282292008-05-28 14:19:27 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00005 * Copyright (C) 2008 Bernhard Reutner-Fischer
Bernhard Reutner-Fischer17282292008-05-28 14:19:27 +00006 *
7 * Licensed under GPLv2 or later, see file License in this tarball for details.
8 */
9
Denis Vlasenkod5736c52008-06-18 19:49:46 +000010#ifdef __DO_STRRSTR_TEST
11#include <stdlib.h>
12#include <string.h>
13#include <stdio.h>
14#else
Bernhard Reutner-Fischer619b87d2008-06-17 12:45:39 +000015#include "libbb.h"
Denis Vlasenkod5736c52008-06-18 19:49:46 +000016#endif
Bernhard Reutner-Fischer17282292008-05-28 14:19:27 +000017
Bernhard Reutner-Fischer13436ea2008-06-17 12:11:34 +000018/*
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 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000022char* FAST_FUNC strrstr(const char *haystack, const char *needle)
Bernhard Reutner-Fischer17282292008-05-28 14:19:27 +000023{
Bernhard Reutner-Fischer13436ea2008-06-17 12:11:34 +000024 char *r = NULL;
Denis Vlasenkod5736c52008-06-18 19:49:46 +000025
26 if (!needle[0])
Denis Vlasenko1363f0d2008-06-18 20:01:12 +000027 return (char*)haystack + strlen(haystack);
Denis Vlasenkod5736c52008-06-18 19:49:46 +000028 while (1) {
Bernhard Reutner-Fischer4954d472008-06-18 08:32:25 +000029 char *p = strstr(haystack, needle);
Denis Vlasenkod5736c52008-06-18 19:49:46 +000030 if (!p)
31 return r;
32 r = p;
33 haystack = p + 1;
34 }
Bernhard Reutner-Fischer17282292008-05-28 14:19:27 +000035}
36
Bernhard Reutner-Fischer13436ea2008-06-17 12:11:34 +000037#ifdef __DO_STRRSTR_TEST
Bernhard Reutner-Fischer13436ea2008-06-17 12:11:34 +000038int main(int argc, char **argv)
39{
Denis Vlasenkod5736c52008-06-18 19:49:46 +000040 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 };
Bernhard Reutner-Fischer13436ea2008-06-17 12:11:34 +000056
Denis Vlasenkod5736c52008-06-18 19:49:46 +000057 int i;
Bernhard Reutner-Fischer13436ea2008-06-17 12:11:34 +000058
Denis Vlasenkod5736c52008-06-18 19:49:46 +000059 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 }
Bernhard Reutner-Fischer13436ea2008-06-17 12:11:34 +000068
Denis Vlasenkod5736c52008-06-18 19:49:46 +000069 return 0;
Bernhard Reutner-Fischer13436ea2008-06-17 12:11:34 +000070}
71#endif