From 55da53d0db0c7a9fd82b2abc809560b3089df6a5 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Fri, 26 Sep 2003 15:25:30 +0000 Subject: [PATCH] Better handling of collision with AIX stpncpy() function. --- lib/ChangeLog | 6 ++++++ lib/stpncpy.c | 15 +++++++++++++-- lib/stpncpy.h | 16 +++++++--------- m4/ChangeLog | 4 ++++ m4/stpncpy.m4 | 10 ++++++++++ 5 files changed, 40 insertions(+), 11 deletions(-) diff --git a/lib/ChangeLog b/lib/ChangeLog index fb74cdb575..987ae0a58d 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,9 @@ +2003-09-26 Bruno Haible + + * stpncpy.h (gnu_stpncpy): New declaration. + (stpncpy): Define as alias for gnu_stpncpy. + * stpncpy.c [!_LIBC]: Define gnu_stpncpy, not stpncpy. + 2003-09-26 Paul Eggert * error.c (SIZE_MAX) [!defined SIZE_MAX]: Define. diff --git a/lib/stpncpy.c b/lib/stpncpy.c index c302e398d7..3f7473f946 100644 --- a/lib/stpncpy.c +++ b/lib/stpncpy.c @@ -27,12 +27,21 @@ /* Specification. */ #include "stpncpy.h" +#if !HAVE_STPNCPY + +#ifndef _LIBC +/* We cannot generally use the name 'stpncpy' since AIX 4 defines an unusable + variant of the function but we cannot use it. */ +# undef stpncpy +# define stpncpy gnu_stpncpy +#endif + #ifndef weak_alias # define __stpncpy stpncpy #endif -/* Copy no more than N characters of SRC to DEST, returning the address of - the terminating '\0' in DEST, if any, or else DEST + N. */ +/* Copy no more than N bytes of SRC to DST, returning a pointer past the + last non-NUL byte written into DST. */ char * __stpncpy (char *dest, const char *src, size_t n) { @@ -93,3 +102,5 @@ __stpncpy (char *dest, const char *src, size_t n) #ifdef weak_alias weak_alias (__stpncpy, stpncpy) #endif + +#endif diff --git a/lib/stpncpy.h b/lib/stpncpy.h index a457ee9d94..cf658eb071 100644 --- a/lib/stpncpy.h +++ b/lib/stpncpy.h @@ -18,18 +18,16 @@ #ifndef _STPNCPY_H #define _STPNCPY_H -#if HAVE_STPNCPY - -/* Get stpncpy() declaration. */ #include -#else - -#include +#if !HAVE_STPNCPY -/* Copy no more than N characters of SRC to DST, returning the address of - the last character written into DST. */ -extern char *stpncpy (char *dst, const char *src, size_t n); +/* Copy no more than N bytes of SRC to DST, returning a pointer past the + last non-NUL byte written into DST. */ +/* When not using the GNU libc we use the stpncpy implementation we + provide here. */ +extern char *gnu_stpncpy (char *dst, const char *src, size_t n); +#define stpncpy(Dst, Src, N) gnu_stpncpy (Dst, Src, N) #endif diff --git a/m4/ChangeLog b/m4/ChangeLog index f5c561e742..61b46fe8df 100644 --- a/m4/ChangeLog +++ b/m4/ChangeLog @@ -1,3 +1,7 @@ +2003-09-26 Bruno Haible + + * stpncpy.m4: Add comments about the AIX stpncpy(). + 2003-09-25 Simon Josefsson Bruno Haible diff --git a/m4/stpncpy.m4 b/m4/stpncpy.m4 index 768cced027..fc47e65ccc 100644 --- a/m4/stpncpy.m4 +++ b/m4/stpncpy.m4 @@ -11,6 +11,16 @@ AC_DEFUN([gl_FUNC_STPNCPY], dnl Persuade glibc to declare stpncpy(). AC_REQUIRE([AC_GNU_SOURCE]) + dnl Both glibc and AIX (4.3.3, 5.1) have an stpncpy() function + dnl declared in . Its side effects are the same as those + dnl of strncpy(): + dnl stpncpy (dest, src, n) + dnl overwrites dest[0..n-1], min(strlen(src),n) bytes coming from src, + dnl and the remaining bytes being NULs. However, the return value is + dnl in glibc: dest + min(strlen(src),n) + dnl in AIX: dest + max(0,n-1) + dnl Only the glibc return value is useful in practice. + AC_CACHE_CHECK([for working stpncpy], gl_cv_func_stpncpy, [ AC_TRY_RUN([ #include -- 2.30.2