Commit 17998b22 by Kaveh R. Ghazi Committed by Kaveh Ghazi

libiberty.h (xstrndup): Declare.

include:
	* libiberty.h (xstrndup): Declare.

libiberty:
	* Makefile.in (CFILES): Add strndup.c and xstrndup.c.
	(REQUIRED_OFILES): Add xstrndup.o.
	(CONFIGURED_OFILES): Add strndup.o.
	Regenerate dependencies.

	* configure.ac (funcs, AC_CHECK_FUNCS): Add strndup.

	* strndup.c, xstrndup.c: New.

	* config.in, configure, functions.texi: Regenerate.

From-SVN: r97034
parent bb99744f
2005-03-24 Kaveh R. Ghazi <ghazi@caip.rutgers.edu>
* libiberty.h (xstrndup): Declare.
2005-03-22 Kaveh R. Ghazi <ghazi@caip.rutgers.edu> 2005-03-22 Kaveh R. Ghazi <ghazi@caip.rutgers.edu>
* libiberty.h (make_relative_prefix): Add ATTRIBUTE_MALLOC. * libiberty.h (make_relative_prefix): Add ATTRIBUTE_MALLOC.
......
...@@ -255,6 +255,10 @@ extern PTR xcalloc PARAMS ((size_t, size_t)) ATTRIBUTE_MALLOC; ...@@ -255,6 +255,10 @@ extern PTR xcalloc PARAMS ((size_t, size_t)) ATTRIBUTE_MALLOC;
extern char *xstrdup PARAMS ((const char *)) ATTRIBUTE_MALLOC; extern char *xstrdup PARAMS ((const char *)) ATTRIBUTE_MALLOC;
/* Copy at most N characters from string into a buffer without fail. */
extern char *xstrndup PARAMS ((const char *, size_t)) ATTRIBUTE_MALLOC;
/* Copy an existing memory buffer to a new memory buffer without fail. */ /* Copy an existing memory buffer to a new memory buffer without fail. */
extern PTR xmemdup PARAMS ((const PTR, size_t, size_t)) ATTRIBUTE_MALLOC; extern PTR xmemdup PARAMS ((const PTR, size_t, size_t)) ATTRIBUTE_MALLOC;
......
2005-03-24 Kaveh R. Ghazi <ghazi@caip.rutgers.edu> 2005-03-24 Kaveh R. Ghazi <ghazi@caip.rutgers.edu>
* Makefile.in (CFILES): Add strndup.c and xstrndup.c.
(REQUIRED_OFILES): Add xstrndup.o.
(CONFIGURED_OFILES): Add strndup.o.
Regenerate dependencies.
* configure.ac (funcs, AC_CHECK_FUNCS): Add strndup.
* strndup.c, xstrndup.c: New.
* config.in, configure, functions.texi: Regenerate.
2005-03-24 Kaveh R. Ghazi <ghazi@caip.rutgers.edu>
* xmemdup.c, xstrdup.c: Expose the tail call. * xmemdup.c, xstrdup.c: Expose the tail call.
2005-03-09 Mark Mitchell <mark@codesourcery.com> 2005-03-09 Mark Mitchell <mark@codesourcery.com>
......
...@@ -150,12 +150,13 @@ CFILES = alloca.c argv.c asprintf.c atexit.c \ ...@@ -150,12 +150,13 @@ CFILES = alloca.c argv.c asprintf.c atexit.c \
safe-ctype.c setenv.c sigsetmask.c snprintf.c sort.c spaces.c \ safe-ctype.c setenv.c sigsetmask.c snprintf.c sort.c spaces.c \
splay-tree.c stpcpy.c stpncpy.c strcasecmp.c strchr.c strdup.c \ splay-tree.c stpcpy.c stpncpy.c strcasecmp.c strchr.c strdup.c \
strerror.c strncasecmp.c strncmp.c strrchr.c strsignal.c \ strerror.c strncasecmp.c strncmp.c strrchr.c strsignal.c \
strstr.c strtod.c strtol.c strtoul.c \ strstr.c strtod.c strtol.c strtoul.c strndup.c \
ternary.c tmpnam.c \ ternary.c tmpnam.c \
unlink-if-ordinary.c \ unlink-if-ordinary.c \
vasprintf.c vfork.c vfprintf.c vprintf.c vsnprintf.c vsprintf.c \ vasprintf.c vfork.c vfprintf.c vprintf.c vsnprintf.c vsprintf.c \
waitpid.c \ waitpid.c \
xatexit.c xexit.c xmalloc.c xmemdup.c xstrdup.c xstrerror.c xatexit.c xexit.c xmalloc.c xmemdup.c xstrdup.c xstrerror.c \
xstrndup.c
# These are always included in the library. The first four are listed # These are always included in the library. The first four are listed
# first and by compile time to optimize parallel builds. # first and by compile time to optimize parallel builds.
...@@ -175,7 +176,7 @@ REQUIRED_OFILES = ./regex.o ./cplus-dem.o ./cp-demangle.o ./md5.o \ ...@@ -175,7 +176,7 @@ REQUIRED_OFILES = ./regex.o ./cplus-dem.o ./cp-demangle.o ./md5.o \
./ternary.o \ ./ternary.o \
./unlink-if-ordinary.o \ ./unlink-if-ordinary.o \
./xatexit.o ./xexit.o ./xmalloc.o ./xmemdup.o ./xstrdup.o \ ./xatexit.o ./xexit.o ./xmalloc.o ./xmemdup.o ./xstrdup.o \
./xstrerror.o ./xstrerror.o ./xstrndup.o
# These are all the objects that configure may add to the library via # These are all the objects that configure may add to the library via
# $funcs or EXTRA_OFILES. This list exists here only for "make # $funcs or EXTRA_OFILES. This list exists here only for "make
...@@ -195,7 +196,7 @@ CONFIGURED_OFILES = ./asprintf.o ./atexit.o \ ...@@ -195,7 +196,7 @@ CONFIGURED_OFILES = ./asprintf.o ./atexit.o \
./random.o ./rename.o ./rindex.o \ ./random.o ./rename.o ./rindex.o \
./setenv.o ./sigsetmask.o ./snprintf.o ./stpcpy.o ./stpncpy.o \ ./setenv.o ./sigsetmask.o ./snprintf.o ./stpcpy.o ./stpncpy.o \
./strcasecmp.o ./strchr.o ./strdup.o ./strncasecmp.o \ ./strcasecmp.o ./strchr.o ./strdup.o ./strncasecmp.o \
./strncmp.o ./strrchr.o ./strstr.o \ ./strncmp.o ./strndup.o ./strrchr.o ./strstr.o \
./strtod.o ./strtol.o ./strtoul.o \ ./strtod.o ./strtol.o ./strtoul.o \
./tmpnam.o \ ./tmpnam.o \
./vasprintf.o ./vfork.o ./vfprintf.o ./vprintf.o ./vsnprintf.o \ ./vasprintf.o ./vfork.o ./vfprintf.o ./vprintf.o ./vsnprintf.o \
...@@ -916,6 +917,12 @@ $(CONFIGURED_OFILES): stamp-picdir ...@@ -916,6 +917,12 @@ $(CONFIGURED_OFILES): stamp-picdir
else true; fi else true; fi
$(COMPILE.c) $(srcdir)/strncmp.c $(OUTPUT_OPTION) $(COMPILE.c) $(srcdir)/strncmp.c $(OUTPUT_OPTION)
./strndup.o: $(srcdir)/strndup.c $(INCDIR)/ansidecl.h
if [ x"$(PICFLAG)" != x ]; then \
$(COMPILE.c) $(PICFLAG) $(srcdir)/strndup.c -o pic/$@; \
else true; fi
$(COMPILE.c) $(srcdir)/strndup.c $(OUTPUT_OPTION)
./strrchr.o: $(srcdir)/strrchr.c $(INCDIR)/ansidecl.h ./strrchr.o: $(srcdir)/strrchr.c $(INCDIR)/ansidecl.h
if [ x"$(PICFLAG)" != x ]; then \ if [ x"$(PICFLAG)" != x ]; then \
$(COMPILE.c) $(PICFLAG) $(srcdir)/strrchr.c -o pic/$@; \ $(COMPILE.c) $(PICFLAG) $(srcdir)/strrchr.c -o pic/$@; \
...@@ -1058,3 +1065,10 @@ $(CONFIGURED_OFILES): stamp-picdir ...@@ -1058,3 +1065,10 @@ $(CONFIGURED_OFILES): stamp-picdir
else true; fi else true; fi
$(COMPILE.c) $(srcdir)/xstrerror.c $(OUTPUT_OPTION) $(COMPILE.c) $(srcdir)/xstrerror.c $(OUTPUT_OPTION)
./xstrndup.o: $(srcdir)/xstrndup.c config.h $(INCDIR)/ansidecl.h \
$(INCDIR)/libiberty.h
if [ x"$(PICFLAG)" != x ]; then \
$(COMPILE.c) $(PICFLAG) $(srcdir)/xstrndup.c -o pic/$@; \
else true; fi
$(COMPILE.c) $(srcdir)/xstrndup.c $(OUTPUT_OPTION)
...@@ -181,6 +181,9 @@ ...@@ -181,6 +181,9 @@
/* Define to 1 if you have the `strncasecmp' function. */ /* Define to 1 if you have the `strncasecmp' function. */
#undef HAVE_STRNCASECMP #undef HAVE_STRNCASECMP
/* Define to 1 if you have the `strndup' function. */
#undef HAVE_STRNDUP
/* Define to 1 if you have the `strrchr' function. */ /* Define to 1 if you have the `strrchr' function. */
#undef HAVE_STRRCHR #undef HAVE_STRRCHR
......
...@@ -4775,6 +4775,7 @@ funcs="$funcs clock" ...@@ -4775,6 +4775,7 @@ funcs="$funcs clock"
funcs="$funcs ffs" funcs="$funcs ffs"
funcs="$funcs getcwd" funcs="$funcs getcwd"
funcs="$funcs getpagesize" funcs="$funcs getpagesize"
funcs="$funcs gettimeofday"
funcs="$funcs index" funcs="$funcs index"
funcs="$funcs insque" funcs="$funcs insque"
funcs="$funcs memchr" funcs="$funcs memchr"
...@@ -4797,6 +4798,7 @@ funcs="$funcs strcasecmp" ...@@ -4797,6 +4798,7 @@ funcs="$funcs strcasecmp"
funcs="$funcs strchr" funcs="$funcs strchr"
funcs="$funcs strdup" funcs="$funcs strdup"
funcs="$funcs strncasecmp" funcs="$funcs strncasecmp"
funcs="$funcs strndup"
funcs="$funcs strrchr" funcs="$funcs strrchr"
funcs="$funcs strstr" funcs="$funcs strstr"
funcs="$funcs strtod" funcs="$funcs strtod"
...@@ -4884,10 +4886,12 @@ if test "x" = "y"; then ...@@ -4884,10 +4886,12 @@ if test "x" = "y"; then
for ac_func in asprintf atexit basename bcmp bcopy bsearch bzero calloc clock \ for ac_func in asprintf atexit basename bcmp bcopy bsearch bzero calloc clock \
getcwd getpagesize index insque mkstemps memchr memcmp memcpy \ getcwd getpagesize gettimeofday index insque mkstemps memchr memcmp memcpy \
memmove mempcpy memset putenv random rename rindex sigsetmask \ memmove mempcpy memset putenv random rename rindex sigsetmask \
strcasecmp setenv stpcpy stpncpy strchr strdup strncasecmp strrchr strstr \ strcasecmp setenv stpcpy stpncpy strchr strdup strncasecmp strndup strrchr strstr \
strtod strtol strtoul tmpnam vasprintf vfprintf vprintf \ strtod strtol strtoul tmpnam vasprintf vfprintf vprintf \
vsprintf waitpid getrusage on_exit psignal strerror strsignal \ vsprintf waitpid getrusage on_exit psignal strerror strsignal \
sysconf times sbrk gettimeofday ffs snprintf vsnprintf \ sysconf times sbrk gettimeofday ffs snprintf vsnprintf \
......
...@@ -248,6 +248,7 @@ funcs="$funcs strcasecmp" ...@@ -248,6 +248,7 @@ funcs="$funcs strcasecmp"
funcs="$funcs strchr" funcs="$funcs strchr"
funcs="$funcs strdup" funcs="$funcs strdup"
funcs="$funcs strncasecmp" funcs="$funcs strncasecmp"
funcs="$funcs strndup"
funcs="$funcs strrchr" funcs="$funcs strrchr"
funcs="$funcs strstr" funcs="$funcs strstr"
funcs="$funcs strtod" funcs="$funcs strtod"
...@@ -275,7 +276,7 @@ if test "x" = "y"; then ...@@ -275,7 +276,7 @@ if test "x" = "y"; then
AC_CHECK_FUNCS(asprintf atexit basename bcmp bcopy bsearch bzero calloc clock \ AC_CHECK_FUNCS(asprintf atexit basename bcmp bcopy bsearch bzero calloc clock \
getcwd getpagesize gettimeofday index insque mkstemps memchr memcmp memcpy \ getcwd getpagesize gettimeofday index insque mkstemps memchr memcmp memcpy \
memmove mempcpy memset putenv random rename rindex sigsetmask \ memmove mempcpy memset putenv random rename rindex sigsetmask \
strcasecmp setenv stpcpy stpncpy strchr strdup strncasecmp strrchr strstr \ strcasecmp setenv stpcpy stpncpy strchr strdup strncasecmp strndup strrchr strstr \
strtod strtol strtoul tmpnam vasprintf vfprintf vprintf \ strtod strtol strtoul tmpnam vasprintf vfprintf vprintf \
vsprintf waitpid getrusage on_exit psignal strerror strsignal \ vsprintf waitpid getrusage on_exit psignal strerror strsignal \
sysconf times sbrk gettimeofday ffs snprintf vsnprintf \ sysconf times sbrk gettimeofday ffs snprintf vsnprintf \
......
...@@ -339,6 +339,14 @@ between calls to @code{getpwd}. ...@@ -339,6 +339,14 @@ between calls to @code{getpwd}.
@end deftypefn @end deftypefn
@c gettimeofday.c:12
@deftypefn int gettimeofday (struct timeval *@var{tp}, void *@var{tz})
Writes the current time to @var{tp}. This implementation requires
that @var{tz} be NULL. Returns 0 on success, -1 on failure.
@end deftypefn
@c hex.c:30 @c hex.c:30
@deftypefn Extension void hex_init (void) @deftypefn Extension void hex_init (void)
...@@ -893,6 +901,15 @@ Compares the first @var{n} bytes of two strings, returning a value as ...@@ -893,6 +901,15 @@ Compares the first @var{n} bytes of two strings, returning a value as
@end deftypefn @end deftypefn
@c strndup.c:23
@deftypefn Extension char* strndup (const char *@var{s}, size_t @var{n})
Returns a pointer to a copy of @var{s} with at most @var{n} characters
in memory obtained from @code{malloc}, or @code{NULL} if insufficient
memory was available. The result is always NUL terminated.
@end deftypefn
@c strrchr.c:6 @c strrchr.c:6
@deftypefn Supplemental char* strrchr (const char *@var{s}, int @var{c}) @deftypefn Supplemental char* strrchr (const char *@var{s}, int @var{c})
...@@ -1008,6 +1025,16 @@ not be used in new projects. Use @code{mkstemp} instead. ...@@ -1008,6 +1025,16 @@ not be used in new projects. Use @code{mkstemp} instead.
@end deftypefn @end deftypefn
@c unlink-if-ordinary.c:27
@deftypefn Supplemental int unlink_if_ordinary (const char*)
Unlinks the named file, unless it is special (e.g. a device file).
Returns 0 when the file was unlinked, a negative value (and errno set) when
there was an error deleting the file, and a positive value if no attempt
was made to unlink the file because it is special.
@end deftypefn
@c vasprintf.c:51 @c vasprintf.c:51
@deftypefn Extension int vasprintf (char **@var{resptr}, const char *@var{format}, va_list @var{args}) @deftypefn Extension int vasprintf (char **@var{resptr}, const char *@var{format}, va_list @var{args})
...@@ -1153,4 +1180,13 @@ will never return a @code{NULL} pointer. ...@@ -1153,4 +1180,13 @@ will never return a @code{NULL} pointer.
@end deftypefn @end deftypefn
@c xstrndup.c:23
@deftypefn Replacement char* xstrndup (const char *@var{s}, size_t @var{n})
Returns a pointer to a copy of @var{s} with at most @var{n} characters
without fail, using @code{xmalloc} to obtain memory. The result is
always NUL terminated.
@end deftypefn
/* Implement the strndup function.
Copyright (C) 2005 Free Software Foundation, Inc.
Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>.
This file is part of the libiberty library.
Libiberty is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
Libiberty 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with libiberty; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
/*
@deftypefn Extension char* strndup (const char *@var{s}, size_t @var{n})
Returns a pointer to a copy of @var{s} with at most @var{n} characters
in memory obtained from @code{malloc}, or @code{NULL} if insufficient
memory was available. The result is always NUL terminated.
@end deftypefn
*/
#include "ansidecl.h"
#ifdef ANSI_PROTOTYPES
#include <stddef.h>
#else
#define size_t unsigned long
#endif
extern size_t strlen PARAMS ((const char*));
extern PTR malloc PARAMS ((size_t));
extern PTR memcpy PARAMS ((PTR, const PTR, size_t));
char *
strndup(s, n)
const char *s;
size_t n;
{
char *result;
size_t len = strlen (s);
if (n < len)
len = n;
result = malloc (len + 1);
if (!result)
return 0;
result[len] = '\0';
return memcpy (result, s, len);
}
/* Implement the xstrndup function.
Copyright (C) 2005 Free Software Foundation, Inc.
Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>.
This file is part of the libiberty library.
Libiberty is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
Libiberty 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with libiberty; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
/*
@deftypefn Replacement char* xstrndup (const char *@var{s}, size_t @var{n})
Returns a pointer to a copy of @var{s} with at most @var{n} characters
without fail, using @code{xmalloc} to obtain memory. The result is
always NUL terminated.
@end deftypefn
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/types.h>
#ifdef HAVE_STRING_H
#include <string.h>
#else
# ifdef HAVE_STRINGS_H
# include <strings.h>
# endif
#endif
#include "ansidecl.h"
#include "libiberty.h"
char *
xstrndup (s, n)
const char *s;
size_t n;
{
char *result;
size_t len = strlen (s);
if (n < len)
len = n;
result = xmalloc (len + 1);
result[len] = '\0';
return memcpy (result, s, len);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment