xref: /titanic_44/usr/src/lib/libast/common/string/strlcat.c (revision ac88567a7a5bb7f01cf22cf366bc9d6203e24d7a)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 /*
24  * strlcat implementation
25  */
26 
27 #define strlcat		______strlcat
28 
29 #include <ast.h>
30 
31 #undef	strlcat
32 
33 #undef	_def_map_ast
34 #include <ast_map.h>
35 
36 #if _lib_strlcat
37 
38 NoN(strlcat)
39 
40 #else
41 
42 /*
43  * append t onto s limiting total size of s to n
44  * s 0 terminated if n>0
45  * min(n,strlen(s))+strlen(t) returned
46  */
47 
48 #if defined(__EXPORT__)
49 #define extern	__EXPORT__
50 #endif
51 
52 extern size_t
53 strlcat(register char* s, register const char* t, register size_t n)
54 {
55 	register size_t	m;
56 	const char*	o = t;
57 
58 	if (m = n)
59 	{
60 		while (n && *s)
61 		{
62 			n--;
63 			s++;
64 		}
65 		m -= n;
66 		if (n)
67 			do
68 			{
69 				if (!--n)
70 				{
71 					*s = 0;
72 					break;
73 				}
74 			} while (*s++ = *t++);
75 		else
76 			*s = 0;
77 	}
78 	if (!n)
79 		while (*t++);
80 	return (t - o) + m - 1;
81 }
82 
83 #endif
84