1 /*
2 Copyright (c) 1994 Cygnus Support.
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms are permitted
6 provided that the above copyright notice and this paragraph are
7 duplicated in all such forms and that any documentation,
8 and/or other materials related to such
9 distribution and use acknowledge that the software was developed
10 at Cygnus Support, Inc.  Cygnus Support, Inc. may not be used to
11 endorse or promote products derived from this software without
12 specific prior written permission.
13 THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 /*
18 FUNCTION
19 	<<strxfrm>>---transform string
20 
21 INDEX
22 	strxfrm
23 
24 SYNOPSIS
25 	#include <string.h>
26 	size_t strxfrm(char *restrict <[s1]>, const char *restrict <[s2]>,
27                        size_t <[n]>);
28 
29 DESCRIPTION
30 	This function transforms the string pointed to by <[s2]> and
31 	places the resulting string into the array pointed to by
32 	<[s1]>. The transformation is such that if the <<strcmp>>
33 	function is applied to the two transformed strings, it returns
34 	a value greater than, equal to, or less than zero,
35 	correspoinding to the result of a <<strcoll>> function applied
36 	to the same two original strings.
37 
38 	No more than <[n]> characters are placed into the resulting
39 	array pointed to by <[s1]>, including the terminating null
40 	character. If <[n]> is zero, <[s1]> may be a null pointer. If
41 	copying takes place between objects that overlap, the behavior
42 	is undefined.
43 
44 	(NOT Cygwin:) The current implementation of <<strxfrm>> simply copies
45 	the input and does not support any language-specific transformations.
46 
47 RETURNS
48 	The <<strxfrm>> function returns the length of the transformed string
49 	(not including the terminating null character). If the value returned
50 	is <[n]> or more, the contents of the array pointed to by
51 	<[s1]> are indeterminate.
52 
53 PORTABILITY
54 <<strxfrm>> is ANSI C.
55 
56 <<strxfrm>> requires no supporting OS subroutines.
57 
58 QUICKREF
59 	strxfrm ansi pure
60 */
61 
62 #include <string.h>
63 
64 size_t
strxfrm(char * __restrict s1,const char * __restrict s2,size_t n)65 strxfrm (char *__restrict s1,
66 	const char *__restrict s2,
67 	size_t n)
68 {
69   size_t res;
70   res = 0;
71   while (n-- > 0)
72     {
73       if ((*s1++ = *s2++) != '\0')
74         ++res;
75       else
76         return res;
77     }
78   while (*s2)
79     {
80       ++s2;
81       ++res;
82     }
83 
84   return res;
85 }
86