changeset 4221:06a92cabf1fc

New module stpncpy.
author Bruno Haible <bruno@clisp.org>
date Wed, 29 Jan 2003 16:47:24 +0000
parents f88f398d22ea
children 7f12edd9b7d4
files ChangeLog MODULES.html.sh lib/ChangeLog lib/stpncpy.c lib/stpncpy.h m4/ChangeLog m4/stpncpy.m4 modules/stpncpy
diffstat 8 files changed, 232 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Jan 29 08:10:31 2003 +0000
+++ b/ChangeLog	Wed Jan 29 16:47:24 2003 +0000
@@ -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.
--- a/MODULES.html.sh	Wed Jan 29 08:10:31 2003 +0000
+++ b/MODULES.html.sh	Wed Jan 29 16:47:24 2003 +0000
@@ -1490,9 +1490,9 @@
   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_module bcopy
   func_module memrchr
   func_module stpcpy
-  #func_module stpncpy
+  func_module stpncpy
   func_module strcase
   func_module strdup
   func_module strnlen
--- a/lib/ChangeLog	Wed Jan 29 08:10:31 2003 +0000
+++ b/lib/ChangeLog	Wed Jan 29 16:47:24 2003 +0000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/stpncpy.c	Wed Jan 29 16:47:24 2003 +0000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/stpncpy.h	Wed Jan 29 16:47:24 2003 +0000
@@ -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 */
--- a/m4/ChangeLog	Wed Jan 29 08:10:31 2003 +0000
+++ b/m4/ChangeLog	Wed Jan 29 16:47:24 2003 +0000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/m4/stpncpy.m4	Wed Jan 29 16:47:24 2003 +0000
@@ -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], [
+  :
+])
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/stpncpy	Wed Jan 29 16:47:24 2003 +0000
@@ -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
+