diff options
| author | Roger A. Faulkner <Roger.Faulkner@Oracle.COM> | 2010-08-12 14:55:22 -0700 |
|---|---|---|
| committer | Roger A. Faulkner <Roger.Faulkner@Oracle.COM> | 2010-08-12 14:55:22 -0700 |
| commit | 23a1ccea6aac035f084a7a4cdc968687d1b02daf (patch) | |
| tree | 6ed5e310ce6dd96f997b0c0f9735805d513d898a /usr/src/lib/libc/port/gen/strstr.c | |
| parent | 29c3196fe2acc65721d8b9b5ea708d3a87facde0 (diff) | |
| download | illumos-joyent-23a1ccea6aac035f084a7a4cdc968687d1b02daf.tar.gz | |
PSARC 2010/299 GNU/Linux/BSD compatibility functions
6960818 add get_nprocs(), getline(), strdupa(), strndup() to libc
6901783 strndup would be nice
6824404 libc should provide ffsl() & ffsll()
6793969 RFE: Add|stpcpy|to libc
6735446 Want a __progname symbol for BSD-style source compatibility
6421095 Solaris should provide strcasestr
6275498 Provide string compare functions wcscasecmp,wcsncasecmp in solaris like linux
--HG--
rename : usr/src/lib/libc/port/gen/strcasecmp.c => usr/src/lib/libc/port/gen/ascii_strcasecmp.c
rename : usr/src/lib/libc/port/gen/strncasecmp.c => usr/src/lib/libc/port/gen/ascii_strncasecmp.c
rename : usr/src/lib/libc/sparc/gen/strcasecmp.s => usr/src/lib/libc/sparc/gen/ascii_strcasecmp.s
rename : usr/src/lib/libc/sparcv9/gen/strcasecmp.s => usr/src/lib/libc/sparcv9/gen/ascii_strcasecmp.s
Diffstat (limited to 'usr/src/lib/libc/port/gen/strstr.c')
| -rw-r--r-- | usr/src/lib/libc/port/gen/strstr.c | 61 |
1 files changed, 50 insertions, 11 deletions
diff --git a/usr/src/lib/libc/port/gen/strstr.c b/usr/src/lib/libc/port/gen/strstr.c index a8d8d655af..1f87034ada 100644 --- a/usr/src/lib/libc/port/gen/strstr.c +++ b/usr/src/lib/libc/port/gen/strstr.c @@ -20,15 +20,12 @@ */ /* - * Copyright 2008 Sun Microsystems, Inc. All rights reserved. - * Use is subject to license terms. + * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved. */ /* Copyright (c) 1988 AT&T */ /* All Rights Reserved */ -#pragma ident "%Z%%M% %I% %E% SMI" - #include "lint.h" #include <string.h> #include <stddef.h> @@ -37,9 +34,9 @@ /* * strstr() locates the first occurrence in the string as1 of * the sequence of characters (excluding the terminating null - * character) in the string as2. strstr() returns a pointer + * character) in the string as2. strstr() returns a pointer * to the located string, or a null pointer if the string is - * not found. If as2 is "", the function returns as1. + * not found. If as2 is empty, the function returns as1. */ char * @@ -54,19 +51,61 @@ strstr(const char *as1, const char *as2) if (s2 == NULL || *s2 == '\0') return ((char *)s1); + c = *s2; + while (*s1 != '\0') { + if (c == *s1++) { + tptr = s1; + while ((c = *++s2) == *s1++ && c != '\0') + continue; + if (c == '\0') + return ((char *)tptr - 1); + s1 = tptr; + s2 = as2; + c = *s2; + } + } - while (*s1) - if (*s1++ == c) { + return (NULL); +} + +/* + * strnstr() locates the first occurrence in the string as1 of + * the sequence of characters (excluding the terminating null + * character) in the string as2, where not more than n characters + * from the string as1 are searched. strnstr() returns a pointer + * to the located string, or a null pointer if the string is + * not found. If as2 is empty, the function returns as1. + */ + +char * +strnstr(const char *as1, const char *as2, size_t n) +{ + const char *s1, *s2; + const char *tptr; + size_t k; + char c; + + s1 = as1; + s2 = as2; + + if (s2 == NULL || *s2 == '\0') + return ((char *)s1); + + c = *s2; + while (*s1 != '\0' && n--) { + if (c == *s1++) { + k = n; tptr = s1; - while ((c = *++s2) == *s1++ && c) - ; - if (c == 0) + while ((c = *++s2) == *s1++ && c != '\0' && k--) + continue; + if (c == '\0') return ((char *)tptr - 1); s1 = tptr; s2 = as2; c = *s2; } + } return (NULL); } |
