[OPEN-ILS-DEV] non-portable code: strndup() in osrf-chat.c

Jeroen Ruigrok van der Werven asmodai at in-nomine.org
Mon Apr 30 18:03:30 EDT 2007


-On [20070430 22:07], Mike Rylander (mrylander at gmail.com) wrote:
> Just to keep the list up to date, we're working through this in IRC.
> The plan is looking like some autotools magic to detect strndup (and
> strnlen) and if not found to include a local implementation.  Those
> will probably be inserted via a ports.[ch] set that will be included
> by utils.h (that's used basically everywhere).
>
> The plan may change ... that is all  :)

With a friend of mine we came up with a strnlen() implementation that should
be portable for all platforms (MIT licensed). And it works nicely with the
strndup() code I found.

#include <stdio.h>

#include "strndup.h"
#include "strnlen.h"

int
main(void)
{
        const char string[] = "This is a teststring"; /* 20 */

        printf("length: %d - string: %s\n", strnlen(string, -1),
               strndup(string, -1));
        printf("length: %d - string: %s\n", strnlen(string, 0),
               strndup(string, 0));
        printf("length: %d - string: %s\n", strnlen(string, 1), 
               strndup(string, 1));
        printf("length: %d - string: %s\n", strnlen(string, 5), 
               strndup(string, 5));
        printf("length: %d - string: %s\n", strnlen(string, 15), 
               strndup(string, 15));
        printf("length: %d - string: %s\n", strnlen(string, 19), 
               strndup(string, 19));
        printf("length: %d - string: %s\n", strnlen(string, 20), 
               strndup(string, 20));
        printf("length: %d - string: %s\n", strnlen(string, 21), 
               strndup(string, 21));
        printf("length: %d - string: %s\n", strnlen(string, 22), 
               strndup(string, 22));

        return (0);
}

Yields:

[23:30] [asmodai at oni] (629) {0} % ./test 
length: 20 - string: This is a teststring
length: 0 - string: 
length: 1 - string: T
length: 5 - string: This 
length: 15 - string: This is a tests
length: 19 - string: This is a teststrin
length: 20 - string: This is a teststring
length: 20 - string: This is a teststring
length: 20 - string: This is a teststring

00000000  6c 65 6e 67 74 68 3a 20  32 30 20 2d 20 73 74 72  |length: 20 - str|
00000010  69 6e 67 3a 20 54 68 69  73 20 69 73 20 61 20 74  |ing: This is a t|
00000020  65 73 74 73 74 72 69 6e  67 0a 6c 65 6e 67 74 68  |eststring.length|
00000030  3a 20 30 20 2d 20 73 74  72 69 6e 67 3a 20 0a 6c  |: 0 - string: .l|
00000040  65 6e 67 74 68 3a 20 31  20 2d 20 73 74 72 69 6e  |ength: 1 - strin|
00000050  67 3a 20 54 0a 6c 65 6e  67 74 68 3a 20 35 20 2d  |g: T.length: 5 -|
00000060  20 73 74 72 69 6e 67 3a  20 54 68 69 73 20 0a 6c  | string: This .l|

To prove the result of 5 is correct. ;)
The 20 for the -1 might seem odd, but it isn't considering size_t is an
unsigned type (typically unsigned int32_t) and following the two's complement
rules you get SIZE_T_MAX (4294967295) for -1 (and 4294967294 for -2, and so
on), which is obviously larger than the maxlen of 20.

Right now I have it as:

strndup.[ch]
strnlen.[ch]

I am not yet sure if this is preferred over a single ports.[ch]. I think i am
more inclined to do so to avoid namespace pollution and #include'ing all over
the place.

I'll throw these files in a report tomorrow or so. Now it's time for bed. :)

-- 
Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org> / asmodai
イェルーン ラウフロック ヴァン デル ウェルヴェン
http://www.in-nomine.org/ | http://www.rangaku.org/
Where can I find a man who has forgotten words? I would like to have a word
with him...


More information about the Open-ils-dev mailing list