1f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes/*	$NetBSD: strxfrm.c,v 1.12 2012/06/25 22:32:46 abs Exp $	*/
2f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes
3f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes/*-
4f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * Copyright (c) 1990, 1993
5f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes *	The Regents of the University of California.  All rights reserved.
6f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes *
7f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * This code is derived from software contributed to Berkeley by
8f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * Chris Torek.
9f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes *
10f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * Redistribution and use in source and binary forms, with or without
11f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * modification, are permitted provided that the following conditions
12f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * are met:
13f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * 1. Redistributions of source code must retain the above copyright
14f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes *    notice, this list of conditions and the following disclaimer.
15f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * 2. Redistributions in binary form must reproduce the above copyright
16f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes *    notice, this list of conditions and the following disclaimer in the
17f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes *    documentation and/or other materials provided with the distribution.
18f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * 3. Neither the name of the University nor the names of its contributors
19f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes *    may be used to endorse or promote products derived from this software
20f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes *    without specific prior written permission.
21f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes *
22f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * SUCH DAMAGE.
33f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes */
34f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes
35f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes#include <sys/cdefs.h>
36f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes#if defined(LIBC_SCCS) && !defined(lint)
37f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes#if 0
38f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughesstatic char sccsid[] = "@(#)strxfrm.c	8.1 (Berkeley) 6/4/93";
39f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes#else
40f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes__RCSID("$NetBSD: strxfrm.c,v 1.12 2012/06/25 22:32:46 abs Exp $");
41f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes#endif
42f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes#endif /* LIBC_SCCS and not lint */
43f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes
44f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes#include <assert.h>
45f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes#include <string.h>
46f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes
47f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes/*
48f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * Transform src, storing the result in dst, such that
49f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * strcmp() on transformed strings returns what strcoll()
50f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes * on the original untransformed strings would return.
51f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes */
52f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughessize_t
53f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughesstrxfrm(char *dst, const char *src, size_t n)
54f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes{
55f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes	size_t srclen, copysize;
56f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes
57f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes	_DIAGASSERT(src != NULL);
58f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes
59f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes	/*
60f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes	 * Since locales are unimplemented, this is just a copy.
61f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes	 */
62f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes	srclen = strlen(src);
63f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes	if (n != 0) {
64f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes		_DIAGASSERT(dst != NULL);
65f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes		copysize = srclen < n ? srclen : n - 1;
66f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes		(void)memcpy(dst, src, copysize);
67f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes		dst[copysize] = 0;
68f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes	}
69f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes	return (srclen);
70f8b3a920a895a53b207017249080087d562bd0d2Elliott Hughes}
71