From 716024cd274740e5ee0a906854b489160f9f02f3 Mon Sep 17 00:00:00 2001 From: Peter Avalos Date: Wed, 14 Jan 2009 16:01:28 -0500 Subject: [PATCH] Sync lib/libc/string with FreeBSD: -Move legacy functions to strings.h. -A few WARNS and style cleanup. -Add ffsl(), fls(), flsl(), ffsll(), flsll(). -Merge index.3 and rindex.3 since they are so similar. Do the same for strrchr(). -Add memrchr(). -Remove advertising clause from copyrights. -Add memmem() which is basically the same as strstr(). -Add stpcpy(). -Add a SECURITY CONSIDERATIONS section to remind people about buffer overflows, etc. -Add restrict keyword where required. -Add strndup(). -Correctly document the return value of strerror() and strerror_r() and the contents of the returned buffer for unknown error codes. -Add NLS catalogs support to strerror(), strerror_r() and strsignal(). Controlled by NLS define. -For strl* files, use a less restrictive copyright (original author changed it). -strmode.3: strmode does not return 0. -Move swab()'s prototype to unistd.h IAW SUS. -Simplify by removing unneeded local variables and explicit null termination is wcs* functions. -Move wcscoll() and wcsxfrm() to string/ and do locale-sensitive collation for single-byte locales. -Add wcsdup(). -Remove unneeded includes. -Reimplement wcsrchr(3) more efficiently, using a single forward scan (like strrchr(3)) instead of scanning forwards to find the end of the string then scanning backwards to find the character. -Slightly optimize wcswidth(). -Tell contrib srcs that we have strndup now since some of them provide their own static definition which will blow up a buildworld. --- gnu/usr.bin/binutils217/libiberty/config.h | 2 +- gnu/usr.bin/cc41/libiberty/config.h | 2 +- gnu/usr.bin/gdb/libiberty/config.h | 2 +- include/string.h | 148 +++++++++------ include/strings.h | 52 ++++-- kerberos5/include/config.h | 4 +- lib/libbind/port_after.h | 2 +- lib/libc/locale/Makefile.inc | 6 +- lib/libc/string/Makefile.inc | 82 ++++++--- lib/libc/string/bcmp.3 | 15 +- lib/libc/string/bcmp.c | 12 +- lib/libc/string/bcopy.3 | 15 +- lib/libc/string/bcopy.c | 39 ++-- lib/libc/string/bstring.3 | 6 +- lib/libc/string/bzero.3 | 15 +- lib/libc/string/ffs.3 | 77 ++++++-- lib/libc/string/ffs.c | 16 +- include/strings.h => lib/libc/string/ffsl.c | 23 ++- include/strings.h => lib/libc/string/ffsll.c | 23 ++- include/strings.h => lib/libc/string/fls.c | 23 ++- include/strings.h => lib/libc/string/flsl.c | 23 ++- include/strings.h => lib/libc/string/flsll.c | 23 ++- lib/libc/string/index.3 | 62 ++++--- lib/libc/string/index.c | 31 ++-- lib/libc/string/memccpy.3 | 6 +- lib/libc/string/memccpy.c | 7 +- lib/libc/string/memchr.3 | 38 +++- lib/libc/string/memchr.c | 7 +- lib/libc/string/memcmp.3 | 6 +- lib/libc/string/memcmp.c | 7 +- lib/libc/string/memcpy.3 | 6 +- lib/libc/string/{wcswidth.3 => memmem.3} | 85 +++++---- .../{locale/wcsxfrm.c => string/memmem.c} | 62 ++++--- lib/libc/string/memmove.3 | 6 +- lib/libc/string/memrchr.c | 39 ++++ lib/libc/string/memset.3 | 6 +- lib/libc/string/memset.c | 11 +- lib/libc/string/rindex.3 | 86 --------- lib/libc/string/rindex.c | 25 +-- include/strings.h => lib/libc/string/stpcpy.c | 23 ++- lib/libc/string/strcasecmp.3 | 15 +- lib/libc/string/strcasecmp.c | 8 +- lib/libc/string/strcasestr.c | 6 +- lib/libc/string/strcat.3 | 68 ++++++- lib/libc/string/strcat.c | 8 +- lib/libc/string/strchr.3 | 50 ++--- lib/libc/string/strcmp.3 | 6 +- lib/libc/string/strcmp.c | 7 +- lib/libc/string/strcoll.3 | 16 +- lib/libc/string/strcpy.3 | 73 ++++++-- lib/libc/string/strcpy.c | 9 +- lib/libc/string/strcspn.3 | 13 +- lib/libc/string/strdup.3 | 32 +++- lib/libc/string/strdup.c | 6 +- lib/libc/string/strerror.3 | 27 ++- lib/libc/string/strerror.c | 68 +++++-- lib/libc/string/string.3 | 11 +- lib/libc/string/strlcat.c | 39 ++-- lib/libc/string/strlcpy.3 | 43 +++-- lib/libc/string/strlcpy.c | 48 ++--- lib/libc/string/strlen.3 | 6 +- lib/libc/string/strlen.c | 7 +- lib/libc/string/strmode.3 | 11 +- lib/libc/string/strmode.c | 6 +- lib/libc/string/strncat.c | 9 +- lib/libc/string/strncmp.c | 7 +- lib/libc/string/strncpy.c | 9 +- .../strings.h => lib/libc/string/strndup.c | 29 ++- lib/libc/string/strnstr.c | 6 +- lib/libc/string/strpbrk.3 | 6 +- lib/libc/string/strpbrk.c | 6 +- lib/libc/string/strrchr.3 | 92 ---------- lib/libc/string/strsep.3 | 29 ++- lib/libc/string/strsep.c | 7 +- lib/libc/string/strsignal.c | 69 +++++-- lib/libc/string/strspn.3 | 15 +- lib/libc/string/strstr.3 | 7 +- lib/libc/string/strstr.c | 6 +- lib/libc/string/strtok.3 | 13 +- lib/libc/string/strtok.c | 171 +++++++----------- lib/libc/string/strxfrm.3 | 11 +- lib/libc/string/strxfrm.c | 18 +- lib/libc/string/swab.3 | 12 +- lib/libc/string/swab.c | 17 +- lib/libc/string/wcscat.c | 26 +-- lib/libc/string/wcscmp.c | 8 +- lib/libc/{locale => string}/wcscoll.3 | 29 ++- lib/libc/string/wcscoll.c | 97 ++++++++++ lib/libc/string/wcscpy.c | 19 +- lib/libc/string/wcscspn.c | 3 +- .../{locale/wcscoll.c => string/wcsdup.c} | 24 +-- lib/libc/string/wcslcat.c | 3 +- lib/libc/string/wcslcpy.c | 3 +- lib/libc/string/wcslen.c | 5 +- lib/libc/string/wcsncat.c | 5 +- lib/libc/string/wcsncmp.c | 8 +- lib/libc/string/wcsncpy.c | 8 +- lib/libc/string/wcspbrk.c | 2 +- lib/libc/string/wcsrchr.c | 27 ++- lib/libc/string/wcsspn.c | 3 +- lib/libc/string/wcsstr.c | 8 +- lib/libc/string/{strtok.3 => wcstok.3} | 140 +++++--------- lib/libc/string/wcstok.c | 85 +++++++++ lib/libc/string/wcswidth.3 | 6 +- lib/libc/string/wcswidth.c | 46 +++-- lib/libc/{locale => string}/wcsxfrm.3 | 58 +++--- lib/libc/string/{strxfrm.c => wcsxfrm.c} | 74 +++++--- lib/libc/string/wmemchr.3 | 17 +- lib/libc/string/wmemchr.c | 4 +- lib/libc/string/wmemcmp.c | 7 +- lib/libc/string/wmemcpy.c | 5 +- lib/libc/string/wmemmove.c | 3 +- lib/libc/string/wmemset.c | 3 +- lib/libmagic/config.h | 2 +- 114 files changed, 1623 insertions(+), 1350 deletions(-) copy include/strings.h => lib/libc/string/ffsl.c (84%) copy include/strings.h => lib/libc/string/ffsll.c (84%) copy include/strings.h => lib/libc/string/fls.c (84%) copy include/strings.h => lib/libc/string/flsl.c (84%) copy include/strings.h => lib/libc/string/flsll.c (84%) copy lib/libc/string/{wcswidth.3 => memmem.3} (54%) rename lib/libc/{locale/wcsxfrm.c => string/memmem.c} (55%) create mode 100644 lib/libc/string/memrchr.c delete mode 100644 lib/libc/string/rindex.3 copy include/strings.h => lib/libc/string/stpcpy.c (80%) copy include/strings.h => lib/libc/string/strndup.c (77%) delete mode 100644 lib/libc/string/strrchr.3 rename lib/libc/{locale => string}/wcscoll.3 (80%) create mode 100644 lib/libc/string/wcscoll.c rename lib/libc/{locale/wcscoll.c => string/wcsdup.c} (77%) copy lib/libc/string/{strtok.3 => wcstok.3} (57%) create mode 100644 lib/libc/string/wcstok.c rename lib/libc/{locale => string}/wcsxfrm.3 (73%) copy lib/libc/string/{strxfrm.c => wcsxfrm.c} (57%) diff --git a/gnu/usr.bin/binutils217/libiberty/config.h b/gnu/usr.bin/binutils217/libiberty/config.h index c256c3eb6c..bff011868d 100644 --- a/gnu/usr.bin/binutils217/libiberty/config.h +++ b/gnu/usr.bin/binutils217/libiberty/config.h @@ -235,7 +235,7 @@ #define HAVE_STRNCASECMP 1 /* Define to 1 if you have the `strndup' function. */ -/* #undef HAVE_STRNDUP */ +#define HAVE_STRNDUP 1 /* Define to 1 if you have the `strrchr' function. */ #define HAVE_STRRCHR 1 diff --git a/gnu/usr.bin/cc41/libiberty/config.h b/gnu/usr.bin/cc41/libiberty/config.h index ac0e88fc34..107c9cb483 100644 --- a/gnu/usr.bin/cc41/libiberty/config.h +++ b/gnu/usr.bin/cc41/libiberty/config.h @@ -235,7 +235,7 @@ #define HAVE_STRNCASECMP 1 /* Define to 1 if you have the `strndup' function. */ -/* #undef HAVE_STRNDUP */ +#define HAVE_STRNDUP 1 /* Define to 1 if you have the `strrchr' function. */ #define HAVE_STRRCHR 1 diff --git a/gnu/usr.bin/gdb/libiberty/config.h b/gnu/usr.bin/gdb/libiberty/config.h index 33f02f7835..09a266fe78 100644 --- a/gnu/usr.bin/gdb/libiberty/config.h +++ b/gnu/usr.bin/gdb/libiberty/config.h @@ -235,7 +235,7 @@ #define HAVE_STRNCASECMP 1 /* Define to 1 if you have the `strndup' function. */ -/* #undef HAVE_STRNDUP */ +#define HAVE_STRNDUP 1 /* Define to 1 if you have the `strrchr' function. */ #define HAVE_STRRCHR 1 diff --git a/include/string.h b/include/string.h index 3b716b909f..7141981f94 100644 --- a/include/string.h +++ b/include/string.h @@ -31,84 +31,116 @@ * SUCH DAMAGE. * * @(#)string.h 8.1 (Berkeley) 6/2/93 - * $FreeBSD: src/include/string.h,v 1.6.2.3 2001/12/25 00:36:57 ache Exp $ + * $FreeBSD: src/include/string.h,v 1.27 2008/12/08 21:04:24 kib Exp $ * $DragonFly: src/include/string.h,v 1.9 2008/06/05 17:53:10 swildner Exp $ */ #ifndef _STRING_H_ #define _STRING_H_ -#ifndef _SYS_TYPES_H_ +#include +#include #include -#endif -#ifndef _MACHINE_STDINT_H_ -#include +/* + * Prototype functions which were historically defined in , but + * are required by POSIX to be prototyped in . + */ +#if __BSD_VISIBLE +#include #endif #ifndef _SIZE_T_DECLARED -#define _SIZE_T_DECLARED -typedef __size_t size_t; /* open group */ +typedef __size_t size_t; +#define _SIZE_T_DECLARED #endif -#include -#include - __BEGIN_DECLS -void *memchr (const void *, int, size_t); -void *memmove (void *, const void *, size_t); -char *strchr (const char *, int); -int strcoll (const char *, const char *); -size_t strcspn (const char *, const char *); -char *strerror (int); -char *strpbrk (const char *, const char *); -char *strrchr (const char *, int); -size_t strspn (const char *, const char *); -char *strstr (const char *, const char *); -char *strtok (char *, const char *); -size_t strxfrm (char *, const char *, size_t); - +#if __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE +void *memccpy(void * __restrict, const void * __restrict, int, size_t); +#endif +void *memchr(const void *, int, size_t) __pure; +void *memrchr(const void *, int, size_t) __pure; #if !defined(_KERNEL_VIRTUAL) -int memcmp (const void *, const void *, size_t); -void *memcpy (void *, const void *, size_t); -void *memset (void *, int, size_t); -char *strcat (char *, const char *); -int strcmp (const char *, const char *); -char *strcpy (char *, const char *); -size_t strlen (const char *); -char *strncat (char *, const char *, size_t); -int strncmp (const char *, const char *, size_t); -char *strncpy (char *, const char *, size_t); +int memcmp(const void *, const void *, size_t) __pure; +void *memcpy(void * __restrict, const void * __restrict, size_t); #endif - -/* Nonstandard routines */ -#if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE) -void *memccpy (void *, const void *, int, size_t); -int strcasecmp (const char *, const char *); -char *strcasestr (const char *, const char *); -char *strdup (const char *); -int strerror_r (int, char *, size_t); -void strmode (mode_t, char *); -int strncasecmp (const char *, const char *, size_t); -char *strnstr (const char *, const char *, size_t); -char *strsep (char **, const char *); -char *strsignal (int); -char *strtok_r (char *, const char *, char **); -void swab (const void *, void *, size_t); - +#if __BSD_VISIBLE +void *memmem(const void *, size_t, const void *, size_t); +#endif +void *memmove(void *, const void *, size_t); #if !defined(_KERNEL_VIRTUAL) -void bcopy (const void *, void *, size_t); -void bzero (void *, size_t); -int ffs (int); -int bcmp (const void *, const void *, size_t); -char *index (const char *, int); -char *rindex (const char *, int); -size_t strlcat (char *, const char *, size_t); -size_t strlcpy (char *, const char *, size_t); +void *memset(void *, int, size_t); #endif - +#if __BSD_VISIBLE +char *stpcpy(char *, const char *); +char *strcasestr(const char *, const char *) __pure; +#endif +#if !defined(_KERNEL_VIRTUAL) +char *strcat(char * __restrict, const char * __restrict); #endif +char *strchr(const char *, int) __pure; +#if !defined(_KERNEL_VIRTUAL) +int strcmp(const char *, const char *) __pure; +#endif +int strcoll(const char *, const char *); +#if !defined(_KERNEL_VIRTUAL) +char *strcpy(char * __restrict, const char * __restrict); +#endif +size_t strcspn(const char *, const char *) __pure; +#if __POSIX_VISIBLE >= 200112 || __XSI_VISIBLE +char *strdup(const char *); +#endif +char *strerror(int); +#if __POSIX_VISIBLE >= 200112 +int strerror_r(int, char *, size_t); +#endif +#if !defined(_KERNEL_VIRTUAL) +#if __BSD_VISIBLE +size_t strlcat(char *, const char *, size_t); +size_t strlcpy(char *, const char *, size_t); +#endif /* __BSD_VISIBLE */ +size_t strlen(const char *) __pure; +#endif /* !_KERNEL_VIRTUAL */ +#if __BSD_VISIBLE +void strmode(mode_t, char *); +#endif +#if !defined(_KERNEL_VIRTUAL) +char *strncat(char * __restrict, const char * __restrict, size_t); +int strncmp(const char *, const char *, size_t) __pure; +char *strncpy(char * __restrict, const char * __restrict, size_t); +#endif +#if __BSD_VISIBLE +char *strndup(const char *, size_t); +char *strnstr(const char *, const char *, size_t) __pure; +#endif +char *strpbrk(const char *, const char *) __pure; +char *strrchr(const char *, int) __pure; +#if __BSD_VISIBLE +char *strsep(char **, const char *); +char *strsignal(int); +#endif +size_t strspn(const char *, const char *) __pure; +char *strstr(const char *, const char *) __pure; +char *strtok(char * __restrict, const char * __restrict); +#if __POSIX_VISIBLE >= 199506 || __XSI_VISIBLE >= 500 +char *strtok_r(char *, const char *, char **); +#endif +size_t strxfrm(char * __restrict, const char * __restrict, size_t); +#if __BSD_VISIBLE + +#ifndef _SWAB_DECLARED +#define _SWAB_DECLARED + +#ifndef _SSIZE_T_DECLARED +typedef __ssize_t ssize_t; +#define _SSIZE_T_DECLARED +#endif /* _SSIZE_T_DECLARED */ + +void swab(const void * __restrict, void * __restrict, ssize_t); +#endif /* _SWAB_DECLARED */ +#endif /* __BSD_VISIBLE */ __END_DECLS #endif /* _STRING_H_ */ diff --git a/include/strings.h b/include/strings.h index 2a63fa439b..cf39da74ba 100644 --- a/include/strings.h +++ b/include/strings.h @@ -1,6 +1,6 @@ /*- - * Copyright (c) 1990, 1993 - * The Regents of the University of California. All rights reserved. + * Copyright (c) 2002 Mike Barcroft + * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,18 +10,11 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) @@ -30,7 +23,40 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * @(#)strings.h 8.1 (Berkeley) 6/2/93 + * $FreeBSD: src/include/strings.h,v 1.7 2008/11/03 10:22:19 kib Exp $ */ -#include +#ifndef _STRINGS_H_ +#define _STRINGS_H_ + +#include +#include + +#ifndef _SIZE_T_DECLARED +typedef __size_t size_t; +#define _SIZE_T_DECLARED +#endif + +__BEGIN_DECLS +#if !defined(_KERNEL_VIRTUAL) +int bcmp(const void *, const void *, size_t) __pure; /* LEGACY */ +void bcopy(const void *, void *, size_t); /* LEGACY */ +void bzero(void *, size_t); /* LEGACY */ +int ffs(int) __pure2; +#endif +#ifdef __BSD_VISIBLE +int ffsl(long) __pure2; +int ffsll(long long) __pure2; +int fls(int) __pure2; +int flsl(long) __pure2; +int flsll(long long) __pure2; +#endif +#if !defined(_KERNEL_VIRTUAL) +char *index(const char *, int) __pure; /* LEGACY */ +char *rindex(const char *, int) __pure; /* LEGACY */ +#endif +int strcasecmp(const char *, const char *) __pure; +int strncasecmp(const char *, const char *, size_t) __pure; +__END_DECLS + +#endif /* _STRINGS_H_ */ diff --git a/kerberos5/include/config.h b/kerberos5/include/config.h index b1a78e9a60..8a0ff7609e 100644 --- a/kerberos5/include/config.h +++ b/kerberos5/include/config.h @@ -724,7 +724,7 @@ static /**/const char *const rcsid[] = { (const char *)rcsid, "@(#)" msg } #define HAVE_STRNCASECMP 1 /* Define if you have the function `strndup'. */ -/* #undef HAVE_STRNDUP */ +#define HAVE_STRNDUP 1 /* Define if you have the function `strnlen'. */ /* #undef HAVE_STRNLEN */ @@ -1189,7 +1189,7 @@ static /**/const char *const rcsid[] = { (const char *)rcsid, "@(#)" msg } /* #undef NEED_SNPRINTF_PROTO */ /* define if the system is missing a prototype for strndup() */ -#define NEED_STRNDUP_PROTO 1 +/* #undef NEED_STRNDUP_PROTO */ /* define if the system is missing a prototype for strsep() */ /* #undef NEED_STRSEP_PROTO */ diff --git a/lib/libbind/port_after.h b/lib/libbind/port_after.h index 72fd0c857f..9bc46f9886 100644 --- a/lib/libbind/port_after.h +++ b/lib/libbind/port_after.h @@ -24,7 +24,7 @@ #undef HAS_IN_ADDR6 #define HAVE_SOCKADDR_STORAGE 1 #undef NEED_GETTIMEOFDAY -#undef HAVE_STRNDUP +#define HAVE_STRNDUP 1 #undef USE_FIONBIO_IOCTL #undef USE_SYSERROR_LIST #undef INNETGR_ARGS diff --git a/lib/libc/locale/Makefile.inc b/lib/libc/locale/Makefile.inc index 8855f79668..7f7141aaa5 100644 --- a/lib/libc/locale/Makefile.inc +++ b/lib/libc/locale/Makefile.inc @@ -5,8 +5,8 @@ SRCS+= collate.c collcmp.c SRCS+= _def_messages.c _def_monetary.c _def_numeric.c _def_time.c SRCS+= localeconv.c nl_langinfo.c setlocale.c __mb_cur_max.c -SRCS+= wcscoll.c wcsftime.c wcstod.c wcstol.c wcstoll.c wcstoimax.c wcstoul.c -SRCS+= wcstoull.c wcstoumax.c wcsxfrm.c aliasname.c +SRCS+= wcsftime.c wcstod.c wcstol.c wcstoll.c wcstoimax.c wcstoul.c +SRCS+= wcstoull.c wcstoumax.c aliasname.c SRCS+= ___runetype_mb.c _wctrans.c iswctype.c multibyte_c90.c SRCS+= multibyte_amd1.c rune.c runeglue.c runetable.c setrunelocale.c @@ -16,7 +16,7 @@ CFLAGS+= -I${.CURDIR}/../libc/locale MAN+= nl_langinfo.3 setlocale.3 MAN+= btowc.3 iswalnum.3 iswctype.3 towlower.3 towctrans.3 mblen.3 mbrlen.3 MAN+= mbrtowc.3 mbsinit.3 mbsrtowcs.3 mbstowcs.3 mbtowc.3 wcrtomb.3 -MAN+= wcscoll.3 wcsftime.3 wcstod.3 wcstol.3 wcsrtombs.3 wcstombs.3 wcsxfrm.3 +MAN+= wcsftime.3 wcstod.3 wcstol.3 wcsrtombs.3 wcstombs.3 MAN+= wctob.3 wctomb.3 wctrans.3 wctype.3 wcwidth.3 MLINKS+=iswalnum.3 iswalpha.3 iswalnum.3 iswblank.3 diff --git a/lib/libc/string/Makefile.inc b/lib/libc/string/Makefile.inc index e82a77c7af..43fa9200ac 100644 --- a/lib/libc/string/Makefile.inc +++ b/lib/libc/string/Makefile.inc @@ -1,5 +1,5 @@ # @(#)Makefile.inc 8.1 (Berkeley) 6/4/93 -# $FreeBSD: src/lib/libc/string/Makefile.inc,v 1.19.2.5 2001/12/25 00:36:53 ache Exp $ +# $FreeBSD: src/lib/libc/string/Makefile.inc,v 1.42 2009/01/09 16:24:10 ru Exp $ # $DragonFly: src/lib/libc/string/Makefile.inc,v 1.5 2008/05/31 04:51:55 swildner Exp $ .PATH: ${.CURDIR}/../libc/${MACHINE_ARCH}/string ${.CURDIR}/../libc/string @@ -7,47 +7,73 @@ CFLAGS+= -I${.CURDIR}/../libc/locale # machine-independent string sources -MISRCS+=bcmp.c bcopy.c bzero.c ffs.c index.c memccpy.c memchr.c memcmp.c \ - memcpy.c memmove.c memset.c rindex.c strcasecmp.c strcat.c strchr.c \ - strcmp.c strcoll.c strcpy.c strcspn.c strdup.c strerror.c \ - strlcat.c strlcpy.c strlen.c strmode.c strncat.c strncmp.c strncpy.c \ - strcasestr.c strnstr.c \ +MISRCS+=bcmp.c bcopy.c bzero.c ffs.c ffsl.c ffsll.c fls.c flsl.c flsll.c \ + index.c memccpy.c memchr.c memrchr.c memcmp.c \ + memcpy.c memmem.c memmove.c memset.c rindex.c stpcpy.c strcasecmp.c \ + strcat.c strcasestr.c strchr.c strcmp.c strcoll.c strcpy.c strcspn.c \ + strdup.c strerror.c strlcat.c strlcpy.c strlen.c strmode.c strncat.c \ + strncmp.c strncpy.c strndup.c strnstr.c \ strpbrk.c strrchr.c strsep.c strsignal.c strspn.c strstr.c strtok.c \ - strxfrm.c swab.c wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c \ + strxfrm.c swab.c wcscat.c wcschr.c wcscmp.c wcscoll.c wcscpy.c \ + wcscspn.c wcsdup.c \ wcslcat.c wcslcpy.c wcslen.c wcsncat.c wcsncmp.c wcsncpy.c wcspbrk.c \ - wcsrchr.c wcsspn.c wcsstr.c wcswidth.c wmemchr.c wmemcmp.c wmemcpy.c \ - wmemmove.c wmemset.c + wcsrchr.c wcsspn.c wcsstr.c wcstok.c wcswidth.c wcsxfrm.c wmemchr.c \ + wmemcmp.c \ + wmemcpy.c wmemmove.c wmemset.c # machine-dependent string sources -.if exists (${.CURDIR}/../libc/${MACHINE_ARCH}/string/Makefile.inc) +.if exists(${.CURDIR}/../libc/${MACHINE_ARCH}/string/Makefile.inc) .include "${.CURDIR}/../libc/${MACHINE_ARCH}/string/Makefile.inc" .endif .if ${LIB} == "c" MAN+= bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 index.3 memccpy.3 memchr.3 \ - memcmp.3 memcpy.3 memmove.3 memset.3 rindex.3 strcasecmp.3 strcat.3 \ + memcmp.3 memcpy.3 memmem.3 memmove.3 memset.3 strcasecmp.3 strcat.3 \ strchr.3 strcmp.3 strcoll.3 strcpy.3 strcspn.3 strdup.3 strerror.3 \ - string.3 strlcpy.3 strlen.3 strmode.3 strpbrk.3 strrchr.3 strsep.3 \ - strspn.3 strstr.3 strtok.3 strxfrm.3 swab.3 wcswidth.3 wmemchr.3 + string.3 strlcpy.3 strlen.3 strmode.3 strpbrk.3 strsep.3 \ + strspn.3 strstr.3 strtok.3 strxfrm.3 swab.3 wcscoll.3 wcstok.3 \ + wcswidth.3 wcsxfrm.3 wmemchr.3 +MLINKS+=ffs.3 ffsl.3 \ + ffs.3 ffsll.3 \ + ffs.3 fls.3 \ + ffs.3 flsl.3 \ + ffs.3 flsll.3 +MLINKS+=index.3 rindex.3 +MLINKS+=memchr.3 memrchr.3 MLINKS+=strcasecmp.3 strncasecmp.3 MLINKS+=strcat.3 strncat.3 +MLINKS+=strchr.3 strrchr.3 MLINKS+=strcmp.3 strncmp.3 -MLINKS+=strcpy.3 strncpy.3 -MLINKS+=strerror.3 perror.3 strerror.3 sys_errlist.3 strerror.3 sys_nerr.3 -MLINKS+=strerror.3 strerror_r.3 +MLINKS+=strcpy.3 stpcpy.3 \ + strcpy.3 strncpy.3 +MLINKS+=strdup.3 strndup.3 +MLINKS+=strerror.3 perror.3 \ + strerror.3 strerror_r.3 \ + strerror.3 sys_errlist.3 \ + strerror.3 sys_nerr.3 MLINKS+=strlcpy.3 strlcat.3 +MLINKS+=strstr.3 strcasestr.3 \ + strstr.3 strnstr.3 MLINKS+=strtok.3 strtok_r.3 -MLINKS+=strstr.3 strcasestr.3 -MLINKS+=strstr.3 strnstr.3 -MLINKS+=wmemchr.3 wmemcmp.3 wmemchr.3 wmemcpy.3 \ - wmemchr.3 wmemmove.3 wmemchr.3 wmemset.3 \ - wmemchr.3 wcscat.3 wmemchr.3 wcschr.3 \ - wmemchr.3 wcscmp.3 wmemchr.3 wcscpy.3 \ - wmemchr.3 wcscspn.3 wmemchr.3 wcslcat.3 \ - wmemchr.3 wcslcpy.3 wmemchr.3 wcslen.3 \ - wmemchr.3 wcsncat.3 wmemchr.3 wcsncmp.3 \ - wmemchr.3 wcsncpy.3 wmemchr.3 wcspbrk.3 \ - wmemchr.3 wcsrchr.3 wmemchr.3 wcsspn.3 \ - wmemchr.3 wcsstr.3 +MLINKS+=wmemchr.3 wcscat.3 \ + wmemchr.3 wcschr.3 \ + wmemchr.3 wcscmp.3 \ + wmemchr.3 wcscpy.3 \ + wmemchr.3 wcscspn.3 \ + wmemchr.3 wcsdup.3 \ + wmemchr.3 wcslcat.3 \ + wmemchr.3 wcslcpy.3 \ + wmemchr.3 wcslen.3 \ + wmemchr.3 wcsncat.3 \ + wmemchr.3 wcsncmp.3 \ + wmemchr.3 wcsncpy.3 \ + wmemchr.3 wcspbrk.3 \ + wmemchr.3 wcsrchr.3 \ + wmemchr.3 wcsspn.3 \ + wmemchr.3 wcsstr.3 \ + wmemchr.3 wmemcmp.3 \ + wmemchr.3 wmemcpy.3 \ + wmemchr.3 wmemmove.3 \ + wmemchr.3 wmemset.3 .endif diff --git a/lib/libc/string/bcmp.3 b/lib/libc/string/bcmp.3 index 21dbbd6619..b49624e5fb 100644 --- a/lib/libc/string/bcmp.3 +++ b/lib/libc/string/bcmp.3 @@ -11,10 +11,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -32,7 +28,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)bcmp.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/bcmp.3,v 1.4.2.4 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/bcmp.3,v 1.11 2007/01/09 00:28:11 imp Exp $ .\" $DragonFly: src/lib/libc/string/bcmp.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" .Dd June 4, 1993 @@ -44,7 +40,7 @@ .Sh LIBRARY .Lb libc .Sh SYNOPSIS -.In string.h +.In strings.h .Ft int .Fn bcmp "const void *b1" "const void *b2" "size_t len" .Sh DESCRIPTION @@ -73,3 +69,10 @@ A .Fn bcmp function first appeared in .Bx 4.2 . +Its prototype existed previously in +.In string.h +before it was moved to +.In strings.h +for +.St -p1003.1-2001 +compliance. diff --git a/lib/libc/string/bcmp.c b/lib/libc/string/bcmp.c index 94ea6dcd9f..231032da3f 100644 --- a/lib/libc/string/bcmp.c +++ b/lib/libc/string/bcmp.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -31,11 +27,11 @@ * SUCH DAMAGE. * * @(#)bcmp.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/bcmp.c,v 1.1.1.1.14.1 2001/07/09 23:30:03 obrien Exp $ + * $FreeBSD: src/lib/libc/string/bcmp.c,v 1.6 2007/01/09 00:28:11 imp Exp $ * $DragonFly: src/lib/libc/string/bcmp.c,v 1.4 2005/09/18 16:32:34 asmodai Exp $ */ -#include +#include /* * bcmp -- vax cmpc3 instruction @@ -46,12 +42,12 @@ bcmp(const void *b1, const void *b2, size_t length) char *p1, *p2; if (length == 0) - return(0); + return (0); p1 = (char *)b1; p2 = (char *)b2; do if (*p1++ != *p2++) break; while (--length); - return(length); + return (length); } diff --git a/lib/libc/string/bcopy.3 b/lib/libc/string/bcopy.3 index cb60b9d48b..4b71b4f0a5 100644 --- a/lib/libc/string/bcopy.3 +++ b/lib/libc/string/bcopy.3 @@ -12,10 +12,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -33,7 +29,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)bcopy.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/bcopy.3,v 1.3.2.4 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/bcopy.3,v 1.10 2007/01/09 00:28:11 imp Exp $ .\" $DragonFly: src/lib/libc/string/bcopy.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" .Dd June 4, 1993 @@ -45,7 +41,7 @@ .Sh LIBRARY .Lb libc .Sh SYNOPSIS -.In string.h +.In strings.h .Ft void .Fn bcopy "const void *src" "void *dst" "size_t len" .Sh DESCRIPTION @@ -73,3 +69,10 @@ A .Fn bcopy function appeared in .Bx 4.2 . +Its prototype existed previously in +.In string.h +before it was moved to +.In strings.h +for +.St -p1003.1-2001 +compliance. diff --git a/lib/libc/string/bcopy.c b/lib/libc/string/bcopy.c index 45acf02a77..ac8a9eb8a8 100644 --- a/lib/libc/string/bcopy.c +++ b/lib/libc/string/bcopy.c @@ -13,10 +13,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -34,12 +30,11 @@ * SUCH DAMAGE. * * @(#)bcopy.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/bcopy.c,v 1.1.1.1.14.1 2001/07/09 23:30:03 obrien Exp $ + * $FreeBSD: src/lib/libc/string/bcopy.c,v 1.7 2007/01/09 00:28:11 imp Exp $ * $DragonFly: src/lib/libc/string/bcopy.c,v 1.4 2005/09/18 16:32:34 asmodai Exp $ */ - -#include -#include + +#include /* * sizeof(word) MUST BE A POWER OF TWO @@ -55,18 +50,22 @@ typedef int word; /* "word" used for optimal copy speed */ * This is the routine that actually implements * (the portable versions of) bcopy, memcpy, and memmove. */ -#ifdef MEMCOPY +#if defined(MEMCOPY) || defined(MEMMOVE) +#include + void * -memcpy(void *dst0, const void *src0, size_t length) +#ifdef MEMCOPY +memcpy #else -#ifdef MEMMOVE -void * -memmove(void *dst0, const void *src0, size_t length) +memmove +#endif +(void *dst0, const void *src0, size_t length) #else +#include + void bcopy(const void *src0, void *dst0, size_t length) #endif -#endif { char *dst = dst0; const char *src = src0; @@ -85,13 +84,13 @@ bcopy(const void *src0, void *dst0, size_t length) /* * Copy forward. */ - t = (int)src; /* only need low bits */ - if ((t | (int)dst) & wmask) { + t = (uintptr_t)src; /* only need low bits */ + if ((t | (uintptr_t)dst) & wmask) { /* * Try to align operands. This cannot be done * unless the low bits match. */ - if ((t ^ (int)dst) & wmask || length < wsize) + if ((t ^ (uintptr_t)dst) & wmask || length < wsize) t = length; else t = wsize - (t & wmask); @@ -113,9 +112,9 @@ bcopy(const void *src0, void *dst0, size_t length) */ src += length; dst += length; - t = (int)src; - if ((t | (int)dst) & wmask) { - if ((t ^ (int)dst) & wmask || length <= wsize) + t = (uintptr_t)src; + if ((t | (uintptr_t)dst) & wmask) { + if ((t ^ (uintptr_t)dst) & wmask || length <= wsize) t = length; else t &= wmask; diff --git a/lib/libc/string/bstring.3 b/lib/libc/string/bstring.3 index 74ccdc452c..ae89fe55c4 100644 --- a/lib/libc/string/bstring.3 +++ b/lib/libc/string/bstring.3 @@ -11,10 +11,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -32,7 +28,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)bstring.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/bstring.3,v 1.3.2.3 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/bstring.3,v 1.8 2007/01/09 00:28:11 imp Exp $ .\" $DragonFly: src/lib/libc/string/bstring.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" .Dd June 4, 1993 diff --git a/lib/libc/string/bzero.3 b/lib/libc/string/bzero.3 index 33f341f1de..52706b9fda 100644 --- a/lib/libc/string/bzero.3 +++ b/lib/libc/string/bzero.3 @@ -12,10 +12,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -33,7 +29,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)bzero.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/bzero.3,v 1.3.2.4 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/bzero.3,v 1.10 2007/01/09 00:28:11 imp Exp $ .\" $DragonFly: src/lib/libc/string/bzero.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" .Dd June 4, 1993 @@ -45,7 +41,7 @@ .Sh LIBRARY .Lb libc .Sh SYNOPSIS -.In string.h +.In strings.h .Ft void .Fn bzero "void *b" "size_t len" .Sh DESCRIPTION @@ -70,3 +66,10 @@ A function appeared in .Bx 4.3 . +Its prototype existed previously in +.In string.h +before it was moved to +.In strings.h +for +.St -p1003.1-2001 +compliance. diff --git a/lib/libc/string/ffs.3 b/lib/libc/string/ffs.3 index c16fc377e2..8e10a7b5eb 100644 --- a/lib/libc/string/ffs.3 +++ b/lib/libc/string/ffs.3 @@ -11,10 +11,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -32,30 +28,60 @@ .\" SUCH DAMAGE. .\" .\" @(#)ffs.3 8.2 (Berkeley) 4/19/94 -.\" $FreeBSD: src/lib/libc/string/ffs.3,v 1.3.2.2 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/ffs.3,v 1.13 2009/01/13 13:19:42 kib Exp $ .\" $DragonFly: src/lib/libc/string/ffs.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" -.Dd April 19, 1994 +.Dd October 26, 2008 .Dt FFS 3 .Os .Sh NAME -.Nm ffs -.Nd find first bit set in a bit string +.Nm ffs , +.Nm ffsl , +.Nm ffsll , +.Nm fls , +.Nm flsl , +.Nm flsll +.Nd find first or last bit set in a bit string .Sh LIBRARY .Lb libc .Sh SYNOPSIS -.In string.h +.In strings.h .Ft int .Fn ffs "int value" +.Ft int +.Fn ffsl "long value" +.Ft int +.Ft int +.Fn ffsll "long long value" +.Fn fls "int value" +.Ft int +.Fn flsl "long value" +.Ft int +.Fn flsll "long long value" .Sh DESCRIPTION The -.Fn ffs -function finds the first bit set in +.Fn ffs , +.Fn ffsl +and +.Fn ffsll +functions find the first bit set +(beginning with the least significant bit) +in +.Fa value +and return the index of that bit. +.Pp +The +.Fn fls , +.Fn flsl +and +.Fn flsll +functions find the last bit set in .Fa value -and returns the index of that bit. -Bits are numbered starting from 1, starting at the right-most -bit. -A return value of 0 means that the argument was zero. +and return the index of that bit. +.Pp +Bits are numbered starting at 1 (the least significant bit). +A return value of zero from any of these functions means that the +argument was zero. .Sh SEE ALSO .Xr bitstring 3 .Sh HISTORY @@ -63,3 +89,24 @@ The .Fn ffs function appeared in .Bx 4.3 . +Its prototype existed previously in +.In string.h +before it was moved to +.In strings.h +for +.St -p1003.1-2001 +compliance. +.Pp +The +.Fn ffsl , +.Fn fls +and +.Fn flsl +functions appeared in +.Fx 5.3 . +The +.Fn ffsll +and +.Fn flsll +functions appeared in +.Fx 7.1 . diff --git a/lib/libc/string/ffs.c b/lib/libc/string/ffs.c index 7d0c571779..8cd5549645 100644 --- a/lib/libc/string/ffs.c +++ b/lib/libc/string/ffs.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -31,14 +27,14 @@ * SUCH DAMAGE. * * @(#)ffs.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/ffs.c,v 1.1.1.1.14.1 2001/07/09 23:30:03 obrien Exp $ + * $FreeBSD: src/lib/libc/string/ffs.c,v 1.8 2007/01/09 00:28:11 imp Exp $ * $DragonFly: src/lib/libc/string/ffs.c,v 1.4 2005/09/18 16:32:34 asmodai Exp $ */ -#include +#include /* - * ffs -- vax ffs instruction + * Find First Set bit */ int ffs(int mask) @@ -46,8 +42,8 @@ ffs(int mask) int bit; if (mask == 0) - return(0); + return (0); for (bit = 1; !(mask & 1); bit++) - mask >>= 1; - return(bit); + mask = (unsigned int)mask >> 1; + return (bit); } diff --git a/include/strings.h b/lib/libc/string/ffsl.c similarity index 84% copy from include/strings.h copy to lib/libc/string/ffsl.c index 2a63fa439b..201deb5443 100644 --- a/include/strings.h +++ b/lib/libc/string/ffsl.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -30,7 +26,22 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * @(#)strings.h 8.1 (Berkeley) 6/2/93 + * $FreeBSD: src/lib/libc/string/ffsl.c,v 1.4 2008/11/03 10:14:47 kib Exp $ */ -#include +#include + +/* + * Find First Set bit + */ +int +ffsl(long mask) +{ + int bit; + + if (mask == 0) + return (0); + for (bit = 1; !(mask & 1); bit++) + mask = (unsigned long)mask >> 1; + return (bit); +} diff --git a/include/strings.h b/lib/libc/string/ffsll.c similarity index 84% copy from include/strings.h copy to lib/libc/string/ffsll.c index 2a63fa439b..125a26ca6c 100644 --- a/include/strings.h +++ b/lib/libc/string/ffsll.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -30,7 +26,22 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * @(#)strings.h 8.1 (Berkeley) 6/2/93 + * $FreeBSD: src/lib/libc/string/ffsll.c,v 1.1 2008/11/03 10:22:19 kib Exp $ */ -#include +#include + +/* + * Find First Set bit + */ +int +ffsll(long long mask) +{ + int bit; + + if (mask == 0) + return (0); + for (bit = 1; !(mask & 1); bit++) + mask = (unsigned long long)mask >> 1; + return (bit); +} diff --git a/include/strings.h b/lib/libc/string/fls.c similarity index 84% copy from include/strings.h copy to lib/libc/string/fls.c index 2a63fa439b..cbc5f28938 100644 --- a/include/strings.h +++ b/lib/libc/string/fls.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -30,7 +26,22 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * @(#)strings.h 8.1 (Berkeley) 6/2/93 + * $FreeBSD: src/lib/libc/string/fls.c,v 1.3 2007/01/09 00:28:12 imp Exp $ */ -#include +#include + +/* + * Find Last Set bit + */ +int +fls(int mask) +{ + int bit; + + if (mask == 0) + return (0); + for (bit = 1; mask != 1; bit++) + mask = (unsigned int)mask >> 1; + return (bit); +} diff --git a/include/strings.h b/lib/libc/string/flsl.c similarity index 84% copy from include/strings.h copy to lib/libc/string/flsl.c index 2a63fa439b..a04975e5a1 100644 --- a/include/strings.h +++ b/lib/libc/string/flsl.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -30,7 +26,22 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * @(#)strings.h 8.1 (Berkeley) 6/2/93 + * $FreeBSD: src/lib/libc/string/flsl.c,v 1.3 2007/01/09 00:28:12 imp Exp $ */ -#include +#include + +/* + * Find Last Set bit + */ +int +flsl(long mask) +{ + int bit; + + if (mask == 0) + return (0); + for (bit = 1; mask != 1; bit++) + mask = (unsigned long)mask >> 1; + return (bit); +} diff --git a/include/strings.h b/lib/libc/string/flsll.c similarity index 84% copy from include/strings.h copy to lib/libc/string/flsll.c index 2a63fa439b..b8e27b1dfc 100644 --- a/include/strings.h +++ b/lib/libc/string/flsll.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -30,7 +26,22 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * @(#)strings.h 8.1 (Berkeley) 6/2/93 + * $FreeBSD: src/lib/libc/string/flsll.c,v 1.1 2008/11/03 10:22:19 kib Exp $ */ -#include +#include + +/* + * Find Last Set bit + */ +int +flsll(long long mask) +{ + int bit; + + if (mask == 0) + return (0); + for (bit = 1; mask != 1; bit++) + mask = (unsigned long long)mask >> 1; + return (bit); +} diff --git a/lib/libc/string/index.3 b/lib/libc/string/index.3 index 20b9eb14f8..234a32cca7 100644 --- a/lib/libc/string/index.3 +++ b/lib/libc/string/index.3 @@ -11,10 +11,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -32,43 +28,58 @@ .\" SUCH DAMAGE. .\" .\" @(#)index.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/index.3,v 1.3.2.3 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/index.3,v 1.12 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/index.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" .Dd June 4, 1993 .Dt INDEX 3 .Os .Sh NAME -.Nm index +.Nm index , +.Nm rindex .Nd locate character in string .Sh LIBRARY .Lb libc .Sh SYNOPSIS -.In string.h -.Ft char * +.In strings.h +.Ft "char *" .Fn index "const char *s" "int c" +.Ft "char *" +.Fn rindex "const char *s" "int c" .Sh DESCRIPTION The .Fn index function -locates the first character matching +locates the first occurrence of .Fa c (converted to a -.Em char ) -in the null-terminated string +.Vt char ) +in the string pointed to by .Fa s . -.Sh RETURN VALUES -A pointer to the character is returned if it is found; otherwise -.Dv NULL -is returned. -If +The terminating null character is considered part of the string; +therefore if .Fa c -is '\e0', +is +.Ql \e0 , +the functions locate the terminating +.Ql \e0 . +.Pp +The +.Fn rindex +function is identical to +.Fn index , +except it locates the last occurrence of +.Fa c . +.Sh RETURN VALUES +The functions .Fn index -locates the terminating '\e0'. +and +.Fn rindex +return a pointer to the located character, or +.Dv NULL +if the character does not appear in the string. .Sh SEE ALSO .Xr memchr 3 , -.Xr rindex 3 , .Xr strchr 3 , .Xr strcspn 3 , .Xr strpbrk 3 , @@ -78,7 +89,16 @@ locates the terminating '\e0'. .Xr strstr 3 , .Xr strtok 3 .Sh HISTORY -A +The .Fn index -function appeared in +and +.Fn rindex +functions appeared in .At v6 . +Their prototypes existed previously in +.In string.h +before they were moved to +.In strings.h +for +.St -p1003.1-2001 +compliance. diff --git a/lib/libc/string/index.c b/lib/libc/string/index.c index 58e2060e5b..a68eb5dbfe 100644 --- a/lib/libc/string/index.c +++ b/lib/libc/string/index.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -31,26 +27,33 @@ * SUCH DAMAGE. * * @(#)index.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/index.c,v 1.1.1.1.14.1 2001/07/09 23:30:03 obrien Exp $ + * $FreeBSD: src/lib/libc/string/index.c,v 1.8 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/index.c,v 1.4 2005/09/18 16:32:34 asmodai Exp $ */ -#include -#include #include -char * #ifdef STRCHR -strchr(const char *p, int ch) +#include + +char * +strchr #else -index(const char *p, int ch) +#include + +char * +index #endif +(const char *p, int ch) { + char c; + + c = ch; for (;; ++p) { - if (*p == ch) - return((char *)p); - if (!*p) - return((char *)NULL); + if (*p == c) + return ((char *)p); + if (*p == '\0') + return (NULL); } /* NOTREACHED */ } diff --git a/lib/libc/string/memccpy.3 b/lib/libc/string/memccpy.3 index 2e810faf00..b822ab822f 100644 --- a/lib/libc/string/memccpy.3 +++ b/lib/libc/string/memccpy.3 @@ -9,10 +9,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -30,7 +26,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)memccpy.3 8.1 (Berkeley) 6/9/93 -.\" $FreeBSD: src/lib/libc/string/memccpy.3,v 1.4.2.2 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/memccpy.3,v 1.7 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/memccpy.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" .Dd June 9, 1993 diff --git a/lib/libc/string/memccpy.c b/lib/libc/string/memccpy.c index b2950ccf60..df9d75c46f 100644 --- a/lib/libc/string/memccpy.c +++ b/lib/libc/string/memccpy.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -31,11 +27,10 @@ * SUCH DAMAGE. * * @(#)memccpy.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/memccpy.c,v 1.2.12.1 2001/07/09 23:30:03 obrien Exp $ + * $FreeBSD: src/lib/libc/string/memccpy.c,v 1.6 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/memccpy.c,v 1.5 2005/09/18 16:32:34 asmodai Exp $ */ -#include #include void * diff --git a/lib/libc/string/memchr.3 b/lib/libc/string/memchr.3 index 470aa9bcfd..e4f3f0e2fc 100644 --- a/lib/libc/string/memchr.3 +++ b/lib/libc/string/memchr.3 @@ -13,10 +13,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -34,10 +30,10 @@ .\" SUCH DAMAGE. .\" .\" @(#)memchr.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/memchr.3,v 1.3.2.4 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/memchr.3,v 1.10 2008/04/10 00:12:44 delphij Exp $ .\" $DragonFly: src/lib/libc/string/memchr.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" -.Dd June 4, 1993 +.Dd April 9, 2008 .Dt MEMCHR 3 .Os .Sh NAME @@ -49,6 +45,8 @@ .In string.h .Ft void * .Fn memchr "const void *b" "int c" "size_t len" +.Ft void * +.Fn memrchr "const void *b" "int c" "size_t len" .Sh DESCRIPTION The .Fn memchr @@ -58,15 +56,27 @@ locates the first occurrence of (converted to an unsigned char) in string .Fa b . +.Pp +The +.Fn memrchr +function behaves like +.Fn memchr , +except that it locates the last occurrence of +.Fa c +in string +.Fa b . .Sh RETURN VALUES The .Fn memchr -function -returns a pointer to the byte located, +and +.Fn memrchr +functions +return a pointer to the byte located, or NULL if no such byte exists within .Fa len bytes. .Sh SEE ALSO +.Xr memmem 3 , .Xr strchr 3 , .Xr strcspn 3 , .Xr strpbrk 3 , @@ -81,3 +91,15 @@ The function conforms to .St -isoC . +.Pp +The +.Fn memrchr +function is a GNU extension and conforms to no standard. +.Sh HISTORY +The +.Fn memrchr +function first appeared in GNU libc 2.1.91, this implementation +first appeared in +.Fx 8.0 , +coming from +.Ox 4.3 . diff --git a/lib/libc/string/memchr.c b/lib/libc/string/memchr.c index e8ba3922c1..c19c819795 100644 --- a/lib/libc/string/memchr.c +++ b/lib/libc/string/memchr.c @@ -13,10 +13,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -34,11 +30,10 @@ * SUCH DAMAGE. * * @(#)memchr.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/memchr.c,v 1.1.1.1.14.1 2001/07/09 23:30:03 obrien Exp $ + * $FreeBSD: src/lib/libc/string/memchr.c,v 1.5 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/memchr.c,v 1.5 2005/09/20 15:00:45 asmodai Exp $ */ -#include #include void * diff --git a/lib/libc/string/memcmp.3 b/lib/libc/string/memcmp.3 index 1b25bd4b8b..184ca16383 100644 --- a/lib/libc/string/memcmp.3 +++ b/lib/libc/string/memcmp.3 @@ -13,10 +13,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -34,7 +30,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)memcmp.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/memcmp.3,v 1.5.2.3 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/memcmp.3,v 1.9 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/memcmp.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" .Dd June 4, 1993 diff --git a/lib/libc/string/memcmp.c b/lib/libc/string/memcmp.c index e6bfe334aa..3578cb24cf 100644 --- a/lib/libc/string/memcmp.c +++ b/lib/libc/string/memcmp.c @@ -13,10 +13,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -34,11 +30,10 @@ * SUCH DAMAGE. * * @(#)memcmp.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/memcmp.c,v 1.1.1.1.14.1 2001/07/09 23:30:03 obrien Exp $ + * $FreeBSD: src/lib/libc/string/memcmp.c,v 1.5 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/memcmp.c,v 1.4 2005/09/18 16:32:34 asmodai Exp $ */ -#include #include /* diff --git a/lib/libc/string/memcpy.3 b/lib/libc/string/memcpy.3 index d77fa04715..fd700bf4f4 100644 --- a/lib/libc/string/memcpy.3 +++ b/lib/libc/string/memcpy.3 @@ -13,10 +13,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -34,7 +30,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)memcpy.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/memcpy.3,v 1.3.2.4 2002/02/11 02:10:47 dd Exp $ +.\" $FreeBSD: src/lib/libc/string/memcpy.3,v 1.8 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/memcpy.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" .Dd June 4, 1993 diff --git a/lib/libc/string/wcswidth.3 b/lib/libc/string/memmem.3 similarity index 54% copy from lib/libc/string/wcswidth.3 copy to lib/libc/string/memmem.3 index d75042657e..c55c525118 100644 --- a/lib/libc/string/wcswidth.3 +++ b/lib/libc/string/memmem.3 @@ -1,8 +1,4 @@ -.\" $NetBSD: wcswidth.3,v 1.2 2006/10/16 08:48:45 wiz Exp $ -.\" FreeBSD: src/lib/libc/string/wcswidth.3,v 1.2 2002/12/09 14:04:05 ru Exp -.\" -.\" Copyright (c) 2002 Tim J. Robbins -.\" All rights reserved. +.\" Copyright (c) 2005 Pascal Gloor .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions @@ -12,6 +8,9 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. +.\" 3. The name of the author may not be used to endorse or promote +.\" products derived from this software without specific prior written +.\" permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE @@ -25,41 +24,63 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $DragonFly: src/lib/libc/string/wcswidth.3,v 1.1 2008/05/31 04:51:55 swildner Exp $ +.\" $FreeBSD: src/lib/libc/string/memmem.3,v 1.3 2005/11/24 06:56:21 ru Exp $ .\" -.Dd May 30, 2008 -.Dt WCSWIDTH 3 +.Dd August 24, 2005 +.Dt MEMMEM 3 .Os .Sh NAME -.Nm wcswidth -.Nd "number of column positions in wide-character string" +.Nm memmem +.Nd "locate a byte substring in a byte string" .Sh LIBRARY .Lb libc .Sh SYNOPSIS -.In wchar.h -.Ft int -.Fn wcswidth "const wchar_t *pwcs" "size_t n" +.In string.h +.Ft "void *" +.Fo memmem +.Fa "const void *big" "size_t big_len" +.Fa "const void *little" "size_t little_len" +.Fc .Sh DESCRIPTION The -.Fn wcswidth -function determines the number of column positions required for the first -.Fa n -characters of -.Fa pwcs , -or until a nul wide character (L'\e0') is encountered. +.Fn memmem +function +locates the first occurrence of the byte string +.Fa little +in the byte string +.Fa big . .Sh RETURN VALUES -The -.Fn wcswidth -function returns 0 if -.Fa pwcs -is an empty string (L""), -\-1 if a non-printing wide character is encountered, -otherwise it returns the number of column positions occupied. +If +.Fa big_len +is smaller than +.Fa little_len , +if +.Fa little_len +is 0, if +.Fa big_len +is 0 or if +.Fa little +occurs nowhere in +.Fa big , +.Dv NULL +is returned; +otherwise a pointer to the first character of the first occurrence of +.Fa little +is returned. .Sh SEE ALSO -.Xr iswprint 3 , -.Xr wcwidth 3 -.Sh STANDARDS +.Xr memchr 3 , +.Xr strchr 3 , +.Xr strstr 3 +.Sh CONFORMING TO +.Fn memmem +is a GNU extension. +.Sh HISTORY The -.Fn wcswidth -function conforms to -.St -p1003.1-2001 . +.Fn memmem +function first appeared in +.Fx 6.0 . +.Sh AUTHORS +.An Pascal Gloor Aq pascal.gloor@spale.com +.Sh BUGS +This function was broken in Linux libc up to and including version 5.0.9 +and in GNU libc prior to version 2.1. diff --git a/lib/libc/locale/wcsxfrm.c b/lib/libc/string/memmem.c similarity index 55% rename from lib/libc/locale/wcsxfrm.c rename to lib/libc/string/memmem.c index 3220a67286..4e6d8cdad8 100644 --- a/lib/libc/locale/wcsxfrm.c +++ b/lib/libc/string/memmem.c @@ -1,9 +1,5 @@ -/* $NetBSD: src/lib/libc/locale/wcsxfrm.c,v 1.1 2003/03/02 22:18:16 tshiozak Exp $ */ -/* $DragonFly: src/lib/libc/locale/wcsxfrm.c,v 1.2 2005/11/20 09:18:37 swildner Exp $ */ - /*- - * Copyright (c)2003 Citrus Project, - * All rights reserved. + * Copyright (c) 2005 Pascal Gloor * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -13,6 +9,9 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior written + * permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE @@ -25,32 +24,41 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. + * + * $FreeBSD: src/lib/libc/string/memmem.c,v 1.1 2005/08/25 18:26:58 andre Exp $ */ -#include -#include +#include /* - * Compare strings with using collating information. + * Find the first occurrence of the byte string s in byte string l. */ -size_t -wcsxfrm(wchar_t *s1, const wchar_t *s2, size_t n) + +void * +memmem(const void *l, size_t l_len, const void *s, size_t s_len) { - size_t len; - - /* XXX: LC_COLLATE should be implemented. */ - - len = wcslen(s2); - if (len < n) - wcscpy(s1, s2); - else { - /* - * SUSv3 says: - * If the value returned is n or more, the contents - * of the array pointed to by ws1 are unspecified. - */ - /* thus, do nothing */; - } - - return(len); + char *cur, *last; + const char *cl = (const char *)l; + const char *cs = (const char *)s; + + /* we need something to compare */ + if (l_len == 0 || s_len == 0) + return (NULL); + + /* "s" must be smaller or equal to "l" */ + if (l_len < s_len) + return (NULL); + + /* special case where s_len == 1 */ + if (s_len == 1) + return (memchr(l, (int)*cs, l_len)); + + /* the last position where its possible to find "s" in "l" */ + last = (char *)cl + l_len - s_len; + + for (cur = (char *)cl; cur <= last; cur++) + if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0) + return (cur); + + return (NULL); } diff --git a/lib/libc/string/memmove.3 b/lib/libc/string/memmove.3 index 5bf21baf43..8263a0e21c 100644 --- a/lib/libc/string/memmove.3 +++ b/lib/libc/string/memmove.3 @@ -13,10 +13,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -34,7 +30,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)memmove.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/memmove.3,v 1.3.2.3 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/memmove.3,v 1.7 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/memmove.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" .Dd June 4, 1993 diff --git a/lib/libc/string/memrchr.c b/lib/libc/string/memrchr.c new file mode 100644 index 0000000000..05d24df66b --- /dev/null +++ b/lib/libc/string/memrchr.c @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2007 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * $OpenBSD: memrchr.c,v 1.2 2007/11/27 16:22:12 martynas Exp $ + * $FreeBSD: src/lib/libc/string/memrchr.c,v 1.1 2008/04/10 00:12:44 delphij Exp $ + */ + +#include + +/* + * Reverse memchr() + * Find the last occurrence of 'c' in the buffer 's' of size 'n'. + */ +void * +memrchr(const void *s, int c, size_t n) +{ + const unsigned char *cp; + + if (n != 0) { + cp = (unsigned char *)s + n; + do { + if (*(--cp) == (unsigned char)c) + return((void *)cp); + } while (--n != 0); + } + return(NULL); +} diff --git a/lib/libc/string/memset.3 b/lib/libc/string/memset.3 index 50358869d5..61c4193298 100644 --- a/lib/libc/string/memset.3 +++ b/lib/libc/string/memset.3 @@ -13,10 +13,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -34,7 +30,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)memset.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/memset.3,v 1.3.2.4 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/memset.3,v 1.8 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/memset.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" .Dd June 4, 1993 diff --git a/lib/libc/string/memset.c b/lib/libc/string/memset.c index 32e4e845fd..e0215b884a 100644 --- a/lib/libc/string/memset.c +++ b/lib/libc/string/memset.c @@ -13,10 +13,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -34,19 +30,20 @@ * SUCH DAMAGE. * * @(#)memset.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/memset.c,v 1.4.2.1 2001/07/09 23:30:03 obrien Exp $ + * $FreeBSD: src/lib/libc/string/memset.c,v 1.9 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/memset.c,v 1.4 2005/09/18 16:32:34 asmodai Exp $ */ #include #include -#include #define wsize sizeof(u_int) #define wmask (wsize - 1) #ifdef BZERO +#include + #define RETURN return #define VAL 0 #define WIDEVAL 0 @@ -54,6 +51,8 @@ void bzero(void *dst0, size_t length) #else +#include + #define RETURN return (dst0) #define VAL c0 #define WIDEVAL c diff --git a/lib/libc/string/rindex.3 b/lib/libc/string/rindex.3 deleted file mode 100644 index 1d7dd47059..0000000000 --- a/lib/libc/string/rindex.3 +++ /dev/null @@ -1,86 +0,0 @@ -.\" Copyright (c) 1990, 1991, 1993 -.\" The Regents of the University of California. All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" Chris Torek. -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. -.\" 4. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" @(#)rindex.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/rindex.3,v 1.3.2.3 2001/12/14 18:33:59 ru Exp $ -.\" $DragonFly: src/lib/libc/string/rindex.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ -.\" -.Dd June 4, 1993 -.Dt RINDEX 3 -.Os -.Sh NAME -.Nm rindex -.Nd locate character in string -.Sh LIBRARY -.Lb libc -.Sh SYNOPSIS -.In string.h -.Ft char * -.Fn rindex "const char *s" "int c" -.Sh DESCRIPTION -The -.Fn rindex -function -locates the last character -matching -.Fa c -(converted to a -.Em char ) -in the null-terminated string -.Fa s . -.Sh RETURN VALUES -A pointer to the character is returned if it is found; otherwise -NULL is returned. -If -.Fa c -is -.Ql \e0 , -.Fn rindex -locates the terminating -.Ql \e0 . -.Sh SEE ALSO -.Xr index 3 , -.Xr memchr 3 , -.Xr strchr 3 , -.Xr strcspn 3 , -.Xr strpbrk 3 , -.Xr strrchr 3 , -.Xr strsep 3 , -.Xr strspn 3 , -.Xr strstr 3 , -.Xr strtok 3 -.Sh HISTORY -A -.Fn rindex -function appeared in -.At v6 . diff --git a/lib/libc/string/rindex.c b/lib/libc/string/rindex.c index cdec16f122..c995a4f6dc 100644 --- a/lib/libc/string/rindex.c +++ b/lib/libc/string/rindex.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -31,27 +27,34 @@ * SUCH DAMAGE. * * @(#)rindex.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/rindex.c,v 1.1.1.1.14.1 2001/07/09 23:30:03 obrien Exp $ + * $FreeBSD: src/lib/libc/string/rindex.c,v 1.8 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/rindex.c,v 1.4 2005/09/18 16:32:34 asmodai Exp $ */ #include + +#ifdef STRRCHR #include char * -#ifdef STRRCHR -strrchr(const char *p, int ch) +strrchr #else -rindex(const char *p, int ch) +#include + +char * +rindex #endif +(const char *p, int ch) { char *save; + char c; + c = ch; for (save = NULL;; ++p) { - if (*p == ch) + if (*p == c) save = (char *)p; - if (!*p) - return(save); + if (*p == '\0') + return (save); } /* NOTREACHED */ } diff --git a/include/strings.h b/lib/libc/string/stpcpy.c similarity index 80% copy from include/strings.h copy to lib/libc/string/stpcpy.c index 2a63fa439b..f1dc4baf12 100644 --- a/include/strings.h +++ b/lib/libc/string/stpcpy.c @@ -1,5 +1,7 @@ -/*- - * Copyright (c) 1990, 1993 +/* + * Copyright (c) 1999 + * David E. O'Brien + * Copyright (c) 1988, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -10,11 +12,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -30,7 +28,16 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * @(#)strings.h 8.1 (Berkeley) 6/2/93 + * @(#)strcpy.c 8.1 (Berkeley) 6/4/93 + * $FreeBSD: src/lib/libc/string/stpcpy.c,v 1.1 2002/10/03 19:39:20 obrien Exp $ */ #include + +char * +stpcpy(char *to, const char *from) +{ + + for (; (*to = *from); ++from, ++to); + return(to); +} diff --git a/lib/libc/string/strcasecmp.3 b/lib/libc/string/strcasecmp.3 index 8067c82dec..642f58357d 100644 --- a/lib/libc/string/strcasecmp.3 +++ b/lib/libc/string/strcasecmp.3 @@ -11,10 +11,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -32,7 +28,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)strcasecmp.3 8.1 (Berkeley) 6/9/93 -.\" $FreeBSD: src/lib/libc/string/strcasecmp.3,v 1.6.2.3 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/strcasecmp.3,v 1.12 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/strcasecmp.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" .Dd June 9, 1993 @@ -45,7 +41,7 @@ .Sh LIBRARY .Lb libc .Sh SYNOPSIS -.In string.h +.In strings.h .Ft int .Fn strcasecmp "const char *s1" "const char *s2" .Ft int @@ -96,3 +92,10 @@ and .Fn strncasecmp functions first appeared in .Bx 4.4 . +Their prototypes existed previously in +.In string.h +before they were moved to +.In strings.h +for +.St -p1003.1-2001 +compliance. diff --git a/lib/libc/string/strcasecmp.c b/lib/libc/string/strcasecmp.c index 95c08dac1f..2c7af34f2a 100644 --- a/lib/libc/string/strcasecmp.c +++ b/lib/libc/string/strcasecmp.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -31,12 +27,12 @@ * SUCH DAMAGE. * * @(#)strcasecmp.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/strcasecmp.c,v 1.2.8.1 2001/07/09 23:30:03 obrien Exp $ + * $FreeBSD: src/lib/libc/string/strcasecmp.c,v 1.7 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/strcasecmp.c,v 1.8 2005/04/29 16:12:52 dillon Exp $ */ #include -#include +#include #include int diff --git a/lib/libc/string/strcasestr.c b/lib/libc/string/strcasestr.c index f9b96c2eec..fa3c832a05 100644 --- a/lib/libc/string/strcasestr.c +++ b/lib/libc/string/strcasestr.c @@ -13,10 +13,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -33,7 +29,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $FreeBSD: src/lib/libc/string/strcasestr.c,v 1.2.2.1 2001/12/25 00:36:53 ache Exp $ + * $FreeBSD: src/lib/libc/string/strcasestr.c,v 1.4 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/strcasestr.c,v 1.4 2005/09/18 16:32:34 asmodai Exp $ */ diff --git a/lib/libc/string/strcat.3 b/lib/libc/string/strcat.3 index 36d6985451..237d0f21e9 100644 --- a/lib/libc/string/strcat.3 +++ b/lib/libc/string/strcat.3 @@ -13,10 +13,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -34,7 +30,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)strcat.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/strcat.3,v 1.5.2.3 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/strcat.3,v 1.15 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/strcat.3,v 1.3 2007/10/19 20:46:53 swildner Exp $ .\" .Dd June 4, 1993 @@ -49,9 +45,9 @@ .Sh SYNOPSIS .In string.h .Ft char * -.Fn strcat "char *s" "const char *append" +.Fn strcat "char * restrict s" "const char * restrict append" .Ft char * -.Fn strncat "char *s" "const char *append" "size_t count" +.Fn strncat "char * restrict s" "const char * restrict append" "size_t count" .Sh DESCRIPTION The .Fn strcat @@ -85,6 +81,64 @@ and functions return the pointer .Fa s . +.Sh SECURITY CONSIDERATIONS +The +.Fn strcat +function is easily misused in a manner +which enables malicious users to arbitrarily change +a running program's functionality through a buffer overflow attack. +(See +the FSA.) +.Pp +Avoid using +.Fn strcat . +Instead, use +.Fn strncat +or +.Fn strlcat +and ensure that no more characters are copied to the destination buffer +than it can hold. +.Pp +Note that +.Fn strncat +can also be problematic. +It may be a security concern for a string to be truncated at all. +Since the truncated string will not be as long as the original, +it may refer to a completely different resource +and usage of the truncated resource +could result in very incorrect behavior. +Example: +.Bd -literal +void +foo(const char *arbitrary_string) +{ + char onstack[8]; + +#if defined(BAD) + /* + * This first strcat is bad behavior. Do not use strcat! + */ + strcat(onstack, arbitrary_string); /* BAD! */ +#elif defined(BETTER) + /* + * The following two lines demonstrate better use of + * strncat(). + */ + strncat(onstack, arbitrary_string, + sizeof(onstack) - strlen(onstack) - 1); +#elif defined(BEST) + /* + * These lines are even more robust due to testing for + * truncation. + */ + if (strlen(arbitrary_string) + 1 > + sizeof(onstack) - strlen(onstack)) + err(1, "onstack would be truncated"); + strncat(onstack, arbitrary_string, + sizeof(onstack) - strlen(onstack) - 1); +#endif +} +.Ed .Sh SEE ALSO .Xr bcopy 3 , .Xr memccpy 3 , diff --git a/lib/libc/string/strcat.c b/lib/libc/string/strcat.c index 41cc881c7d..421a4f28fa 100644 --- a/lib/libc/string/strcat.c +++ b/lib/libc/string/strcat.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -31,14 +27,14 @@ * SUCH DAMAGE. * * @(#)strcat.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/strcat.c,v 1.2.2.1 2001/07/09 23:30:03 obrien Exp $ + * $FreeBSD: src/lib/libc/string/strcat.c,v 1.8 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/strcat.c,v 1.4 2005/09/18 16:32:34 asmodai Exp $ */ #include char * -strcat(char *s, const char *append) +strcat(char * __restrict s, const char * __restrict append) { char *save = s; diff --git a/lib/libc/string/strchr.3 b/lib/libc/string/strchr.3 index 2a5a390b5e..98343297ab 100644 --- a/lib/libc/string/strchr.3 +++ b/lib/libc/string/strchr.3 @@ -13,10 +13,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -34,56 +30,68 @@ .\" SUCH DAMAGE. .\" .\" @(#)strchr.3 8.2 (Berkeley) 4/19/94 -.\" $FreeBSD: src/lib/libc/string/strchr.3,v 1.4.2.4 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/strchr.3,v 1.13 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/strchr.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" .Dd April 19, 1994 .Dt STRCHR 3 .Os .Sh NAME -.Nm strchr +.Nm strchr , +.Nm strrchr .Nd locate character in string .Sh LIBRARY .Lb libc .Sh SYNOPSIS .In string.h -.Ft char * +.Ft "char *" .Fn strchr "const char *s" "int c" +.Ft "char *" +.Fn strrchr "const char *s" "int c" .Sh DESCRIPTION The .Fn strchr function locates the first occurrence of -.Ar c +.Fa c +(converted to a +.Vt char ) in the string pointed to by -.Ar s . -The terminating -.Dv NUL -character is considered part of the string. -If +.Fa s . +The terminating null character is considered part of the string; +therefore if .Fa c is .Ql \e0 , -.Fn strchr -locates the terminating +the functions locate the terminating .Ql \e0 . +.Pp +The +.Fn strrchr +function is identical to +.Fn strchr +except it locates the last occurrence of +.Fa c . .Sh RETURN VALUES -The function +The functions .Fn strchr -returns a pointer to the located character, or +and +.Fn strrchr +return a pointer to the located character, or .Dv NULL if the character does not appear in the string. .Sh SEE ALSO .Xr memchr 3 , +.Xr memmem 3 , .Xr strcspn 3 , .Xr strpbrk 3 , -.Xr strrchr 3 , .Xr strsep 3 , .Xr strspn 3 , .Xr strstr 3 , .Xr strtok 3 .Sh STANDARDS -The +The functions .Fn strchr -function -conforms to +and +.Fn strrchr +conform to .St -isoC . diff --git a/lib/libc/string/strcmp.3 b/lib/libc/string/strcmp.3 index b4872d5baa..78876a36ae 100644 --- a/lib/libc/string/strcmp.3 +++ b/lib/libc/string/strcmp.3 @@ -13,10 +13,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -34,7 +30,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)strcmp.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/strcmp.3,v 1.6.2.3 2001/10/15 16:45:38 mike Exp $ +.\" $FreeBSD: src/lib/libc/string/strcmp.3,v 1.11 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/strcmp.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" .Dd October 11, 2001 diff --git a/lib/libc/string/strcmp.c b/lib/libc/string/strcmp.c index dcfaf4b229..6c67b787be 100644 --- a/lib/libc/string/strcmp.c +++ b/lib/libc/string/strcmp.c @@ -13,10 +13,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -34,11 +30,10 @@ * SUCH DAMAGE. * * @(#)strcmp.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/strcmp.c,v 1.2.8.1 2001/07/09 23:30:05 obrien Exp $ + * $FreeBSD: src/lib/libc/string/strcmp.c,v 1.6 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/strcmp.c,v 1.4 2005/09/18 16:32:34 asmodai Exp $ */ -#include #include /* diff --git a/lib/libc/string/strcoll.3 b/lib/libc/string/strcoll.3 index f1bd41ac7b..c07c24fd47 100644 --- a/lib/libc/string/strcoll.3 +++ b/lib/libc/string/strcoll.3 @@ -13,10 +13,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -34,7 +30,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)strcoll.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/strcoll.3,v 1.8.2.3 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/strcoll.3,v 1.14 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/strcoll.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" .Dd June 4, 1993 @@ -57,17 +53,21 @@ lexicographically compares the null-terminated strings .Fa s1 and .Fa s2 -according to the current locale collation if any, otherwise call -.Fa strcmp , +according to the current locale collation and returns an integer greater than, equal to, or less than 0, according as .Fa s1 is greater than, equal to, or less than .Fa s2 . +If information about the current locale collation is not available, +the value of +.Fn strcmp s1 s2 +is returned. .Sh SEE ALSO .Xr setlocale 3 , .Xr strcmp 3 , -.Xr strxfrm 3 +.Xr strxfrm 3 , +.Xr wcscoll 3 .Sh STANDARDS The .Fn strcoll diff --git a/lib/libc/string/strcpy.3 b/lib/libc/string/strcpy.3 index b0bdc94767..9f508786e9 100644 --- a/lib/libc/string/strcpy.3 +++ b/lib/libc/string/strcpy.3 @@ -13,10 +13,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -34,7 +30,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)strcpy.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/strcpy.3,v 1.4.2.5 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/strcpy.3,v 1.26 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/strcpy.3,v 1.3 2005/08/05 22:35:10 swildner Exp $ .\" .Dd August 9, 2001 @@ -49,40 +45,48 @@ .Sh SYNOPSIS .In string.h .Ft char * -.Fn strcpy "char *dst" "const char *src" +.Fn stpcpy "char *dst" "const char *src" .Ft char * -.Fn strncpy "char *dst" "const char *src" "size_t len" +.Fn strcpy "char * restrict dst" "const char * restrict src" +.Ft char * +.Fn strncpy "char * restrict dst" "const char * restrict src" "size_t len" .Sh DESCRIPTION The +.Fn stpcpy +and .Fn strcpy -function -copies the string +functions +copy the string .Fa src to .Fa dst (including the terminating .Ql \e0 -character). +character.) .Pp The .Fn strncpy -function copies not more than +function copies at most .Fa len characters from .Fa src into -.Fa dst , -appending -.Ql \e0 -characters if +.Fa dst . +If .Fa src is less than .Fa len -characters long, and -.Em not -terminating +characters long, +the remainder of .Fa dst -otherwise. +is filled with +.Ql \e0 +characters. +Otherwise, +.Fa dst +is +.Em not +terminated. .Sh RETURN VALUES The .Fn strcpy @@ -91,6 +95,12 @@ and functions return .Fa dst . +The +.Fn stpcpy +function returns a pointer to the terminating +.Ql \e0 +character of +.Fa dst . .Sh EXAMPLES The following sets .Va chararray @@ -118,7 +128,7 @@ Note that it does terminate .Va chararray because the length of the source string is greater than or equal -to the length parameter. +to the length argument. .Pp The following copies as many characters from .Va input @@ -151,6 +161,16 @@ Note that because .Xr strlcpy 3 is not defined in any standards, it should only be used when portability is not a concern. +.Sh SECURITY CONSIDERATIONS +The +.Fn strcpy +function is easily misused in a manner which enables malicious users +to arbitrarily change a running program's functionality through a +buffer overflow attack. +(See +the FSA +and +.Sx EXAMPLES . ) .Sh SEE ALSO .Xr bcopy 3 , .Xr memccpy 3 , @@ -165,3 +185,16 @@ and functions conform to .St -isoC . +The +.Fn stpcpy +function is an MS-DOS and GNUism. +The +.Fn stpcpy +function +conforms to no standard. +.Sh HISTORY +The +.Fn stpcpy +function first appeared in +.Fx 4.4 , +coming from 1998-vintage Linux. diff --git a/lib/libc/string/strcpy.c b/lib/libc/string/strcpy.c index 2777b2ce09..61073a6166 100644 --- a/lib/libc/string/strcpy.c +++ b/lib/libc/string/strcpy.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -31,15 +27,14 @@ * SUCH DAMAGE. * * @(#)strcpy.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/strcpy.c,v 1.2.2.1 2001/07/09 23:30:06 obrien Exp $ + * $FreeBSD: src/lib/libc/string/strcpy.c,v 1.8 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/strcpy.c,v 1.4 2005/09/18 16:32:34 asmodai Exp $ */ -#include #include char * -strcpy(char *to, const char *from) +strcpy(char * __restrict to, const char * __restrict from) { char *save = to; diff --git a/lib/libc/string/strcspn.3 b/lib/libc/string/strcspn.3 index 412aef93d0..4789ec4cc8 100644 --- a/lib/libc/string/strcspn.3 +++ b/lib/libc/string/strcspn.3 @@ -13,10 +13,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -34,7 +30,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)strcspn.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/strcspn.3,v 1.3.2.4 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/strcspn.3,v 1.9 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/strcspn.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" .Dd June 4, 1993 @@ -64,6 +60,13 @@ spans the .Em complement of .Fa charset ) . +In other words, it computes the string array index in +.Fa s +of the first character of +.Fa s +which is also in +.Fa charset , +else the index of the first null character. .Sh RETURN VALUES The .Fn strcspn diff --git a/lib/libc/string/strdup.3 b/lib/libc/string/strdup.3 index bec865dcd2..b2ecea6752 100644 --- a/lib/libc/string/strdup.3 +++ b/lib/libc/string/strdup.3 @@ -9,10 +9,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -30,14 +26,15 @@ .\" SUCH DAMAGE. .\" .\" @(#)strdup.3 8.1 (Berkeley) 6/9/93 -.\" $FreeBSD: src/lib/libc/string/strdup.3,v 1.7.2.2 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/strdup.3,v 1.13 2008/12/09 09:24:25 maxim Exp $ .\" $DragonFly: src/lib/libc/string/strdup.3,v 1.5 2004/12/26 12:37:08 swildner Exp $ .\" -.Dd June 9, 1993 +.Dd December 5, 2008 .Dt STRDUP 3 .Os .Sh NAME -.Nm strdup +.Nm strdup , +.Nm strndup .Nd save a copy of a string .Sh LIBRARY .Lb libc @@ -45,6 +42,8 @@ .In string.h .Ft char * .Fn strdup "const char *str" +.Ft char * +.Fn strndup "const char *str" "size_t len" .Sh DESCRIPTION The .Fn strdup @@ -57,7 +56,20 @@ The pointer may subsequently be used as an argument to the function .Xr free 3 . .Pp -If insufficient memory is available, NULL is returned. +If insufficient memory is available, NULL is returned and +.Va errno +is set to +.Er ENOMEM . +.Pp +The +.Fn strndup +function copies at most +.Fa len +characters from the string +.Fa str +always +.Dv NUL +terminating the copied string. .Sh ERRORS The .Nm @@ -79,3 +91,7 @@ The .Fn strdup function first appeared in .Bx 4.4 . +The +.Fn strndup +function was added in +.Fx 8.0 . diff --git a/lib/libc/string/strdup.c b/lib/libc/string/strdup.c index a8dae4e1b7..7e66a630e2 100644 --- a/lib/libc/string/strdup.c +++ b/lib/libc/string/strdup.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -31,7 +27,7 @@ * SUCH DAMAGE. * * @(#)strdup.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/strdup.c,v 1.2.14.1 2001/07/09 23:30:06 obrien Exp $ + * $FreeBSD: src/lib/libc/string/strdup.c,v 1.5 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/strdup.c,v 1.3 2005/09/18 16:32:34 asmodai Exp $ */ diff --git a/lib/libc/string/strerror.3 b/lib/libc/string/strerror.3 index 9d70e20126..206608a81f 100644 --- a/lib/libc/string/strerror.3 +++ b/lib/libc/string/strerror.3 @@ -13,10 +13,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -34,10 +30,10 @@ .\" SUCH DAMAGE. .\" .\" @(#)strerror.3 8.1 (Berkeley) 6/9/93 -.\" $FreeBSD: src/lib/libc/string/strerror.3,v 1.7.2.7 2003/01/17 13:39:50 mike Exp $ +.\" $FreeBSD: src/lib/libc/string/strerror.3,v 1.24 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/strerror.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" -.Dd December 19, 2002 +.Dd October 12, 2004 .Dt STRERROR 3 .Os .Sh NAME @@ -103,18 +99,17 @@ a colon and space .Pq Dq Li ":\ " ; otherwise, only the error message string is printed. .Pp -If -.Fa errnum -is not a recognized error number, -.Fn strerror -returns an error message string containing +If the error number is not recognized, these functions return an error message +string containing .Dq Li "Unknown error:\ " -followed by the error number in decimal, while +followed by the error number in decimal. +The +.Fn strerror +and .Fn strerror_r -leaves -.Fa strerrbuf -unchanged and returns -.Er EINVAL . +functions return +.Er EINVAL +as a warning. Error numbers recognized by this implementation fall in the range 0 < .Fa errnum diff --git a/lib/libc/string/strerror.c b/lib/libc/string/strerror.c index 45b020685e..bb89f5773c 100644 --- a/lib/libc/string/strerror.c +++ b/lib/libc/string/strerror.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -30,29 +26,36 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $FreeBSD: src/lib/libc/string/strerror.c,v 1.2.14.3 2003/01/17 13:39:32 mike Exp $ + * @(#)strerror.c 8.1 (Berkeley) 6/4/93 + * $FreeBSD: src/lib/libc/string/strerror.c,v 1.16 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/strerror.c,v 1.2 2003/06/17 04:26:46 dillon Exp $ */ +#if defined(NLS) +#include +#endif + +#include #include -#include #include +#include -#define UPREFIX "Unknown error: " +#define UPREFIX "Unknown error" /* * Define a buffer size big enough to describe a 64-bit signed integer * converted to ASCII decimal (19 bytes), with an optional leading sign - * (1 byte); finally, we get the prefix and a trailing NUL from UPREFIX. + * (1 byte); finally, we get the prefix, delimiter (": ") and a trailing + * NUL from UPREFIX. */ -#define EBUFSIZE (20 + sizeof(UPREFIX)) +#define EBUFSIZE (20 + 2 + sizeof(UPREFIX)) /* * Doing this by hand instead of linking with stdio(3) avoids bloat for * statically linked binaries. */ static void -errstr(int num, char *buf, size_t len) +errstr(int num, char *uprefix, char *buf, size_t len) { char *t; unsigned int uerr; @@ -66,31 +69,56 @@ errstr(int num, char *buf, size_t len) } while (uerr /= 10); if (num < 0) *--t = '-'; - strlcpy(buf, UPREFIX, len); + *--t = ' '; + *--t = ':'; + strlcpy(buf, uprefix, len); strlcat(buf, t, len); } int strerror_r(int errnum, char *strerrbuf, size_t buflen) { + int retval = 0; +#if defined(NLS) + int saved_errno = errno; + nl_catd catd; + catd = catopen("libc", NL_CAT_LOCALE); +#endif if (errnum < 1 || errnum >= sys_nerr) { - errstr(errnum, strerrbuf, buflen); - return (EINVAL); + errstr(errnum, +#if defined(NLS) + catgets(catd, 1, 0xffff, UPREFIX), +#else + UPREFIX, +#endif + strerrbuf, buflen); + retval = EINVAL; + } else { + if (strlcpy(strerrbuf, +#if defined(NLS) + catgets(catd, 1, errnum, sys_errlist[errnum]), +#else + sys_errlist[errnum], +#endif + buflen) >= buflen) + retval = ERANGE; } - if (strlcpy(strerrbuf, sys_errlist[errnum], buflen) >= buflen) - return (ERANGE); - return (0); + +#if defined(NLS) + catclose(catd); + errno = saved_errno; +#endif + + return (retval); } char * strerror(int num) { - static char ebuf[EBUFSIZE]; + static char ebuf[NL_TEXTMAX]; - if (num > 0 && num < sys_nerr) - return ((char *)sys_errlist[num]); + if (strerror_r(num, ebuf, sizeof(ebuf)) != 0) errno = EINVAL; - errstr(num, ebuf, sizeof(ebuf)); return (ebuf); } diff --git a/lib/libc/string/string.3 b/lib/libc/string/string.3 index a24d15cfb3..01ae38699b 100644 --- a/lib/libc/string/string.3 +++ b/lib/libc/string/string.3 @@ -11,10 +11,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -32,13 +28,14 @@ .\" SUCH DAMAGE. .\" .\" @(#)string.3 8.2 (Berkeley) 12/11/93 -.\" $FreeBSD: src/lib/libc/string/string.3,v 1.6.2.4 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/string.3,v 1.14 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/string.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" .Dd December 11, 1993 .Dt STRING 3 .Os .Sh NAME +.Nm stpcpy , .Nm strcat , .Nm strncat , .Nm strchr , @@ -65,6 +62,8 @@ .Sh SYNOPSIS .In string.h .Ft char * +.Fn stpcpy "char *dst" "const char *src" +.Ft char * .Fn strcat "char *s" "const char * append" .Ft char * .Fn strncat "char *s" "const char *append" "size_t count" @@ -121,6 +120,7 @@ for size limitations. .Xr bstring 3 , .Xr index 3 , .Xr rindex 3 , +.Xr stpcpy 3 , .Xr strcasecmp 3 , .Xr strcat 3 , .Xr strchr 3 , @@ -148,7 +148,6 @@ The .Fn strerror , .Fn strlen , .Fn strpbrk , -.Fn strsep , .Fn strspn , .Fn strcspn , .Fn strstr , diff --git a/lib/libc/string/strlcat.c b/lib/libc/string/strlcat.c index 742354367b..12a14d78b7 100644 --- a/lib/libc/string/strlcat.c +++ b/lib/libc/string/strlcat.c @@ -1,31 +1,20 @@ /* * Copyright (c) 1998 Todd C. Miller - * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. * - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY - * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL - * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * - * $OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp $ - * $FreeBSD: src/lib/libc/string/strlcat.c,v 1.2.4.2 2001/07/09 23:30:06 obrien Exp $ + * $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ + * $FreeBSD: src/lib/libc/string/strlcat.c,v 1.11 2009/01/12 06:09:34 delphij Exp $ * $DragonFly: src/lib/libc/string/strlcat.c,v 1.4 2004/12/18 21:37:43 asmodai Exp $ */ @@ -36,8 +25,8 @@ * Appends src to string dst of size siz (unlike strncat, siz is the * full size of dst, not space left). At most siz-1 characters * will be copied. Always NUL terminates (unless siz <= strlen(dst)). - * Returns strlen(initial dst) + strlen(src); if retval >= siz, - * truncation occurred. + * Returns strlen(src) + MIN(siz, strlen(initial dst)). + * If retval >= siz, truncation occurred. */ size_t strlcat(char *dst, const char *src, size_t siz) diff --git a/lib/libc/string/strlcpy.3 b/lib/libc/string/strlcpy.3 index fbcd4cf8ed..9932fded37 100644 --- a/lib/libc/string/strlcpy.3 +++ b/lib/libc/string/strlcpy.3 @@ -1,18 +1,18 @@ -.\" $OpenBSD: strlcpy.3,v 1.5 1999/06/06 15:17:32 aaron Exp $ +.\" $OpenBSD: strlcpy.3,v 1.19 2007/05/31 19:19:32 jmc Exp $ .\" -.\" Copyright (c) 1998 Todd C. Miller -.\" All rights reserved. +.\" Copyright (c) 1998, 2000 Todd C. Miller .\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. The name of the author may not be used to endorse or promote products -.\" derived from this software without specific prior written permission. +.\" Permission to use, copy, modify, and distribute this software for any +.\" purpose with or without fee is hereby granted, provided that the above +.\" copyright notice and this permission notice appear in all copies. +.\" +.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" .\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY @@ -25,7 +25,7 @@ .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF .\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" -.\" $FreeBSD: src/lib/libc/string/strlcpy.3,v 1.4.2.8 2002/01/19 12:29:40 yar Exp $ +.\" $FreeBSD: src/lib/libc/string/strlcpy.3,v 1.14 2009/01/12 06:10:48 delphij Exp $ .\" $DragonFly: src/lib/libc/string/strlcpy.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" .Dd June 22, 1998 @@ -48,7 +48,8 @@ The .Fn strlcpy and .Fn strlcat -functions copy and concatenate strings respectively. They are designed +functions copy and concatenate strings respectively. +They are designed to be safer, more consistent, and less error prone replacements for .Xr strncpy 3 and @@ -64,7 +65,7 @@ is larger than 0 or, in the case of .Fn strlcat , as long as there is at least one byte free in .Fa dst ) . -Note that you should include a byte for the NUL in +Note that a byte for the NUL should be included in .Fa size . Also note that .Fn strlcpy @@ -109,7 +110,8 @@ The and .Fn strlcat functions return the total length of the string they tried to -create. For +create. +For .Fn strlcpy that means the length of .Fa src . @@ -120,7 +122,7 @@ that means the initial length of plus the length of .Fa src . -While this may seem somewhat confusing it was done to make +While this may seem somewhat confusing, it was done to make truncation detection simple. .Pp Note however, that if @@ -167,8 +169,8 @@ if (strlcat(pname, file, sizeof(pname)) >= sizeof(pname)) goto toolong; .Ed .Pp -Since we know how many characters we copied the first time, we can -speed things up a bit by using a copy instead of an append: +Since it is known how many characters were copied the first time, things +can be sped up a bit by using a copy instead of an append .Bd -literal -offset indent char *dir, *file, pname[MAXPATHLEN]; size_t n; @@ -193,6 +195,7 @@ As a matter of fact, the first version of this manual page got it wrong. .Xr strncat 3 , .Xr strncpy 3 .Sh HISTORY +The .Fn strlcpy and .Fn strlcat diff --git a/lib/libc/string/strlcpy.c b/lib/libc/string/strlcpy.c index e19d0ecd2f..b5ada2d7fe 100644 --- a/lib/libc/string/strlcpy.c +++ b/lib/libc/string/strlcpy.c @@ -1,33 +1,20 @@ -/* $OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $ */ - /* * Copyright (c) 1998 Todd C. Miller - * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. * - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY - * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL - * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * - * $OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $ - * $FreeBSD: src/lib/libc/string/strlcpy.c,v 1.2.4.1 2001/07/09 23:30:06 obrien Exp $ + * $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ + * $FreeBSD: src/lib/libc/string/strlcpy.c,v 1.10 2008/10/19 10:11:35 delphij Exp $ * $DragonFly: src/lib/libc/string/strlcpy.c,v 1.4 2005/09/18 16:32:34 asmodai Exp $ */ @@ -39,18 +26,19 @@ * will be copied. Always NUL terminates (unless siz == 0). * Returns strlen(src); if retval >= siz, truncation occurred. */ -size_t strlcpy(char *dst, const char *src, size_t siz) +size_t +strlcpy(char *dst, const char *src, size_t siz) { char *d = dst; const char *s = src; size_t n = siz; /* Copy as many bytes as will fit */ - if (n != 0 && --n != 0) { - do { - if ((*d++ = *s++) == 0) + if (n != 0) { + while (--n != 0) { + if ((*d++ = *s++) == '\0') break; - } while (--n != 0); + } } /* Not enough room in dst, add NUL and traverse rest of src */ diff --git a/lib/libc/string/strlen.3 b/lib/libc/string/strlen.3 index ca1d72d442..fcfd854b94 100644 --- a/lib/libc/string/strlen.3 +++ b/lib/libc/string/strlen.3 @@ -13,10 +13,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -34,7 +30,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)strlen.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/strlen.3,v 1.3.2.3 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/strlen.3,v 1.7 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/strlen.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" .Dd June 4, 1993 diff --git a/lib/libc/string/strlen.c b/lib/libc/string/strlen.c index 3afd658d22..4021af9398 100644 --- a/lib/libc/string/strlen.c +++ b/lib/libc/string/strlen.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -31,11 +27,10 @@ * SUCH DAMAGE. * * @(#)strlen.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/strlen.c,v 1.1.1.1.14.1 2001/07/09 23:30:07 obrien Exp $ + * $FreeBSD: src/lib/libc/string/strlen.c,v 1.5 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/strlen.c,v 1.4 2005/09/18 16:32:34 asmodai Exp $ */ -#include #include size_t diff --git a/lib/libc/string/strmode.3 b/lib/libc/string/strmode.3 index fdc76fd847..7a603ec5b4 100644 --- a/lib/libc/string/strmode.3 +++ b/lib/libc/string/strmode.3 @@ -9,10 +9,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -30,7 +26,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)strmode.3 8.3 (Berkeley) 7/28/94 -.\" $FreeBSD: src/lib/libc/string/strmode.3,v 1.6.2.2 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/strmode.3,v 1.10 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/strmode.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" .Dd July 28, 1994 @@ -136,11 +132,6 @@ None of the above apply. The last character is a plus sign ``+'' if any there are any alternate or additional access control methods associated with the inode, otherwise it will be a space. -.Sh RETURN VALUES -The -.Fn strmode -function -always returns 0. .Sh SEE ALSO .Xr chmod 1 , .Xr find 1 , diff --git a/lib/libc/string/strmode.c b/lib/libc/string/strmode.c index d01f0af736..4711962739 100644 --- a/lib/libc/string/strmode.c +++ b/lib/libc/string/strmode.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -31,7 +27,7 @@ * SUCH DAMAGE. * * @(#)strmode.c 8.3 (Berkeley) 8/15/94 - * $FreeBSD: src/lib/libc/string/strmode.c,v 1.1.1.2.6.1 2001/07/09 23:30:07 obrien Exp $ + * $FreeBSD: src/lib/libc/string/strmode.c,v 1.5 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/strmode.c,v 1.4 2005/09/18 16:32:34 asmodai Exp $ */ diff --git a/lib/libc/string/strncat.c b/lib/libc/string/strncat.c index b44143f68f..f238bb354b 100644 --- a/lib/libc/string/strncat.c +++ b/lib/libc/string/strncat.c @@ -13,10 +13,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -34,11 +30,10 @@ * SUCH DAMAGE. * * @(#)strncat.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/strncat.c,v 1.1.1.1.14.1 2001/07/09 23:30:07 obrien Exp $ + * $FreeBSD: src/lib/libc/string/strncat.c,v 1.7 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/strncat.c,v 1.4 2005/09/18 16:32:34 asmodai Exp $ */ -#include #include /* @@ -46,7 +41,7 @@ * are written at dst (at most n+1 bytes being appended). Return dst. */ char * -strncat(char *dst, const char *src, size_t n) +strncat(char * __restrict dst, const char * __restrict src, size_t n) { if (n != 0) { char *d = dst; diff --git a/lib/libc/string/strncmp.c b/lib/libc/string/strncmp.c index 1f388319c9..32d3957b74 100644 --- a/lib/libc/string/strncmp.c +++ b/lib/libc/string/strncmp.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -31,11 +27,10 @@ * SUCH DAMAGE. * * @(#)strncmp.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/strncmp.c,v 1.2.8.1 2001/07/09 23:30:07 obrien Exp $ + * $FreeBSD: src/lib/libc/string/strncmp.c,v 1.6 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/strncmp.c,v 1.4 2005/09/18 16:32:34 asmodai Exp $ */ -#include #include int diff --git a/lib/libc/string/strncpy.c b/lib/libc/string/strncpy.c index cdd58d25bc..6eb0dfacec 100644 --- a/lib/libc/string/strncpy.c +++ b/lib/libc/string/strncpy.c @@ -13,10 +13,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -34,11 +30,10 @@ * SUCH DAMAGE. * * @(#)strncpy.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/strncpy.c,v 1.1.1.1.14.1 2001/07/09 23:30:07 obrien Exp $ + * $FreeBSD: src/lib/libc/string/strncpy.c,v 1.7 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/strncpy.c,v 1.4 2005/09/18 16:32:34 asmodai Exp $ */ -#include #include /* @@ -46,7 +41,7 @@ * Return dst. */ char * -strncpy(char *dst, const char *src, size_t n) +strncpy(char * __restrict dst, const char * __restrict src, size_t n) { if (n != 0) { char *d = dst; diff --git a/include/strings.h b/lib/libc/string/strndup.c similarity index 77% copy from include/strings.h copy to lib/libc/string/strndup.c index 2a63fa439b..55a8b72045 100644 --- a/include/strings.h +++ b/lib/libc/string/strndup.c @@ -1,5 +1,5 @@ -/*- - * Copyright (c) 1990, 1993 +/* + * Copyright (c) 1988, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -30,7 +26,26 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * @(#)strings.h 8.1 (Berkeley) 6/2/93 + * $NetBSD: strndup.c,v 1.3 2007/01/14 23:41:24 cbiere Exp $ + * $FreeBSD: src/lib/libc/string/strndup.c,v 1.1 2008/12/06 09:37:54 kib Exp $ */ +#include +#include #include + +char * +strndup(const char *str, size_t n) +{ + size_t len; + char *copy; + + for (len = 0; len < n && str[len]; len++) + continue; + + if ((copy = malloc(len + 1)) == NULL) + return (NULL); + memcpy(copy, str, len); + copy[len] = '\0'; + return (copy); +} diff --git a/lib/libc/string/strnstr.c b/lib/libc/string/strnstr.c index 0482b1b96f..85c725f8ef 100644 --- a/lib/libc/string/strnstr.c +++ b/lib/libc/string/strnstr.c @@ -14,10 +14,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -35,7 +31,7 @@ * SUCH DAMAGE. * * @(#)strstr.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/strnstr.c,v 1.2.2.1 2001/12/09 06:50:03 mike Exp $ + * $FreeBSD: src/lib/libc/string/strnstr.c,v 1.4 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/strnstr.c,v 1.4 2006/03/20 17:24:20 dillon Exp $ */ diff --git a/lib/libc/string/strpbrk.3 b/lib/libc/string/strpbrk.3 index 3dcc9646b9..25cadcaee0 100644 --- a/lib/libc/string/strpbrk.3 +++ b/lib/libc/string/strpbrk.3 @@ -13,10 +13,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -34,7 +30,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)strpbrk.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/strpbrk.3,v 1.3.2.4 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/strpbrk.3,v 1.8 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/strpbrk.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" .Dd June 4, 1993 diff --git a/lib/libc/string/strpbrk.c b/lib/libc/string/strpbrk.c index 975ee3ca2b..1b4ac1e8da 100644 --- a/lib/libc/string/strpbrk.c +++ b/lib/libc/string/strpbrk.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -31,7 +27,7 @@ * SUCH DAMAGE. * * @(#)strpbrk.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/strpbrk.c,v 1.1.1.1.14.1 2001/07/09 23:30:07 obrien Exp $ + * $FreeBSD: src/lib/libc/string/strpbrk.c,v 1.5 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/strpbrk.c,v 1.5 2005/09/18 16:32:34 asmodai Exp $ */ diff --git a/lib/libc/string/strrchr.3 b/lib/libc/string/strrchr.3 deleted file mode 100644 index 64f06b506d..0000000000 --- a/lib/libc/string/strrchr.3 +++ /dev/null @@ -1,92 +0,0 @@ -.\" Copyright (c) 1990, 1991, 1993 -.\" The Regents of the University of California. All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" Chris Torek and the American National Standards Committee X3, -.\" on Information Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. -.\" 4. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" @(#)strrchr.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/strrchr.3,v 1.5.2.4 2001/12/14 18:33:59 ru Exp $ -.\" $DragonFly: src/lib/libc/string/strrchr.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ -.\" -.Dd June 4, 1993 -.Dt STRRCHR 3 -.Os -.Sh NAME -.Nm strrchr -.Nd locate character in string -.Sh LIBRARY -.Lb libc -.Sh SYNOPSIS -.In string.h -.Ft char * -.Fn strrchr "const char *s" "int c" -.Sh DESCRIPTION -The -.Fn strrchr -function -locates the last occurrence of -.Fa c -(converted to a char) -in the string -.Fa s . -If -.Fa c -is -.Ql \e0 , -.Fn strrchr -locates the terminating -.Ql \e0 . -.Sh RETURN VALUES -The -.Fn strrchr -function -returns a pointer to the character, -or a null -pointer if -.Fa c -does not occur anywhere in -.Fa s . -.Sh SEE ALSO -.Xr memchr 3 , -.Xr strchr 3 , -.Xr strcspn 3 , -.Xr strpbrk 3 , -.Xr strsep 3 , -.Xr strspn 3 , -.Xr strstr 3 , -.Xr strtok 3 -.Sh STANDARDS -The -.Fn strrchr -function -conforms to -.St -isoC . diff --git a/lib/libc/string/strsep.3 b/lib/libc/string/strsep.3 index 23000311ad..7d5c973d9f 100644 --- a/lib/libc/string/strsep.3 +++ b/lib/libc/string/strsep.3 @@ -12,10 +12,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -33,10 +29,10 @@ .\" SUCH DAMAGE. .\" .\" @(#)strsep.3 8.1 (Berkeley) 6/9/93 -.\" $FreeBSD: src/lib/libc/string/strsep.3,v 1.6.2.6 2002/08/09 16:58:28 archie Exp $ +.\" $FreeBSD: src/lib/libc/string/strsep.3,v 1.16 2008/12/05 15:50:59 pjd Exp $ .\" $DragonFly: src/lib/libc/string/strsep.3,v 1.2 2003/06/17 04:26:46 dillon Exp $ .\" -.Dd June 9, 1993 +.Dd December 5, 2008 .Dt STRSEP 3 .Os .Sh NAME @@ -66,7 +62,9 @@ The original value of .Fa *stringp is returned. .Pp -An ``empty'' field (i.e., a character in the string +An +.Dq empty +field (i.e., a character in the string .Fa delim occurs as the first character of .Fa *stringp ) @@ -84,6 +82,21 @@ returns .Sh EXAMPLES The following uses .Fn strsep +to parse a string, and prints each token in separate line: +.Bd -literal -offset indent +char *token, *string, *tofree; + +tofree = string = strdup("abc,def,ghi"); +assert(string != NULL); + +while ((token = strsep(&string, ",")) != NULL) + printf("%s\en", token); + +free(tofree); +.Ed +.Pp +The following uses +.Fn strsep to parse a string, containing tokens delimited by white space, into an argument vector: .Bd -literal -offset indent @@ -114,7 +127,7 @@ While the .Fn strtok function should be preferred for portability reasons (it conforms to .St -isoC ) -it is unable to handle empty fields, i.e. detect fields delimited by +it is unable to handle empty fields, i.e., detect fields delimited by two adjacent delimiter characters, or to be used for more than a single string at a time. The diff --git a/lib/libc/string/strsep.c b/lib/libc/string/strsep.c index 619c25f4c5..2d7e4b6128 100644 --- a/lib/libc/string/strsep.c +++ b/lib/libc/string/strsep.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -31,11 +27,10 @@ * SUCH DAMAGE. * * @(#)strsep.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/strsep.c,v 1.2.12.1 2001/07/09 23:30:07 obrien Exp $ + * $FreeBSD: src/lib/libc/string/strsep.c,v 1.6 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/strsep.c,v 1.4 2005/09/18 16:32:34 asmodai Exp $ */ -#include #include #include diff --git a/lib/libc/string/strsignal.c b/lib/libc/string/strsignal.c index 2c7d68684b..1a975d5f72 100644 --- a/lib/libc/string/strsignal.c +++ b/lib/libc/string/strsignal.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -31,41 +27,80 @@ * SUCH DAMAGE. * * @(#)strerror.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/strsignal.c,v 1.1.2.1 2001/07/09 23:30:07 obrien Exp $ + * $FreeBSD: src/lib/libc/string/strsignal.c,v 1.8 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/strsignal.c,v 1.4 2005/09/18 16:32:34 asmodai Exp $ */ -#include +#if defined(NLS) +#include +#endif + +#include +#include #include #include +#define UPREFIX "Unknown signal" + +/* XXX: negative 'num' ? (REGR) */ char * strsignal(int num) { -#define UPREFIX "Unknown signal: " - static char ebuf[40] = UPREFIX; /* 64-bit number + slop */ - unsigned int signum; - char *p, *t; - char tmp[40]; + static char ebuf[NL_TEXTMAX]; + char tmp[20]; + size_t n; + int signum; + char *t, *p; - signum = num; /* convert to unsigned */ - if (signum < sys_nsig) - return ((char *)sys_siglist[signum]); +#if defined(NLS) + int saved_errno = errno; + nl_catd catd; + catd = catopen("libc", NL_CAT_LOCALE); +#endif - /* Do this by hand, so we don't link to stdio(3). */ - t = tmp; + if (num > 0 && num < sys_nsig) { + n = strlcpy(ebuf, +#if defined(NLS) + catgets(catd, 2, num, sys_siglist[num]), +#else + sys_siglist[num], +#endif + sizeof(ebuf)); + } else { + n = strlcpy(ebuf, +#if defined(NLS) + catgets(catd, 2, 0xffff, UPREFIX), +#else + UPREFIX, +#endif + sizeof(ebuf)); + } + + signum = num; if (num < 0) signum = -signum; + + t = tmp; do { *t++ = "0123456789"[signum % 10]; } while (signum /= 10); if (num < 0) *t++ = '-'; - for (p = ebuf + sizeof(UPREFIX) - 1;;) { + + p = (ebuf + n); + *p++ = ':'; + *p++ = ' '; + + for (;;) { *p++ = *--t; if (t <= tmp) break; } *p = '\0'; + +#if defined(NLS) + catclose(catd); + errno = saved_errno; +#endif return (ebuf); } diff --git a/lib/libc/string/strspn.3 b/lib/libc/string/strspn.3 index 84af87d2ef..5845d18ad3 100644 --- a/lib/libc/string/strspn.3 +++ b/lib/libc/string/strspn.3 @@ -13,10 +13,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -34,7 +30,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)strspn.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/strspn.3,v 1.4.2.4 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/strspn.3,v 1.10 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/strspn.3,v 1.2 2003/06/17 04:26:47 dillon Exp $ .\" .Dd June 4, 1993 @@ -57,8 +53,15 @@ spans the initial part of the null-terminated string .Fa s as long as the characters from .Fa s -occur in string +occur in the null-terminated string .Fa charset . +In other words, it computes the string array index in +.Fa s +of the first character of +.Fa s +which is not in +.Fa charset , +else the index of the first null character. .Sh RETURN VALUES The .Fn strspn diff --git a/lib/libc/string/strstr.3 b/lib/libc/string/strstr.3 index 9114762bd9..a7e3834899 100644 --- a/lib/libc/string/strstr.3 +++ b/lib/libc/string/strstr.3 @@ -14,10 +14,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -35,7 +31,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)strstr.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/strstr.3,v 1.3.2.5 2001/12/25 00:36:53 ache Exp $ +.\" $FreeBSD: src/lib/libc/string/strstr.3,v 1.14 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/strstr.3,v 1.5 2005/08/05 22:35:10 swildner Exp $ .\" .Dd October 11, 2001 @@ -137,6 +133,7 @@ ptr = strnstr(largestring, smallstring, 4); .Ed .Sh SEE ALSO .Xr memchr 3 , +.Xr memmem 3 , .Xr strchr 3 , .Xr strcspn 3 , .Xr strpbrk 3 , diff --git a/lib/libc/string/strstr.c b/lib/libc/string/strstr.c index 164ab323c9..3fb21bf50d 100644 --- a/lib/libc/string/strstr.c +++ b/lib/libc/string/strstr.c @@ -13,10 +13,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -34,7 +30,7 @@ * SUCH DAMAGE. * * @(#)strstr.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/strstr.c,v 1.1.1.1.14.1 2001/07/09 23:30:07 obrien Exp $ + * $FreeBSD: src/lib/libc/string/strstr.c,v 1.5 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/strstr.c,v 1.4 2005/04/28 13:25:12 joerg Exp $ */ diff --git a/lib/libc/string/strtok.3 b/lib/libc/string/strtok.3 index 55ec4c9a1e..3266878696 100644 --- a/lib/libc/string/strtok.3 +++ b/lib/libc/string/strtok.3 @@ -22,12 +22,6 @@ .\" disclaimer in the documentation and/or other materials provided .\" with the distribution. .\" -.\" 3. All advertising materials mentioning features or use of this -.\" software must display the following acknowledgement: -.\" -.\" This product includes software developed by Softweyr LLC, the -.\" University of California, Berkeley, and its contributors. -.\" .\" 4. Neither the name of Softweyr LLC, the University nor the names .\" of its contributors may be used to endorse or promote products .\" derived from this software without specific prior written @@ -48,7 +42,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)strtok.3 8.2 (Berkeley) 2/3/94 -.\" $FreeBSD: src/lib/libc/string/strtok.3,v 1.10.2.8 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/strtok.3,v 1.26 2007/12/12 18:33:06 wes Exp $ .\" $DragonFly: src/lib/libc/string/strtok.3,v 1.4 2006/02/17 19:35:06 swildner Exp $ .\" .Dd November 27, 1998 @@ -100,7 +94,9 @@ function is a reentrant version of The context pointer .Fa last must be provided on each call. +The .Fn strtok_r +function may also be used to nest two parsing loops within one another, as long as separate context pointers are used. .Pp @@ -147,7 +143,8 @@ for (word = strtok_r(test, sep, &brkt); .Xr strrchr 3 , .Xr strsep 3 , .Xr strspn 3 , -.Xr strstr 3 +.Xr strstr 3 , +.Xr wcstok 3 .Sh STANDARDS The .Fn strtok diff --git a/lib/libc/string/strtok.c b/lib/libc/string/strtok.c index ded182e389..39e4e02a63 100644 --- a/lib/libc/string/strtok.c +++ b/lib/libc/string/strtok.c @@ -1,4 +1,4 @@ -/* +/*- * Copyright (c) 1998 Softweyr LLC. All rights reserved. * * strtok_r, from Berkeley strtok @@ -10,20 +10,11 @@ * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: - * * 1. Redistributions of source code must retain the above copyright * notices, this list of conditions and the following disclaimer. - * * 2. Redistributions in binary form must reproduce the above copyright * notices, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * - * This product includes software developed by Softweyr LLC, the - * University of California, Berkeley, and its contributors. - * * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -40,127 +31,105 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $FreeBSD: src/lib/libc/string/strtok.c,v 1.2.6.1 2001/07/09 23:30:07 obrien Exp $ + * @(#)strtok.c 8.1 (Berkeley) 6/4/93 + * $FreeBSD: src/lib/libc/string/strtok.c,v 1.10 2007/12/12 18:33:06 wes Exp $ * $DragonFly: src/lib/libc/string/strtok.c,v 1.3 2005/04/28 13:25:12 joerg Exp $ */ #include +#ifdef DEBUG_STRTOK +#include +#endif #include +char *__strtok_r(char *, const char *, char **); + +__weak_reference(__strtok_r, strtok_r); + char * -strtok_r(char *s, const char *delim, char **last) +__strtok_r(char *s, const char *delim, char **last) { - const char *spanp; - int c, sc; - char *tok; - - if (s == NULL && (s = *last) == NULL) - { - return NULL; - } - - /* - * Skip (span) leading delimiters (s += strspn(s, delim), sort of). - */ + const char *spanp; + int c, sc; + char *tok; + + if (s == NULL && (s = *last) == NULL) + return (NULL); + + /* + * Skip (span) leading delimiters (s += strspn(s, delim), sort of). + */ cont: - c = *s++; - for (spanp = delim; (sc = *spanp++) != 0; ) - { - if (c == sc) - { - goto cont; - } - } - - if (c == 0) /* no non-delimiter characters */ - { - *last = NULL; - return NULL; - } - tok = s - 1; - - /* - * Scan token (scan for delimiters: s += strcspn(s, delim), sort of). - * Note that delim must have one NUL; we stop if we see that, too. - */ - for (;;) - { c = *s++; - spanp = delim; - do - { - if ((sc = *spanp++) == c) - { - if (c == 0) - { - s = NULL; - } - else - { - char *w = s - 1; - *w = '\0'; - } - *last = s; - return tok; - } + for (spanp = delim; (sc = *spanp++) != 0;) { + if (c == sc) + goto cont; } - while (sc != 0); - } - /* NOTREACHED */ -} + if (c == 0) { /* no non-delimiter characters */ + *last = NULL; + return (NULL); + } + tok = s - 1; + + /* + * Scan token (scan for delimiters: s += strcspn(s, delim), sort of). + * Note that delim must have one NUL; we stop if we see that, too. + */ + for (;;) { + c = *s++; + spanp = delim; + do { + if ((sc = *spanp++) == c) { + if (c == 0) + s = NULL; + else + s[-1] = '\0'; + *last = s; + return (tok); + } + } while (sc != 0); + } + /* NOTREACHED */ +} char * strtok(char *s, const char *delim) { - static char *last; + static char *last; - return strtok_r(s, delim, &last); + return (__strtok_r(s, delim, &last)); } - -#if defined(DEBUG_STRTOK) - +#ifdef DEBUG_STRTOK /* * Test the tokenizer. */ int -main() +main(void) { - char test[80], blah[80]; - char *sep = "\\/:;=-"; - char *word, *phrase, *brkt, *brkb; - - printf("String tokenizer test:\n"); - - strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function."); - - for (word = strtok(test, sep); - word; - word = strtok(NULL, sep)) - { - printf("Next word is \"%s\".\n", word); - } + char blah[80], test[80]; + char *brkb, *brkt, *phrase, *sep, *word; - phrase = "foo"; + sep = "\\/:;=-"; + phrase = "foo"; - strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function."); + printf("String tokenizer test:\n"); + strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function."); + for (word = strtok(test, sep); word; word = strtok(NULL, sep)) + printf("Next word is \"%s\".\n", word); + strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function."); - for (word = strtok_r(test, sep, &brkt); - word; - word = strtok_r(NULL, sep, &brkt)) - { - strcpy(blah, "blah:blat:blab:blag"); + for (word = strtok_r(test, sep, &brkt); word; + word = strtok_r(NULL, sep, &brkt)) { + strcpy(blah, "blah:blat:blab:blag"); - for (phrase = strtok_r(blah, sep, &brkb); - phrase; - phrase = strtok_r(NULL, sep, &brkb)) - { - printf("So far we're at %s:%s\n", word, phrase); + for (phrase = strtok_r(blah, sep, &brkb); phrase; + phrase = strtok_r(NULL, sep, &brkb)) + printf("So far we're at %s:%s\n", word, phrase); } - } - return 0; + return (0); } #endif /* DEBUG_STRTOK */ diff --git a/lib/libc/string/strxfrm.3 b/lib/libc/string/strxfrm.3 index bc469a927b..6651ad41f2 100644 --- a/lib/libc/string/strxfrm.3 +++ b/lib/libc/string/strxfrm.3 @@ -13,10 +13,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -34,7 +30,7 @@ .\" SUCH DAMAGE. .\" .\" @(#)strxfrm.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/strxfrm.3,v 1.11.2.3 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/strxfrm.3,v 1.18 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/strxfrm.3,v 1.2 2003/06/17 04:26:47 dillon Exp $ .\" .Dd June 4, 1993 @@ -48,7 +44,7 @@ .Sh SYNOPSIS .In string.h .Ft size_t -.Fn strxfrm "char *dst" "const char *src" "size_t n" +.Fn strxfrm "char * restrict dst" "const char * restrict src" "size_t n" .Sh DESCRIPTION The .Fn strxfrm @@ -91,7 +87,8 @@ are indeterminate. .Sh SEE ALSO .Xr setlocale 3 , .Xr strcmp 3 , -.Xr strcoll 3 +.Xr strcoll 3 , +.Xr wcsxfrm 3 .Sh STANDARDS The .Fn strxfrm diff --git a/lib/libc/string/strxfrm.c b/lib/libc/string/strxfrm.c index 434a5550f9..fecf7fa37d 100644 --- a/lib/libc/string/strxfrm.c +++ b/lib/libc/string/strxfrm.c @@ -24,7 +24,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $FreeBSD: src/lib/libc/string/strxfrm.c,v 1.11.2.1 2001/07/09 23:30:07 obrien Exp $ + * $FreeBSD: src/lib/libc/string/strxfrm.c,v 1.17 2008/10/19 09:10:44 delphij Exp $ * $DragonFly: src/lib/libc/string/strxfrm.c,v 1.3 2005/09/18 16:32:34 asmodai Exp $ */ @@ -33,7 +33,7 @@ #include "collate.h" size_t -strxfrm(char *dest, const char *src, size_t len) +strxfrm(char * __restrict dest, const char * __restrict src, size_t len) { int prim, sec, l; size_t slen; @@ -45,18 +45,8 @@ strxfrm(char *dest, const char *src, size_t len) return 0; } - if (__collate_load_error) { - slen = strlen(src); - if (len > 0) { - if (slen < len) - strcpy(dest, src); - else { - strncpy(dest, src, len - 1); - dest[len - 1] = '\0'; - } - } - return slen; - } + if (__collate_load_error) + return strlcpy(dest, src, len); slen = 0; prim = sec = 0; diff --git a/lib/libc/string/swab.3 b/lib/libc/string/swab.3 index 5a74d3e44c..7a9e731430 100644 --- a/lib/libc/string/swab.3 +++ b/lib/libc/string/swab.3 @@ -9,10 +9,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -30,10 +26,10 @@ .\" SUCH DAMAGE. .\" .\" @(#)swab.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/string/swab.3,v 1.4.2.2 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/swab.3,v 1.10 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/swab.3,v 1.2 2003/06/17 04:26:47 dillon Exp $ .\" -.Dd June 4, 1993 +.Dd December 10, 2004 .Dt SWAB 3 .Os .Sh NAME @@ -42,9 +38,9 @@ .Sh LIBRARY .Lb libc .Sh SYNOPSIS -.In string.h +.In unistd.h .Ft void -.Fn swab "const void *src" "void *dst" "size_t len" +.Fn swab "const void * restrict src" "void * restrict dst" "ssize_t len" .Sh DESCRIPTION The function .Fn swab diff --git a/lib/libc/string/swab.c b/lib/libc/string/swab.c index d88b090846..0767aa5849 100644 --- a/lib/libc/string/swab.c +++ b/lib/libc/string/swab.c @@ -13,10 +13,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -34,28 +30,27 @@ * SUCH DAMAGE. * * @(#)swab.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/swab.c,v 1.1.1.1.14.1 2001/07/09 23:30:07 obrien Exp $ + * $FreeBSD: src/lib/libc/string/swab.c,v 1.7 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/swab.c,v 1.4 2005/09/18 16:32:34 asmodai Exp $ */ -#include +#include void -swab(const void *from, void *to, size_t len) +swab(const void * __restrict from, void * __restrict to, ssize_t len) { unsigned long temp; int n; char *fp, *tp; - n = (len >> 1) + 1; + n = len >> 1; fp = (char *)from; tp = (char *)to; #define STEP temp = *fp++,*tp++ = *fp++,*tp++ = temp /* round to multiple of 8 */ - while ((--n) & 07) + for (; n & 0x7; --n) STEP; - n >>= 3; - while (--n >= 0) { + for (n >>= 3; n > 0; --n) { STEP; STEP; STEP; STEP; STEP; STEP; STEP; STEP; } diff --git a/lib/libc/string/wcscat.c b/lib/libc/string/wcscat.c index 201d093d94..127002fbd9 100644 --- a/lib/libc/string/wcscat.c +++ b/lib/libc/string/wcscat.c @@ -24,29 +24,23 @@ * SUCH DAMAGE. * * citrus Id: wcscat.c,v 1.1 1999/12/29 21:47:45 tshiozak Exp - * * $NetBSD: wcscat.c,v 1.1 2000/12/23 23:14:36 itojun Exp $ - * $FreeBSD: src/lib/libc/string/wcscat.c,v 1.3.2.1 2001/07/11 23:48:38 obrien Exp $ + * $FreeBSD: src/lib/libc/string/wcscat.c,v 1.8 2002/09/26 09:28:55 tjr Exp $ * $DragonFly: src/lib/libc/string/wcscat.c,v 1.3 2005/09/18 16:32:34 asmodai Exp $ */ -#include #include wchar_t * -wcscat(wchar_t *s1, const wchar_t *s2) +wcscat(wchar_t * __restrict s1, const wchar_t * __restrict s2) { - wchar_t *p; - wchar_t *q; - const wchar_t *r; + wchar_t *cp; + + cp = s1; + while (*cp != L'\0') + cp++; + while ((*cp++ = *s2++) != L'\0') + ; - p = s1; - while (*p) - p++; - q = p; - r = s2; - while (*r) - *q++ = *r++; - *q = '\0'; - return s1; + return (s1); } diff --git a/lib/libc/string/wcscmp.c b/lib/libc/string/wcscmp.c index 3c49a752b3..eb14605efe 100644 --- a/lib/libc/string/wcscmp.c +++ b/lib/libc/string/wcscmp.c @@ -13,10 +13,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -34,11 +30,11 @@ * SUCH DAMAGE. * * @(#)strcmp.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/wcscmp.c,v 1.3.2.1 2001/07/11 23:48:38 obrien Exp $ + * $NetBSD: wcscmp.c,v 1.3 2001/01/05 12:13:12 itojun Exp $ + * $FreeBSD: src/lib/libc/string/wcscmp.c,v 1.8 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/wcscmp.c,v 1.3 2005/09/18 16:32:34 asmodai Exp $ */ -#include #include /* diff --git a/lib/libc/locale/wcscoll.3 b/lib/libc/string/wcscoll.3 similarity index 80% rename from lib/libc/locale/wcscoll.3 rename to lib/libc/string/wcscoll.3 index b8fdc91377..819ae8767a 100644 --- a/lib/libc/locale/wcscoll.3 +++ b/lib/libc/string/wcscoll.3 @@ -1,5 +1,3 @@ -.\" $NetBSD: wcscoll.3,v 1.2 2006/10/14 07:44:58 wiz Exp $ -.\" .\" Copyright (c) 1990, 1991, 1993 .\" The Regents of the University of California. All rights reserved. .\" @@ -15,10 +13,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -37,10 +31,9 @@ .\" .\" @(#)strcoll.3 8.1 (Berkeley) 6/4/93 .\" FreeBSD: src/lib/libc/string/strcoll.3,v 1.11 2001/10/01 16:09:00 ru Exp -.\" FreeBSD: /repoman/r/ncvs/src/lib/libc/string/wcscoll.3,v 1.2 2002/12/09 14:04:05 ru Exp -.\" $DragonFly: src/lib/libc/locale/wcscoll.3,v 1.1 2008/05/31 04:51:55 swildner Exp $ +.\" $FreeBSD: src/lib/libc/string/wcscoll.3,v 1.3 2007/01/09 00:28:12 imp Exp $ .\" -.Dd May 30, 2008 +.Dd October 4, 2002 .Dt WCSCOLL 3 .Os .Sh NAME @@ -55,7 +48,7 @@ .Sh DESCRIPTION The .Fn wcscoll -function compares the nul-terminated strings +function compares the null-terminated strings .Fa s1 and .Fa s2 @@ -69,8 +62,9 @@ is equivalent to .Sh RETURN VALUES The .Fn wcscoll -function returns an integer greater than, equal to, or less than -0, if +function +returns an integer greater than, equal to, or less than 0, +if .Fa s1 is greater than, equal to, or less than .Fa s2 . @@ -101,13 +95,14 @@ Cannot allocate enough memory for temporary buffers. .Sh STANDARDS The .Fn wcscoll -function conforms to +function +conforms to .St -isoC-99 . .Sh BUGS The current implementation of .Fn wcscoll -function disregards -.Dv LC_COLLATE -locales, and falls back to using the +only works in single-byte +.Dv LC_CTYPE +locales, and falls back to using .Fn wcscmp -function. +in locales with extended character sets. diff --git a/lib/libc/string/wcscoll.c b/lib/libc/string/wcscoll.c new file mode 100644 index 0000000000..93329c869c --- /dev/null +++ b/lib/libc/string/wcscoll.c @@ -0,0 +1,97 @@ +/*- + * Copyright (c) 2002 Tim J. Robbins + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD: src/lib/libc/string/wcscoll.c,v 1.3 2004/04/07 09:47:56 tjr Exp $ + */ + +#include +#include +#include +#include +#include "collate.h" + +static char *__mbsdup(const wchar_t *); + +/* + * Placeholder implementation of wcscoll(). Attempts to use the single-byte + * collation ordering where possible, and falls back on wcscmp() in locales + * with extended character sets. + */ +int +wcscoll(const wchar_t *ws1, const wchar_t *ws2) +{ + char *mbs1, *mbs2; + int diff, sverrno; + + if (__collate_load_error || MB_CUR_MAX > 1) + /* + * Locale has no special collating order, could not be + * loaded, or has an extended character set; do a fast binary + * comparison. + */ + return (wcscmp(ws1, ws2)); + + if ((mbs1 = __mbsdup(ws1)) == NULL || (mbs2 = __mbsdup(ws2)) == NULL) { + /* + * Out of memory or illegal wide chars; fall back to wcscmp() + * but leave errno indicating the error. Callers that don't + * check for error will get a reasonable but often slightly + * incorrect result. + */ + sverrno = errno; + free(mbs1); + errno = sverrno; + return (wcscmp(ws1, ws2)); + } + + diff = strcoll(mbs1, mbs2); + sverrno = errno; + free(mbs1); + free(mbs2); + errno = sverrno; + + return (diff); +} + +static char * +__mbsdup(const wchar_t *ws) +{ + static const mbstate_t initial; + mbstate_t st; + const wchar_t *wcp; + size_t len; + char *mbs; + + wcp = ws; + st = initial; + if ((len = wcsrtombs(NULL, &wcp, 0, &st)) == (size_t)-1) + return (NULL); + if ((mbs = malloc(len + 1)) == NULL) + return (NULL); + st = initial; + wcsrtombs(mbs, &ws, len + 1, &st); + + return (mbs); +} diff --git a/lib/libc/string/wcscpy.c b/lib/libc/string/wcscpy.c index e608476b55..09ef18243c 100644 --- a/lib/libc/string/wcscpy.c +++ b/lib/libc/string/wcscpy.c @@ -26,25 +26,20 @@ * citrus Id: wcscpy.c,v 1.2 2000/12/21 04:51:09 itojun Exp * * $NetBSD: wcscpy.c,v 1.1 2000/12/23 23:14:36 itojun Exp $ - * $FreeBSD: src/lib/libc/string/wcscpy.c,v 1.3.2.1 2001/07/11 23:48:38 obrien Exp $ + * $FreeBSD: src/lib/libc/string/wcscpy.c,v 1.8 2002/09/26 09:23:07 tjr Exp $ * $DragonFly: src/lib/libc/string/wcscpy.c,v 1.3 2005/09/18 16:32:34 asmodai Exp $ */ -#include #include wchar_t * -wcscpy(wchar_t *s1, const wchar_t *s2) +wcscpy(wchar_t * __restrict s1, const wchar_t * __restrict s2) { - wchar_t *p; - const wchar_t *q; + wchar_t *cp; - *s1 = '\0'; - p = s1; - q = s2; - while (*q) - *p++ = *q++; - *p = '\0'; + cp = s1; + while ((*cp++ = *s2++) != L'\0') + ; - return s1; + return (s1); } diff --git a/lib/libc/string/wcscspn.c b/lib/libc/string/wcscspn.c index 4f906ba477..4fbc29b69b 100644 --- a/lib/libc/string/wcscspn.c +++ b/lib/libc/string/wcscspn.c @@ -26,11 +26,10 @@ * citrus Id: wcscspn.c,v 1.1 1999/12/29 21:47:45 tshiozak Exp * * $NetBSD: wcscspn.c,v 1.1 2000/12/23 23:14:36 itojun Exp $ - * $FreeBSD: src/lib/libc/string/wcscspn.c,v 1.3.2.1 2001/07/11 23:48:38 obrien Exp $ + * $FreeBSD: src/lib/libc/string/wcscspn.c,v 1.6 2002/09/21 00:29:23 tjr Exp $ * $DragonFly: src/lib/libc/string/wcscspn.c,v 1.3 2005/09/18 16:32:34 asmodai Exp $ */ -#include #include size_t diff --git a/lib/libc/locale/wcscoll.c b/lib/libc/string/wcsdup.c similarity index 77% rename from lib/libc/locale/wcscoll.c rename to lib/libc/string/wcsdup.c index cf41b62b69..a8fb551307 100644 --- a/lib/libc/locale/wcscoll.c +++ b/lib/libc/string/wcsdup.c @@ -1,8 +1,5 @@ -/* $NetBSD: src/lib/libc/locale/wcscoll.c,v 1.1 2003/03/02 22:18:16 tshiozak Exp $ */ -/* $DragonFly: src/lib/libc/locale/wcscoll.c,v 1.1 2005/03/16 06:54:41 joerg Exp $ */ - /*- - * Copyright (c)2003 Citrus Project, + * Copyright (c) 2005 Tim J. Robbins. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -25,16 +22,21 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. + * + * $FreeBSD: src/lib/libc/string/wcsdup.c,v 1.1 2005/08/13 05:54:33 tjr Exp $ */ +#include #include -/* - * Compare strings with using collating information. - */ -int -wcscoll(const wchar_t *s1, const wchar_t *s2) +wchar_t * +wcsdup(const wchar_t *s) { - /* XXX: LC_COLLATE should be implemented. */ - return (wcscmp(s1, s2)); + wchar_t *copy; + size_t len; + + len = wcslen(s) + 1; + if ((copy = malloc(len * sizeof(wchar_t))) == NULL) + return (NULL); + return (wmemcpy(copy, s, len)); } diff --git a/lib/libc/string/wcslcat.c b/lib/libc/string/wcslcat.c index 0e1c0120fd..8ce3efc598 100644 --- a/lib/libc/string/wcslcat.c +++ b/lib/libc/string/wcslcat.c @@ -26,12 +26,11 @@ * * $OpenBSD: strlcat.c,v 1.3 2000/11/24 11:10:02 itojun Exp $ * $NetBSD: wcslcat.c,v 1.1 2000/12/23 23:14:36 itojun Exp $ - * $FreeBSD: src/lib/libc/string/wcslcat.c,v 1.3.2.1 2001/07/11 23:48:38 obrien Exp $ + * $FreeBSD: src/lib/libc/string/wcslcat.c,v 1.6 2002/09/21 00:29:23 tjr Exp $ * $DragonFly: src/lib/libc/string/wcslcat.c,v 1.3 2005/09/18 16:32:34 asmodai Exp $ */ #include -#include #include /* diff --git a/lib/libc/string/wcslcpy.c b/lib/libc/string/wcslcpy.c index 79dce6c7da..a567f72105 100644 --- a/lib/libc/string/wcslcpy.c +++ b/lib/libc/string/wcslcpy.c @@ -26,12 +26,11 @@ * * $OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $ * $NetBSD: wcslcpy.c,v 1.1 2000/12/23 23:14:36 itojun Exp $ - * $FreeBSD: src/lib/libc/string/wcslcpy.c,v 1.3.2.1 2001/07/11 23:48:38 obrien Exp $ + * $FreeBSD: src/lib/libc/string/wcslcpy.c,v 1.6 2002/09/21 00:29:23 tjr Exp $ * $DragonFly: src/lib/libc/string/wcslcpy.c,v 1.3 2005/09/18 16:32:34 asmodai Exp $ */ #include -#include #include /* diff --git a/lib/libc/string/wcslen.c b/lib/libc/string/wcslen.c index e7c1d7f052..3a23812cce 100644 --- a/lib/libc/string/wcslen.c +++ b/lib/libc/string/wcslen.c @@ -25,11 +25,10 @@ * * citrus Id: wcslen.c,v 1.1 1999/12/29 21:47:45 tshiozak Exp * $NetBSD: wcslen.c,v 1.1 2000/12/23 23:14:36 itojun Exp $ - * $FreeBSD: src/lib/libc/string/wcslen.c,v 1.3.2.1 2001/07/11 23:48:38 obrien Exp $ + * $FreeBSD: src/lib/libc/string/wcslen.c,v 1.6 2002/09/21 00:29:23 tjr Exp $ * $DragonFly: src/lib/libc/string/wcslen.c,v 1.3 2005/09/18 16:32:34 asmodai Exp $ */ -#include #include size_t @@ -41,5 +40,5 @@ wcslen(const wchar_t *s) while (*p) p++; - return p - s; + return (p - s); } diff --git a/lib/libc/string/wcsncat.c b/lib/libc/string/wcsncat.c index 00de21859c..0535b73478 100644 --- a/lib/libc/string/wcsncat.c +++ b/lib/libc/string/wcsncat.c @@ -25,15 +25,14 @@ * * citrus Id: wcsncat.c,v 1.1 1999/12/29 21:47:45 tshiozak Exp * $NetBSD: wcsncat.c,v 1.1 2000/12/23 23:14:36 itojun Exp $ - * $FreeBSD: src/lib/libc/string/wcsncat.c,v 1.3.2.1 2001/07/11 23:48:38 obrien Exp $ + * $FreeBSD: src/lib/libc/string/wcsncat.c,v 1.7 2002/09/21 00:29:23 tjr Exp $ * $DragonFly: src/lib/libc/string/wcsncat.c,v 1.3 2005/09/18 16:32:34 asmodai Exp $ */ -#include #include wchar_t * -wcsncat(wchar_t *s1, const wchar_t *s2, size_t n) +wcsncat(wchar_t * __restrict s1, const wchar_t * __restrict s2, size_t n) { wchar_t *p; wchar_t *q; diff --git a/lib/libc/string/wcsncmp.c b/lib/libc/string/wcsncmp.c index 19f4f2a28a..1101e95881 100644 --- a/lib/libc/string/wcsncmp.c +++ b/lib/libc/string/wcsncmp.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -31,11 +27,11 @@ * SUCH DAMAGE. * * @(#)strncmp.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/wcsncmp.c,v 1.3.2.1 2001/07/11 23:48:38 obrien Exp $ + * $NetBSD: wcsncmp.c,v 1.3 2001/01/05 12:13:13 itojun Exp $ + * $FreeBSD: src/lib/libc/string/wcsncmp.c,v 1.8 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/wcsncmp.c,v 1.3 2005/09/18 16:32:34 asmodai Exp $ */ -#include #include int diff --git a/lib/libc/string/wcsncpy.c b/lib/libc/string/wcsncpy.c index d82d60acac..40b84815f4 100644 --- a/lib/libc/string/wcsncpy.c +++ b/lib/libc/string/wcsncpy.c @@ -13,10 +13,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -34,7 +30,7 @@ * SUCH DAMAGE. * * @(#)strncpy.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/wcsncpy.c,v 1.3.2.2 2002/11/12 11:24:24 tjr Exp $ + * $FreeBSD: src/lib/libc/string/wcsncpy.c,v 1.10 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/wcsncpy.c,v 1.2 2003/06/17 04:26:47 dillon Exp $ */ @@ -45,7 +41,7 @@ * Return dst. */ wchar_t * -wcsncpy(wchar_t *dst, const wchar_t *src, size_t n) +wcsncpy(wchar_t * __restrict dst, const wchar_t * __restrict src, size_t n) { if (n != 0) { wchar_t *d = dst; diff --git a/lib/libc/string/wcspbrk.c b/lib/libc/string/wcspbrk.c index 3d2d64da27..dfcdd79d33 100644 --- a/lib/libc/string/wcspbrk.c +++ b/lib/libc/string/wcspbrk.c @@ -25,7 +25,7 @@ * * citrus Id: wcspbrk.c,v 1.2 2000/12/21 05:07:25 itojun Exp * $NetBSD: wcspbrk.c,v 1.1 2000/12/23 23:14:37 itojun Exp $ - * $FreeBSD: src/lib/libc/string/wcspbrk.c,v 1.3.2.1 2001/07/11 23:48:38 obrien Exp $ + * $FreeBSD: src/lib/libc/string/wcspbrk.c,v 1.6 2002/09/21 00:29:23 tjr Exp $ * $DragonFly: src/lib/libc/string/wcspbrk.c,v 1.3 2005/04/28 13:25:12 joerg Exp $ */ diff --git a/lib/libc/string/wcsrchr.c b/lib/libc/string/wcsrchr.c index 7bf797b731..741a018257 100644 --- a/lib/libc/string/wcsrchr.c +++ b/lib/libc/string/wcsrchr.c @@ -1,5 +1,5 @@ /*- - * Copyright (c)1999 Citrus Project, + * Copyright (c) 2002 Tim J. Robbins * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -23,9 +23,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * citrus Id: wcsrchr.c,v 1.2 2000/12/21 05:07:25 itojun Exp - * $NetBSD: wcsrchr.c,v 1.1 2000/12/23 23:14:37 itojun Exp $ - * $FreeBSD: src/lib/libc/string/wcsrchr.c,v 1.3.2.1 2001/07/11 23:48:38 obrien Exp $ + * $FreeBSD: src/lib/libc/string/wcsrchr.c,v 1.7 2002/10/23 10:52:04 tjr Exp $ * $DragonFly: src/lib/libc/string/wcsrchr.c,v 1.3 2005/04/28 13:25:12 joerg Exp $ */ @@ -35,17 +33,16 @@ wchar_t * wcsrchr(const wchar_t *s, wchar_t c) { - const wchar_t *p; + const wchar_t *last; - p = s; - while (*p) - p++; - while (s <= p) { - if (*p == c) { - /* LINTED interface specification */ - return(__DECONST(wchar_t *, p)); - } - p--; + last = NULL; + for (;;) { + if (*s == c) + last = s; + if (*s == L'\0') + break; + s++; } - return NULL; + + return (__DECONST(wchar_t *, last)); } diff --git a/lib/libc/string/wcsspn.c b/lib/libc/string/wcsspn.c index 7a21916aa0..e6f8bbb0dc 100644 --- a/lib/libc/string/wcsspn.c +++ b/lib/libc/string/wcsspn.c @@ -26,11 +26,10 @@ * citrus Id: wcsspn.c,v 1.1 1999/12/29 21:47:45 tshiozak Exp * * $NetBSD: wcsspn.c,v 1.1 2000/12/23 23:14:37 itojun Exp $ - * $FreeBSD: src/lib/libc/string/wcsspn.c,v 1.3.2.2 2001/11/27 13:39:22 des Exp $ + * $FreeBSD: src/lib/libc/string/wcsspn.c,v 1.7 2002/09/21 00:29:23 tjr Exp $ * $DragonFly: src/lib/libc/string/wcsspn.c,v 1.3 2005/09/18 16:32:34 asmodai Exp $ */ -#include #include size_t diff --git a/lib/libc/string/wcsstr.c b/lib/libc/string/wcsstr.c index 88571becdd..068a5b652a 100644 --- a/lib/libc/string/wcsstr.c +++ b/lib/libc/string/wcsstr.c @@ -13,10 +13,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -34,7 +30,7 @@ * SUCH DAMAGE. * * @(#)strstr.c 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/libc/string/wcsstr.c,v 1.3.2.2 2002/11/12 11:20:20 tjr Exp $ + * $FreeBSD: src/lib/libc/string/wcsstr.c,v 1.9 2007/01/09 00:28:12 imp Exp $ * $DragonFly: src/lib/libc/string/wcsstr.c,v 1.3 2005/04/28 13:25:12 joerg Exp $ */ @@ -45,7 +41,7 @@ * Find the first occurrence of find in s. */ wchar_t * -wcsstr(const wchar_t *s, const wchar_t *find) +wcsstr(const wchar_t * __restrict s, const wchar_t * __restrict find) { wchar_t c, sc; size_t len; diff --git a/lib/libc/string/strtok.3 b/lib/libc/string/wcstok.3 similarity index 57% copy from lib/libc/string/strtok.3 copy to lib/libc/string/wcstok.3 index 55ec4c9a1e..4ddfb040e0 100644 --- a/lib/libc/string/strtok.3 +++ b/lib/libc/string/wcstok.3 @@ -47,41 +47,32 @@ .\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" @(#)strtok.3 8.2 (Berkeley) 2/3/94 -.\" $FreeBSD: src/lib/libc/string/strtok.3,v 1.10.2.8 2001/12/14 18:33:59 ru Exp $ -.\" $DragonFly: src/lib/libc/string/strtok.3,v 1.4 2006/02/17 19:35:06 swildner Exp $ +.\" $FreeBSD: src/lib/libc/string/wcstok.3,v 1.4 2002/10/15 09:49:54 tjr Exp $ .\" -.Dd November 27, 1998 -.Dt STRTOK 3 +.Dd October 3, 2002 +.Dt WCSTOK 3 .Os .Sh NAME -.Nm strtok , -.Nm strtok_r -.Nd string tokens +.Nm wcstok +.Nd split wide-character string into tokens .Sh LIBRARY .Lb libc .Sh SYNOPSIS -.In string.h -.Ft char * -.Fn strtok "char *str" "const char *sep" -.Ft char * -.Fn strtok_r "char *str" "const char *sep" "char **last" +.In wchar.h +.Ft wchar_t * +.Fn wcstok "wchar_t * restrict str" "const wchar_t * restrict sep" "wchar_t ** restrict last" .Sh DESCRIPTION -.Bf -symbolic -This interface is obsoleted by -.Xr strsep 3 . -.Ef -.Pp The -.Fn strtok +.Fn wcstok function -is used to isolate sequential tokens in a null-terminated string, +is used to isolate sequential tokens in a null-terminated wide character +string, .Fa str . These tokens are separated in the string by at least one of the characters in .Fa sep . The first time that -.Fn strtok +.Fn wcstok is called, .Fa str should be specified; subsequent calls, wishing to obtain further tokens @@ -89,89 +80,54 @@ from the same string, should pass a null pointer instead. The separator string, .Fa sep , must be supplied each time, and may change between calls. -.Pp -The implementation will behave as if no library function calls -.Fn strtok . -.Pp -The -.Fn strtok_r -function is a reentrant version of -.Fn strtok . The context pointer .Fa last must be provided on each call. -.Fn strtok_r -may also be used to nest two parsing loops within one another, as -long as separate context pointers are used. .Pp The -.Fn strtok -and +.Fn wcstok +function is the wide character counterpart of the .Fn strtok_r -functions -return a pointer to the beginning of each subsequent token in the string, -after replacing the token itself with a -.Dv NUL -character. +function. +.Sh RETURN VALUES +The +.Fn wcstok +function +returns a pointer to the beginning of each subsequent token in the string, +after replacing the token itself with a null wide character (L'\e0'). When no more tokens remain, a null pointer is returned. .Sh EXAMPLES -The following uses -.Fn strtok_r -to parse two strings using separate contexts: -.Bd -literal -char test[80], blah[80]; -char *sep = "\e\e/:;=-"; -char *word, *phrase, *brkt, *brkb; - -strcpy(test, "This;is.a:test:of=the/string\e\etokenizer-function."); - -for (word = strtok_r(test, sep, &brkt); - word; - word = strtok_r(NULL, sep, &brkt)) -{ - strcpy(blah, "blah:blat:blab:blag"); +The following code fragment splits a wide character string on +.Tn ASCII +space, tab and newline characters and writes the tokens to +standard output: +.Bd -literal -offset indent +const wchar_t *seps = L" \et\en"; +wchar_t *last, *tok, text[] = L" \enone\ettwo\et\etthree \en"; - for (phrase = strtok_r(blah, sep, &brkb); - phrase; - phrase = strtok_r(NULL, sep, &brkb)) - { - printf("So far we're at %s:%s\en", word, phrase); - } -} +for (tok = wcstok(text, seps, &last); tok != NULL; + tok = wcstok(NULL, seps, &last)) + wprintf(L"%ls\en", tok); .Ed +.Sh COMPATIBILITY +Some early implementations of +.Fn wcstok +omit the +context pointer argument, +.Fa last , +and maintain state across calls in a static variable like +.Fn strtok +does. .Sh SEE ALSO -.Xr memchr 3 , -.Xr strchr 3 , -.Xr strcspn 3 , -.Xr strpbrk 3 , -.Xr strrchr 3 , -.Xr strsep 3 , -.Xr strspn 3 , -.Xr strstr 3 +.Xr strtok 3 , +.Xr wcschr 3 , +.Xr wcscspn 3 , +.Xr wcspbrk 3 , +.Xr wcsrchr 3 , +.Xr wcsspn 3 .Sh STANDARDS The -.Fn strtok +.Fn wcstok function conforms to -.St -isoC . -.Sh AUTHORS -.An Wes Peters , -Softweyr LLC: -.Aq wes@softweyr.com -.Pp -Based on the -.Fx 3.0 -implementation. -.Sh BUGS -The System V -.Fn strtok , -if handed a string containing only delimiter characters, -will not alter the next starting point, so that a call to -.Fn strtok -with a different (or empty) delimiter string -may return a -.Pf non- Dv NULL -value. -Since this implementation always alters the next starting point, -such a sequence of calls would always return -.Dv NULL . +.St -isoC-99 . diff --git a/lib/libc/string/wcstok.c b/lib/libc/string/wcstok.c new file mode 100644 index 0000000000..cdab7672f3 --- /dev/null +++ b/lib/libc/string/wcstok.c @@ -0,0 +1,85 @@ +/*- + * Copyright (c) 1998 Softweyr LLC. All rights reserved. + * + * strtok_r, from Berkeley strtok + * Oct 13, 1998 by Wes Peters + * + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notices, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notices, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE + * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD: src/lib/libc/string/wcstok.c,v 1.3 2007/12/12 18:33:06 wes Exp $ + */ + +#include + +wchar_t * +wcstok(wchar_t * __restrict s, const wchar_t * __restrict delim, + wchar_t ** __restrict last) +{ + const wchar_t *spanp; + wchar_t *tok; + wchar_t c, sc; + + if (s == NULL && (s = *last) == NULL) + return (NULL); + + /* + * Skip (span) leading delimiters (s += wcsspn(s, delim), sort of). + */ +cont: + c = *s++; + for (spanp = delim; (sc = *spanp++) != L'\0';) { + if (c == sc) + goto cont; + } + + if (c == L'\0') { /* no non-delimiter characters */ + *last = NULL; + return (NULL); + } + tok = s - 1; + + /* + * Scan token (scan for delimiters: s += wcscspn(s, delim), sort of). + * Note that delim must have one NUL; we stop if we see that, too. + */ + for (;;) { + c = *s++; + spanp = delim; + do { + if ((sc = *spanp++) == c) { + if (c == L'\0') + s = NULL; + else + s[-1] = L'\0'; + *last = s; + return (tok); + } + } while (sc != L'\0'); + } + /* NOTREACHED */ +} diff --git a/lib/libc/string/wcswidth.3 b/lib/libc/string/wcswidth.3 index d75042657e..94573d7bfc 100644 --- a/lib/libc/string/wcswidth.3 +++ b/lib/libc/string/wcswidth.3 @@ -1,6 +1,3 @@ -.\" $NetBSD: wcswidth.3,v 1.2 2006/10/16 08:48:45 wiz Exp $ -.\" FreeBSD: src/lib/libc/string/wcswidth.3,v 1.2 2002/12/09 14:04:05 ru Exp -.\" .\" Copyright (c) 2002 Tim J. Robbins .\" All rights reserved. .\" @@ -25,6 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" +.\" $FreeBSD: src/lib/libc/string/wcswidth.3,v 1.2 2002/12/09 14:04:05 ru Exp $ .\" $DragonFly: src/lib/libc/string/wcswidth.3,v 1.1 2008/05/31 04:51:55 swildner Exp $ .\" .Dd May 30, 2008 @@ -46,7 +44,7 @@ function determines the number of column positions required for the first .Fa n characters of .Fa pwcs , -or until a nul wide character (L'\e0') is encountered. +or until a null wide character (L'\e0') is encountered. .Sh RETURN VALUES The .Fn wcswidth diff --git a/lib/libc/string/wcswidth.c b/lib/libc/string/wcswidth.c index ba99293f20..36da6b7b3f 100644 --- a/lib/libc/string/wcswidth.c +++ b/lib/libc/string/wcswidth.c @@ -1,6 +1,14 @@ -/*- - * Copyright (c)1999 Citrus Project, - * All rights reserved. +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * (c) UNIX System Laboratories, Inc. + * All or some portions of this file are derived from material licensed + * to the University of California by American Telephone and Telegraph + * Co. or Unix System Laboratories, Inc. and are reproduced herein with + * the permission of UNIX System Laboratories, Inc. + * + * This code is derived from software contributed to Berkeley by + * Paul Borman at Krystal Technologies. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,11 +18,14 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) @@ -23,27 +34,22 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * citrus Id: wcswidth.c,v 1.1 1999/12/29 21:47:45 tshiozak Exp - * - * $NetBSD: wcswidth.c,v 1.1 2000/12/23 23:14:37 itojun Exp $ - * $FreeBSD: src/lib/libc/string/wcswidth.c,v 1.3.2.1 2001/07/11 23:48:38 obrien Exp $ - * $DragonFly: src/lib/libc/string/wcswidth.c,v 1.3 2005/09/18 16:32:34 asmodai Exp $ + * $FreeBSD: src/lib/libc/string/wcswidth.c,v 1.7 2007/01/09 00:28:12 imp Exp $ */ -#include #include int -wcswidth(const wchar_t *s, size_t n) +wcswidth(const wchar_t *pwcs, size_t n) { - int w; + wchar_t wc; + int len, l; - w = 0; - while (n && *s) { - w += wcwidth(*s); - s++; - n--; + len = 0; + while (n-- > 0 && (wc = *pwcs++) != L'\0') { + if ((l = wcwidth(wc)) < 0) + return (-1); + len += l; } - - return w; + return (len); } diff --git a/lib/libc/locale/wcsxfrm.3 b/lib/libc/string/wcsxfrm.3 similarity index 73% rename from lib/libc/locale/wcsxfrm.3 rename to lib/libc/string/wcsxfrm.3 index 47edda63a1..16087fda6e 100644 --- a/lib/libc/locale/wcsxfrm.3 +++ b/lib/libc/string/wcsxfrm.3 @@ -1,5 +1,3 @@ -.\" $NetBSD: wcsxfrm.3,v 1.3 2006/10/16 08:42:16 wiz Exp $ -.\" .\" Copyright (c) 1990, 1991, 1993 .\" The Regents of the University of California. All rights reserved. .\" @@ -15,10 +13,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -37,10 +31,9 @@ .\" .\" @(#)strxfrm.3 8.1 (Berkeley) 6/4/93 .\" FreeBSD: src/lib/libc/string/strxfrm.3,v 1.16 2002/09/06 11:24:06 tjr Exp -.\" FreeBSD: /repoman/r/ncvs/src/lib/libc/string/wcsxfrm.3,v 1.2 2002/12/09 14:04:05 ru Exp -.\" $DragonFly: src/lib/libc/locale/wcsxfrm.3,v 1.1 2008/05/31 04:51:55 swildner Exp $ +.\" $FreeBSD: src/lib/libc/string/wcsxfrm.3,v 1.3 2007/01/09 00:28:12 imp Exp $ .\" -.Dd May 30, 2008 +.Dd October 4, 2002 .Dt WCSXFRM 3 .Os .Sh NAME @@ -55,34 +48,39 @@ .Sh DESCRIPTION The .Fn wcsxfrm -function transforms a nul-terminated wide character string pointed to by +function transforms a null-terminated wide character string pointed to by .Fa src -according to the current locale collation order then copies the -transformed string into +according to the current locale collation order +then copies the transformed string +into .Fa dst . No more than .Fa n wide characters are copied into .Fa dst , -including the terminating nul character added. +including the terminating null character added. If .Fa n is set to 0 -(it helps to determine an actual size needed for transformation), +(it helps to determine an actual size needed +for transformation), .Fa dst -is permitted to be a null pointer. +is permitted to be a +.Dv NULL +pointer. .Pp Comparing two strings using .Fn wcscmp after .Fn wcsxfrm -is equivalent to comparing two original strings with +is equivalent to comparing +two original strings with .Fn wcscoll . .Sh RETURN VALUES Upon successful completion, .Fn wcsxfrm returns the length of the transformed string not including -the terminating nul character. +the terminating null character. If this value is .Fa n or more, the contents of @@ -96,13 +94,29 @@ are indeterminate. .Sh STANDARDS The .Fn wcsxfrm -function conforms to +function +conforms to .St -isoC-99 . .Sh BUGS The current implementation of .Fn wcsxfrm -function disregards -.Dv LC_COLLATE -locales, and falls back to using the +only works in single-byte +.Dv LC_CTYPE +locales, and falls back to using .Fn wcsncpy -function. +in locales with extended character sets. +.Pp +Comparing two strings using +.Fn wcscmp +after +.Fn wcsxfrm +is +.Em not +always equivalent to comparison with +.Fn wcscoll ; +.Fn wcsxfrm +only stores information about primary collation weights into +.Fa dst , +whereas +.Fn wcscoll +compares characters using both primary and secondary weights. diff --git a/lib/libc/string/strxfrm.c b/lib/libc/string/wcsxfrm.c similarity index 57% copy from lib/libc/string/strxfrm.c copy to lib/libc/string/wcsxfrm.c index 434a5550f9..f2def4f12a 100644 --- a/lib/libc/string/strxfrm.c +++ b/lib/libc/string/wcsxfrm.c @@ -24,51 +24,59 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $FreeBSD: src/lib/libc/string/strxfrm.c,v 1.11.2.1 2001/07/09 23:30:07 obrien Exp $ - * $DragonFly: src/lib/libc/string/strxfrm.c,v 1.3 2005/09/18 16:32:34 asmodai Exp $ + * FreeBSD: src/lib/libc/string/strxfrm.c,v 1.15 2002/09/06 11:24:06 tjr Exp + * $FreeBSD: src/lib/libc/string/wcsxfrm.c,v 1.3 2004/04/07 09:47:56 tjr Exp $ */ #include #include +#include #include "collate.h" +static char *__mbsdup(const wchar_t *); + +/* + * Placeholder wcsxfrm() implementation. See wcscoll.c for a description of + * the logic used. + */ size_t -strxfrm(char *dest, const char *src, size_t len) +wcsxfrm(wchar_t * __restrict dest, const wchar_t * __restrict src, size_t len) { int prim, sec, l; size_t slen; - char *s, *ss; + char *mbsrc, *s, *ss; - if (!*src) { - if (len > 0) - *dest = '\0'; - return 0; + if (*src == L'\0') { + if (len != 0) + *dest = L'\0'; + return (0); } - if (__collate_load_error) { - slen = strlen(src); + if (__collate_load_error || MB_CUR_MAX > 1) { + slen = wcslen(src); if (len > 0) { if (slen < len) - strcpy(dest, src); + wcscpy(dest, src); else { - strncpy(dest, src, len - 1); - dest[len - 1] = '\0'; + wcsncpy(dest, src, len - 1); + dest[len - 1] = L'\0'; } } - return slen; + return (slen); } + mbsrc = __mbsdup(src); slen = 0; prim = sec = 0; - ss = s = __collate_substitute(src); - while (*s) { - while (*s && !prim) { + ss = s = __collate_substitute(mbsrc); + while (*s != '\0') { + while (*s != '\0' && prim == 0) { __collate_lookup(s, &l, &prim, &sec); s += l; } - if (prim) { + if (prim != 0) { if (len > 1) { - *dest++ = (char)prim; + *dest++ = (wchar_t)prim; len--; } slen++; @@ -76,8 +84,30 @@ strxfrm(char *dest, const char *src, size_t len) } } free(ss); - if (len > 0) - *dest = '\0'; + free(mbsrc); + if (len != 0) + *dest = L'\0'; + + return (slen); +} + +static char * +__mbsdup(const wchar_t *ws) +{ + static const mbstate_t initial; + mbstate_t st; + const wchar_t *wcp; + size_t len; + char *mbs; + + wcp = ws; + st = initial; + if ((len = wcsrtombs(NULL, &wcp, 0, &st)) == (size_t)-1) + return (NULL); + if ((mbs = malloc(len + 1)) == NULL) + return (NULL); + st = initial; + wcsrtombs(mbs, &ws, len + 1, &st); - return slen; + return (mbs); } diff --git a/lib/libc/string/wmemchr.3 b/lib/libc/string/wmemchr.3 index 19a3422f78..1a3624a619 100644 --- a/lib/libc/string/wmemchr.3 +++ b/lib/libc/string/wmemchr.3 @@ -15,10 +15,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -37,10 +33,10 @@ .\" .\" from: @(#)strcpy.3 8.1 (Berkeley) 6/4/93 .\" -.\" $FreeBSD: src/lib/libc/string/wmemchr.3,v 1.3.2.2 2001/12/14 18:33:59 ru Exp $ +.\" $FreeBSD: src/lib/libc/string/wmemchr.3,v 1.8 2007/01/09 00:28:12 imp Exp $ .\" $DragonFly: src/lib/libc/string/wmemchr.3,v 1.2 2003/06/17 04:26:47 dillon Exp $ .\" -.Dd December 22, 2000 +.Dd August 13, 2005 .Dt WMEMCHR 3 .Os .Sh NAME @@ -54,6 +50,7 @@ .Nm wcscmp , .Nm wcscpy , .Nm wcscspn , +.Nm wcsdup , .Nm wcslcat , .Nm wcslcpy , .Nm wcslen , @@ -89,6 +86,8 @@ .Fn wcscpy "wchar_t * restrict s1" "const wchar_t * restrict s2" .Ft size_t .Fn wcscspn "const wchar_t *s1" "const wchar_t *s2" +.Ft wchar_t * +.Fn wcsdup "const wchar_t *s" .Ft size_t .Fn wcslcat "wchar_t *s1" "const wchar_t *s2" "size_t n" .Ft size_t @@ -108,7 +107,7 @@ .Ft size_t .Fn wcsspn "const wchar_t *s1" "const wchar_t *s2" .Ft wchar_t * -.Fn wcsstr "const wchar_t *s1" "const wchar_t *s2" +.Fn wcsstr "const wchar_t * restrict s1" "const wchar_t * restrict s2" .Sh DESCRIPTION The functions implement string manipulation operations over wide character strings. @@ -126,6 +125,7 @@ counterpart, such as .Xr strcmp 3 , .Xr strcpy 3 , .Xr strcspn 3 , +.Xr strdup 3 , .Xr strlcat 3 , .Xr strlcpy 3 , .Xr strlen 3 , @@ -140,7 +140,8 @@ counterpart, such as These functions conform to .St -isoC-99 , with the exception of -.Fn wcslcat +.Fn wcsdup , +.Fn wcslcat , and .Fn wcslcpy , which are extensions. diff --git a/lib/libc/string/wmemchr.c b/lib/libc/string/wmemchr.c index 5afa4ab625..5e1d15aae6 100644 --- a/lib/libc/string/wmemchr.c +++ b/lib/libc/string/wmemchr.c @@ -25,7 +25,7 @@ * * citrus Id: wmemchr.c,v 1.2 2000/12/20 14:08:31 itojun Exp * $NetBSD: wmemchr.c,v 1.1 2000/12/23 23:14:37 itojun Exp $ - * $FreeBSD: src/lib/libc/string/wmemchr.c,v 1.3.2.1 2001/07/11 23:48:38 obrien Exp $ + * $FreeBSD: src/lib/libc/string/wmemchr.c,v 1.6 2002/09/21 00:29:23 tjr Exp $ * $DragonFly: src/lib/libc/string/wmemchr.c,v 1.3 2005/04/28 13:25:12 joerg Exp $ */ @@ -44,5 +44,5 @@ wmemchr(const wchar_t *s, wchar_t c, size_t n) } s++; } - return NULL; + return (NULL); } diff --git a/lib/libc/string/wmemcmp.c b/lib/libc/string/wmemcmp.c index 6a3b84b41f..788425cf90 100644 --- a/lib/libc/string/wmemcmp.c +++ b/lib/libc/string/wmemcmp.c @@ -25,11 +25,10 @@ * * citrus Id: wmemcmp.c,v 1.2 2000/12/20 14:08:31 itojun Exp * $NetBSD: wmemcmp.c,v 1.1 2000/12/23 23:14:37 itojun Exp $ - * $FreeBSD: src/lib/libc/string/wmemcmp.c,v 1.3.2.1 2001/07/11 23:48:38 obrien Exp $ + * $FreeBSD: src/lib/libc/string/wmemcmp.c,v 1.6 2002/09/21 00:29:23 tjr Exp $ * $DragonFly: src/lib/libc/string/wmemcmp.c,v 1.3 2005/09/18 16:32:34 asmodai Exp $ */ -#include #include int @@ -40,10 +39,10 @@ wmemcmp(const wchar_t *s1, const wchar_t *s2, size_t n) for (i = 0; i < n; i++) { if (*s1 != *s2) { /* wchar might be unsigned */ - return *s1 > *s2 ? 1 : -1; + return (*s1 > *s2 ? 1 : -1); } s1++; s2++; } - return 0; + return (0); } diff --git a/lib/libc/string/wmemcpy.c b/lib/libc/string/wmemcpy.c index c54e66b3de..30c714bdca 100644 --- a/lib/libc/string/wmemcpy.c +++ b/lib/libc/string/wmemcpy.c @@ -25,16 +25,15 @@ * * citrus Id: wmemcpy.c,v 1.2 2000/12/20 14:08:31 itojun Exp * $NetBSD: wmemcpy.c,v 1.1 2000/12/23 23:14:37 itojun Exp $ - * $FreeBSD: src/lib/libc/string/wmemcpy.c,v 1.3.2.1 2001/07/11 23:48:38 obrien Exp $ + * $FreeBSD: src/lib/libc/string/wmemcpy.c,v 1.7 2002/09/21 00:29:23 tjr Exp $ * $DragonFly: src/lib/libc/string/wmemcpy.c,v 1.3 2005/09/18 16:32:34 asmodai Exp $ */ -#include #include #include wchar_t * -wmemcpy(wchar_t *d, const wchar_t *s, size_t n) +wmemcpy(wchar_t * __restrict d, const wchar_t * __restrict s, size_t n) { return (wchar_t *)memcpy(d, s, n * sizeof(wchar_t)); diff --git a/lib/libc/string/wmemmove.c b/lib/libc/string/wmemmove.c index b884985be5..ff65453e06 100644 --- a/lib/libc/string/wmemmove.c +++ b/lib/libc/string/wmemmove.c @@ -25,11 +25,10 @@ * * citrus Id: wmemmove.c,v 1.2 2000/12/20 14:08:31 itojun Exp * $NetBSD: wmemmove.c,v 1.1 2000/12/23 23:14:37 itojun Exp $ - * $FreeBSD: src/lib/libc/string/wmemmove.c,v 1.3.2.1 2001/07/11 23:48:38 obrien Exp $ + * $FreeBSD: src/lib/libc/string/wmemmove.c,v 1.6 2002/09/21 00:29:23 tjr Exp $ * $DragonFly: src/lib/libc/string/wmemmove.c,v 1.3 2005/09/18 16:32:34 asmodai Exp $ */ -#include #include #include diff --git a/lib/libc/string/wmemset.c b/lib/libc/string/wmemset.c index a593f693c6..d43b75b0a7 100644 --- a/lib/libc/string/wmemset.c +++ b/lib/libc/string/wmemset.c @@ -25,11 +25,10 @@ * * citrus Id: wmemset.c,v 1.2 2000/12/20 14:08:31 itojun Exp * $NetBSD: wmemset.c,v 1.1 2000/12/23 23:14:37 itojun Exp $ - * $FreeBSD: src/lib/libc/string/wmemset.c,v 1.3.2.1 2001/07/11 23:48:38 obrien Exp $ + * $FreeBSD: src/lib/libc/string/wmemset.c,v 1.6 2002/09/21 00:29:23 tjr Exp $ * $DragonFly: src/lib/libc/string/wmemset.c,v 1.3 2005/09/18 16:32:34 asmodai Exp $ */ -#include #include wchar_t * diff --git a/lib/libmagic/config.h b/lib/libmagic/config.h index 51f9c3ac71..ee4be9bb13 100644 --- a/lib/libmagic/config.h +++ b/lib/libmagic/config.h @@ -72,7 +72,7 @@ #define HAVE_STRING_H 1 /* Define to 1 if you have the `strndup' function. */ -/* #undef HAVE_STRNDUP */ +#define HAVE_STRNDUP 1 /* Define to 1 if you have the `strtof' function. */ /* #undef HAVE_STRTOF */ -- 2.41.0