From 1080617a7d04cdf1a1d3b1e390859af371d485d7 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Wed, 29 Jan 2003 16:47:24 +0000 Subject: [PATCH] New module stpncpy. --- ChangeLog | 5 +++ MODULES.html.sh | 8 ++--- lib/ChangeLog | 5 +++ lib/stpncpy.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/stpncpy.h | 36 ++++++++++++++++++++ m4/ChangeLog | 4 +++ m4/stpncpy.m4 | 55 ++++++++++++++++++++++++++++++ modules/stpncpy | 22 ++++++++++++ 8 files changed, 232 insertions(+), 4 deletions(-) create mode 100644 lib/stpncpy.c create mode 100644 lib/stpncpy.h create mode 100644 m4/stpncpy.m4 create mode 100644 modules/stpncpy diff --git a/ChangeLog b/ChangeLog index f660e309b..65e315ee2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2003-01-29 Bruno Haible + + * modules/stpncpy: New module. + * MODULES.html.sh (func_all_modules): Add it. + 2003-01-28 Bruno Haible * modules/c-ctype: New module. diff --git a/MODULES.html.sh b/MODULES.html.sh index d91d65465..bc92b86bc 100755 --- a/MODULES.html.sh +++ b/MODULES.html.sh @@ -1490,9 +1490,9 @@ func_all_modules () func_wrap H3 func_echo "$element" - #func_begin_table - #func_module c-ctype - #func_end_table + func_begin_table + func_module c-ctype + func_end_table element="String handling " element=`printf "%s" "$element" | sed -e "$sed_lt" -e "$sed_gt"` @@ -1504,7 +1504,7 @@ func_all_modules () func_module bcopy func_module memrchr func_module stpcpy - #func_module stpncpy + func_module stpncpy func_module strcase func_module strdup func_module strnlen diff --git a/lib/ChangeLog b/lib/ChangeLog index 71c1b81b4..da60b4add 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,8 @@ +2003-01-29 Bruno Haible + + * stpncpy.h: New file, from GNU gettext with modifications. + * stpncpy.c: New file, from GNU gettext with modifications. + 2003-01-28 Bruno Haible * c-ctype.h: New file, from GNU gettext, with changes suggested by diff --git a/lib/stpncpy.c b/lib/stpncpy.c new file mode 100644 index 000000000..243cbc7bb --- /dev/null +++ b/lib/stpncpy.c @@ -0,0 +1,101 @@ +/* Copyright (C) 1993, 1995-1997, 2002-2003 Free Software Foundation, Inc. + + NOTE: The canonical source of this file is maintained with the GNU C Library. + Bugs can be reported to bug-glibc@gnu.org. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + USA. */ + +/* This is almost copied from strncpy.c, written by Torbjorn Granlund. */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +/* Specification. */ +#include "stpncpy.h" + +#ifdef _LIBC +# include +#else +# include +#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. */ +char * +__stpncpy (char *dest, const char *src, size_t n) +{ + char c; + char *s = dest; + + if (n >= 4) + { + size_t n4 = n >> 2; + + for (;;) + { + c = *src++; + *dest++ = c; + if (c == '\0') + break; + c = *src++; + *dest++ = c; + if (c == '\0') + break; + c = *src++; + *dest++ = c; + if (c == '\0') + break; + c = *src++; + *dest++ = c; + if (c == '\0') + break; + if (--n4 == 0) + goto last_chars; + } + n -= dest - s; + goto zero_fill; + } + + last_chars: + n &= 3; + if (n == 0) + return dest; + + for (;;) + { + c = *src++; + --n; + *dest++ = c; + if (c == '\0') + break; + if (n == 0) + return dest; + } + + zero_fill: + while (n-- > 0) + dest[n] = '\0'; + + return dest - 1; +} +#ifdef weak_alias +weak_alias (__stpncpy, stpncpy) +#endif diff --git a/lib/stpncpy.h b/lib/stpncpy.h new file mode 100644 index 000000000..a457ee9d9 --- /dev/null +++ b/lib/stpncpy.h @@ -0,0 +1,36 @@ +/* String copying. + Copyright (C) 1995, 2001-2003 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +#ifndef _STPNCPY_H +#define _STPNCPY_H + +#if HAVE_STPNCPY + +/* Get stpncpy() declaration. */ +#include + +#else + +#include + +/* 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); + +#endif + +#endif /* _STPNCPY_H */ diff --git a/m4/ChangeLog b/m4/ChangeLog index dc9aad931..39b00d880 100644 --- a/m4/ChangeLog +++ b/m4/ChangeLog @@ -1,3 +1,7 @@ +2003-01-29 Bruno Haible + + * stpncpy.m4: New file. + 2003-01-23 Jim Meyering * dirfd.m4 (UTILS_FUNC_DIRFD): Correct typo: s/-1/no/ that kept this diff --git a/m4/stpncpy.m4 b/m4/stpncpy.m4 new file mode 100644 index 000000000..768cced02 --- /dev/null +++ b/m4/stpncpy.m4 @@ -0,0 +1,55 @@ +# stpncpy.m4 serial 1 +dnl Copyright (C) 2002-2003 Free Software Foundation, Inc. +dnl This file is free software, distributed under the terms of the GNU +dnl General Public License. As a special exception to the GNU General +dnl Public License, this file may be distributed as part of a program +dnl that contains a configuration script generated by Autoconf, under +dnl the same distribution terms as the rest of that program. + +AC_DEFUN([gl_FUNC_STPNCPY], +[ + dnl Persuade glibc to declare stpncpy(). + AC_REQUIRE([AC_GNU_SOURCE]) + + AC_CACHE_CHECK([for working stpncpy], gl_cv_func_stpncpy, [ + AC_TRY_RUN([ +#include +extern char *stpncpy (char *dest, const char *src, size_t n); +int main () { + const char *src = "Hello"; + char dest[10]; + /* AIX 4.3.3 and AIX 5.1 stpncpy() returns dest+1 here. */ + strcpy (dest, "\377\377\377\377\377\377"); + if (stpncpy (dest, src, 2) != dest + 2) exit(1); + /* AIX 4.3.3 and AIX 5.1 stpncpy() returns dest+4 here. */ + strcpy (dest, "\377\377\377\377\377\377"); + if (stpncpy (dest, src, 5) != dest + 5) exit(1); + /* AIX 4.3.3 and AIX 5.1 stpncpy() returns dest+6 here. */ + strcpy (dest, "\377\377\377\377\377\377"); + if (stpncpy (dest, src, 7) != dest + 5) exit(1); + exit(0); +} +], gl_cv_func_stpncpy=yes, gl_cv_func_stpncpy=no, + [AC_EGREP_CPP([Thanks for using GNU], [ +#include +#ifdef __GNU_LIBRARY__ + Thanks for using GNU +#endif +], gl_cv_func_stpncpy=yes, gl_cv_func_stpncpy=no)])]) + + if test $gl_cv_func_stpncpy = yes; then + AC_DEFINE(HAVE_STPNCPY, 1, + [Define if you have the stpncpy() function and it works.]) + else + AC_LIBOBJ([stpncpy]) + AC_DEFINE(stpncpy, rpl_stpncpy, + [Define to rpl_stpncpy if the replacement function should be used.]) + gl_PREREQ_STPNCPY + fi +]) + +# Prerequisites of lib/stpncpy.c. +AC_DEFUN([gl_PREREQ_STPNCPY], [ + : +]) + diff --git a/modules/stpncpy b/modules/stpncpy new file mode 100644 index 000000000..0646e2798 --- /dev/null +++ b/modules/stpncpy @@ -0,0 +1,22 @@ +Description: +stpncpy() function: copy a size-bounded string, returning a pointer to its end. + +Files: +lib/stpncpy.h +lib/stpncpy.c +m4/stpncpy.m4 + +Depends-on: + +configure.ac: +gl_FUNC_STPNCPY + +Makefile.am: +lib_SOURCES += stpncpy.h + +Include: +"stpncpy.h" + +Maintainer: +Bruno Haible, glibc + -- 2.11.0