Implement window size passing between real tty and virtual console.
[dragonfly/vkernel-mp.git] / contrib / tar / lib / strstr.c
blobc41e90349ff7b96be0cbcfdf2194649f551048b3
1 /* Copyright (C) 1994, 1999 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 * My personal strstr() implementation that beats most other algorithms.
20 * Until someone tells me otherwise, I assume that this is the
21 * fastest implementation of strstr() in C.
22 * I deliberately chose not to comment it. You should have at least
23 * as much fun trying to understand it, as I had to write it :-).
25 * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */
27 #if HAVE_CONFIG_H
28 # include <config.h>
29 #endif
31 #if defined _LIBC || defined HAVE_STRING_H
32 # include <string.h>
33 #endif
34 #include <sys/types.h>
36 typedef unsigned chartype;
38 #undef strstr
40 char *
41 strstr (const char *phaystack, const char *pneedle)
43 register const unsigned char *haystack, *needle;
44 register chartype b, c;
46 haystack = (const unsigned char *) phaystack;
47 needle = (const unsigned char *) pneedle;
49 b = *needle;
50 if (b != '\0')
52 haystack--; /* possible ANSI violation */
55 c = *++haystack;
56 if (c == '\0')
57 goto ret0;
59 while (c != b);
61 c = *++needle;
62 if (c == '\0')
63 goto foundneedle;
64 ++needle;
65 goto jin;
67 for (;;)
69 register chartype a;
70 register const unsigned char *rhaystack, *rneedle;
74 a = *++haystack;
75 if (a == '\0')
76 goto ret0;
77 if (a == b)
78 break;
79 a = *++haystack;
80 if (a == '\0')
81 goto ret0;
82 shloop:; }
83 while (a != b);
85 jin: a = *++haystack;
86 if (a == '\0')
87 goto ret0;
89 if (a != c)
90 goto shloop;
92 rhaystack = haystack-- + 1;
93 rneedle = needle;
94 a = *rneedle;
96 if (*rhaystack == a)
99 if (a == '\0')
100 goto foundneedle;
101 ++rhaystack;
102 a = *++needle;
103 if (*rhaystack != a)
104 break;
105 if (a == '\0')
106 goto foundneedle;
107 ++rhaystack;
108 a = *++needle;
110 while (*rhaystack == a);
112 needle = rneedle; /* took the register-poor approach */
114 if (a == '\0')
115 break;
118 foundneedle:
119 return (char*) haystack;
120 ret0:
121 return 0;