diff --git a/HACKING.txt b/HACKING.txt index f614b6904..6c142e13f 100644 --- a/HACKING.txt +++ b/HACKING.txt @@ -84,9 +84,7 @@ libc/ netbsd/ stdio/ stdlib/ - string/ unistd/ - wchar/ # These are legacy files of unknown provenance. In the past, bionic was a # mess of random versions of random files from all three of FreeBSD, NetBSD, # and OpenBSD! We've been working to clean that up, but these directories diff --git a/libc/Android.mk b/libc/Android.mk index 1bee427e7..9ab7e8e24 100644 --- a/libc/Android.mk +++ b/libc/Android.mk @@ -45,7 +45,6 @@ libc_common_src_files := \ bionic/ether_aton.c \ bionic/ether_ntoa.c \ bionic/fdprintf.c \ - bionic/flockfile.c \ bionic/ftime.c \ bionic/fts.c \ bionic/getdtablesize.c \ @@ -109,8 +108,6 @@ libc_common_src_files := \ unistd/syslog.c \ unistd/system.c \ unistd/time.c \ - wchar/wcswidth.c \ - wchar/wcsxfrm.c \ # Fortify implementations of libc functions. libc_common_src_files += \ @@ -149,6 +146,7 @@ libc_bionic_src_files := \ bionic/eventfd_read.cpp \ bionic/eventfd_write.cpp \ bionic/ffs.cpp \ + bionic/flockfile.cpp \ bionic/fork.cpp \ bionic/futimens.cpp \ bionic/getauxval.cpp \ @@ -261,7 +259,6 @@ libc_upstream_freebsd_src_files := \ upstream-freebsd/lib/libc/string/wcscspn.c \ upstream-freebsd/lib/libc/string/wcsdup.c \ upstream-freebsd/lib/libc/string/wcslcat.c \ - upstream-freebsd/lib/libc/string/wcslcpy.c \ upstream-freebsd/lib/libc/string/wcsncasecmp.c \ upstream-freebsd/lib/libc/string/wcsncat.c \ upstream-freebsd/lib/libc/string/wcsncmp.c \ @@ -319,6 +316,80 @@ libc_upstream_netbsd_src_files := \ upstream-netbsd/lib/libc/thread-stub/__isthreaded.c \ upstream-netbsd/lib/libc/unistd/killpg.c \ +libc_upstream_openbsd_src_files := \ + upstream-openbsd/lib/libc/gen/exec.c \ + upstream-openbsd/lib/libc/gen/ftok.c \ + upstream-openbsd/lib/libc/gen/fnmatch.c \ + upstream-openbsd/lib/libc/gen/toupper_.c \ + upstream-openbsd/lib/libc/gen/tolower_.c \ + upstream-openbsd/lib/libc/string/strstr.c \ + upstream-openbsd/lib/libc/string/strsep.c \ + upstream-openbsd/lib/libc/string/wcslcpy.c \ + upstream-openbsd/lib/libc/string/strcasecmp.c \ + upstream-openbsd/lib/libc/string/strdup.c \ + upstream-openbsd/lib/libc/string/strcspn.c \ + upstream-openbsd/lib/libc/string/strtok.c \ + upstream-openbsd/lib/libc/string/strpbrk.c \ + upstream-openbsd/lib/libc/string/strspn.c \ + upstream-openbsd/lib/libc/string/wcswidth.c \ + upstream-openbsd/lib/libc/string/strndup.c \ + upstream-openbsd/lib/libc/stdio/ungetc.c \ + upstream-openbsd/lib/libc/stdio/vsnprintf.c \ + upstream-openbsd/lib/libc/stdio/tmpnam.c \ + upstream-openbsd/lib/libc/stdio/setbuffer.c \ + upstream-openbsd/lib/libc/stdio/fsetpos.c \ + upstream-openbsd/lib/libc/stdio/setbuf.c \ + upstream-openbsd/lib/libc/stdio/vprintf.c \ + upstream-openbsd/lib/libc/stdio/perror.c \ + upstream-openbsd/lib/libc/stdio/remove.c \ + upstream-openbsd/lib/libc/stdio/getc.c \ + upstream-openbsd/lib/libc/stdio/funopen.c \ + upstream-openbsd/lib/libc/stdio/ferror.c \ + upstream-openbsd/lib/libc/stdio/putchar.c \ + upstream-openbsd/lib/libc/stdio/vscanf.c \ + upstream-openbsd/lib/libc/stdio/fflush.c \ + upstream-openbsd/lib/libc/stdio/fpurge.c \ + upstream-openbsd/lib/libc/stdio/fputs.c \ + upstream-openbsd/lib/libc/stdio/tempnam.c \ + upstream-openbsd/lib/libc/stdio/printf.c \ + upstream-openbsd/lib/libc/stdio/fileno.c \ + upstream-openbsd/lib/libc/stdio/stdio.c \ + upstream-openbsd/lib/libc/stdio/getdelim.c \ + upstream-openbsd/lib/libc/stdio/fseek.c \ + upstream-openbsd/lib/libc/stdio/fputc.c \ + upstream-openbsd/lib/libc/stdio/fgetln.c \ + upstream-openbsd/lib/libc/stdio/fdopen.c \ + upstream-openbsd/lib/libc/stdio/fgets.c \ + upstream-openbsd/lib/libc/stdio/fgetc.c \ + upstream-openbsd/lib/libc/stdio/feof.c \ + upstream-openbsd/lib/libc/stdio/vasprintf.c \ + upstream-openbsd/lib/libc/stdio/refill.c \ + upstream-openbsd/lib/libc/stdio/getline.c \ + upstream-openbsd/lib/libc/stdio/rewind.c \ + upstream-openbsd/lib/libc/stdio/wbuf.c \ + upstream-openbsd/lib/libc/stdio/gets.c \ + upstream-openbsd/lib/libc/stdio/fgetpos.c \ + upstream-openbsd/lib/libc/stdio/fscanf.c \ + upstream-openbsd/lib/libc/stdio/putc.c \ + upstream-openbsd/lib/libc/stdio/vsprintf.c \ + upstream-openbsd/lib/libc/stdio/rget.c \ + upstream-openbsd/lib/libc/stdio/clrerr.c \ + upstream-openbsd/lib/libc/stdio/scanf.c \ + upstream-openbsd/lib/libc/stdio/puts.c \ + upstream-openbsd/lib/libc/stdio/getchar.c \ + upstream-openbsd/lib/libc/stdio/ftell.c \ + upstream-openbsd/lib/libc/stdio/fwalk.c \ + upstream-openbsd/lib/libc/stdlib/strtoul.c \ + upstream-openbsd/lib/libc/stdlib/strtoull.c \ + upstream-openbsd/lib/libc/stdlib/atoi.c \ + upstream-openbsd/lib/libc/stdlib/atoll.c \ + upstream-openbsd/lib/libc/stdlib/atol.c \ + upstream-openbsd/lib/libc/stdlib/strtoimax.c \ + upstream-openbsd/lib/libc/stdlib/strtoumax.c \ + upstream-openbsd/lib/libc/stdlib/strtoll.c \ + upstream-openbsd/lib/libc/stdlib/strtol.c \ + upstream-openbsd/lib/libc/locale/wcsxfrm.c \ + libc_arch_static_src_files := \ bionic/dl_iterate_phdr_static.cpp \ @@ -514,7 +585,7 @@ include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) -LOCAL_SRC_FILES := $(call all-c-files-under,upstream-openbsd) +LOCAL_SRC_FILES := $(libc_upstream_openbsd_src_files) LOCAL_CFLAGS := \ $(libc_common_cflags) \ -I$(LOCAL_PATH)/upstream-openbsd \ diff --git a/libc/arch-arm/arm.mk b/libc/arch-arm/arm.mk index 55f99783a..89a1ce050 100644 --- a/libc/arch-arm/arm.mk +++ b/libc/arch-arm/arm.mk @@ -10,19 +10,13 @@ libc_bionic_src_files_arm := \ bionic/mmap.cpp libc_common_src_files_arm += \ + bionic/index.cpp \ bionic/memchr.c \ bionic/memmove.c.arm \ bionic/memrchr.c \ bionic/strchr.cpp \ bionic/strnlen.c \ - string/bcopy.c \ - string/index.c \ - string/strlcat.c \ - string/strlcpy.c \ - string/strncat.c \ - string/strncmp.c \ - string/strncpy.c \ - string/strrchr.c \ + bionic/strrchr.cpp \ upstream-freebsd/lib/libc/string/wcscat.c \ upstream-freebsd/lib/libc/string/wcschr.c \ upstream-freebsd/lib/libc/string/wcscmp.c \ @@ -30,6 +24,12 @@ libc_common_src_files_arm += \ upstream-freebsd/lib/libc/string/wcslen.c \ upstream-freebsd/lib/libc/string/wcsrchr.c \ upstream-freebsd/lib/libc/string/wmemcmp.c \ + upstream-openbsd/lib/libc/string/bcopy.c \ + upstream-openbsd/lib/libc/string/strlcat.c \ + upstream-openbsd/lib/libc/string/strlcpy.c \ + upstream-openbsd/lib/libc/string/strncat.c \ + upstream-openbsd/lib/libc/string/strncmp.c \ + upstream-openbsd/lib/libc/string/strncpy.c \ # The C++ fortify function implementations for which there is an # arm assembler version. diff --git a/libc/arch-arm/generic/generic.mk b/libc/arch-arm/generic/generic.mk index 063d89314..2bc84e02d 100644 --- a/libc/arch-arm/generic/generic.mk +++ b/libc/arch-arm/generic/generic.mk @@ -1,9 +1,9 @@ libc_bionic_src_files_arm += \ arch-arm/generic/bionic/memcpy.S \ arch-arm/generic/bionic/memset.S \ - string/strcat.c \ arch-arm/generic/bionic/strcmp.S \ arch-arm/generic/bionic/strcpy.S \ arch-arm/generic/bionic/strlen.c \ bionic/__strcat_chk.cpp \ bionic/__strcpy_chk.cpp \ + upstream-openbsd/lib/libc/string/strcat.c \ diff --git a/libc/arch-arm64/arm64.mk b/libc/arch-arm64/arm64.mk index a64594917..a0eb95417 100644 --- a/libc/arch-arm64/arm64.mk +++ b/libc/arch-arm64/arm64.mk @@ -1,27 +1,17 @@ # arm64 specific configs libc_common_src_files_arm64 := \ - bionic/memchr.c \ - bionic/memcmp.c \ - bionic/memcpy.c \ - bionic/memmove.c \ - bionic/memrchr.c \ - bionic/memset.c \ + bionic/index.cpp \ + bionic/memchr.c \ + bionic/__memcmp16.cpp \ + bionic/memcmp.c \ + bionic/memcpy.c \ + bionic/memmove.c \ + bionic/memrchr.c \ + bionic/memset.c \ bionic/strchr.cpp \ - bionic/strnlen.c \ - string/bcopy.c \ - string/index.c \ - string/memcmp16.c \ - string/strcat.c \ - string/strcmp.c \ - string/strcpy.c \ - string/strlcat.c \ - string/strlcpy.c \ - string/strlen.c \ - string/strncat.c \ - string/strncmp.c \ - string/strncpy.c \ - string/strrchr.c \ + bionic/strnlen.c \ + bionic/strrchr.cpp \ upstream-freebsd/lib/libc/string/wcscat.c \ upstream-freebsd/lib/libc/string/wcschr.c \ upstream-freebsd/lib/libc/string/wcscmp.c \ @@ -29,6 +19,16 @@ libc_common_src_files_arm64 := \ upstream-freebsd/lib/libc/string/wcslen.c \ upstream-freebsd/lib/libc/string/wcsrchr.c \ upstream-freebsd/lib/libc/string/wmemcmp.c \ + upstream-openbsd/lib/libc/string/bcopy.c \ + upstream-openbsd/lib/libc/string/strcat.c \ + upstream-openbsd/lib/libc/string/strcmp.c \ + upstream-openbsd/lib/libc/string/strcpy.c \ + upstream-openbsd/lib/libc/string/strlcat.c \ + upstream-openbsd/lib/libc/string/strlcpy.c \ + upstream-openbsd/lib/libc/string/strlen.c \ + upstream-openbsd/lib/libc/string/strncat.c \ + upstream-openbsd/lib/libc/string/strncmp.c \ + upstream-openbsd/lib/libc/string/strncpy.c \ # Fortify implementations of libc functions. libc_common_src_files_arm64 += \ diff --git a/libc/arch-mips/mips.mk b/libc/arch-mips/mips.mk index 0fa1ed6eb..1af4a651b 100644 --- a/libc/arch-mips/mips.mk +++ b/libc/arch-mips/mips.mk @@ -10,23 +10,14 @@ libc_bionic_src_files_mips += \ bionic/mmap.cpp libc_common_src_files_mips += \ + bionic/index.cpp \ bionic/memchr.c \ bionic/memcmp.c \ bionic/memmove.c \ bionic/memrchr.c \ bionic/strchr.cpp \ bionic/strnlen.c \ - string/bcopy.c \ - string/index.c \ - string/strcat.c \ - string/strcmp.c \ - string/strcpy.c \ - string/strlcat.c \ - string/strlcpy.c \ - string/strncat.c \ - string/strncmp.c \ - string/strncpy.c \ - string/strrchr.c \ + bionic/strrchr.cpp \ upstream-freebsd/lib/libc/string/wcscat.c \ upstream-freebsd/lib/libc/string/wcschr.c \ upstream-freebsd/lib/libc/string/wcscmp.c \ @@ -34,6 +25,15 @@ libc_common_src_files_mips += \ upstream-freebsd/lib/libc/string/wcslen.c \ upstream-freebsd/lib/libc/string/wcsrchr.c \ upstream-freebsd/lib/libc/string/wmemcmp.c \ + upstream-openbsd/lib/libc/string/bcopy.c \ + upstream-openbsd/lib/libc/string/strcat.c \ + upstream-openbsd/lib/libc/string/strcmp.c \ + upstream-openbsd/lib/libc/string/strcpy.c \ + upstream-openbsd/lib/libc/string/strlcat.c \ + upstream-openbsd/lib/libc/string/strlcpy.c \ + upstream-openbsd/lib/libc/string/strncat.c \ + upstream-openbsd/lib/libc/string/strncmp.c \ + upstream-openbsd/lib/libc/string/strncpy.c \ # Fortify implementations of libc functions. libc_common_src_files_mips += \ diff --git a/libc/arch-mips64/mips64.mk b/libc/arch-mips64/mips64.mk index 11ceb40b3..28806e183 100644 --- a/libc/arch-mips64/mips64.mk +++ b/libc/arch-mips64/mips64.mk @@ -1,30 +1,31 @@ # mips64 specific configs libc_common_src_files_mips64 := \ - bionic/memchr.c \ - bionic/memcmp.c \ - bionic/memmove.c \ - bionic/memrchr.c \ - bionic/strchr.cpp \ - bionic/strnlen.c \ - string/bcopy.c \ - string/index.c \ - string/strcat.c \ - string/strcmp.c \ - string/strcpy.c \ - string/strlcat.c \ - string/strlcpy.c \ - string/strncat.c \ - string/strncmp.c \ - string/strncpy.c \ - string/strrchr.c \ - upstream-freebsd/lib/libc/string/wcscat.c \ - upstream-freebsd/lib/libc/string/wcschr.c \ - upstream-freebsd/lib/libc/string/wcscmp.c \ - upstream-freebsd/lib/libc/string/wcscpy.c \ - upstream-freebsd/lib/libc/string/wcslen.c \ - upstream-freebsd/lib/libc/string/wcsrchr.c \ - upstream-freebsd/lib/libc/string/wmemcmp.c \ + bionic/index.cpp \ + bionic/memchr.c \ + bionic/memcmp.c \ + bionic/memmove.c \ + bionic/memrchr.c \ + bionic/strchr.cpp \ + bionic/strnlen.c \ + bionic/strrchr.cpp \ + upstream-freebsd/lib/libc/string/wcscat.c \ + upstream-freebsd/lib/libc/string/wcschr.c \ + upstream-freebsd/lib/libc/string/wcscmp.c \ + upstream-freebsd/lib/libc/string/wcscpy.c \ + upstream-freebsd/lib/libc/string/wcslen.c \ + upstream-freebsd/lib/libc/string/wcsrchr.c \ + upstream-freebsd/lib/libc/string/wmemcmp.c \ + upstream-openbsd/lib/libc/string/bcopy.c \ + upstream-openbsd/lib/libc/string/strcat.c \ + upstream-openbsd/lib/libc/string/strcmp.c \ + upstream-openbsd/lib/libc/string/strcpy.c \ + upstream-openbsd/lib/libc/string/strlcat.c \ + upstream-openbsd/lib/libc/string/strlcpy.c \ + upstream-openbsd/lib/libc/string/strlen.c \ + upstream-openbsd/lib/libc/string/strncat.c \ + upstream-openbsd/lib/libc/string/strncmp.c \ + upstream-openbsd/lib/libc/string/strncpy.c \ # Fortify implementations of libc functions. libc_common_src_files_mips64 += \ @@ -54,10 +55,8 @@ libc_bionic_src_files_mips64 := \ # FIXME TODO ## libc_bionic_src_files_mips64 += arch-mips64/string/memcpy.S ## libc_bionic_src_files_mips64 += arch-mips64/string/memset.S -## libc_bionic_src_files_mips64 += arch-mips64/string/mips_strlen.c libc_bionic_src_files_mips64 += bionic/memcpy.c libc_bionic_src_files_mips64 += bionic/memset.c -libc_bionic_src_files_mips64 += string/strlen.c libc_crt_target_cflags_mips64 := \ diff --git a/libc/arch-x86/x86.mk b/libc/arch-x86/x86.mk index b024acbf8..846bb4262 100644 --- a/libc/arch-x86/x86.mk +++ b/libc/arch-x86/x86.mk @@ -58,15 +58,16 @@ libc_bionic_src_files_x86 += \ arch-x86/string/strncmp.S \ arch-x86/string/strcat.S \ arch-x86/string/memcmp.S \ - string/memcmp16.c \ - string/strcpy.c \ - string/strncat.c \ - string/strncpy.c \ - string/strlcat.c \ - string/strlcpy.c \ + bionic/__memcmp16.cpp \ upstream-freebsd/lib/libc/string/wcscpy.c \ upstream-freebsd/lib/libc/string/wcscat.c \ - upstream-freebsd/lib/libc/string/wmemcmp.c + upstream-freebsd/lib/libc/string/wmemcmp.c \ + upstream-openbsd/lib/libc/string/strcpy.c \ + upstream-openbsd/lib/libc/string/strlcat.c \ + upstream-openbsd/lib/libc/string/strlcpy.c \ + upstream-openbsd/lib/libc/string/strncat.c \ + upstream-openbsd/lib/libc/string/strncpy.c \ + endif libc_bionic_src_files_x86 += \ diff --git a/libc/arch-x86_64/x86_64.mk b/libc/arch-x86_64/x86_64.mk index 44831a6d9..9171c50f6 100644 --- a/libc/arch-x86_64/x86_64.mk +++ b/libc/arch-x86_64/x86_64.mk @@ -1,6 +1,7 @@ # x86_64 specific configs libc_common_src_files_x86_64 := \ + bionic/index.cpp \ bionic/memchr.c \ bionic/memcmp.c \ bionic/memcpy.c \ @@ -9,18 +10,7 @@ libc_common_src_files_x86_64 := \ bionic/memset.c \ bionic/strchr.cpp \ bionic/strnlen.c \ - string/bcopy.c \ - string/index.c \ - string/strcat.c \ - string/strcmp.c \ - string/strcpy.c \ - string/strlcat.c \ - string/strlcpy.c \ - string/strlen.c \ - string/strncat.c \ - string/strncmp.c \ - string/strncpy.c \ - string/strrchr.c \ + bionic/strrchr.cpp \ upstream-freebsd/lib/libc/string/wcscat.c \ upstream-freebsd/lib/libc/string/wcschr.c \ upstream-freebsd/lib/libc/string/wcscmp.c \ @@ -28,6 +18,16 @@ libc_common_src_files_x86_64 := \ upstream-freebsd/lib/libc/string/wcslen.c \ upstream-freebsd/lib/libc/string/wcsrchr.c \ upstream-freebsd/lib/libc/string/wmemcmp.c \ + upstream-openbsd/lib/libc/string/bcopy.c \ + upstream-openbsd/lib/libc/string/strcat.c \ + upstream-openbsd/lib/libc/string/strcmp.c \ + upstream-openbsd/lib/libc/string/strcpy.c \ + upstream-openbsd/lib/libc/string/strlcat.c \ + upstream-openbsd/lib/libc/string/strlcpy.c \ + upstream-openbsd/lib/libc/string/strlen.c \ + upstream-openbsd/lib/libc/string/strncat.c \ + upstream-openbsd/lib/libc/string/strncmp.c \ + upstream-openbsd/lib/libc/string/strncpy.c \ # Fortify implementations of libc functions. libc_common_src_files_x86_64 += \ @@ -51,8 +51,7 @@ libc_bionic_src_files_x86_64 := \ arch-x86_64/bionic/sigsetjmp.S \ arch-x86_64/bionic/syscall.S \ arch-x86_64/bionic/vfork.S \ - string/memcmp16.c \ - + bionic/__memcmp16.cpp \ libc_crt_target_cflags_x86_64 += \ -m64 \ diff --git a/libc/string/memcmp16.c b/libc/bionic/__memcmp16.cpp similarity index 93% rename from libc/string/memcmp16.c rename to libc/bionic/__memcmp16.cpp index 12677221b..bee0df162 100644 --- a/libc/string/memcmp16.c +++ b/libc/bionic/__memcmp16.cpp @@ -30,14 +30,13 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include -/* Unoptimised version of __memcmp16 */ -int __memcmp16(const unsigned short *ptr1, const unsigned short *ptr2, size_t n) -{ - size_t i; +// Unoptimized version of __memcmp16. +int __memcmp16(const unsigned short *ptr1, const unsigned short *ptr2, size_t n) { - for (i = 0; i < n; i++) { - if (*ptr1 != *ptr2) + for (size_t i = 0; i < n; i++) { + if (*ptr1 != *ptr2) { return *ptr1 - *ptr2; + } ptr1++; ptr2++; } diff --git a/libc/bionic/__strrchr_chk.cpp b/libc/bionic/__strrchr_chk.cpp index 4037207c2..69198c042 100644 --- a/libc/bionic/__strrchr_chk.cpp +++ b/libc/bionic/__strrchr_chk.cpp @@ -1,4 +1,3 @@ -/* $OpenBSD: rindex.c,v 1.6 2005/08/08 08:05:37 espie Exp $ */ /* * Copyright (c) 1988 Regents of the University of California. * All rights reserved. diff --git a/libc/bionic/flockfile.c b/libc/bionic/flockfile.cpp similarity index 63% rename from libc/bionic/flockfile.c rename to libc/bionic/flockfile.cpp index 368fb1582..c60497d3d 100644 --- a/libc/bionic/flockfile.c +++ b/libc/bionic/flockfile.cpp @@ -26,50 +26,33 @@ * SUCH DAMAGE. */ -/* implement flockfile(), ftrylockfile() and funlockfile() - * - * we can't use the OpenBSD implementation which uses kernel-specific - * APIs not available on Linux. - * - * Instead, we use a pthread_mutex_t within the FILE* internal state. - * See fileext.h for details. - * - * the behaviour, if fclose() is called while the corresponding - * file is locked is totally undefined. - */ -#include -#include #include +#include + #include "fileext.h" +// We can't use the OpenBSD implementation which uses kernel-specific +// APIs not available on Linux. Instead we use a pthread_mutex_t within +// struct __sfileext (see fileext.h). -void -flockfile(FILE * fp) -{ - if (fp != NULL) { - _FLOCK_LOCK(fp); - } +void flockfile(FILE* fp) { + if (fp != NULL) { + pthread_mutex_lock(&_FLOCK(fp)); + } } +int ftrylockfile(FILE* fp) { + // The specification for ftrylockfile() says it returns 0 on success, + // or non-zero on error. So return an errno code directly on error. + if (fp == NULL) { + return EINVAL; + } -int -ftrylockfile(FILE *fp) -{ - /* The specification for ftrylockfile() says it returns 0 on success, - * or non-zero on error. So return an errno code directly on error. - */ - int ret = EINVAL; - - if (fp != NULL) { - ret = _FLOCK_TRYLOCK(fp); - } - return ret; + return pthread_mutex_trylock(&_FLOCK(fp)); } -void -funlockfile(FILE * fp) -{ - if (fp != NULL) { - _FLOCK_UNLOCK(fp); - } +void funlockfile(FILE* fp) { + if (fp != NULL) { + pthread_mutex_unlock(&_FLOCK(fp)); + } } diff --git a/libc/string/index.c b/libc/bionic/index.cpp similarity index 88% rename from libc/string/index.c rename to libc/bionic/index.cpp index 81bfba490..cc22d81fa 100644 --- a/libc/string/index.c +++ b/libc/bionic/index.cpp @@ -1,4 +1,3 @@ -/* $OpenBSD: index.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ /*- * Copyright (c) 1990 The Regents of the University of California. * All rights reserved. @@ -28,16 +27,9 @@ * SUCH DAMAGE. */ +#undef _FORTIFY_SOURCE #include -char * -index(const char *p, int ch) -{ - for (;; ++p) { - if (*p == (char) ch) - return((char *)p); - if (!*p) - return((char *)NULL); - } - /* NOTREACHED */ +char* index(const char* p, int ch) { + return __strchr_chk(p, ch, __BIONIC_FORTIFY_UNKNOWN_SIZE); } diff --git a/libc/bionic/strchr.cpp b/libc/bionic/strchr.cpp index 029fbd9d7..5bd3f19f8 100644 --- a/libc/bionic/strchr.cpp +++ b/libc/bionic/strchr.cpp @@ -30,6 +30,6 @@ #undef _FORTIFY_SOURCE #include -extern "C" char* strchr(const char* p, int ch) { +char* strchr(const char* p, int ch) { return __strchr_chk(p, ch, __BIONIC_FORTIFY_UNKNOWN_SIZE); } diff --git a/libc/string/strrchr.c b/libc/bionic/strrchr.cpp similarity index 90% rename from libc/string/strrchr.c rename to libc/bionic/strrchr.cpp index fe2306a26..3fdb47cae 100644 --- a/libc/string/strrchr.c +++ b/libc/bionic/strrchr.cpp @@ -1,4 +1,3 @@ -/* $OpenBSD: rindex.c,v 1.6 2005/08/08 08:05:37 espie Exp $ */ /* * Copyright (c) 1988 Regents of the University of California. * All rights reserved. @@ -28,10 +27,9 @@ * SUCH DAMAGE. */ +#undef _FORTIFY_SOURCE #include -char * -strrchr(const char *p, int ch) -{ - return __strrchr_chk(p, ch, __BIONIC_FORTIFY_UNKNOWN_SIZE); +char* strrchr(const char* p, int ch) { + return __strrchr_chk(p, ch, __BIONIC_FORTIFY_UNKNOWN_SIZE); } diff --git a/libc/include/string.h b/libc/include/string.h index 37d22c4db..8963e3d0f 100644 --- a/libc/include/string.h +++ b/libc/include/string.h @@ -49,6 +49,7 @@ extern char* strchr(const char *, int) __purefunc; extern char* __strchr_chk(const char *, int, size_t); extern char* strrchr(const char *, int) __purefunc; +extern char* __strrchr_chk(const char *, int, size_t); extern size_t strlen(const char *) __purefunc; extern size_t __strlen_chk(const char *, size_t); @@ -263,8 +264,6 @@ char* strchr(const char *s, int c) { return __strchr_chk(s, c, bos); } -extern char* __strrchr_chk(const char *, int, size_t); - __BIONIC_FORTIFY_INLINE char* strrchr(const char *s, int c) { size_t bos = __bos(s); diff --git a/libc/include/wchar.h b/libc/include/wchar.h index 76ac02c88..32cf12763 100644 --- a/libc/include/wchar.h +++ b/libc/include/wchar.h @@ -147,6 +147,9 @@ extern wchar_t *wmemset(wchar_t *, wchar_t, size_t); extern int wprintf(const wchar_t *, ...); extern int wscanf(const wchar_t *, ...); +extern size_t wcslcat(wchar_t*, const wchar_t*, size_t); +extern size_t wcslcpy(wchar_t*, const wchar_t*, size_t); + /* No really supported. These are just for making libstdc++-v3 happy. */ typedef void *wctrans_t; extern wint_t towctrans(wint_t, wctrans_t); diff --git a/libc/stdio/fileext.h b/libc/stdio/fileext.h index b36a44866..864192408 100644 --- a/libc/stdio/fileext.h +++ b/libc/stdio/fileext.h @@ -52,18 +52,9 @@ do { \ _UB(fp)._base = NULL; \ _UB(fp)._size = 0; \ WCIO_INIT(fp); \ - _FLOCK_INIT(fp); \ + _FLOCK(fp).value = __PTHREAD_RECURSIVE_MUTEX_INIT_VALUE; \ } while (0) -/* Helper macros to avoid a function call when you know that fp is not NULL. - * Notice that we keep _FLOCK_INIT() fast by slightly breaking our pthread - * encapsulation. - */ -#define _FLOCK_INIT(fp) _FLOCK(fp).value = __PTHREAD_RECURSIVE_MUTEX_INIT_VALUE -#define _FLOCK_LOCK(fp) pthread_mutex_lock(&_FLOCK(fp)) -#define _FLOCK_TRYLOCK(fp) pthread_mutex_trylock(&_FLOCK(fp)) -#define _FLOCK_UNLOCK(fp) pthread_mutex_unlock(&_FLOCK(fp)) - #define _FILEEXT_SETUP(f, fext) \ do { \ (f)->_ext._base = (unsigned char *)(fext); \ diff --git a/libc/upstream-freebsd/lib/libc/string/wcslcpy.c b/libc/upstream-freebsd/lib/libc/string/wcslcpy.c deleted file mode 100644 index b104a0622..000000000 --- a/libc/upstream-freebsd/lib/libc/string/wcslcpy.c +++ /dev/null @@ -1,70 +0,0 @@ -/* - * 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. - * - * 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. - * - * from OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp - */ - -#include -#if 0 -#if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: wcslcpy.c,v 1.1 2000/12/23 23:14:36 itojun Exp $"); -#endif /* LIBC_SCCS and not lint */ -#endif -__FBSDID("$FreeBSD$"); - -#include -#include - -/* - * Copy src to string dst of size siz. At most siz-1 characters - * will be copied. Always NUL terminates (unless siz == 0). - * Returns wcslen(src); if retval >= siz, truncation occurred. - */ -size_t -wcslcpy(wchar_t *dst, const wchar_t *src, size_t siz) -{ - wchar_t *d = dst; - const wchar_t *s = src; - size_t n = siz; - - /* Copy as many bytes as will fit */ - if (n != 0 && --n != 0) { - do { - if ((*d++ = *s++) == 0) - break; - } while (--n != 0); - } - - /* Not enough room in dst, add NUL and traverse rest of src */ - if (n == 0) { - if (siz != 0) - *d = '\0'; /* NUL-terminate dst */ - while (*s++) - ; - } - - return(s - src - 1); /* count does not include NUL */ -} diff --git a/libc/wchar/wcswidth.c b/libc/upstream-openbsd/lib/libc/locale/wcsxfrm.c similarity index 63% rename from libc/wchar/wcswidth.c rename to libc/upstream-openbsd/lib/libc/locale/wcsxfrm.c index b142074f1..98db4a98f 100644 --- a/libc/wchar/wcswidth.c +++ b/libc/upstream-openbsd/lib/libc/locale/wcsxfrm.c @@ -1,14 +1,10 @@ +/* $OpenBSD: wcsxfrm.c,v 1.2 2012/12/05 23:20:00 deraadt Exp $ */ +/* $OpenBSD: wcsxfrm.c,v 1.2 2012/12/05 23:20:00 deraadt Exp $ */ +/* $NetBSD: multibyte_sb.c,v 1.4 2003/08/07 16:43:04 agc Exp $ */ + /* - * 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. + * Copyright (c) 1991 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 @@ -18,7 +14,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. - * 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. * @@ -35,23 +31,12 @@ * SUCH DAMAGE. */ -#include -__FBSDID("$FreeBSD$"); - #include -int -wcswidth(const wchar_t *pwcs, size_t n) +size_t +wcsxfrm(wchar_t *dest, const wchar_t *src, size_t n) { - wchar_t wc; - int len, l; - - len = 0; - while (n-- > 0 && (wc = *pwcs++) != L'\0') { - if ((l = wcwidth(wc)) < 0) - return (-1); - len += l; - } - return (len); + if (n == 0) + return wcslen(src); + return wcslcpy(dest, src, n); } - diff --git a/libc/string/bcopy.c b/libc/upstream-openbsd/lib/libc/string/bcopy.c similarity index 100% rename from libc/string/bcopy.c rename to libc/upstream-openbsd/lib/libc/string/bcopy.c diff --git a/libc/string/strcat.c b/libc/upstream-openbsd/lib/libc/string/strcat.c similarity index 100% rename from libc/string/strcat.c rename to libc/upstream-openbsd/lib/libc/string/strcat.c diff --git a/libc/string/strcmp.c b/libc/upstream-openbsd/lib/libc/string/strcmp.c similarity index 100% rename from libc/string/strcmp.c rename to libc/upstream-openbsd/lib/libc/string/strcmp.c diff --git a/libc/string/strcpy.c b/libc/upstream-openbsd/lib/libc/string/strcpy.c similarity index 89% rename from libc/string/strcpy.c rename to libc/upstream-openbsd/lib/libc/string/strcpy.c index eb21d6780..71d90d410 100644 --- a/libc/string/strcpy.c +++ b/libc/upstream-openbsd/lib/libc/string/strcpy.c @@ -29,7 +29,16 @@ * SUCH DAMAGE. */ +#if !defined(_KERNEL) && !defined(_STANDALONE) #include +#else +#include +#endif + +#if defined(APIWARN) +__warn_references(strcpy, + "warning: strcpy() is almost always misused, please use strlcpy()"); +#endif char * strcpy(char *to, const char *from) diff --git a/libc/string/strlcat.c b/libc/upstream-openbsd/lib/libc/string/strlcat.c similarity index 100% rename from libc/string/strlcat.c rename to libc/upstream-openbsd/lib/libc/string/strlcat.c diff --git a/libc/string/strlcpy.c b/libc/upstream-openbsd/lib/libc/string/strlcpy.c similarity index 100% rename from libc/string/strlcpy.c rename to libc/upstream-openbsd/lib/libc/string/strlcpy.c diff --git a/libc/string/strlen.c b/libc/upstream-openbsd/lib/libc/string/strlen.c similarity index 100% rename from libc/string/strlen.c rename to libc/upstream-openbsd/lib/libc/string/strlen.c diff --git a/libc/string/strncat.c b/libc/upstream-openbsd/lib/libc/string/strncat.c similarity index 100% rename from libc/string/strncat.c rename to libc/upstream-openbsd/lib/libc/string/strncat.c diff --git a/libc/string/strncmp.c b/libc/upstream-openbsd/lib/libc/string/strncmp.c similarity index 99% rename from libc/string/strncmp.c rename to libc/upstream-openbsd/lib/libc/string/strncmp.c index 17688084b..0aea80d7d 100644 --- a/libc/string/strncmp.c +++ b/libc/upstream-openbsd/lib/libc/string/strncmp.c @@ -38,6 +38,7 @@ int strncmp(const char *s1, const char *s2, size_t n) { + if (n == 0) return (0); do { diff --git a/libc/string/strncpy.c b/libc/upstream-openbsd/lib/libc/string/strncpy.c similarity index 100% rename from libc/string/strncpy.c rename to libc/upstream-openbsd/lib/libc/string/strncpy.c diff --git a/libc/upstream-openbsd/lib/libc/string/wcslcpy.c b/libc/upstream-openbsd/lib/libc/string/wcslcpy.c new file mode 100644 index 000000000..f49936a88 --- /dev/null +++ b/libc/upstream-openbsd/lib/libc/string/wcslcpy.c @@ -0,0 +1,52 @@ +/* $OpenBSD: wcslcpy.c,v 1.5 2011/07/24 15:21:28 millert Exp $ */ +/* $NetBSD: wcslcpy.c,v 1.2 2001/01/03 14:33:02 lukem Exp $ */ + +/* + * Copyright (c) 1998 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. + */ + +#include +#include + +/* + * Copy src to string dst of size siz. At most siz-1 characters + * will be copied. Always NUL terminates (unless siz == 0). + * Returns wcslen(src); if retval >= siz, truncation occurred. + */ +size_t +wcslcpy(wchar_t *dst, const wchar_t *src, size_t siz) +{ + wchar_t *d = dst; + const wchar_t *s = src; + size_t n = siz; + + /* Copy as many bytes as will fit */ + if (n != 0) { + while (--n != 0) { + if ((*d++ = *s++) == '\0') + break; + } + } + + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) { + if (siz != 0) + *d = '\0'; /* NUL-terminate dst */ + while (*s++) + ; + } + + return(s - src - 1); /* count does not include NUL */ +} diff --git a/libc/wchar/wcsxfrm.c b/libc/upstream-openbsd/lib/libc/string/wcswidth.c similarity index 58% rename from libc/wchar/wcsxfrm.c rename to libc/upstream-openbsd/lib/libc/string/wcswidth.c index 042ea567e..8ea1bdf6e 100644 --- a/libc/wchar/wcsxfrm.c +++ b/libc/upstream-openbsd/lib/libc/string/wcswidth.c @@ -1,7 +1,9 @@ +/* $OpenBSD: wcswidth.c,v 1.4 2011/04/04 18:16:24 stsp Exp $ */ +/* $NetBSD: wcswidth.c,v 1.2 2001/01/03 14:29:37 lukem Exp $ */ + /*- - * Copyright (c) 1995 Alex Tatmanjants - * at Electronni Visti IA, Kiev, Ukraine. - * All rights reserved. + * Copyright (c)1999 Citrus Project, + * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -12,10 +14,10 @@ * 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 ``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 AUTHOR 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) @@ -23,35 +25,26 @@ * 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. + * + * citrus Id: wcswidth.c,v 1.1 1999/12/29 21:47:45 tshiozak Exp */ #include -/* - * Placeholder wcsxfrm() implementation. See wcscoll.c for a description of - * the logic used. - */ -size_t -wcsxfrm(wchar_t * __restrict dest, const wchar_t * __restrict src, size_t len) +int +wcswidth(const wchar_t *s, size_t n) { - int prim, sec, l; - size_t slen; - char *mbsrc, *s, *ss; + int w, q; - if (*src == L'\0') { - if (len != 0) - *dest = L'\0'; - return (0); - } + w = 0; + while (n && *s) { + q = wcwidth(*s); + if (q == -1) + return (-1); + w += q; + s++; + n--; + } - slen = wcslen(src); - if (len > 0) { - if (slen < len) - wcscpy(dest, src); - else { - wcsncpy(dest, src, len - 1); - dest[len - 1] = L'\0'; - } - } - return (slen); + return w; }