diff options
author | Andrey A. Chernov <ache@FreeBSD.org> | 1999-02-12 23:40:41 +0000 |
---|---|---|
committer | Andrey A. Chernov <ache@FreeBSD.org> | 1999-02-12 23:40:41 +0000 |
commit | f0e6ee9f051689347530d49dc3bb59bbe237b52f (patch) | |
tree | a8d5c63d3acb3f749fec3abb83cebfc512c44162 /lib/libc/string | |
parent | 60979a53a073b41c7c9ade8d544df412d4b94899 (diff) | |
download | src-f0e6ee9f051689347530d49dc3bb59bbe237b52f.tar.gz src-f0e6ee9f051689347530d49dc3bb59bbe237b52f.zip |
fix wrong return result
fix n=0 case
improve manpage
Notes
Notes:
svn path=/head/; revision=43945
Diffstat (limited to 'lib/libc/string')
-rw-r--r-- | lib/libc/string/strxfrm.3 | 23 | ||||
-rw-r--r-- | lib/libc/string/strxfrm.c | 43 |
2 files changed, 43 insertions, 23 deletions
diff --git a/lib/libc/string/strxfrm.3 b/lib/libc/string/strxfrm.3 index 308f942fad53..ebe6bd03fa84 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 ecb6d38b5ce7..26c4442ded9a 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; } |