summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorache <ache@FreeBSD.org>1999-02-12 23:40:41 +0000
committerache <ache@FreeBSD.org>1999-02-12 23:40:41 +0000
commitfa61c225af56ef56610108e5006c1075d5448be5 (patch)
treec6ab601937fa59a915a0977fee0815fa1c22bfc3 /lib
parent652b6fba3670328fd067d0e6e5ec6cfb5bd3e888 (diff)
downloadFreeBSD-src-fa61c225af56ef56610108e5006c1075d5448be5.zip
FreeBSD-src-fa61c225af56ef56610108e5006c1075d5448be5.tar.gz
fix wrong return result
fix n=0 case improve manpage
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/string/strxfrm.323
-rw-r--r--lib/libc/string/strxfrm.c43
2 files changed, 43 insertions, 23 deletions
diff --git a/lib/libc/string/strxfrm.3 b/lib/libc/string/strxfrm.3
index 308f942..ebe6bd0 100644
--- a/lib/libc/string/strxfrm.3
+++ b/lib/libc/string/strxfrm.3
@@ -51,11 +51,21 @@ The
function transforms a null-terminated string pointed to by
.Fa src
according to the current locale collation if any,
-then copies not more than
-.Fa n-1
-characters of the resulting string into
+then copies the resulting string
+into
+.Fa dst .
+Not more than
+.Fa n
+characters are copied into
.Fa dst ,
-terminating it with a null character and then returns the resulting length.
+including the terminating null character added.
+If
+.Fa n
+is set to 0,
+.Fa dst
+is permitted to be a NULL pointer (it helps to determine an actual size needed
+for transformation).
+.Pp
Comparing two strings using
.Fn strcmp
after
@@ -63,6 +73,11 @@ after
is equal to comparing
two original strings with
.Fn strcoll .
+.Sh RETURN VALUES
+Upon successful completion,
+.Fn strxfrm
+returns the length of the transformed string not including
+the terminating null character.
.Sh BUGS
Sometimes the behavior of this function is unpredictable.
.Sh SEE ALSO
diff --git a/lib/libc/string/strxfrm.c b/lib/libc/string/strxfrm.c
index ecb6d38..26c4442 100644
--- a/lib/libc/string/strxfrm.c
+++ b/lib/libc/string/strxfrm.c
@@ -24,7 +24,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $Id: strxfrm.c,v 1.8 1997/02/22 15:03:31 peter Exp $
+ * $Id: strxfrm.c,v 1.9 1998/06/05 09:49:51 ache Exp $
*/
#include <stdlib.h>
@@ -38,43 +38,48 @@ strxfrm(dest, src, len)
size_t len;
{
int prim, sec, l;
- char *d = dest, *s, *ss;
-
- if (len < 1)
- return 0;
+ size_t slen;
+ char *s, *ss;
if (!*src) {
- *d = '\0';
+ if (len > 0)
+ *dest = '\0';
return 0;
}
if (__collate_load_error) {
- size_t slen = strlen(src);
-
- if (slen < len) {
- strcpy(d, src);
- return slen;
+ slen = strlen(src);
+ if (len > 0) {
+ if (slen < len)
+ strcpy(dest, src);
+ else {
+ strncpy(dest, src, len - 1);
+ dest[len - 1] = '\0';
+ }
}
- strncpy(d, src, len - 1);
- d[len - 1] = '\0';
- return len - 1;
+ return slen;
}
+ slen = 0;
prim = sec = 0;
ss = s = __collate_substitute(src);
- while (*s && len > 1) {
+ while (*s) {
while (*s && !prim) {
__collate_lookup(s, &l, &prim, &sec);
s += l;
}
if (prim) {
- *d++ = (char)prim;
- len--;
+ if (len > 1) {
+ *dest++ = (char)prim;
+ len--;
+ }
+ slen++;
prim = 0;
}
}
free(ss);
- *d = '\0';
+ if (len > 0)
+ *dest = '\0';
- return d - dest;
+ return slen;
}
OpenPOWER on IntegriCloud