X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f Date: Tue, 18 Jan 2005 11:40:14 -0700 From: Brian Inglis Subject: strxfrm To: DJGPP-workers Message-id: Organization: Systematic Software MIME-version: 1.0 X-Mailer: Forte Agent 1.93/32.576 English (American) Content-type: text/plain; charset=us-ascii Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id j0IIeJEw019129 Reply-To: djgpp-workers AT delorie DOT com I was checking the Standard for setlocale() and noticed that strxfrm() should be affected, and we don't have a liblocal.02 version. So I checked the current version in CVS and it is ancient K&R code that does not return the correct value when the third size parameter is zero, and what it is doing seemed overly complex for what it should be doing. So here's an update to make strxfrm.c meet the spec and simplify operation, and updated documentation. Thanks. Take care, Brian Inglis Index: src/libc/ansi/string/strxfrm.c =================================================================== RCS file: /cvs/djgpp/djgpp/src/libc/ansi/string/strxfrm.c,v retrieving revision 1.1 diff -B -b -w -i -p -u -t -r1.1 strxfrm.c --- src/libc/ansi/string/strxfrm.c 29 Nov 1994 09:40:32 -0000 1.1 +++ src/libc/ansi/string/strxfrm.c 18 Jan 2005 18:15:29 -0000 @@ -1,4 +1,6 @@ +/* Copyright (C) 2005 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */ + #include size_t @@ -2,27 +4,15 @@ #include size_t -strxfrm(dst, src, n) - char *dst; - const char *src; - size_t n; +strxfrm( char *dst, const char *src, size_t n) { - size_t r = 0; - int c; + size_t r = strlen( src ); - if (n != 0) { - while ((c = *src++) != 0) - { - r++; - if (--n == 0) - { - while (*src++ != 0) - r++; - break; - } - *dst++ = c; - } - *dst = 0; - } + /* if dst has space for src */ + if (n > r) + /* copy string */ + strcpy( dst, src); + + /* return len to copy/copied excluding nul */ return r; } Index: src/libc/ansi/string/strxfrm.txh =================================================================== RCS file: /cvs/djgpp/djgpp/src/libc/ansi/string/strxfrm.txh,v retrieving revision 1.3 diff -B -b -w -i -p -u -t -r1.3 strxfrm.txh --- src/libc/ansi/string/strxfrm.txh 29 Jan 2003 12:31:53 -0000 1.3 +++ src/libc/ansi/string/strxfrm.txh 18 Jan 2005 18:15:29 -0000 @@ -18,8 +18,8 @@ transforms of @code{s1} and @code{s2}. @subheading Return Value -The actual number of bytes required to transform @var{s2}, including the -@code{NULL}. +The actual number of bytes required to transform @var{s2}, excluding the +@code{nul}. @subheading Portability