summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClaudio Fontana <sick_soul@users.sourceforge.net>2006-07-21 23:47:07 +0000
committerClaudio Fontana <sick_soul@users.sourceforge.net>2006-07-21 23:47:07 +0000
commit4902bd18cc771b4b8674ac27bec3d473cdf84cf2 (patch)
tree345100de7a5cad4f2b222e6c88598f10ea3f4df3
parent9a72ecd6dc36eab0497dc428d10a6ad1dd87a19a (diff)
downloadidutils-4902bd18cc771b4b8674ac27bec3d473cdf84cf2.tar.gz
idutils-4902bd18cc771b4b8674ac27bec3d473cdf84cf2.tar.bz2
idutils-4902bd18cc771b4b8674ac27bec3d473cdf84cf2.zip
* add lib/ files of newest gnulib
-rw-r--r--lib/strndup.c66
-rw-r--r--lib/strndup.h30
-rw-r--r--lib/strnlen.c33
-rw-r--r--lib/strnlen.h32
-rw-r--r--lib/wcwidth.h70
-rw-r--r--lib/xstrndup.c39
-rw-r--r--lib/xstrndup.h24
7 files changed, 294 insertions, 0 deletions
diff --git a/lib/strndup.c b/lib/strndup.c
new file mode 100644
index 0000000..2626373
--- /dev/null
+++ b/lib/strndup.c
@@ -0,0 +1,66 @@
+/* Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2005, 2006 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@prep.ai.mit.edu.
+
+ 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+#if !_LIBC
+# include "strndup.h"
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+
+#if !_LIBC
+# include "strnlen.h"
+# ifndef __strnlen
+# define __strnlen strnlen
+# endif
+#endif
+
+#undef __strndup
+#if _LIBC
+# undef strndup
+#endif
+
+#ifndef weak_alias
+# define __strndup strndup
+#endif
+
+char *
+__strndup (s, n)
+ const char *s;
+ size_t n;
+{
+ size_t len = __strnlen (s, n);
+ char *new = malloc (len + 1);
+
+ if (new == NULL)
+ return NULL;
+
+ new[len] = '\0';
+ return memcpy (new, s, len);
+}
+#ifdef libc_hidden_def
+libc_hidden_def (__strndup)
+#endif
+#ifdef weak_alias
+weak_alias (__strndup, strndup)
+#endif
diff --git a/lib/strndup.h b/lib/strndup.h
new file mode 100644
index 0000000..8eae493
--- /dev/null
+++ b/lib/strndup.h
@@ -0,0 +1,30 @@
+/* Duplicate a size-bounded string.
+ Copyright (C) 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
+
+#if HAVE_STRNDUP
+
+/* Get strndup() declaration. */
+#include <string.h>
+
+#else
+
+#include <stddef.h>
+
+/* Return a newly allocated copy of at most N bytes of STRING. */
+extern char *strndup (const char *string, size_t n);
+
+#endif
diff --git a/lib/strnlen.c b/lib/strnlen.c
new file mode 100644
index 0000000..09ba788
--- /dev/null
+++ b/lib/strnlen.c
@@ -0,0 +1,33 @@
+/* Find the length of STRING, but scan at most MAXLEN characters.
+ Copyright (C) 2005, 2006 Free Software Foundation, Inc.
+ Written by Simon Josefsson.
+
+ 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "strnlen.h"
+
+/* Find the length of STRING, but scan at most MAXLEN characters.
+ If no '\0' terminator is found in that many characters, return MAXLEN. */
+
+size_t
+strnlen (const char *string, size_t maxlen)
+{
+ const char *end = memchr (string, '\0', maxlen);
+ return end ? (size_t) (end - string) : maxlen;
+}
diff --git a/lib/strnlen.h b/lib/strnlen.h
new file mode 100644
index 0000000..ba74dba
--- /dev/null
+++ b/lib/strnlen.h
@@ -0,0 +1,32 @@
+/* Find the length of STRING, but scan at most MAXLEN characters.
+ Copyright (C) 2005 Free Software Foundation, Inc.
+ Written by Simon Josefsson.
+
+ 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
+
+#ifndef STRNLEN_H
+#define STRNLEN_H
+
+/* Get strnlen declaration, if available. */
+#include <string.h>
+
+#if defined HAVE_DECL_STRNLEN && !HAVE_DECL_STRNLEN
+/* Find the length (number of bytes) of STRING, but scan at most
+ MAXLEN bytes. If no '\0' terminator is found in that many bytes,
+ return MAXLEN. */
+extern size_t strnlen(const char *string, size_t maxlen);
+#endif
+
+#endif /* STRNLEN_H */
diff --git a/lib/wcwidth.h b/lib/wcwidth.h
new file mode 100644
index 0000000..9af75e0
--- /dev/null
+++ b/lib/wcwidth.h
@@ -0,0 +1,70 @@
+/* Determine the number of screen columns needed for a character.
+ Copyright (C) 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
+
+#ifndef _gl_WCWIDTH_H
+#define _gl_WCWIDTH_H
+
+#if HAVE_WCHAR_T
+
+/* Get wcwidth if available, along with wchar_t. */
+# if HAVE_WCHAR_H
+/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
+ <wchar.h>.
+ BSD/OS 4.1 has a bug: <stdio.h> and <time.h> must be included before
+ <wchar.h>. */
+# include <stdio.h>
+# include <time.h>
+# include <wchar.h>
+# endif
+
+/* Get iswprint. */
+# if HAVE_WCTYPE_H
+# include <wctype.h>
+# endif
+# if !defined iswprint && !HAVE_ISWPRINT
+# define iswprint(wc) 1
+# endif
+
+# ifndef HAVE_DECL_WCWIDTH
+"this configure-time declaration test was not run"
+# endif
+# ifndef wcwidth
+# if !HAVE_WCWIDTH
+
+/* wcwidth doesn't exist, so assume all printable characters have
+ width 1. */
+static inline int
+wcwidth (wchar_t wc)
+{
+ return wc == 0 ? 0 : iswprint (wc) ? 1 : -1;
+}
+
+# elif !HAVE_DECL_WCWIDTH
+
+/* wcwidth exists but is not declared. */
+extern
+# ifdef __cplusplus
+"C"
+# endif
+int wcwidth (int /* actually wchar_t */);
+
+# endif
+# endif
+
+#endif /* HAVE_WCHAR_H */
+
+#endif /* _gl_WCWIDTH_H */
diff --git a/lib/xstrndup.c b/lib/xstrndup.c
new file mode 100644
index 0000000..a62d4bd
--- /dev/null
+++ b/lib/xstrndup.c
@@ -0,0 +1,39 @@
+/* Duplicate a bounded initial segment of a string, with out-of-memory
+ checking.
+ Copyright (C) 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+/* Specification. */
+#include "xstrndup.h"
+
+#include "strndup.h"
+#include "xalloc.h"
+
+/* Return a newly allocated copy of at most N bytes of STRING.
+ In other words, return a copy of the initial segment of length N of
+ STRING. */
+char *
+xstrndup (const char *string, size_t n)
+{
+ char *s = strndup (string, n);
+ if (! s)
+ xalloc_die ();
+ return s;
+}
diff --git a/lib/xstrndup.h b/lib/xstrndup.h
new file mode 100644
index 0000000..88354cf
--- /dev/null
+++ b/lib/xstrndup.h
@@ -0,0 +1,24 @@
+/* Duplicate a bounded initial segment of a string, with out-of-memory
+ checking.
+ Copyright (C) 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
+
+#include <stddef.h>
+
+/* Return a newly allocated copy of at most N bytes of STRING.
+ In other words, return a copy of the initial segment of length N of
+ STRING. */
+extern char *xstrndup (const char *string, size_t n);