summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2003-01-29 16:47:24 +0000
committerBruno Haible <bruno@clisp.org>2003-01-29 16:47:24 +0000
commit1080617a7d04cdf1a1d3b1e390859af371d485d7 (patch)
tree5c86242eeb49f1f3582a273459962b3241810e7d
parentb86c47ccc62b3615e100cc3bea08dfe0d930f57b (diff)
downloadgnulib-1080617a7d04cdf1a1d3b1e390859af371d485d7.tar.gz
New module stpncpy.
-rw-r--r--ChangeLog5
-rwxr-xr-xMODULES.html.sh8
-rw-r--r--lib/ChangeLog5
-rw-r--r--lib/stpncpy.c101
-rw-r--r--lib/stpncpy.h36
-rw-r--r--m4/ChangeLog4
-rw-r--r--m4/stpncpy.m455
-rw-r--r--modules/stpncpy22
8 files changed, 232 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index f660e309b7..65e315ee23 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2003-01-29 Bruno Haible <bruno@clisp.org>
+
+ * modules/stpncpy: New module.
+ * MODULES.html.sh (func_all_modules): Add it.
+
2003-01-28 Bruno Haible <bruno@clisp.org>
* modules/c-ctype: New module.
diff --git a/MODULES.html.sh b/MODULES.html.sh
index d91d654655..bc92b86bce 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 <string.h>"
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 71c1b81b48..da60b4adda 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,8 @@
+2003-01-29 Bruno Haible <bruno@clisp.org>
+
+ * stpncpy.h: New file, from GNU gettext with modifications.
+ * stpncpy.c: New file, from GNU gettext with modifications.
+
2003-01-28 Bruno Haible <bruno@clisp.org>
* 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 0000000000..243cbc7bb6
--- /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 <config.h>
+#endif
+
+/* Specification. */
+#include "stpncpy.h"
+
+#ifdef _LIBC
+# include <string.h>
+#else
+# include <sys/types.h>
+#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 0000000000..a457ee9d94
--- /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 <string.h>
+
+#else
+
+#include <stddef.h>
+
+/* 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 dc9aad9311..39b00d8804 100644
--- a/m4/ChangeLog
+++ b/m4/ChangeLog
@@ -1,3 +1,7 @@
+2003-01-29 Bruno Haible <bruno@clisp.org>
+
+ * stpncpy.m4: New file.
+
2003-01-23 Jim Meyering <jim@meyering.net>
* 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 0000000000..768cced027
--- /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 <string.h> to declare stpncpy().
+ AC_REQUIRE([AC_GNU_SOURCE])
+
+ AC_CACHE_CHECK([for working stpncpy], gl_cv_func_stpncpy, [
+ AC_TRY_RUN([
+#include <stdlib.h>
+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 <features.h>
+#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 0000000000..0646e27983
--- /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
+