fix wrong return result

fix n=0 case
improve manpage
This commit is contained in:
Andrey A. Chernov 1999-02-12 23:40:41 +00:00
parent 60979a53a0
commit f0e6ee9f05
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=43945
2 changed files with 43 additions and 23 deletions

View file

@ -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

View file

@ -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;
}