delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2005/01/18/13:41:18

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 <Brian DOT Inglis AT SystematicSw DOT ab DOT ca>
Subject: strxfrm
To: DJGPP-workers <DJGPP-workers AT delorie DOT com>
Message-id: <tilqu0hfoiinq6vb3qef9t11a2vfia789d@4ax.com>
Organization: Systematic Software
MIME-version: 1.0
X-Mailer: Forte Agent 1.93/32.576 English (American)
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 <string.h>
 
 size_t
@@ -2,27 +4,15 @@
 #include <string.h>
 
 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
 

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019