X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f X-Recipient: djgpp-workers AT delorie DOT com X-Authenticated: #27081556 X-Provags-ID: V01U2FsdGVkX19MvjJP6dNMvrNMu8036dRrEZovA2feGQU7lMl+D+ PjPErhDjzJWfEQ From: Juan Manuel Guerrero To: djgpp-workers AT delorie DOT com Subject: strndup and strnlen implementation. Date: Sat, 3 May 2008 20:18:07 +0200 User-Agent: KMail/1.9.5 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200805032018.07873.juan.guerrero@gmx.de> X-Y-GMX-Trusted: 0 Reply-To: djgpp-workers AT delorie DOT com Here are trivial to implement of GNU compatibility functions that I needed for porting purposes. Regards, Juan M. Guerrero 2008-05-03 Juan Manuel Guerrero Diffs against djgpp CVS head of 2008-04-25. * include/string.h: Declaration of strnlen and strndup added. * src/libc/compat/string/strndup.c: Implementation of strndup. * src/libc/compat/string/strndup.txh: Documentation of strndup. * src/libc/compat/string/strnlen.c: Implementation of strnlen. * src/libc/compat/string/strnlen.txh: Documentation of strnlen. * src/docs/kb/wc204.txi: Info about strndup and strnlen added. diff -aprNU3 djgpp.orig/include/string.h djgpp/include/string.h --- djgpp.orig/include/string.h 2007-12-11 07:01:20 +0000 +++ djgpp/include/string.h 2008-05-03 19:18:32 +0000 @@ -77,6 +77,7 @@ char * rindex(const char *_string, int char * stpcpy(char *_dest, const char *_src); char * stpncpy(char *_dest, const char *_src, size_t _n); char * strdup(const char *_s); +char * strndup(const char *_s, size_t _n); size_t strlcat(char *_dest, const char *_src, size_t _size); size_t strlcpy(char *_dest, const char *_src, size_t _size); char * strlwr(char *_s); @@ -84,6 +85,7 @@ int strcasecmp(const char *_s1, const ch int stricmp(const char *_s1, const char *_s2); int strncasecmp(const char *_s1, const char *_s2, size_t _n); int strnicmp(const char *_s1, const char *_s2, size_t _n); +size_t strnlen(const char *_s, size_t _n); char * strsep(char **_stringp, const char *_delim); char * strupr(char *_s); diff -aprNU3 djgpp.orig/src/docs/kb/wc204.txi djgpp/src/docs/kb/wc204.txi --- djgpp.orig/src/docs/kb/wc204.txi 2008-05-01 00:45:48 +0000 +++ djgpp/src/docs/kb/wc204.txi 2008-05-03 19:18:32 +0000 @@ -1143,3 +1143,7 @@ family of functions. The @code{%n$} and @code{*m$} numeric conversion specifiers are now supported by @code{_doprnt} and the @code{printf} family of functions. + +@findex strndup AT r{, added to the library} +@findex strnlen AT r{, added to the library} +GNU compatibility functions @code{strndup} and @code{strnlen} were added. diff -aprNU3 djgpp.orig/src/libc/compat/string/strndup.c djgpp/src/libc/compat/string/strndup.c --- djgpp.orig/src/libc/compat/string/strndup.c 1970-01-01 00:00:00 +0000 +++ djgpp/src/libc/compat/string/strndup.c 2008-05-03 19:18:32 +0000 @@ -0,0 +1,25 @@ +/* Copyright (C) 2008 DJ Delorie, see COPYING.DJ for details */ +#include +#include + +char * +strndup(const char *str, size_t n) +{ + char *copy; + size_t str_len; + + + if (str == NULL) + return NULL; + + str_len = strlen(str); + if (str_len > n) + str_len = n; + + copy = malloc(str_len + 1); + if (copy == NULL) + return NULL; + strcpy(copy, str); + + return copy; +} diff -aprNU3 djgpp.orig/src/libc/compat/string/strndup.txh djgpp/src/libc/compat/string/strndup.txh --- djgpp.orig/src/libc/compat/string/strndup.txh 1970-01-01 00:00:00 +0000 +++ djgpp/src/libc/compat/string/strndup.txh 2008-05-03 19:20:34 +0000 @@ -0,0 +1,36 @@ +@node strndup, string +@findex strndup +@subheading Syntax + +@example +#include + +char *strndup(const char *@var{source}, size_t @var{n}); +@end example + +@subheading Description + +Returns a newly allocated area of memory that contains a duplicate with at most +@var{n} characters of the string pointed to by @var{source}. The result is +always NUL terminated. The memory returned by this call must be freed by the +caller. + +@subheading Return Value + +Returns the newly allocated string, or @var{NULL} if there +is no more memory. + +@subheading Portability + +@port-note ansi This function is a GNU extension. + +@portability !ansi, !posix + +@subheading Example + +@example +char *foo() +@{ + return strndup("hello world", 5); +@} +@end example diff -aprNU3 djgpp.orig/src/libc/compat/string/strnlen.c djgpp/src/libc/compat/string/strnlen.c --- djgpp.orig/src/libc/compat/string/strnlen.c 1970-01-01 00:00:00 +0000 +++ djgpp/src/libc/compat/string/strnlen.c 2008-05-03 19:18:32 +0000 @@ -0,0 +1,14 @@ +/* Copyright (C) 2008 DJ Delorie, see COPYING.DJ for details */ +#include + +size_t +strnlen(const char *str, size_t n) +{ + char *start; + + + for (start = str; n && *str; n--, str++) + ; + + return str - start; +} diff -aprNU3 djgpp.orig/src/libc/compat/string/strnlen.txh djgpp/src/libc/compat/string/strnlen.txh --- djgpp.orig/src/libc/compat/string/strnlen.txh 1970-01-01 00:00:00 +0000 +++ djgpp/src/libc/compat/string/strnlen.txh 2008-05-03 19:20:34 +0000 @@ -0,0 +1,34 @@ +@node strnlen, string +@findex strnlen +@subheading Syntax + +@example +#include + +char *strnlen(const char *@var{string}, size_t @var{n}); +@end example + +@subheading Description + +This function returns the number of characters in @var{string} until it +reaches a @code{NUL} character or the maximum: @var{n} number of characters +have been inspected. + +@subheading Return Value + +The length of the string or @var{n}. + +@subheading Portability + +@port-note ansi This function is a GNU extension. + +@portability !ansi, !posix + +@subheading Example + +@example +size_t foo() +@{ + return strnlen("hello world", 5); +@} +@end example