160472Sbostic /*-
2*63598Sbostic * Copyright (c) 1993
3*63598Sbostic * The Regents of the University of California. All rights reserved.
460472Sbostic *
560472Sbostic * This code is derived from software contributed to Berkeley by
660472Sbostic * Paul Borman at Krystal Technologies.
760472Sbostic *
860472Sbostic * %sccs.include.redist.c%
960472Sbostic */
1060472Sbostic
1160472Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*63598Sbostic static char sccsid[] = "@(#)mbrune.c 8.1 (Berkeley) 06/27/93";
1360472Sbostic #endif /* LIBC_SCCS and not lint */
1460472Sbostic
1560472Sbostic #include <limits.h>
1660472Sbostic #include <rune.h>
1760472Sbostic #include <stddef.h>
1860472Sbostic #include <string.h>
1960472Sbostic
2060472Sbostic char *
mbrune(string,c)2160472Sbostic mbrune(string, c)
2260472Sbostic const char *string;
2360472Sbostic rune_t c;
2460472Sbostic {
2560472Sbostic char const *result;
2660472Sbostic rune_t r;
2760472Sbostic
2860472Sbostic while ((r = sgetrune(string, MB_LEN_MAX, &result))) {
2960472Sbostic if (r == c)
3060472Sbostic return ((char *)string);
3160472Sbostic string = result == string ? string + 1 : result;
3260472Sbostic }
3360472Sbostic
3460472Sbostic return (c == *string ? (char *)string : NULL);
3560472Sbostic }
3660472Sbostic
3760472Sbostic char *
mbrrune(string,c)3860472Sbostic mbrrune(string, c)
3960472Sbostic const char *string;
4060472Sbostic rune_t c;
4160472Sbostic {
4260472Sbostic const char *last = 0;
4360472Sbostic char const *result;
4460472Sbostic rune_t r;
4560472Sbostic
4660472Sbostic while ((r = sgetrune(string, MB_LEN_MAX, &result))) {
4760472Sbostic if (r == c)
4860472Sbostic last = string;
4960472Sbostic string = result == string ? string + 1 : result;
5060472Sbostic }
5160472Sbostic return (c == *string ? (char *)string : (char *)last);
5260472Sbostic }
5360472Sbostic
5460472Sbostic char *
mbmb(string,pattern)5560472Sbostic mbmb(string, pattern)
5660472Sbostic const char *string;
5760472Sbostic char *pattern;
5860472Sbostic {
5960472Sbostic rune_t first, r;
6060472Sbostic size_t plen, slen;
6160472Sbostic char const *result;
6260472Sbostic
6360472Sbostic plen = strlen(pattern);
6460472Sbostic slen = strlen(string);
6560472Sbostic if (plen > slen)
6660472Sbostic return (0);
6760472Sbostic
6860472Sbostic first = sgetrune(pattern, plen, &result);
6960472Sbostic if (result == string)
7060472Sbostic return (0);
7160472Sbostic
7260472Sbostic while (slen >= plen && (r = sgetrune(string, slen, &result))) {
7360472Sbostic if (r == first) {
7460472Sbostic if (strncmp(string, pattern, slen) == 0)
7560472Sbostic return ((char *) string);
7660472Sbostic }
7760472Sbostic if (result == string) {
7860472Sbostic --slen;
7960472Sbostic ++string;
8060472Sbostic } else {
8160472Sbostic slen -= result - string;
8260472Sbostic string = result;
8360472Sbostic }
8460472Sbostic }
8560472Sbostic return (0);
8660472Sbostic }
87